Skip to content

Commit 5689f96

Browse files
ashleyschuettstephenplusplus
authored andcommitted
compute: implement subnetworks (#1435)
compute: implement subnetworks
1 parent 6cbbc37 commit 5689f96

11 files changed

Lines changed: 1366 additions & 6 deletions

File tree

docs/toc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@
101101
}, {
102102
"title": "Snapshot",
103103
"type": "compute/snapshot"
104+
}, {
105+
"title": "Subnetwork",
106+
"type": "compute/subnetwork"
104107
}, {
105108
"title": "VM",
106109
"type": "compute/vm"

lib/compute/index.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,6 +1908,121 @@ Compute.prototype.getSnapshots = function(options, callback) {
19081908
});
19091909
};
19101910

1911+
/**
1912+
* Get a list of subnetworks in this project.
1913+
*
1914+
* @resource [Subnetworks Overview]{@link https://cloud.google.com/compute/docs/subnetworks}
1915+
* @resource [Subnetworks: list API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/subnetworks}
1916+
*
1917+
* @param {object=} options - Subnetwork search options.
1918+
* @param {boolean} options.autoPaginate - Have pagination handled
1919+
* automatically. Default: true.
1920+
* @param {string} options.filter - Search filter in the format of
1921+
* `{name} {comparison} {filterString}`.
1922+
* - **`name`**: the name of the field to compare
1923+
* - **`comparison`**: the comparison operator, `eq` (equal) or `ne`
1924+
* (not equal)
1925+
* - **`filterString`**: the string to filter to. For string fields, this
1926+
* can be a regular expression.
1927+
* @param {number} options.maxApiCalls - Maximum number of API calls to make.
1928+
* @param {number} options.maxResults - Maximum number of subnetworks to return.
1929+
* @param {string} options.pageToken - A previously-returned page token
1930+
* representing part of the larger set of results to view.
1931+
* @param {function} callback - The callback function.
1932+
* @param {?error} callback.err - An error returned while making this request.
1933+
* @param {module:compute/subnetwork} callback.subnetworks - Subnetwork objects
1934+
* from your project.
1935+
* @param {?object} callback.nextQuery - If present, query with this object to
1936+
* check for more results.
1937+
* @param {object} callback.apiResponse - The full API response.
1938+
*
1939+
* @example
1940+
* gce.getSubnetworks(function(err, subnetworks) {
1941+
* // `subnetworks` is an array of `Subnetworks` objects.
1942+
* });
1943+
*
1944+
* //-
1945+
* // To control how many API requests are made and page through the results
1946+
* // manually, set `autoPaginate` to `false`.
1947+
* //-
1948+
* function callback(err, subnetworks, nextQuery, apiResponse) {
1949+
* if (nextQuery) {
1950+
* // More results exist.
1951+
* gce.getSubnetworks(nextQuery, callback);
1952+
* }
1953+
* }
1954+
*
1955+
* gce.getSubnetworks({
1956+
* autoPaginate: false
1957+
* }, callback);
1958+
*
1959+
* //-
1960+
* // Get the subnetworks from your project as a readable object stream.
1961+
* //-
1962+
* gce.getSubnetworks()
1963+
* .on('error', console.error)
1964+
* .on('data', function(subnetwork) {
1965+
* // `subnetwork` is a `Subnetwork` object.
1966+
* })
1967+
* .on('end', function() {
1968+
* // All subnetworks retrieved.
1969+
* });
1970+
*
1971+
* //-
1972+
* // If you anticipate many results, you can end a stream early to prevent
1973+
* // unnecessary processing and API requests.
1974+
* //-
1975+
* gce.getSubnetworks()
1976+
* .on('data', function(subnetwork) {
1977+
* this.end();
1978+
* });
1979+
*/
1980+
Compute.prototype.getSubnetworks = function(options, callback) {
1981+
var self = this;
1982+
1983+
if (is.fn(options)) {
1984+
callback = options;
1985+
options = {};
1986+
}
1987+
1988+
options = options || {};
1989+
1990+
this.request({
1991+
uri: '/aggregated/subnetworks',
1992+
qs: options
1993+
}, function(err, resp) {
1994+
if (err) {
1995+
callback(err, null, null, resp);
1996+
return;
1997+
}
1998+
1999+
var nextQuery = null;
2000+
2001+
if (resp.nextPageToken) {
2002+
nextQuery = extend({}, options, {
2003+
pageToken: resp.nextPageToken
2004+
});
2005+
}
2006+
2007+
var regions = resp.items || {};
2008+
2009+
var subnetworks = Object.keys(regions).reduce(function(acc, regionName) {
2010+
var region = self.region(regionName.replace('regions/', ''));
2011+
var subnetworks = regions[regionName].subnetworks || [];
2012+
2013+
subnetworks.forEach(function(subnetwork) {
2014+
var subnetworkInstance = region.subnetwork(subnetwork.name);
2015+
subnetworkInstance.metadata = subnetwork;
2016+
acc.push(subnetworkInstance);
2017+
});
2018+
2019+
return acc;
2020+
}, []);
2021+
2022+
callback(null, subnetworks, nextQuery, resp);
2023+
});
2024+
};
2025+
19112026
/**
19122027
* Get a list of virtual machine instances.
19132028
*
@@ -2272,6 +2387,7 @@ streamRouter.extend(Compute, [
22722387
'getRules',
22732388
'getServices',
22742389
'getSnapshots',
2390+
'getSubnetworks',
22752391
'getVMs',
22762392
'getZones'
22772393
]);

lib/compute/network.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,145 @@ Network.prototype.createFirewall = function(name, config, callback) {
214214
this.compute.createFirewall(name, config, callback);
215215
};
216216

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

0 commit comments

Comments
 (0)