@@ -46,6 +46,12 @@ var Firewall = require('./firewall.js');
46
46
*/
47
47
var Snapshot = require ( './snapshot.js' ) ;
48
48
49
+ /**
50
+ * @type {module:compute/firewall }
51
+ * @private
52
+ */
53
+ var Network = require ( './network.js' ) ;
54
+
49
55
/**
50
56
* @type {module:common/util }
51
57
* @private
@@ -193,20 +199,37 @@ Compute.prototype.snapshot = function(name) {
193
199
return new Snapshot ( this , name ) ;
194
200
} ;
195
201
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
+
196
221
/**
197
222
* Create a firewall rule. For a detailed description of method's options see
198
223
* [API reference](https://goo.gl/kTMHep).
199
224
*
200
225
* @throws {Error } if a firewall name or firewall options are not provided.
201
226
* If allowed ports, source tags or source ranges are not provided.
202
227
*
203
- * @param {module:compute } compute - The Google Compute Engine object this
204
- * firewall belongs to.
205
228
* @param {string } name - Name of the firewall.
206
229
* @param {object } options - Firewall options.
207
230
* @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'.
210
233
* @param {object[]= } options.allowed - List of allowed protocols and ports.
211
234
* @param {string[]= } options.sourceRanges - IP address blocks to which this
212
235
* rule applies (in CIDR format).
@@ -252,45 +275,84 @@ Compute.prototype.createFirewall = function(name, options, callback) {
252
275
throw new Error ( 'Source ranges or source tags must be provided.' ) ;
253
276
}
254
277
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 ;
262
284
} 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' ;
276
286
}
277
287
288
+ options . name = name ;
289
+
278
290
var self = this ;
279
- this . makeReq_ (
280
- 'POST' ,
291
+ this . makeReq_ ( 'POST' ,
281
292
'/global/firewalls' ,
282
293
null ,
283
- body , function ( err , resp ) {
294
+ options , function ( err , resp ) {
284
295
if ( err ) {
285
296
callback ( err ) ;
286
297
return ;
287
298
}
288
299
var firewall = self . firewall ( name ) ;
289
- firewall . name = options ;
300
+ firewall . metadata = options ;
290
301
callback ( null , firewall , resp ) ;
291
302
} ) ;
292
303
} ;
293
304
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 - z 0 - 9 ] { 0 , 61 } [ a - z 0 - 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
+
294
356
/**
295
357
* Get a list of instances. For a detailed description of method's options see
296
358
* [API reference](https://goo.gl/GeDAwy).
@@ -442,7 +504,7 @@ Compute.prototype.getInstances = function(options, callback) {
442
504
* compute.getDisks()
443
505
* .on('error', console.error)
444
506
* .on('data', function(disk) {
445
- * // `disk` is an `Disk` object.
507
+ * // `disk` is a `Disk` object.
446
508
* })
447
509
* .on('end', function() {
448
510
* // All disks retrieved.
@@ -646,7 +708,7 @@ Compute.prototype.getAddresses = function(options, callback) {
646
708
* compute.getSnapshots()
647
709
* .on('error', console.error)
648
710
* .on('data', function(snapshot) {
649
- * // `snapshot` is an `Snapshot` object.
711
+ * // `snapshot` is a `Snapshot` object.
650
712
* })
651
713
* .on('end', function() {
652
714
* // All snapshots retrieved.
@@ -716,7 +778,6 @@ Compute.prototype.getSnapshots = function(options, callback) {
716
778
*
717
779
* compute.getFirewalls(
718
780
* {
719
- * zone: 'europe-west1-b',
720
781
* filter: 'name eq firewall-[0-9]'
721
782
* }, callback);
722
783
*
@@ -736,12 +797,12 @@ Compute.prototype.getSnapshots = function(options, callback) {
736
797
* }, callback);
737
798
*
738
799
* //-
739
- * // Get the snapshots from your project as a readable object stream.
800
+ * // Get the firewalls from your project as a readable object stream.
740
801
* //-
741
- * compute.getSnapshots ()
802
+ * compute.getFirewalls ()
742
803
* .on('error', console.error)
743
804
* .on('data', function(firewall) {
744
- * // `firewall` is an `Firewall` object.
805
+ * // `firewall` is a `Firewall` object.
745
806
* })
746
807
* .on('end', function() {
747
808
* // All firewalls retrieved.
@@ -788,6 +849,100 @@ Compute.prototype.getFirewalls = function(options, callback) {
788
849
} ) ;
789
850
} ;
790
851
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
+
791
946
/**
792
947
* Make a new request object from the provided arguments and wrap the callback
793
948
* to intercept non-successful responses.
@@ -824,7 +979,8 @@ streamRouter.extend(Compute, [
824
979
'getDisks' ,
825
980
'getAddresses' ,
826
981
'getSnapshots' ,
827
- 'getFirewalls'
982
+ 'getFirewalls' ,
983
+ 'getNetworks'
828
984
] ) ;
829
985
830
986
module . exports = Compute ;
0 commit comments