Skip to content

Commit

Permalink
compute: implement InstanceGroups
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed May 16, 2016
1 parent 17e510b commit 1c8a342
Show file tree
Hide file tree
Showing 6 changed files with 896 additions and 186 deletions.
116 changes: 116 additions & 0 deletions lib/compute/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,121 @@ Compute.prototype.getDisks = function(options, callback) {
});
};

/**
* Get a list of instance groups.
*
* @resource [InstanceGroups Overview]{@link https://cloud.google.com/compute/docs/reference/v1/instanceGroups}
* @resource [InstanceGroups: aggregatedList API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/instanceGroups/aggregatedList}
*
* @param {object=} options - Instance group search options.
* @param {boolean} options.autoPaginate - Have pagination handled
* automatically. Default: true.
* @param {string} options.filter - Search filter in the format of
* `{name} {comparison} {filterString}`.
* - **`name`**: the name of the field to compare
* - **`comparison`**: the comparison operator, `eq` (equal) or `ne`
* (not equal)
* - **`filterString`**: the string to filter to. For string fields, this
* can be a regular expression.
* @param {number} options.maxResults - Maximum number of instance groups to
* return.
* @param {string} options.pageToken - A previously-returned page token
* representing part of the larger set of results to view.
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request.
* @param {module:compute/instance-group} callback.instanceGroups -
* InstanceGroup objects from your project.
* @param {?object} callback.nextQuery - If present, query with this object to
* check for more results.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* gce.getInstanceGroups(function(err, instanceGroups) {
* // `instanceGroup` is an array of `InstanceGroup` objects.
* });
*
* //-
* // To control how many API requests are made and page through the results
* // manually, set `autoPaginate` to `false`.
* //-
* function callback(err, groups, nextQuery, apiResponse) {
* if (nextQuery) {
* // More results exist.
* gce.getInstanceGroups(nextQuery, callback);
* }
* }
*
* gce.getInstanceGroups({
* autoPaginate: false
* }, callback);
*
* //-
* // Get the instance groups from your project as a readable object stream.
* //-
* gce.getInstanceGroups()
* .on('error', console.error)
* .on('data', function(instanceGroup) {
* // `instanceGroup` is an `InstanceGroup` object.
* })
* .on('end', function() {
* // All instance groups retrieved.
* });
*
* //-
* // If you anticipate many results, you can end a stream early to prevent
* // unnecessary processing and API requests.
* //-
* gce.getInstanceGroups()
* .on('data', function(instanceGroup) {
* this.end();
* });
*/
Compute.prototype.getInstanceGroups = function(options, callback) {
var self = this;

if (is.fn(options)) {
callback = options;
options = {};
}

options = options || {};

this.request({
uri: '/aggregated/instanceGroups',
qs: options
}, function(err, resp) {
if (err) {
callback(err, null, null, resp);
return;
}

var nextQuery = null;

if (resp.nextPageToken) {
nextQuery = extend({}, options, {
pageToken: resp.nextPageToken
});
}

var zones = resp.items || {};

var instanceGroups = Object.keys(zones).reduce(function(acc, zoneName) {
var zone = self.zone(zoneName.replace('zones/', ''));
var instanceGroups = zones[zoneName].instanceGroups || [];

instanceGroups.forEach(function(group) {
var instanceGroupInstance = zone.instanceGroup(group.name);
instanceGroupInstance.metadata = group;
acc.push(instanceGroupInstance);
});

return acc;
}, []);

callback(null, instanceGroups, nextQuery, resp);
});
};

/**
* Get a list of firewalls.
*
Expand Down Expand Up @@ -2128,6 +2243,7 @@ streamRouter.extend(Compute, [
'getAddresses',
'getAutoscalers',
'getDisks',
'getInstanceGroups',
'getFirewalls',
'getHealthChecks',
'getNetworks',
Expand Down
Loading

0 comments on commit 1c8a342

Please sign in to comment.