Skip to content

Commit 922d44a

Browse files
committed
Compute: add Network support, minor fixes to docs
1 parent 0acb7b6 commit 922d44a

File tree

4 files changed

+424
-38
lines changed

4 files changed

+424
-38
lines changed

lib/compute/index.js

+191-35
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ var Firewall = require('./firewall.js');
4646
*/
4747
var Snapshot = require('./snapshot.js');
4848

49+
/**
50+
* @type {module:compute/firewall}
51+
* @private
52+
*/
53+
var Network = require('./network.js');
54+
4955
/**
5056
* @type {module:common/util}
5157
* @private
@@ -193,20 +199,37 @@ Compute.prototype.snapshot = function(name) {
193199
return new Snapshot(this, name);
194200
};
195201

202+
/**
203+
* Get a reference to a Google Compute Engine network.
204+
*
205+
* @param {string} name - Name of the existing network.
206+
* @return {module:compute/network}
207+
*
208+
* @example
209+
* var gcloud = require('gcloud')({
210+
* keyFilename: '/path/to/keyfile.json'
211+
* });
212+
*
213+
* var compute = gcloud.compute();
214+
*
215+
* var network = compute.network('network-name');
216+
*/
217+
Compute.prototype.network = function(name) {
218+
return new Network(this, name);
219+
};
220+
196221
/**
197222
* Create a firewall rule. For a detailed description of method's options see
198223
* [API reference](https://goo.gl/kTMHep).
199224
*
200225
* @throws {Error} if a firewall name or firewall options are not provided.
201226
* If allowed ports, source tags or source ranges are not provided.
202227
*
203-
* @param {module:compute} compute - The Google Compute Engine object this
204-
* firewall belongs to.
205228
* @param {string} name - Name of the firewall.
206229
* @param {object} options - Firewall options.
207230
* @param {string=} options.description - Description of the firewall.
208-
* @param {string=} options.network - Network to which the firewall applies.
209-
* Default value is 'global/networks/default'.
231+
* @param {string=|module:compute/network} options.network - Network to which
232+
* the firewall applies. Default value is 'global/networks/default'.
210233
* @param {object[]=} options.allowed - List of allowed protocols and ports.
211234
* @param {string[]=} options.sourceRanges - IP address blocks to which this
212235
* rule applies (in CIDR format).
@@ -252,45 +275,84 @@ Compute.prototype.createFirewall = function(name, options, callback) {
252275
throw new Error('Source ranges or source tags must be provided.');
253276
}
254277

255-
var body = {};
256-
body.name = name;
257-
if (util.is(options.description, 'string')) {
258-
body.description = options.description;
259-
}
260-
if (util.is(options.network, 'string')) {
261-
body.network = options.network;
278+
if (util.is(options.network, 'object')) {
279+
options.network =
280+
'projects/' +
281+
options.network.compute.projectId +
282+
'/global/networks/' +
283+
options.network.name;
262284
} else {
263-
body.network = 'global/networks/default';
264-
}
265-
if (util.is(options.allowed, 'array')) {
266-
body.allowed = options.allowed;
267-
}
268-
if (util.is(options.sourceRanges, 'array')) {
269-
body.sourceRanges = options.sourceRanges;
270-
}
271-
if (util.is(options.sourceTags, 'array')) {
272-
body.sourceTags = options.sourceTags;
273-
}
274-
if (util.is(options.targetTags, 'array')) {
275-
body.targetTags = options.targetTags;
285+
options.network = options.network || 'global/networks/default';
276286
}
277287

288+
options.name = name;
289+
278290
var self = this;
279-
this.makeReq_(
280-
'POST',
291+
this.makeReq_('POST',
281292
'/global/firewalls',
282293
null,
283-
body, function(err, resp) {
294+
options, function(err, resp) {
284295
if (err) {
285296
callback(err);
286297
return;
287298
}
288299
var firewall = self.firewall(name);
289-
firewall.name = options;
300+
firewall.metadata = options;
290301
callback(null, firewall, resp);
291302
});
292303
};
293304

305+
/**
306+
* Create a network. For a detailed description of method's options see
307+
* [API reference](https://goo.gl/cWYdER).
308+
*
309+
* @throws {Error} if a network name or an IPv4 range is not provided.
310+
*
311+
* @param {string} name - Name of the network.
312+
* @param {object} options - Network options.
313+
* @param {string} options.IPv4Range - CIDR range of addresses that are legal on
314+
* this network.
315+
* @param {string=} options.description - Description of the network.
316+
* @param {function} callback - The callback function.
317+
*
318+
* @example
319+
* var callback = function(err, network, apiResponse) {
320+
* // `network` is a Network object.
321+
* };
322+
*
323+
* var compute = gcloud.compute({
324+
* projectId: 'grape-spaceship-123'
325+
* });
326+
*
327+
* var network = compute.createNetwork('network',
328+
* {
329+
* IPv4Range: '192.168.0.0/16'
330+
* }, callback);
331+
*/
332+
Compute.prototype.createNetwork = function(name, options, callback) {
333+
if (!name) {
334+
throw new Error('A name is needed to use a Compute Engine Network.');
335+
} else if (!/^(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)$/.test(name)) {
336+
throw new Error('Name must match [a-z]([-a-z0-9]{0,61}[a-z0-9])?');
337+
}
338+
if (!options || !util.is(options.IPv4Range, 'string')) {
339+
throw new Error('An IPv4 range must be provided.');
340+
}
341+
342+
options.name = name;
343+
344+
var self = this;
345+
this.makeReq_('POST', '/global/networks', null, options, function(err, resp) {
346+
if (err) {
347+
callback(err);
348+
return;
349+
}
350+
var network = self.network(name);
351+
network.metadata = options;
352+
callback(null, network, resp);
353+
});
354+
};
355+
294356
/**
295357
* Get a list of instances. For a detailed description of method's options see
296358
* [API reference](https://goo.gl/GeDAwy).
@@ -442,7 +504,7 @@ Compute.prototype.getInstances = function(options, callback) {
442504
* compute.getDisks()
443505
* .on('error', console.error)
444506
* .on('data', function(disk) {
445-
* // `disk` is an `Disk` object.
507+
* // `disk` is a `Disk` object.
446508
* })
447509
* .on('end', function() {
448510
* // All disks retrieved.
@@ -646,7 +708,7 @@ Compute.prototype.getAddresses = function(options, callback) {
646708
* compute.getSnapshots()
647709
* .on('error', console.error)
648710
* .on('data', function(snapshot) {
649-
* // `snapshot` is an `Snapshot` object.
711+
* // `snapshot` is a `Snapshot` object.
650712
* })
651713
* .on('end', function() {
652714
* // All snapshots retrieved.
@@ -716,7 +778,6 @@ Compute.prototype.getSnapshots = function(options, callback) {
716778
*
717779
* compute.getFirewalls(
718780
* {
719-
* zone: 'europe-west1-b',
720781
* filter: 'name eq firewall-[0-9]'
721782
* }, callback);
722783
*
@@ -736,12 +797,12 @@ Compute.prototype.getSnapshots = function(options, callback) {
736797
* }, callback);
737798
*
738799
* //-
739-
* // Get the snapshots from your project as a readable object stream.
800+
* // Get the firewalls from your project as a readable object stream.
740801
* //-
741-
* compute.getSnapshots()
802+
* compute.getFirewalls()
742803
* .on('error', console.error)
743804
* .on('data', function(firewall) {
744-
* // `firewall` is an `Firewall` object.
805+
* // `firewall` is a `Firewall` object.
745806
* })
746807
* .on('end', function() {
747808
* // All firewalls retrieved.
@@ -788,6 +849,100 @@ Compute.prototype.getFirewalls = function(options, callback) {
788849
});
789850
};
790851

852+
/**
853+
* Get a list of networks. For a detailed description of method's options
854+
* see [API reference](https://goo.gl/yx70Gc).
855+
*
856+
* @param {object} options - Network search options.
857+
* @param {boolean=} options.autoPaginate - Have pagination handled
858+
* automatically. Default: true.
859+
* @param {number=} options.maxResults - Maximum number of networks to return.
860+
* @param {string=} options.filter - Search filter. The filter must contain:
861+
* `FIELD_NAME COMPARISON_STRING LITERAL_STRING`. `FIELD_NAME` is the name
862+
* of the field to compare. `COMPARISON` is the comparison operator that can
863+
* be either `eq` or `ne`. `LITERAL_STRING` is the string to filter to.
864+
* For string fields `LITERAL_STRING` can be a regular expression.
865+
* @param {string=} pageToken - Page identifier used in paginated search.
866+
* @param {function} callback - The callback function.
867+
*
868+
* @example
869+
* var callback = function(err, networks) {
870+
* // `networks` is an array of `Network` objects.
871+
* };
872+
*
873+
* compute.getNetworks(
874+
* {
875+
* filter: 'name eq network-[0-9]'
876+
* }, callback);
877+
*
878+
* //-
879+
* // To control how many API requests are made and page through the results
880+
* // manually, set `autoPaginate` to `false`.
881+
* //-
882+
* var callback = function(err, networks, nextQuery, apiResponse) {
883+
* if (nextQuery) {
884+
* // More results exist.
885+
* compute.getNetworks(nextQuery, callback);
886+
* }
887+
* };
888+
*
889+
* compute.getNetworks({
890+
* autoPaginate: false
891+
* }, callback);
892+
*
893+
* //-
894+
* // Get the networks from your project as a readable object stream.
895+
* //-
896+
* compute.getNetworks()
897+
* .on('error', console.error)
898+
* .on('data', function(network) {
899+
* // `network` is a `Network` object.
900+
* })
901+
* .on('end', function() {
902+
* // All networks retrieved.
903+
* });
904+
*
905+
* //-
906+
* // If you anticipate many results, you can end a stream early to prevent
907+
* // unnecessary processing and API requests.
908+
* //-
909+
* compute.getNetworks()
910+
* .on('data', function(network) {
911+
* this.end();
912+
* });
913+
*/
914+
Compute.prototype.getNetworks = function(options, callback) {
915+
if (util.is(options, 'function')) {
916+
callback = options;
917+
options = {};
918+
}
919+
920+
options = options || {};
921+
922+
var self = this;
923+
this.makeReq_('GET', '/global/networks', options, null, function(err, resp) {
924+
if (err) {
925+
callback(err);
926+
return;
927+
}
928+
929+
var nextQuery = null;
930+
931+
if (resp.nextPageToken) {
932+
nextQuery = extend({}, options, {
933+
pageToken: resp.nextPageToken
934+
});
935+
}
936+
937+
var networks = (resp.items || []).map(function(networkObject) {
938+
var network = self.network(networkObject.name);
939+
network.metadata = networkObject;
940+
return network;
941+
});
942+
callback(null, networks, nextQuery, resp);
943+
});
944+
};
945+
791946
/**
792947
* Make a new request object from the provided arguments and wrap the callback
793948
* to intercept non-successful responses.
@@ -824,7 +979,8 @@ streamRouter.extend(Compute, [
824979
'getDisks',
825980
'getAddresses',
826981
'getSnapshots',
827-
'getFirewalls'
982+
'getFirewalls',
983+
'getNetworks'
828984
]);
829985

830986
module.exports = Compute;

0 commit comments

Comments
 (0)