Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compute: implement InstanceGroups #1320

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/toc.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
}, {
"title": "HealthCheck",
"type": "compute/health-check"
}, {
"title": "InstanceGroup",
"type": "compute/instance-group"
}, {
"title": "Network",
"type": "compute/network"
Expand Down
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) {
* // `instanceGroups` 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, instanceGroups, 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 @@ -2130,6 +2245,7 @@ streamRouter.extend(Compute, [
'getDisks',
'getFirewalls',
'getHealthChecks',
'getInstanceGroups',
'getNetworks',
'getOperations',
'getRegions',
Expand Down
Loading