Skip to content

Commit 1be1df5

Browse files
fix docs + add formatPorts
1 parent 87794d5 commit 1be1df5

5 files changed

Lines changed: 66 additions & 38 deletions

File tree

lib/compute/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -953,22 +953,22 @@ Compute.prototype.getDisks = function(options, callback) {
953953
* representing part of the larger set of results to view.
954954
* @param {function} callback - The callback function.
955955
* @param {?error} callback.err - An error returned while making this request.
956-
* @param {module:compute/instance-group} callback.instanceGroups -
956+
* @param {module:compute/instance-group[]} callback.instanceGroups -
957957
* InstanceGroup objects from your project.
958958
* @param {?object} callback.nextQuery - If present, query with this object to
959959
* check for more results.
960960
* @param {object} callback.apiResponse - The full API response.
961961
*
962962
* @example
963963
* gce.getInstanceGroups(function(err, instanceGroups) {
964-
* // `instanceGroup` is an array of `InstanceGroup` objects.
964+
* // `instanceGroups` is an array of `InstanceGroup` objects.
965965
* });
966966
*
967967
* //-
968968
* // To control how many API requests are made and page through the results
969969
* // manually, set `autoPaginate` to `false`.
970970
* //-
971-
* function callback(err, groups, nextQuery, apiResponse) {
971+
* function callback(err, instanceGroups, nextQuery, apiResponse) {
972972
* if (nextQuery) {
973973
* // More results exist.
974974
* gce.getInstanceGroups(nextQuery, callback);

lib/compute/instance-group.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var util = require('../common/util.js');
5151
*/
5252
/**
5353
* You can create and manage groups of virtual machine instances so that you
54-
* don't have to individually control each instance in your project
54+
* don't have to individually control each instance in your project.
5555
*
5656
* @resource [Creating Groups of Instances]{@link https://cloud.google.com/compute/docs/instance-groups}
5757
* @resource [Unmanaged Instance Groups]{@link https://cloud.google.com/compute/docs/instance-groups/unmanaged-groups}
@@ -155,6 +155,24 @@ function InstanceGroup(zone, name) {
155155

156156
nodeutil.inherits(InstanceGroup, ServiceObject);
157157

158+
/**
159+
* Format a map of named ports in the way the API expects.
160+
*
161+
* @private
162+
*
163+
* @param {object} ports - A map of names to ports. The key should be the name,
164+
* and the value the port number.
165+
* @return {object[]} - The formatted array of named ports.
166+
*/
167+
InstanceGroup.formatPorts_ = function(ports) {
168+
return Object.keys(ports).map(function(port) {
169+
return {
170+
name: port,
171+
port: ports[port]
172+
};
173+
});
174+
};
175+
158176
/**
159177
* Add one or more VMs to this instance group.
160178
*
@@ -260,7 +278,7 @@ InstanceGroup.prototype.delete = function(callback) {
260278
* @param {boolean} options.running - Only return instances which are running.
261279
* @param {function} callback - The callback function.
262280
* @param {?error} callback.err - An error returned while making this request.
263-
* @param {module:compute/vm} callback.vms - VM objects from this isntance
281+
* @param {module:compute/vm[]} callback.vms - VM objects from this isntance
264282
* group.
265283
* @param {?object} callback.nextQuery - If present, query with this object to
266284
* check for more results.
@@ -433,20 +451,11 @@ InstanceGroup.prototype.setPorts = function(ports, callback) {
433451

434452
callback = callback || util.noop;
435453

436-
var namedPorts = [];
437-
438-
for (var namedPort in ports) {
439-
namedPorts.push({
440-
name: namedPort,
441-
port: ports[namedPort]
442-
});
443-
}
444-
445454
this.request({
446455
method: 'POST',
447456
uri: '/setNamedPorts',
448457
json: {
449-
namedPorts: namedPorts
458+
namedPorts: InstanceGroup.formatPorts_(ports)
450459
}
451460
}, function(err, resp) {
452461
if (err) {

lib/compute/zone.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -416,15 +416,7 @@ Zone.prototype.createInstanceGroup = function(name, options, callback) {
416416
});
417417

418418
if (body.ports) {
419-
body.namedPorts = [];
420-
421-
for (var namedPort in body.ports) {
422-
body.namedPorts.push({
423-
name: namedPort,
424-
port: body.ports[namedPort]
425-
});
426-
}
427-
419+
body.namedPorts = InstanceGroup.formatPorts_(body.ports);
428420
delete body.ports;
429421
}
430422

@@ -895,22 +887,22 @@ Zone.prototype.getDisks = function(options, callback) {
895887
* representing part of the larger set of results to view.
896888
* @param {function} callback - The callback function.
897889
* @param {?error} callback.err - An error returned while making this request.
898-
* @param {module:compute/instance-group} callback.instanceGroups -
890+
* @param {module:compute/instance-group[]} callback.instanceGroups -
899891
* InstanceGroup objects from this zone.
900892
* @param {?object} callback.nextQuery - If present, query with this object to
901893
* check for more results.
902894
* @param {object} callback.apiResponse - The full API response.
903895
*
904896
* @example
905-
* zone.getInstanceGroups(function(err, groups) {
906-
* // `groups` is an array of `Group` objects.
897+
* zone.getInstanceGroups(function(err, instanceGroups) {
898+
* // `instanceGroups` is an array of `InstanceGroup` objects.
907899
* });
908900
*
909901
* //-
910902
* // To control how many API requests are made and page through the results
911903
* // manually, set `autoPaginate` to `false`.
912904
* //-
913-
* function callback(err, groups, nextQuery, apiResponse) {
905+
* function callback(err, instanceGroups, nextQuery, apiResponse) {
914906
* if (nextQuery) {
915907
* // More results exist.
916908
* zone.getInstanceGroups(nextQuery, callback);

test/compute/instance-group.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ describe('InstanceGroup', function() {
5050
var InstanceGroup;
5151
var instanceGroup;
5252

53+
var staticMethods = {};
54+
5355
var ZONE = {
5456
createInstanceGroup: util.noop,
5557
vm: util.noop
@@ -69,6 +71,7 @@ describe('InstanceGroup', function() {
6971
});
7072

7173
InstanceGroup = require('../../lib/compute/instance-group.js');
74+
staticMethods.formatPorts_ = InstanceGroup.formatPorts_;
7275
});
7376

7477
after(function() {
@@ -77,6 +80,7 @@ describe('InstanceGroup', function() {
7780
});
7881

7982
beforeEach(function() {
83+
extend(InstanceGroup, staticMethods);
8084
instanceGroup = new InstanceGroup(ZONE, NAME);
8185
});
8286

@@ -126,6 +130,20 @@ describe('InstanceGroup', function() {
126130
});
127131
});
128132

133+
describe('formatPorts_', function() {
134+
var PORTS = {
135+
http: 80,
136+
https: 443
137+
};
138+
139+
it('should format an object of named ports', function() {
140+
assert.deepEqual(InstanceGroup.formatPorts_(PORTS), [
141+
{ name: 'http', port: 80 },
142+
{ name: 'https', port: 443 }
143+
]);
144+
});
145+
});
146+
129147
describe('add', function() {
130148
var VMS = [
131149
{ url: 'vm-url' },
@@ -475,15 +493,17 @@ describe('InstanceGroup', function() {
475493
};
476494

477495
it('should format the named ports', function(done) {
478-
var expectedNamedPorts = [
479-
{ name: 'http', port: 80 },
480-
{ name: 'https', port: 443 }
481-
];
496+
var expectedNamedPorts = [];
497+
498+
InstanceGroup.formatPorts_ = function(ports) {
499+
assert.strictEqual(ports, PORTS);
500+
return expectedNamedPorts;
501+
};
482502

483503
instanceGroup.request = function(reqOpts) {
484504
assert.strictEqual(reqOpts.method, 'POST');
485505
assert.strictEqual(reqOpts.uri, '/setNamedPorts');
486-
assert.deepEqual(reqOpts.json.namedPorts, expectedNamedPorts);
506+
assert.strictEqual(reqOpts.json.namedPorts, expectedNamedPorts);
487507
done();
488508
};
489509

test/compute/zone.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@ function FakeDisk() {
3939
this.calledWith_ = [].slice.call(arguments);
4040
}
4141

42+
var formatPortsOverride;
4243
function FakeInstanceGroup() {
4344
this.calledWith_ = [].slice.call(arguments);
4445
}
46+
FakeInstanceGroup.formatPorts_ = function() {
47+
return (formatPortsOverride || util.noop).apply(null, arguments);
48+
};
4549

4650
function FakeOperation() {
4751
this.calledWith_ = [].slice.call(arguments);
@@ -118,6 +122,7 @@ describe('Zone', function() {
118122
});
119123

120124
beforeEach(function() {
125+
formatPortsOverride = null;
121126
gceImagesOverride = null;
122127
zone = new Zone(COMPUTE, ZONE_NAME);
123128
});
@@ -625,13 +630,15 @@ describe('Zone', function() {
625630
};
626631

627632
it('should format named ports', function(done) {
628-
var expectedNamedPorts = [
629-
{ name: 'http', port: 80 },
630-
{ name: 'https', port: 443 }
631-
];
633+
var expectedNamedPorts = [];
634+
635+
formatPortsOverride = function(ports) {
636+
assert.strictEqual(ports, PORTS);
637+
return expectedNamedPorts;
638+
};
632639

633640
zone.request = function(reqOpts) {
634-
assert.deepEqual(reqOpts.json.namedPorts, expectedNamedPorts);
641+
assert.strictEqual(reqOpts.json.namedPorts, expectedNamedPorts);
635642
assert.strictEqual(reqOpts.json.ports, undefined);
636643
done();
637644
};

0 commit comments

Comments
 (0)