Skip to content

Commit 90248b8

Browse files
committed
compute: implement subnetworks
1 parent 79e0e28 commit 90248b8

6 files changed

Lines changed: 705 additions & 0 deletions

File tree

lib/compute/index.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,122 @@ Compute.prototype.getFirewalls = function(options, callback) {
11621162
});
11631163
};
11641164

1165+
/**
1166+
* Get a list of subnetworks in this project.
1167+
*
1168+
* @resource [Subnetworks Overview]{@link https://cloud.google.com/compute/docs/subnetworks}
1169+
* @resource [Subnetworks: list API Documentation]{@link https://cloud.google.com/compute/docs/reference/latest/subnetworks}
1170+
*
1171+
* @param {object=} options - Subnetwork search options.
1172+
* @param {boolean} options.autoPaginate - Have pagination handled
1173+
* automatically. Default: true.
1174+
* @param {string} options.filter - Search filter in the format of
1175+
* `{name} {comparison} {filterString}`.
1176+
* - **`name`**: the name of the field to compare
1177+
* - **`comparison`**: the comparison operator, `eq` (equal) or `ne`
1178+
* (not equal)
1179+
* - **`filterString`**: the string to filter to. For string fields, this
1180+
* can be a regular expression.
1181+
* @param {number} options.maxApiCalls - Maximum number of API calls to make.
1182+
* @param {number} options.maxResults - Maximum number of firewalls to return.
1183+
* @param {string} options.pageToken - A previously-returned page token
1184+
* representing part of the larger set of results to view.
1185+
* @param {function} callback - The callback function.
1186+
* @param {?error} callback.err - An error returned while making this request.
1187+
* @param {module:compute/firewall} callback.subnetworks - Subnetwork objects
1188+
* from your project.
1189+
* @param {?object} callback.nextQuery - If present, query with this object to
1190+
* check for more results.
1191+
* @param {object} callback.apiResponse - The full API response.
1192+
*
1193+
* @example
1194+
* gce.getSubnetworks(function(err, subnetworks) {
1195+
* // `subnetworks` is an array of `Subnetworks` objects.
1196+
* });
1197+
*
1198+
* //-
1199+
* // To control how many API requests are made and page through the results
1200+
* // manually, set `autoPaginate` to `false`.
1201+
* //-
1202+
* function callback(err, subnetworks, nextQuery, apiResponse) {
1203+
* if (nextQuery) {
1204+
* // More results exist.
1205+
* gce.getSubnetworks(nextQuery, callback);
1206+
* }
1207+
* }
1208+
*
1209+
* gce.getSubnetworks({
1210+
* autoPaginate: false
1211+
* }, callback);
1212+
*
1213+
* //-
1214+
* // Get the firewalls from your project as a readable object stream.
1215+
* //-
1216+
* gce.getSubnetworks()
1217+
* .on('error', console.error)
1218+
* .on('data', function(subnetwork) {
1219+
* // `subnetwork` is a `Subnetwork` object.
1220+
* })
1221+
* .on('end', function() {
1222+
* // All subnetworks retrieved.
1223+
* });
1224+
*
1225+
* //-
1226+
* // If you anticipate many results, you can end a stream early to prevent
1227+
* // unnecessary processing and API requests.
1228+
* //-
1229+
* gce.getSubnetworks()
1230+
* .on('data', function(subnetwork) {
1231+
* this.end();
1232+
* });
1233+
*/
1234+
Compute.prototype.getSubnetworks = function(options, callback) {
1235+
var self = this;
1236+
1237+
if (is.fn(options)) {
1238+
callback = options;
1239+
options = {};
1240+
}
1241+
1242+
options = options || {};
1243+
1244+
this.request({
1245+
uri: '/aggregated/subnetworks',
1246+
qs: options
1247+
}, function(err, resp) {
1248+
1249+
if (err) {
1250+
callback(err, null, null, resp);
1251+
return;
1252+
}
1253+
1254+
var nextQuery = null;
1255+
1256+
if (resp.nextPageToken) {
1257+
nextQuery = extend({}, options, {
1258+
pageToken: resp.nextPageToken
1259+
});
1260+
}
1261+
1262+
var regions = resp.items || {};
1263+
1264+
var subnetworks = Object.keys(regions).reduce(function(acc, regionName) {
1265+
var region = self.region(regionName.replace('regions/', ''));
1266+
var subnetworks = regions[regionName].subnetworks || [];
1267+
1268+
subnetworks.forEach(function(subnetwork) {
1269+
var subnetworkInstance = region.subnetwork(subnetwork.name);
1270+
subnetworkInstance.metadata = subnetwork;
1271+
acc.push(subnetworkInstance);
1272+
});
1273+
1274+
return acc;
1275+
}, []);
1276+
1277+
callback(null, subnetworks, nextQuery, resp);
1278+
});
1279+
};
1280+
11651281
/**
11661282
* Get a list of health checks.
11671283
*
@@ -2272,6 +2388,7 @@ streamRouter.extend(Compute, [
22722388
'getRules',
22732389
'getServices',
22742390
'getSnapshots',
2391+
'getSubnetworks',
22752392
'getVMs',
22762393
'getZones'
22772394
]);

lib/compute/network.js

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ var ServiceObject = require('../common/service-object.js');
3737
*/
3838
var util = require('../common/util.js');
3939

40+
/**
41+
* @type {module:compute/region}
42+
* @private
43+
*/
44+
var Region = require('./region.js');
45+
4046
/*! Developer Documentation
4147
*
4248
* @param {module:compute} compute - The Compute module this network belongs to.
@@ -214,6 +220,137 @@ Network.prototype.createFirewall = function(name, config, callback) {
214220
this.compute.createFirewall(name, config, callback);
215221
};
216222

223+
/**
224+
* Create a subnetwork in this network.
225+
*
226+
* @resource [Subnetwork Resource]{@link https://cloud.google.com/compute/docs/reference/v1/subnetworks#resource}
227+
* @resource [Subnetwork: insert API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/subnetworks/insert}
228+
*
229+
* @param {string} name - Name of the subnetwork.
230+
* @param {object} config - See a
231+
* [Subnetwork resource](https://cloud.google.com/compute/docs/reference/v1/subnetworks#resource).
232+
* @param {string=} config.description - An optional description of this
233+
* resource. Provide this property when you create the resource.
234+
* @param {string} config.ipCidrRange - The range of internal addresses that
235+
* are owned by this subnetwork. Provide this property when you create the
236+
* subnetwork. For example, 10.0.0.0/8 or 192.168.0.0/16. Ranges must be
237+
* unique and non-overlapping within a network.
238+
* @param {string} config.region - URL of the region where the Subnetwork
239+
* resides.
240+
* @param {function} callback - The callback function.
241+
* @param {?error} callback.err - An error returned while making this request.
242+
* @param {module:compute/rule} callback.rule - The created Rule object.
243+
* @param {module:compute/operation} callback.operation - An operation object
244+
* that can be used to check the status of the request.
245+
* @param {object} callback.apiResponse - The full API response.
246+
*
247+
* @example
248+
* var name = 'new-subnetwork-name';
249+
*
250+
* var config = {
251+
* region : 'regions/us-east1',
252+
* ipCidrRange: '10.0.1.0/24'
253+
* };
254+
*
255+
* network.createSubnetwork(name, config,
256+
* function (err, subnetwork, operation, apiResponse) {
257+
* // `subnetwork` is a Subnetwork object.
258+
*
259+
* // `operation` is an Operation object that can be used to check the status
260+
* // of the request.
261+
* });
262+
*/
263+
Network.prototype.createSubnetwork = function(name, config, callback) {
264+
config = extend({}, config, {
265+
network: this.formattedName
266+
});
267+
268+
var region = new Region(this.compute, config.region);
269+
delete config.region;
270+
271+
return region.createSubnetwork(name, config, callback);
272+
};
273+
274+
/**
275+
* Get a list of subnetworks in this network.
276+
* * @resource [Subnetworks Overview]{@link https://cloud.google.com/compute/docs/subnetworks}
277+
* @resource [Subnetworks: list API Documentation]{@link https://cloud.google.com/compute/docs/reference/latest/subnetworks}
278+
*
279+
* @param {object=} options - Subnetwork search options.
280+
* @param {boolean} options.autoPaginate - Have pagination handled
281+
* automatically. Default: true.
282+
* @param {string} options.filter - Search filter in the format of
283+
* `{name} {comparison} {filterString}`.
284+
* - **`name`**: the name of the field to compare
285+
* - **`comparison`**: the comparison operator, `eq` (equal) or `ne`
286+
* (not equal)
287+
* - **`filterString`**: the string to filter to. For string fields, this
288+
* can be a regular expression.
289+
* @param {number} options.maxApiCalls - Maximum number of API calls to make.
290+
* @param {number} options.maxResults - Maximum number of firewalls to return.
291+
* @param {string} options.pageToken - A previously-returned page token
292+
* representing part of the larger set of results to view.
293+
* @param {function} callback - The callback function.
294+
* @param {?error} callback.err - An error returned while making this request.
295+
* @param {module:compute/firewall} callback.subnetworks - Subnetwork objects
296+
* from your project.
297+
* @param {?object} callback.nextQuery - If present, query with this object to
298+
* check for more results.
299+
* @param {object} callback.apiResponse - The full API response.
300+
*
301+
* @example
302+
* gce.getSubnetworks(function(err, subnetworks) {
303+
* // `subnetworks` is an array of `Subnetworks` objects.
304+
* });
305+
*
306+
* //-
307+
* // To control how many API requests are made and page through the results
308+
* // manually, set `autoPaginate` to `false`.
309+
* //-
310+
* function callback(err, subnetworks, nextQuery, apiResponse) {
311+
* if (nextQuery) {
312+
* // More results exist.
313+
* gce.getSubnetworks(nextQuery, callback);
314+
* }
315+
* }
316+
*
317+
* gce.getSubnetworks({
318+
* autoPaginate: false
319+
* }, callback);
320+
* * //-
321+
* // Get the firewalls from your project as a readable object stream.
322+
* //-
323+
* gce.getSubnetworks()
324+
* .on('error', console.error)
325+
* .on('data', function(subnetwork) {
326+
* // `subnetwork` is a `Subnetwork` object.
327+
* })
328+
* .on('end', function() {
329+
* // All firewalls retrieved.
330+
* });
331+
*
332+
* //-
333+
* // If you anticipate many results, you can end a stream early to prevent
334+
* // unnecessary processing and API requests.
335+
* //-
336+
* gce.getSubnetworks()
337+
* .on('data', function(subnetwork) {
338+
* this.end();
339+
* });
340+
*/
341+
Network.prototype.getSubnetworks = function(options, callback) {
342+
if (is.fn(options)) {
343+
callback = options;
344+
options = {};
345+
}
346+
347+
options = extend({}, options, {
348+
filter: 'network eq .*' + this.formattedName
349+
});
350+
351+
return this.compute.getSubnetworks(options, callback);
352+
};
353+
217354
/**
218355
* Delete the network.
219356
*

0 commit comments

Comments
 (0)