Skip to content

Commit

Permalink
fix docs + add formatPorts
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed May 18, 2016
1 parent 87794d5 commit 1be1df5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 38 deletions.
6 changes: 3 additions & 3 deletions lib/compute/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -953,22 +953,22 @@ Compute.prototype.getDisks = function(options, callback) {
* 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/instance-group} callback.instanceGroups -
* @param {module:compute/instance-group[]} callback.instanceGroups -
* InstanceGroup 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.getInstanceGroups(function(err, instanceGroups) {
* // `instanceGroup` is an array of `InstanceGroup` objects.
* // `instanceGroups` is an array of `InstanceGroup` objects.
* });
*
* //-
* // To control how many API requests are made and page through the results
* // manually, set `autoPaginate` to `false`.
* //-
* function callback(err, groups, nextQuery, apiResponse) {
* function callback(err, instanceGroups, nextQuery, apiResponse) {
* if (nextQuery) {
* // More results exist.
* gce.getInstanceGroups(nextQuery, callback);
Expand Down
33 changes: 21 additions & 12 deletions lib/compute/instance-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var util = require('../common/util.js');
*/
/**
* You can create and manage groups of virtual machine instances so that you
* don't have to individually control each instance in your project
* don't have to individually control each instance in your project.
*
* @resource [Creating Groups of Instances]{@link https://cloud.google.com/compute/docs/instance-groups}
* @resource [Unmanaged Instance Groups]{@link https://cloud.google.com/compute/docs/instance-groups/unmanaged-groups}
Expand Down Expand Up @@ -155,6 +155,24 @@ function InstanceGroup(zone, name) {

nodeutil.inherits(InstanceGroup, ServiceObject);

/**
* Format a map of named ports in the way the API expects.
*
* @private
*
* @param {object} ports - A map of names to ports. The key should be the name,
* and the value the port number.
* @return {object[]} - The formatted array of named ports.
*/
InstanceGroup.formatPorts_ = function(ports) {
return Object.keys(ports).map(function(port) {
return {
name: port,
port: ports[port]
};
});
};

/**
* Add one or more VMs to this instance group.
*
Expand Down Expand Up @@ -260,7 +278,7 @@ InstanceGroup.prototype.delete = function(callback) {
* @param {boolean} options.running - Only return instances which are running.
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request.
* @param {module:compute/vm} callback.vms - VM objects from this isntance
* @param {module:compute/vm[]} callback.vms - VM objects from this isntance
* group.
* @param {?object} callback.nextQuery - If present, query with this object to
* check for more results.
Expand Down Expand Up @@ -433,20 +451,11 @@ InstanceGroup.prototype.setPorts = function(ports, callback) {

callback = callback || util.noop;

var namedPorts = [];

for (var namedPort in ports) {
namedPorts.push({
name: namedPort,
port: ports[namedPort]
});
}

this.request({
method: 'POST',
uri: '/setNamedPorts',
json: {
namedPorts: namedPorts
namedPorts: InstanceGroup.formatPorts_(ports)
}
}, function(err, resp) {
if (err) {
Expand Down
18 changes: 5 additions & 13 deletions lib/compute/zone.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,7 @@ Zone.prototype.createInstanceGroup = function(name, options, callback) {
});

if (body.ports) {
body.namedPorts = [];

for (var namedPort in body.ports) {
body.namedPorts.push({
name: namedPort,
port: body.ports[namedPort]
});
}

body.namedPorts = InstanceGroup.formatPorts_(body.ports);
delete body.ports;
}

Expand Down Expand Up @@ -895,22 +887,22 @@ Zone.prototype.getDisks = function(options, callback) {
* 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/instance-group} callback.instanceGroups -
* @param {module:compute/instance-group[]} callback.instanceGroups -
* InstanceGroup objects from this zone.
* @param {?object} callback.nextQuery - If present, query with this object to
* check for more results.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* zone.getInstanceGroups(function(err, groups) {
* // `groups` is an array of `Group` objects.
* zone.getInstanceGroups(function(err, instanceGroups) {
* // `instanceGroups` is an array of `InstanceGroup` objects.
* });
*
* //-
* // To control how many API requests are made and page through the results
* // manually, set `autoPaginate` to `false`.
* //-
* function callback(err, groups, nextQuery, apiResponse) {
* function callback(err, instanceGroups, nextQuery, apiResponse) {
* if (nextQuery) {
* // More results exist.
* zone.getInstanceGroups(nextQuery, callback);
Expand Down
30 changes: 25 additions & 5 deletions test/compute/instance-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ describe('InstanceGroup', function() {
var InstanceGroup;
var instanceGroup;

var staticMethods = {};

var ZONE = {
createInstanceGroup: util.noop,
vm: util.noop
Expand All @@ -69,6 +71,7 @@ describe('InstanceGroup', function() {
});

InstanceGroup = require('../../lib/compute/instance-group.js');
staticMethods.formatPorts_ = InstanceGroup.formatPorts_;
});

after(function() {
Expand All @@ -77,6 +80,7 @@ describe('InstanceGroup', function() {
});

beforeEach(function() {
extend(InstanceGroup, staticMethods);
instanceGroup = new InstanceGroup(ZONE, NAME);
});

Expand Down Expand Up @@ -126,6 +130,20 @@ describe('InstanceGroup', function() {
});
});

describe('formatPorts_', function() {
var PORTS = {
http: 80,
https: 443
};

it('should format an object of named ports', function() {
assert.deepEqual(InstanceGroup.formatPorts_(PORTS), [
{ name: 'http', port: 80 },
{ name: 'https', port: 443 }
]);
});
});

describe('add', function() {
var VMS = [
{ url: 'vm-url' },
Expand Down Expand Up @@ -475,15 +493,17 @@ describe('InstanceGroup', function() {
};

it('should format the named ports', function(done) {
var expectedNamedPorts = [
{ name: 'http', port: 80 },
{ name: 'https', port: 443 }
];
var expectedNamedPorts = [];

InstanceGroup.formatPorts_ = function(ports) {
assert.strictEqual(ports, PORTS);
return expectedNamedPorts;
};

instanceGroup.request = function(reqOpts) {
assert.strictEqual(reqOpts.method, 'POST');
assert.strictEqual(reqOpts.uri, '/setNamedPorts');
assert.deepEqual(reqOpts.json.namedPorts, expectedNamedPorts);
assert.strictEqual(reqOpts.json.namedPorts, expectedNamedPorts);
done();
};

Expand Down
17 changes: 12 additions & 5 deletions test/compute/zone.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ function FakeDisk() {
this.calledWith_ = [].slice.call(arguments);
}

var formatPortsOverride;
function FakeInstanceGroup() {
this.calledWith_ = [].slice.call(arguments);
}
FakeInstanceGroup.formatPorts_ = function() {
return (formatPortsOverride || util.noop).apply(null, arguments);
};

function FakeOperation() {
this.calledWith_ = [].slice.call(arguments);
Expand Down Expand Up @@ -118,6 +122,7 @@ describe('Zone', function() {
});

beforeEach(function() {
formatPortsOverride = null;
gceImagesOverride = null;
zone = new Zone(COMPUTE, ZONE_NAME);
});
Expand Down Expand Up @@ -625,13 +630,15 @@ describe('Zone', function() {
};

it('should format named ports', function(done) {
var expectedNamedPorts = [
{ name: 'http', port: 80 },
{ name: 'https', port: 443 }
];
var expectedNamedPorts = [];

formatPortsOverride = function(ports) {
assert.strictEqual(ports, PORTS);
return expectedNamedPorts;
};

zone.request = function(reqOpts) {
assert.deepEqual(reqOpts.json.namedPorts, expectedNamedPorts);
assert.strictEqual(reqOpts.json.namedPorts, expectedNamedPorts);
assert.strictEqual(reqOpts.json.ports, undefined);
done();
};
Expand Down

0 comments on commit 1be1df5

Please sign in to comment.