diff --git a/docs/toc.json b/docs/toc.json index dcbdc7537fe..fbd147aaaca 100644 --- a/docs/toc.json +++ b/docs/toc.json @@ -101,6 +101,9 @@ }, { "title": "Snapshot", "type": "compute/snapshot" + }, { + "title": "Subnetwork", + "type": "compute/subnetwork" }, { "title": "VM", "type": "compute/vm" diff --git a/lib/compute/index.js b/lib/compute/index.js index f70c055b3ba..90faf1a42d5 100644 --- a/lib/compute/index.js +++ b/lib/compute/index.js @@ -1162,122 +1162,6 @@ Compute.prototype.getFirewalls = 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 health checks. * @@ -2024,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. * diff --git a/lib/compute/network.js b/lib/compute/network.js index f61fa54c392..e62b95dd4f7 100644 --- a/lib/compute/network.js +++ b/lib/compute/network.js @@ -37,12 +37,6 @@ var ServiceObject = require('../common/service-object.js'); */ var util = require('../common/util.js'); -/** - * @type {module:compute/region} - * @private - */ -var Region = require('./region.js'); - /*! Developer Documentation * * @param {module:compute} compute - The Compute module this network belongs to. @@ -232,7 +226,8 @@ Network.prototype.createFirewall = function(name, config, callback) { * @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 + * 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. @@ -244,10 +239,10 @@ Network.prototype.createFirewall = function(name, config, callback) { * @param {object} callback.apiResponse - The full API response. * * @example - * var name = 'new-subnetwork-name'; + * var region = gce.region('us-east1'); * * var config = { - * region : 'us-east1', + * region: region, * range: '10.0.1.0/24' * }; * @@ -256,25 +251,24 @@ Network.prototype.createFirewall = function(name, config, callback) { * * // `operation` is an Operation object that can be used to check the status * // of the request. - * }); + * } * - * network.createSubnetwork(name, config, callback); + * network.createSubnetwork('new-subnetwork-name', config, callback); */ Network.prototype.createSubnetwork = function(name, config, callback) { config = extend({}, config, { network: this.formattedName }); - var region; + var region = config.region; - if (config.region instanceof Region) { - region = config.region; - } else { - region = this.compute.region(config.region); + if (is.string(region)) { + region = this.compute.region(region); } + delete config.region; - return region.createSubnetwork(name, config, callback); + region.createSubnetwork(name, config, callback); }; /** diff --git a/lib/compute/region.js b/lib/compute/region.js index 2092c60a825..9a90416be7b 100644 --- a/lib/compute/region.js +++ b/lib/compute/region.js @@ -49,16 +49,16 @@ var Operation = require('./operation.js'); var Rule = require('./rule.js'); /** - * @type {module:compute/subnetwork} + * @type {module:common/service-object} * @private */ -var Subnetwork = require('./subnetwork.js'); +var ServiceObject = require('../common/service-object.js'); /** - * @type {module:common/service-object} + * @type {module:compute/subnetwork} * @private */ -var ServiceObject = require('../common/service-object.js'); +var Subnetwork = require('./subnetwork.js'); /** * @type {module:common/stream-router} @@ -226,127 +226,6 @@ Region.prototype.createAddress = function(name, options, callback) { }); }; -/** - * Get a list of subnetworks in this region. - * - * @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/subnetworks} callback.subnetworks - Subnetwork objects - * from this region. - * @param {?object} callback.nextQuery - If present, query with this object to - * check for more results. - * @param {object} callback.apiResponse - The full API response. - * - * @example - * region.getSubnetworks(function(err, subnetworks) { - * // `subnetworks` is an array of `Subnetwork` 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. - * region.getSubnetworks(nextQuery, callback); - * } - * } - * - * region.getSubnetworks({ - * autoPaginate: false - * }, callback); - * - * //- - * // Get the subnetworks from this region as a readable object stream. - * //- - * region.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. - * //- - * region.getSubnetworks() - * .on('data', function(subnetwork) { - * this.end(); - * }); - */ -Region.prototype.getSubnetworks = function(options, callback) { - var self = this; - - if (is.fn(options)) { - callback = options; - options = {}; - } - - options = options || {}; - - this.request({ - uri: '/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 subnetworks = (resp.items || []).map(function(subnetwork) { - var subnetworkInstance = self.subnetwork(subnetwork.name); - subnetworkInstance.metadata = subnetwork; - return subnetworkInstance; - }); - - callback(null, subnetworks, nextQuery, resp); - }); -}; - -/** - * Get a reference to a Google Compute Engine subnetwork in this region. - * - * @resource [Instances and Networks]{@link https://cloud.google.com/compute/docs/subnetworks} - * - * @param {string} name - Name of the subnetwork. - * @return {module:compute/subnetwork} - * - * @example - * var subnetwork = region.subnetwork('subnetwork-name'); - */ -Region.prototype.subnetwork = function(name) { - return new Subnetwork(this, name); -}; - /** * Create a subnetwork in this region. * @@ -791,6 +670,112 @@ Region.prototype.getRules = function(options, callback) { }); }; +/** + * Get a list of subnetworks in this region. + * + * @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/subnetworks} callback.subnetworks - Subnetwork objects + * from this region. + * @param {?object} callback.nextQuery - If present, query with this object to + * check for more results. + * @param {object} callback.apiResponse - The full API response. + * + * @example + * region.getSubnetworks(function(err, subnetworks) { + * // `subnetworks` is an array of `Subnetwork` 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. + * region.getSubnetworks(nextQuery, callback); + * } + * } + * + * region.getSubnetworks({ + * autoPaginate: false + * }, callback); + * + * //- + * // Get the subnetworks from this region as a readable object stream. + * //- + * region.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. + * //- + * region.getSubnetworks() + * .on('data', function(subnetwork) { + * this.end(); + * }); + */ +Region.prototype.getSubnetworks = function(options, callback) { + var self = this; + + if (is.fn(options)) { + callback = options; + options = {}; + } + + options = options || {}; + + this.request({ + uri: '/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 subnetworks = (resp.items || []).map(function(subnetwork) { + var subnetworkInstance = self.subnetwork(subnetwork.name); + subnetworkInstance.metadata = subnetwork; + return subnetworkInstance; + }); + + callback(null, subnetworks, nextQuery, resp); + }); +}; + /** * Get a reference to a Google Compute Engine region operation. * @@ -819,6 +804,21 @@ Region.prototype.rule = function(name) { return new Rule(this, name); }; +/** + * Get a reference to a Google Compute Engine subnetwork in this region. + * + * @resource [Subnetworks Overview]{@link https://cloud.google.com/compute/docs/subnetworks} + * + * @param {string} name - Name of the subnetwork. + * @return {module:compute/subnetwork} + * + * @example + * var subnetwork = region.subnetwork('subnetwork-name'); + */ +Region.prototype.subnetwork = function(name) { + return new Subnetwork(this, name); +}; + /*! Developer Documentation * * These methods can be used with either a callback or as a readable object diff --git a/lib/compute/subnetwork.js b/lib/compute/subnetwork.js index e8861bafd4a..350d91b9449 100644 --- a/lib/compute/subnetwork.js +++ b/lib/compute/subnetwork.js @@ -20,7 +20,6 @@ 'use strict'; -var format = require('string-format-obj'); var nodeutil = require('util'); /** @@ -45,7 +44,7 @@ var util = require('../common/util.js'); * An Subnetwork object allows you to interact with a Google Compute Engine * subnetwork. * - * @resource [Subnetworks]{@link https://cloud.google.com/compute/docs/subnetworks} + * @resource [Subnetworks Overview]{@link https://cloud.google.com/compute/docs/subnetworks} * @resource [Subnetwork Resource]{@link https://cloud.google.com/compute/docs/reference/v1/subnetworks} * * @constructor @@ -78,7 +77,7 @@ function Subnetwork(region, name) { * // ... * }; * - * function(err, subnetwork, operation, apiResponse) { + * function callback(err, subnetwork, operation, apiResponse) { * // `subnetwork` is a Subnetwork object. * * // `operation` is an Operation object that can be used to check the @@ -146,28 +145,10 @@ function Subnetwork(region, name) { createMethod: region.createSubnetwork.bind(region), methods: methods }); - - this.formattedName = Subnetwork.formatName_(region, name); } nodeutil.inherits(Subnetwork, ServiceObject); -/** - * Format a subnetwork's name how the API expects. - * - * @private - * - * @param {module:region} region - The Region object this subnetwork belongs to. - * @param {string} name - The name of the subnetwork. - * @return {string} - The formatted name. - */ -Subnetwork.formatName_ = function(region, name) { - return format('regions/{region}/subnetworks/{name}', { - region: region.name, - name: name - }); -}; - /** * Delete the subnetwork. * diff --git a/system-test/compute.js b/system-test/compute.js index 99f6b174ddc..2c2b80a68b8 100644 --- a/system-test/compute.js +++ b/system-test/compute.js @@ -1130,25 +1130,24 @@ describe('Compute', function() { }); describe('subnetworks', function() { - var SUBNETWORK_NAME = generateName('subnetwork'); - var subnetwork = region.subnetwork(SUBNETWORK_NAME); - var NETWORK_NAME = generateName('network'); var network = compute.network(NETWORK_NAME); - var CONFIG = { + var SUBNETWORK_NAME = generateName('subnetwork'); + var subnetwork = region.subnetwork(SUBNETWORK_NAME); + + var NETWORK_CONFIG = { autoCreateSubnetworks: false }; - var SUBNETWORK_CONFIG = { network: 'global/networks/' + NETWORK_NAME, - ipCidrRange: '10.0.1.0/24' + range: '10.0.1.0/24' }; before(function(done) { async.series([ - create(network, CONFIG), + create(network, NETWORK_CONFIG), create(subnetwork, SUBNETWORK_CONFIG) ], done); }); @@ -1183,6 +1182,28 @@ describe('Compute', function() { }); }); + it('should get a list of regional subnetworks', function(done) { + region.getSubnetworks(function(err, subnetworks) { + assert.ifError(err); + assert(subnetworks.length > 0); + done(); + }); + }); + + it('should get a list of regional subnetworks in stream', function(done) { + var resultsMatched = 0; + + region.getSubnetworks() + .on('error', done) + .on('data', function() { + resultsMatched++; + }) + .on('end', function() { + assert(resultsMatched > 0); + done(); + }); + }); + it('should access a subnetwork through a Region', function(done) { region.subnetwork(SUBNETWORK_NAME).getMetadata(done); }); diff --git a/test/compute/index.js b/test/compute/index.js index f54e3cd3313..017b412dd7d 100644 --- a/test/compute/index.js +++ b/test/compute/index.js @@ -1141,113 +1141,6 @@ describe('Compute', function() { }); }); - describe('getSubnetworks', function() { - it('should accept only a callback', function(done) { - compute.request = function(reqOpts) { - assert.deepEqual(reqOpts.qs, {}); - done(); - }; - - compute.getSubnetworks(assert.ifError); - }); - - it('should make the correct API request', function(done) { - var options = {}; - - compute.request = function(reqOpts) { - assert.strictEqual(reqOpts.uri, '/aggregated/subnetworks'); - assert.strictEqual(reqOpts.qs, options); - done(); - }; - - compute.getSubnetworks(options, assert.ifError); - }); - - describe('error', function() { - var error = new Error('Error.'); - var apiResponse = { a: 'b', c: 'd' }; - - beforeEach(function() { - compute.request = function(reqOpts, callback) { - callback(error, apiResponse); - }; - }); - - it('should execute callback with error & API response', function(done) { - compute.getSubnetworks({}, function(err, subnetworks, nextQuery, resp) { - assert.strictEqual(err, error); - assert.strictEqual(subnetworks, null); - assert.strictEqual(nextQuery, null); - assert.strictEqual(resp, apiResponse); - - done(); - }); - }); - }); - - describe('success', function() { - var REGION_NAME = 'region-1'; - var FULL_REGION_NAME = 'regions/' + REGION_NAME; - - var subnetwork = { name: 'subnetwork-1' }; - var apiResponse = { - items: {} - }; - - apiResponse.items[FULL_REGION_NAME] = { - subnetworks: [subnetwork] - }; - - beforeEach(function() { - compute.request = function(reqOpts, callback) { - callback(null, apiResponse); - }; - }); - - it('should create Subnetwork objects from the response', function(done) { - var region = {}; - - compute.region = function(name) { - assert.strictEqual(name, REGION_NAME); - return region; - }; - - region.subnetwork = function(name) { - assert.strictEqual(name, subnetwork.name); - setImmediate(done); - return subnetwork; - }; - - compute.getSubnetworks({}, assert.ifError); - }); - - it('should build a nextQuery if necessary', function(done) { - var apiResponseWithNextPageToken = extend({}, apiResponse, { - nextPageToken: 'next-page-token' - }); - - var query = { a: 'b', c: 'd' }; - var originalQuery = extend({}, query); - - compute.request = function(reqOpts, callback) { - callback(null, apiResponseWithNextPageToken); - }; - - compute.getSubnetworks(query, function(err, subnetworks, nextQuery) { - assert.ifError(err); - - assert.deepEqual(query, originalQuery); - - assert.deepEqual(nextQuery, extend({}, query, { - pageToken: apiResponseWithNextPageToken.nextPageToken - })); - - done(); - }); - }); - }); - }); - describe('getFirewalls', function() { it('should accept only a callback', function(done) { compute.request = function(reqOpts) { @@ -2140,6 +2033,123 @@ describe('Compute', function() { }); }); + describe('getSubnetworks', function() { + it('should accept only a callback', function(done) { + compute.request = function(reqOpts) { + assert.deepEqual(reqOpts.qs, {}); + done(); + }; + + compute.getSubnetworks(assert.ifError); + }); + + it('should make the correct API request', function(done) { + var options = {}; + + compute.request = function(reqOpts) { + assert.strictEqual(reqOpts.uri, '/aggregated/subnetworks'); + assert.strictEqual(reqOpts.qs, options); + done(); + }; + + compute.getSubnetworks(options, assert.ifError); + }); + + describe('error', function() { + var error = new Error('Error.'); + var apiResponse = { a: 'b', c: 'd' }; + + beforeEach(function() { + compute.request = function(reqOpts, callback) { + callback(error, apiResponse); + }; + }); + + it('should execute callback with error & API response', function(done) { + compute.getSubnetworks({}, function(err, subnetworks, nextQuery, resp) { + assert.strictEqual(err, error); + assert.strictEqual(subnetworks, null); + assert.strictEqual(nextQuery, null); + assert.strictEqual(resp, apiResponse); + + done(); + }); + }); + }); + + describe('success', function() { + var REGION_NAME = 'region-1'; + var FULL_REGION_NAME = 'regions/' + REGION_NAME; + + var subnetwork = { name: 'subnetwork-1' }; + var apiResponse = { + items: {} + }; + + apiResponse.items[FULL_REGION_NAME] = { + subnetworks: [subnetwork] + }; + + beforeEach(function() { + compute.request = function(reqOpts, callback) { + callback(null, apiResponse); + }; + }); + + it('should create Subnetwork objects from the response', function(done) { + var region = {}; + var fakeSubnetwork = {}; + + compute.region = function(name) { + assert.strictEqual(name, REGION_NAME); + return region; + }; + + region.subnetwork = function(name) { + assert.strictEqual(name, subnetwork.name); + return fakeSubnetwork; + }; + + compute.getSubnetworks({}, function(err, subnetworks, nextQuery, resp) { + assert.ifError(err); + + assert.strictEqual(subnetworks[0], fakeSubnetwork); + assert.strictEqual(subnetworks[0].metadata, subnetwork); + + assert.strictEqual(nextQuery, null); + assert.strictEqual(resp, apiResponse); + + done(); + }); + }); + + it('should build a nextQuery if necessary', function(done) { + var apiResponseWithNextPageToken = extend({}, apiResponse, { + nextPageToken: 'next-page-token' + }); + + var query = { a: 'b', c: 'd' }; + var originalQuery = extend({}, query); + + compute.request = function(reqOpts, callback) { + callback(null, apiResponseWithNextPageToken); + }; + + compute.getSubnetworks(query, function(err, subnetworks, nextQuery) { + assert.ifError(err); + + assert.deepEqual(query, originalQuery); + + assert.deepEqual(nextQuery, extend({}, query, { + pageToken: apiResponseWithNextPageToken.nextPageToken + })); + + done(); + }); + }); + }); + }); + describe('getVMs', function() { it('should work with only a callback', function(done) { compute.request = function(reqOpts) { diff --git a/test/compute/network.js b/test/compute/network.js index cda603e0744..a0fc4dc6790 100644 --- a/test/compute/network.js +++ b/test/compute/network.js @@ -153,29 +153,30 @@ describe('Network', function() { }); describe('createSubnetwork', function() { - it('should makes the correct call to Region', function(done) { + it('should call region.createSubnetwork correctly', function(done) { var name = 'subnetwork-name'; var region = {}; - var config = { a: 'b', c: 'd', region: REGION_NAME}; + var config = { + a: 'b', + c: 'd', + region: REGION_NAME + }; + var expectedConfig = extend({}, config, { - network: network.formattedName, - name: name + network: network.formattedName }); delete expectedConfig.region; - network.compute.region = function(name_) { - assert.strictEqual(name_, REGION_NAME); + network.compute.region = function(name) { + assert.strictEqual(name, REGION_NAME); return region; }; - region.createSubnetwork = function(name_, config_, callback){ - callback(); - }; + region.createSubnetwork = function(name_, config, callback) { + assert.strictEqual(name_, name); + assert.deepEqual(config, expectedConfig); - network.compute.request = function(config_) { - assert.strictEqual(config_.json.name, name); - assert.deepEqual(config_.json, expectedConfig); - done(); + callback(); // done(); }; network.createSubnetwork(name, config, done); @@ -271,31 +272,31 @@ describe('Network', function() { }); }); - describe('getSubnetworks', function() { + describe('getFirewalls', function() { it('should make the correct call to Compute', function(done) { var options = { a: 'b', c: 'd' }; var expectedOptions = extend({}, options, { filter: 'network eq .*' + network.formattedName }); - network.compute.getSubnetworks = function(options, callback) { + network.compute.getFirewalls = function(options, callback) { assert.deepEqual(options, expectedOptions); callback(); }; - network.getSubnetworks(options, done); + network.getFirewalls(options, done); }); it('should not require options', function(done) { - network.compute.getSubnetworks = function(options, callback) { + network.compute.getFirewalls = function(options, callback) { callback(); }; - network.getSubnetworks(done); + network.getFirewalls(done); }); it('should not require any arguments', function(done) { - network.compute.getSubnetworks = function(options, callback) { + network.compute.getFirewalls = function(options, callback) { assert.deepEqual(options, { filter: 'network eq .*' + network.formattedName }); @@ -303,45 +304,45 @@ describe('Network', function() { done(); }; - network.getSubnetworks(); + network.getFirewalls(); }); it('should return the result of calling Compute', function() { - var resultOfgetSubnetworks = {}; + var resultOfGetFirewalls = {}; - network.compute.getSubnetworks = function() { - return resultOfgetSubnetworks; + network.compute.getFirewalls = function() { + return resultOfGetFirewalls; }; - assert.strictEqual(network.getSubnetworks(), resultOfgetSubnetworks); + assert.strictEqual(network.getFirewalls(), resultOfGetFirewalls); }); }); - describe('getFirewalls', function() { - it('should make the correct call to Compute', function(done) { + describe('getSubnetworks', function() { + it('should call to compute.getSubnetworks correctly', function(done) { var options = { a: 'b', c: 'd' }; var expectedOptions = extend({}, options, { filter: 'network eq .*' + network.formattedName }); - network.compute.getFirewalls = function(options, callback) { + network.compute.getSubnetworks = function(options, callback) { assert.deepEqual(options, expectedOptions); callback(); }; - network.getFirewalls(options, done); + network.getSubnetworks(options, done); }); it('should not require options', function(done) { - network.compute.getFirewalls = function(options, callback) { + network.compute.getSubnetworks = function(options, callback) { callback(); }; - network.getFirewalls(done); + network.getSubnetworks(done); }); it('should not require any arguments', function(done) { - network.compute.getFirewalls = function(options, callback) { + network.compute.getSubnetworks = function(options, callback) { assert.deepEqual(options, { filter: 'network eq .*' + network.formattedName }); @@ -349,17 +350,17 @@ describe('Network', function() { done(); }; - network.getFirewalls(); + network.getSubnetworks(); }); it('should return the result of calling Compute', function() { - var resultOfGetFirewalls = {}; + var resultOfGetSubnetworks = {}; - network.compute.getFirewalls = function() { - return resultOfGetFirewalls; + network.compute.getSubnetworks = function() { + return resultOfGetSubnetworks; }; - assert.strictEqual(network.getFirewalls(), resultOfGetFirewalls); + assert.strictEqual(network.getSubnetworks(), resultOfGetSubnetworks); }); }); }); diff --git a/test/compute/region.js b/test/compute/region.js index f1e40b73015..f8c13a0ee92 100644 --- a/test/compute/region.js +++ b/test/compute/region.js @@ -58,8 +58,11 @@ var fakeStreamRouter = { extended = true; methods = arrify(methods); assert.equal(Class.name, 'Region'); - assert.deepEqual(methods, - ['getAddresses', 'getOperations', 'getRules', 'getSubnetworks']); + assert.deepEqual(methods, [ + 'getAddresses', + 'getOperations', + 'getRules', 'getSubnetworks' + ]); } }; @@ -320,36 +323,35 @@ describe('Region', function() { it('should exec callback with Subnetwork, Op & apiResponse', function(done) { - var subnetwork = {}; - var operation = {}; + var subnetwork = {}; + var operation = {}; - region.subnetwork = function(name) { - assert.strictEqual(name, NAME); - return subnetwork; - }; + region.subnetwork = function(name) { + assert.strictEqual(name, NAME); + return subnetwork; + }; - region.operation = function(name) { - assert.strictEqual(name, apiResponse.name); - return operation; - }; + region.operation = function(name) { + assert.strictEqual(name, apiResponse.name); + return operation; + }; - region.createSubnetwork(NAME, OPTIONS, - function(err, subnetwork_, op, resp) { - assert.ifError(err); + region.createSubnetwork(NAME, OPTIONS, + function(err, subnetwork_, op, resp) { + assert.ifError(err); - assert.strictEqual(subnetwork_, subnetwork); + assert.strictEqual(subnetwork_, subnetwork); - assert.strictEqual(op, operation); - assert.strictEqual(op.metadata, resp); + assert.strictEqual(op, operation); + assert.strictEqual(op.metadata, resp); - assert.strictEqual(resp, apiResponse); - done(); + assert.strictEqual(resp, apiResponse); + done(); + }); }); - }); }); }); - describe('getAddresses', function() { it('should accept only a callback', function(done) { region.request = function(reqOpts) { @@ -451,27 +453,27 @@ describe('Region', function() { }); }); - describe('getSubnetworks', function() { + describe('getOperations', function() { it('should accept only a callback', function(done) { region.request = function(reqOpts) { assert.deepEqual(reqOpts.qs, {}); done(); }; - region.getSubnetworks(assert.ifError); + region.getOperations(assert.ifError); }); it('should make the correct API request', function(done) { var query = { a: 'b', c: 'd' }; region.request = function(reqOpts) { - assert.strictEqual(reqOpts.uri, '/subnetworks'); + assert.strictEqual(reqOpts.uri, '/operations'); assert.strictEqual(reqOpts.qs, query); done(); }; - region.getSubnetworks(query, assert.ifError); + region.getOperations(query, assert.ifError); }); describe('error', function() { @@ -485,10 +487,9 @@ describe('Region', function() { }); it('should execute callback with error & API response', function(done) { - region.getSubnetworks({}, - function(err, subnetworks, nextQuery, apiResp) { + region.getOperations({}, function(err, operations, nextQuery, apiResp) { assert.strictEqual(err, error); - assert.strictEqual(subnetworks, null); + assert.strictEqual(operations, null); assert.strictEqual(nextQuery, null); assert.strictEqual(apiResp, apiResponse); done(); @@ -522,7 +523,7 @@ describe('Region', function() { callback(null, apiResponseWithNextPageToken); }; - region.getSubnetworks({}, function(err, subnetworks, nextQuery) { + region.getOperations({}, function(err, operations, nextQuery) { assert.ifError(err); assert.deepEqual(nextQuery, expectedNextQuery); @@ -532,49 +533,48 @@ describe('Region', function() { }); it('should execute callback with Operations & API resp', function(done) { - var subnetwork = {}; + var operation = {}; - region.subnetwork = function(name) { + region.operation = function(name) { assert.strictEqual(name, apiResponse.items[0].name); - return subnetwork; + return operation; }; - region.getSubnetworks({}, - function(err, subnetworks, nextQuery, apiResp) { - assert.ifError(err); + region.getOperations({}, function(err, operations, nextQuery, apiResp) { + assert.ifError(err); - assert.strictEqual(subnetworks[0], subnetwork); - assert.strictEqual(subnetworks[0].metadata, apiResponse.items[0]); + assert.strictEqual(operations[0], operation); + assert.strictEqual(operations[0].metadata, apiResponse.items[0]); - assert.strictEqual(apiResp, apiResponse); + assert.strictEqual(apiResp, apiResponse); - done(); - }); + done(); + }); }); }); }); - describe('getOperations', function() { + describe('getRules', function() { it('should accept only a callback', function(done) { region.request = function(reqOpts) { assert.deepEqual(reqOpts.qs, {}); done(); }; - region.getOperations(assert.ifError); + region.getRules(assert.ifError); }); it('should make the correct API request', function(done) { var query = { a: 'b', c: 'd' }; region.request = function(reqOpts) { - assert.strictEqual(reqOpts.uri, '/operations'); + assert.strictEqual(reqOpts.uri, '/forwardingRules'); assert.strictEqual(reqOpts.qs, query); done(); }; - region.getOperations(query, assert.ifError); + region.getRules(query, assert.ifError); }); describe('error', function() { @@ -588,9 +588,9 @@ describe('Region', function() { }); it('should execute callback with error & API response', function(done) { - region.getOperations({}, function(err, operations, nextQuery, apiResp) { + region.getRules({}, function(err, rules, nextQuery, apiResp) { assert.strictEqual(err, error); - assert.strictEqual(operations, null); + assert.strictEqual(rules, null); assert.strictEqual(nextQuery, null); assert.strictEqual(apiResp, apiResponse); done(); @@ -624,7 +624,7 @@ describe('Region', function() { callback(null, apiResponseWithNextPageToken); }; - region.getOperations({}, function(err, operations, nextQuery) { + region.getRules({}, function(err, rules, nextQuery) { assert.ifError(err); assert.deepEqual(nextQuery, expectedNextQuery); @@ -634,18 +634,18 @@ describe('Region', function() { }); it('should execute callback with Operations & API resp', function(done) { - var operation = {}; + var rule = {}; - region.operation = function(name) { + region.rule = function(name) { assert.strictEqual(name, apiResponse.items[0].name); - return operation; + return rule; }; - region.getOperations({}, function(err, operations, nextQuery, apiResp) { + region.getRules({}, function(err, rules, nextQuery, apiResp) { assert.ifError(err); - assert.strictEqual(operations[0], operation); - assert.strictEqual(operations[0].metadata, apiResponse.items[0]); + assert.strictEqual(rules[0], rule); + assert.strictEqual(rules[0].metadata, apiResponse.items[0]); assert.strictEqual(apiResp, apiResponse); @@ -655,27 +655,27 @@ describe('Region', function() { }); }); - describe('getRules', function() { + describe('getSubnetworks', function() { it('should accept only a callback', function(done) { region.request = function(reqOpts) { assert.deepEqual(reqOpts.qs, {}); done(); }; - region.getRules(assert.ifError); + region.getSubnetworks(assert.ifError); }); it('should make the correct API request', function(done) { var query = { a: 'b', c: 'd' }; region.request = function(reqOpts) { - assert.strictEqual(reqOpts.uri, '/forwardingRules'); + assert.strictEqual(reqOpts.uri, '/subnetworks'); assert.strictEqual(reqOpts.qs, query); done(); }; - region.getRules(query, assert.ifError); + region.getSubnetworks(query, assert.ifError); }); describe('error', function() { @@ -689,11 +689,11 @@ describe('Region', function() { }); it('should execute callback with error & API response', function(done) { - region.getRules({}, function(err, rules, nextQuery, apiResp) { + region.getSubnetworks({}, function(err, subnetworks, nextQuery, resp) { assert.strictEqual(err, error); - assert.strictEqual(rules, null); + assert.strictEqual(subnetworks, null); assert.strictEqual(nextQuery, null); - assert.strictEqual(apiResp, apiResponse); + assert.strictEqual(resp, apiResponse); done(); }); }); @@ -702,7 +702,7 @@ describe('Region', function() { describe('success', function() { var apiResponse = { items: [ - { name: 'operation-name' } + { name: 'subnetwork-name' } ] }; @@ -725,7 +725,7 @@ describe('Region', function() { callback(null, apiResponseWithNextPageToken); }; - region.getRules({}, function(err, rules, nextQuery) { + region.getSubnetworks({}, function(err, subnetworks, nextQuery) { assert.ifError(err); assert.deepEqual(nextQuery, expectedNextQuery); @@ -735,20 +735,20 @@ describe('Region', function() { }); it('should execute callback with Operations & API resp', function(done) { - var rule = {}; + var subnetwork = {}; - region.rule = function(name) { + region.subnetwork = function(name) { assert.strictEqual(name, apiResponse.items[0].name); - return rule; + return subnetwork; }; - region.getRules({}, function(err, rules, nextQuery, apiResp) { + region.getSubnetworks({}, function(err, subnetworks, nextQuery, resp) { assert.ifError(err); - assert.strictEqual(rules[0], rule); - assert.strictEqual(rules[0].metadata, apiResponse.items[0]); + assert.strictEqual(subnetworks[0], subnetwork); + assert.strictEqual(subnetworks[0].metadata, apiResponse.items[0]); - assert.strictEqual(apiResp, apiResponse); + assert.strictEqual(resp, apiResponse); done(); }); @@ -756,17 +756,6 @@ describe('Region', function() { }); }); - describe('subnetwork', function() { - var NAME = 'subnetwork-name'; - - it('should return a Subnetwork object', function() { - var subnetwork = region.subnetwork(NAME); - assert(subnetwork instanceof FakeSubnetwork); - assert.strictEqual(subnetwork.calledWith_[0], region); - assert.strictEqual(subnetwork.calledWith_[1], NAME); - }); - }); - describe('operation', function() { var NAME = 'operation-name'; @@ -788,4 +777,16 @@ describe('Region', function() { assert.strictEqual(rule.calledWith_[1], NAME); }); }); + + describe('subnetwork', function() { + var NAME = 'subnetwork-name'; + + it('should return a Subnetwork object', function() { + var subnetwork = region.subnetwork(NAME); + assert(subnetwork instanceof FakeSubnetwork); + assert.strictEqual(subnetwork.calledWith_[0], region); + assert.strictEqual(subnetwork.calledWith_[1], NAME); + }); + }); }); + diff --git a/test/compute/subnetwork.js b/test/compute/subnetwork.js index 6fc2df1dea5..eaaaf0c2a46 100644 --- a/test/compute/subnetwork.js +++ b/test/compute/subnetwork.js @@ -1,5 +1,5 @@ /** - * Copyright 2015 Google Inc. All Rights Reserved. + * Copyright 2016 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,6 @@ var assert = require('assert'); var extend = require('extend'); -var format = require('string-format-obj'); var mockery = require('mockery-next'); var nodeutil = require('util'); @@ -42,10 +41,6 @@ describe('Subnetwork', function() { createSubnetwork: util.noop, name: REGION_NAME }; - var SUBNETWORK_FULL_NAME = format('regions/{region}/subnetworks/{name}', { - region: REGION_NAME, - name: SUBNETWORK_NAME - }); before(function() { mockery.registerMock( @@ -70,20 +65,22 @@ describe('Subnetwork', function() { }); describe('instantiation', function() { + it('should localize the name', function() { + assert.strictEqual(subnetwork.name, SUBNETWORK_NAME); + }); + it('should localize the region', function() { assert.strictEqual(subnetwork.region, REGION); }); - it('should localize the name', function() { - assert.strictEqual(subnetwork.name, SUBNETWORK_NAME); - }); + it('should inherit from ServiceObject', function() { + var createSubnetworkBound = {}; - it('should inherit from ServiceObject', function(done) { var regionInstance = extend({}, REGION, { createSubnetwork: { bind: function(context) { assert.strictEqual(context, regionInstance); - done(); + return createSubnetworkBound; } } }); @@ -96,6 +93,7 @@ describe('Subnetwork', function() { assert.strictEqual(calledWith.parent, regionInstance); assert.strictEqual(calledWith.baseUrl, '/subnetworks'); assert.strictEqual(calledWith.id, SUBNETWORK_NAME); + assert.strictEqual(calledWith.createMethod, createSubnetworkBound); assert.deepEqual(calledWith.methods, { create: true, exists: true, @@ -105,13 +103,6 @@ describe('Subnetwork', function() { }); }); - describe('formatName_', function() { - it('should format the name', function() { - var formattedName_ = Subnetwork.formatName_(REGION, SUBNETWORK_NAME); - assert.strictEqual(formattedName_, SUBNETWORK_FULL_NAME); - }); - }); - describe('delete', function() { it('should make the correct API request', function(done) { subnetwork.request = function(reqOpts) { @@ -171,6 +162,7 @@ describe('Subnetwork', function() { subnetwork.delete(function(err, operation_, apiResponse_) { assert.ifError(err); assert.strictEqual(operation_, operation); + assert.strictEqual(operation_.metadata, apiResponse); assert.strictEqual(apiResponse_, apiResponse); done(); }); diff --git a/test/docs.js b/test/docs.js index e458b6750ae..ef0362036f3 100644 --- a/test/docs.js +++ b/test/docs.js @@ -16,7 +16,6 @@ 'use strict'; -var assert = require('assert'); var fs = require('fs'); var gcloud = require('../'); var glob = require('glob'); @@ -35,9 +34,8 @@ function runCodeInSandbox(code, sandbox) { }); } catch(err) { // rethrow the error with code for context and resolving issues faster. - var lineCol = err.stack.match('assert-code\.vm:(.+):(.+)'); + var lineCol = err.stack.match('assert-code\.vm:(.+)'); lineCol.line = lineCol[1]; - lineCol.col = lineCol[2]; var lines = code.split('\n') .filter(function(line, index) { @@ -133,7 +131,7 @@ describe('documentation', function() { displayName += '#' + method.id; it('should run ' + displayName + ' example without errors', function() { - assert.doesNotThrow(runCodeInSandbox.bind(null, code, sandbox)); + runCodeInSandbox(code, sandbox); }); }); });