Skip to content

Commit

Permalink
compute: implement subnetworks (#1435)
Browse files Browse the repository at this point in the history
compute: implement subnetworks
  • Loading branch information
ashleyschuett authored and stephenplusplus committed Jul 20, 2016
1 parent 6cbbc37 commit 5689f96
Show file tree
Hide file tree
Showing 11 changed files with 1,366 additions and 6 deletions.
3 changes: 3 additions & 0 deletions docs/toc.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@
}, {
"title": "Snapshot",
"type": "compute/snapshot"
}, {
"title": "Subnetwork",
"type": "compute/subnetwork"
}, {
"title": "VM",
"type": "compute/vm"
Expand Down
116 changes: 116 additions & 0 deletions lib/compute/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1908,6 +1908,121 @@ Compute.prototype.getSnapshots = function(options, callback) {
});
};

/**
* Get a list of subnetworks in this project.
*
* @resource [Subnetworks Overview]{@link https://cloud.google.com/compute/docs/subnetworks}
* @resource [Subnetworks: list API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/subnetworks}
*
* @param {object=} options - Subnetwork 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.maxApiCalls - Maximum number of API calls to make.
* @param {number} options.maxResults - Maximum number of subnetworks 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/subnetwork} callback.subnetworks - Subnetwork 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.getSubnetworks(function(err, subnetworks) {
* // `subnetworks` is an array of `Subnetworks` objects.
* });
*
* //-
* // To control how many API requests are made and page through the results
* // manually, set `autoPaginate` to `false`.
* //-
* function callback(err, subnetworks, nextQuery, apiResponse) {
* if (nextQuery) {
* // More results exist.
* gce.getSubnetworks(nextQuery, callback);
* }
* }
*
* gce.getSubnetworks({
* autoPaginate: false
* }, callback);
*
* //-
* // Get the subnetworks from your project as a readable object stream.
* //-
* gce.getSubnetworks()
* .on('error', console.error)
* .on('data', function(subnetwork) {
* // `subnetwork` is a `Subnetwork` object.
* })
* .on('end', function() {
* // All subnetworks retrieved.
* });
*
* //-
* // If you anticipate many results, you can end a stream early to prevent
* // unnecessary processing and API requests.
* //-
* gce.getSubnetworks()
* .on('data', function(subnetwork) {
* this.end();
* });
*/
Compute.prototype.getSubnetworks = function(options, callback) {
var self = this;

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

options = options || {};

this.request({
uri: '/aggregated/subnetworks',
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 regions = resp.items || {};

var subnetworks = Object.keys(regions).reduce(function(acc, regionName) {
var region = self.region(regionName.replace('regions/', ''));
var subnetworks = regions[regionName].subnetworks || [];

subnetworks.forEach(function(subnetwork) {
var subnetworkInstance = region.subnetwork(subnetwork.name);
subnetworkInstance.metadata = subnetwork;
acc.push(subnetworkInstance);
});

return acc;
}, []);

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

/**
* Get a list of virtual machine instances.
*
Expand Down Expand Up @@ -2272,6 +2387,7 @@ streamRouter.extend(Compute, [
'getRules',
'getServices',
'getSnapshots',
'getSubnetworks',
'getVMs',
'getZones'
]);
Expand Down
139 changes: 139 additions & 0 deletions lib/compute/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,145 @@ Network.prototype.createFirewall = function(name, config, callback) {
this.compute.createFirewall(name, config, callback);
};

/**
* Create a subnetwork in this network.
*
* @resource [Subnetwork Resource]{@link https://cloud.google.com/compute/docs/reference/v1/subnetworks#resource}
* @resource [Subnetwork: insert API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/subnetworks/insert}
*
* @param {string} name - Name of the subnetwork.
* @param {object} config - See a
* [Subnetwork resource](https://cloud.google.com/compute/docs/reference/v1/subnetworks#resource).
* @param {module:compute/region|string} config.region - The region where the
* Subnetwork resides.
* @param {string} config.range - The range of internal addresses that
* are owned by this subnetwork.
* [CIDR](http://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing) range
* of addresses that are legal on this network. (Alias for
* `config.ipCidrRange`)
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request.
* @param {module:compute/subnetwork} callback.subnetwork - The created
* Subnetwork object.
* @param {module:compute/operation} callback.operation - An operation object
* that can be used to check the status of the request.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* var region = gce.region('us-east1');
*
* var config = {
* region: region,
* range: '10.0.1.0/24'
* };
*
* function callback(err, subnetwork, operation, apiResponse) {
* // `subnetwork` is a Subnetwork object.
*
* // `operation` is an Operation object that can be used to check the status
* // of the request.
* }
*
* network.createSubnetwork('new-subnetwork-name', config, callback);
*/
Network.prototype.createSubnetwork = function(name, config, callback) {
config = extend({}, config, {
network: this.formattedName
});

var region = config.region;

if (is.string(region)) {
region = this.compute.region(region);
}

delete config.region;

region.createSubnetwork(name, config, callback);
};

/**
* Get a list of subnetworks in this network.
*
* @resource [Subnetworks Overview]{@link https://cloud.google.com/compute/docs/subnetworks}
* @resource [Subnetworks: list API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/subnetworks}
*
* @param {object=} options - Subnetwork 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.maxApiCalls - Maximum number of API calls to make.
* @param {number} options.maxResults - Maximum number of subnetworks 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/subnetwork} callback.subnetworks - Subnetwork objects
* from this network.
* @param {?object} callback.nextQuery - If present, query with this object to
* check for more results.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* network.getSubnetworks(function(err, subnetworks) {
* // `subnetworks` is an array of `Subnetworks` objects.
* });
*
* //-
* // To control how many API requests are made and page through the results
* // manually, set `autoPaginate` to `false`.
* //-
* function callback(err, subnetworks, nextQuery, apiResponse) {
* if (nextQuery) {
* // More results exist.
* network.getSubnetworks(nextQuery, callback);
* }
* }
*
* network.getSubnetworks({
* autoPaginate: false
* }, callback);
*
* //-
* // Get the subnetworks from this network as a readable object stream.
* //-
* network.getSubnetworks()
* .on('error', console.error)
* .on('data', function(subnetwork) {
* // `subnetwork` is a `Subnetwork` object.
* })
* .on('end', function() {
* // All subnetworks retrieved.
* });
*
* //-
* // If you anticipate many results, you can end a stream early to prevent
* // unnecessary processing and API requests.
* //-
* network.getSubnetworks()
* .on('data', function(subnetwork) {
* this.end();
* });
*/
Network.prototype.getSubnetworks = function(options, callback) {
if (is.fn(options)) {
callback = options;
options = {};
}

options = extend({}, options, {
filter: 'network eq .*' + this.formattedName
});

return this.compute.getSubnetworks(options, callback);
};

/**
* Delete the network.
*
Expand Down
Loading

0 comments on commit 5689f96

Please sign in to comment.