Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
feat: Adds abandonInstances to InstanceGroupManger API (#306)
Browse files Browse the repository at this point in the history
* Adds abandonInstances to InstanceGroupManger API

* Changes callbacks to fat arrow syntax

* Updated callbacks in all functions, not just abandonInstances.
* Removed extraneous `self = this`.
  • Loading branch information
Alan K authored and JustinBeckwith committed May 15, 2019
1 parent a9e9c75 commit c359ce3
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 13 deletions.
76 changes: 63 additions & 13 deletions src/instance-group-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,61 @@ class InstanceGroupManager extends common.ServiceObject {
*/
this.name = name;
}
/**
* Flags the specified instances to be removed from the managed instance group.
* @see [InstanceGroupManagers: abandonInstances API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/instanceGroupManagers/abandonInstances}
* @param {VM|VM[]} vms - VM instances to abandon from
* this instance group manager.
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request.
* @param {Operation} callback.operation - An operation object
* that can be used to check the status of the request.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* const Compute = require('@google-cloud/compute');
* const compute = new Compute();
* const zone = compute.zone('us-central1-a');
* const instanceGroupManager = zone.instanceGroupManager('web-servers');
*
* const vms = [
* gce.zone('us-central1-a').vm('http-server'),
* gce.zone('us-central1-a').vm('https-server')
* ];
*
* instanceGroupManager.abandonInstances(vms, function(err, operation, apiResponse) {
* // `operation` is an Operation object that can be used to check the status
* // of the request.
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* instanceGroupManager.abandonInstances(vms).then(function(data) {
* const operation = data[0];
* const apiResponse = data[1];
* });
*/
abandonInstances(vms, callback) {
this.request(
{
method: 'POST',
uri: '/abandonInstances',
json: {
instances: arrify(vms).map(vm => vm.url),
},
},
(err, resp) => {
if (err) {
callback(err, null, resp);
return;
}
const operation = this.zone.operation(resp.name);
operation.metadata = resp;
callback(null, operation, resp);
}
);
}
/**
* Flags the specified instances in the managed instance group for immediate deletion.
* @see [InstanceGroupManagers: deleteInstances API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/instanceGroupManagers/deleteInstances}
Expand Down Expand Up @@ -189,23 +244,20 @@ class InstanceGroupManager extends common.ServiceObject {
* });
*/
deleteInstances(vms, callback) {
const self = this;
this.request(
{
method: 'POST',
uri: '/deleteInstances',
json: {
instances: arrify(vms).map(function(vm) {
return vm.url;
}),
instances: arrify(vms).map(vm => vm.url),
},
},
function(err, resp) {
(err, resp) => {
if (err) {
callback(err, null, resp);
return;
}
const operation = self.zone.operation(resp.name);
const operation = this.zone.operation(resp.name);
operation.metadata = resp;
callback(null, operation, resp);
}
Expand Down Expand Up @@ -269,7 +321,6 @@ class InstanceGroupManager extends common.ServiceObject {
* });
*/
getManagedInstances(options, callback) {
const self = this;
if (is.fn(options)) {
callback = options;
options = {};
Expand All @@ -281,7 +332,7 @@ class InstanceGroupManager extends common.ServiceObject {
uri: '/listManagedInstances',
qs: options,
},
function(err, resp) {
(err, resp) => {
if (err) {
callback(err, null, null, resp);
return;
Expand All @@ -292,8 +343,8 @@ class InstanceGroupManager extends common.ServiceObject {
pageToken: resp.nextPageToken,
});
}
const vms = arrify(resp.managedInstances).map(function(vm) {
const vmInstance = self.zone.vm(vm.instance);
const vms = arrify(resp.managedInstances).map(vm => {
const vmInstance = this.zone.vm(vm.instance);
vmInstance.metadata = vm;
return vmInstance;
});
Expand Down Expand Up @@ -333,19 +384,18 @@ class InstanceGroupManager extends common.ServiceObject {
* });
*/
resize(size, callback) {
const self = this;
this.request(
{
method: 'POST',
uri: '/resize',
qs: {size: size},
},
function(err, resp) {
(err, resp) => {
if (err) {
callback(err, null, resp);
return;
}
const operation = self.zone.operation(resp.name);
const operation = this.zone.operation(resp.name);
operation.metadata = resp;
callback(null, operation, resp);
}
Expand Down
75 changes: 75 additions & 0 deletions test/instance-group-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,81 @@ describe('InstanceGroupManager', function() {
});
});

describe('abandonInstances', function() {
const VMS = [{url: 'vm-url'}, {url: 'vm-url-2'}];

it('should make the correct API request', function(done) {
instanceGroupManager.request = function(reqOpts) {
assert.strictEqual(reqOpts.method, 'POST');
assert.strictEqual(reqOpts.uri, '/abandonInstances');
assert.deepStrictEqual(reqOpts.json, {
instances: VMS.map(function(vm) {
return vm.url;
}),
});

done();
};

instanceGroupManager.abandonInstances(VMS, assert.ifError);
});

describe('error', function() {
const apiResponse = {};
const error = new Error('Error.');

beforeEach(function() {
instanceGroupManager.request = function(reqOpts, callback) {
callback(error, apiResponse);
};
});

it('should return an error and API response', function(done) {
instanceGroupManager.abandonInstances(VMS, function(
err,
operation,
apiResponse_
) {
assert.strictEqual(err, error);
assert.strictEqual(operation, null);
assert.strictEqual(apiResponse_, apiResponse);
done();
});
});
});

describe('success', function() {
const apiResponse = {name: 'op-name'};

beforeEach(function() {
instanceGroupManager.request = function(reqOpts, callback) {
callback(null, apiResponse);
};
});

it('should return an Operation and API response', function(done) {
const operation = {};

instanceGroupManager.zone.operation = function(name) {
assert.strictEqual(name, apiResponse.name);
return operation;
};

instanceGroupManager.abandonInstances(VMS, function(
err,
operation_,
apiResponse_
) {
assert.ifError(err);
assert.strictEqual(operation_, operation);
assert.strictEqual(operation.metadata, apiResponse);
assert.strictEqual(apiResponse_, apiResponse);
done();
});
});
});
});

describe('deleteInstances', function() {
const VMS = [{url: 'vm-url'}, {url: 'vm-url-2'}];

Expand Down

0 comments on commit c359ce3

Please sign in to comment.