diff --git a/packages/google-cloud-compute/src/vm.js b/packages/google-cloud-compute/src/vm.js index c79c121e6ba..b732819359c 100644 --- a/packages/google-cloud-compute/src/vm.js +++ b/packages/google-cloud-compute/src/vm.js @@ -985,6 +985,58 @@ class VM extends common.ServiceObject { callback || common.util.noop ); } + /** + * Start an instance with customer encrypted disks. + * + * @see [Instances: start API Documentation]{@link https://cloud.google.com/compute/docs/reference/rest/v1/instances/startWithEncryptionKey} + * + * @param {object[]} disks - An array of the encrypted disks and their keys. + * @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('zone-name'); + * const vm = zone.vm('vm-name'); + * + * var disks = [ + * { + * source: 'disk_name', + * diskEncryptionKey: { + * rawKey: '...' + * } + * } + * ] + * + * vm.startWithEncryptionKey(disks, 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. + * //- + * vm.startWithEncryptionKey(disks).then(function(data) { + * const operation = data[0]; + * const apiResponse = data[1]; + * }); + */ + startWithEncryptionKey(disks, callback) { + this.request( + { + method: 'POST', + uri: '/startWithEncryptionKey', + json: { + disks, + }, + }, + callback || common.util.noop + ); + } /** * Stop the instance. * diff --git a/packages/google-cloud-compute/test/vm.js b/packages/google-cloud-compute/test/vm.js index edc479ae563..b099e3084f3 100644 --- a/packages/google-cloud-compute/test/vm.js +++ b/packages/google-cloud-compute/test/vm.js @@ -941,6 +941,33 @@ describe('VM', () => { }); }); + describe('startWithEncryptionKey', () => { + const DISKS = []; + + it('should make the correct API request', done => { + vm.request = function (reqOpts, callback) { + assert.strictEqual(reqOpts.method, 'POST'); + assert.strictEqual(reqOpts.uri, '/startWithEncryptionKey'); + assert.strictEqual(reqOpts.json.disks, DISKS); + + callback(); + }; + + vm.startWithEncryptionKey(DISKS, done); + }); + + it('should not require a callback', done => { + vm.request = function (reqOpts, callback) { + assert.doesNotThrow(() => { + callback(); + done(); + }); + }; + + vm.start(); + }); + }); + describe('stop', () => { it('should make the correct API request', done => { vm.request = function (reqOpts, callback) {