From 330285aab2398aa38ff5e72254820905a935da8a Mon Sep 17 00:00:00 2001 From: Stephen Date: Wed, 20 Jan 2021 12:10:36 -0500 Subject: [PATCH] fix: handle parameters to image.create() --- src/image.js | 67 ++++++++++++++++++++++-------------------- src/index.js | 5 ++-- system-test/compute.js | 6 ++++ test/image.js | 26 ++++++++++------ 4 files changed, 61 insertions(+), 43 deletions(-) diff --git a/src/image.js b/src/image.js index f5e1c0b3..f1a619b6 100644 --- a/src/image.js +++ b/src/image.js @@ -37,37 +37,6 @@ const {promisifyAll} = require('@google-cloud/promisify'); class Image extends common.ServiceObject { constructor(compute, name) { const methods = { - /** - * Create an image. - * - * @method Image#create - * @param {Disk} disk - See {@link Compute#createImage}. - * @param {object} [options] - See {@link Compute#createImage}. - * - * @example - * const Compute = require('@google-cloud/compute'); - * const compute = new Compute(); - * const zone = compute.zone('us-central1-a'); - * const disk = zone.disk('disk1'); - * const image = compute.image('image-name'); - * - * image.create(disk, function(err, image, operation, apiResponse) { - * // `image` is an Image object. - * - * // `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. - * //- - * image.create(disk).then(function(data) { - * const image = data[0]; - * const operation = data[1]; - * const apiResponse = data[2]; - * }); - */ - create: true, /** * Check if the image exists. * @@ -154,11 +123,45 @@ class Image extends common.ServiceObject { * @type {string} */ id: name, - createMethod: compute.createImage.bind(compute), methods: methods, pollIntervalMs: compute.pollIntervalMs, }); } + + /** + * Create an image. + * + * @method Image#create + * @param {Disk} disk - See {@link Compute#createImage}. + * @param {object} [options] - See {@link Compute#createImage}. + * + * @example + * const Compute = require('@google-cloud/compute'); + * const compute = new Compute(); + * const zone = compute.zone('us-central1-a'); + * const disk = zone.disk('disk1'); + * const image = compute.image('image-name'); + * + * image.create(disk, function(err, image, operation, apiResponse) { + * // `image` is an Image object. + * + * // `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. + * //- + * image.create(disk).then(function(data) { + * const image = data[0]; + * const operation = data[1]; + * const apiResponse = data[2]; + * }); + */ + create(disk, options, callback) { + this.parent.createImage(this.id, disk, options, callback); + } + /** * Delete the image. * diff --git a/src/index.js b/src/index.js index 5099b92f..abf4c34b 100644 --- a/src/index.js +++ b/src/index.js @@ -329,8 +329,9 @@ class Compute extends common.Service { * // If the callback is omitted, we'll return a Promise. * //- * compute.createImage('new-image', disk).then(function(data) { - * var operation = data[0]; - * var apiResponse = data[1]; + * var image = data[0]; + * var operation = data[1]; + * var apiResponse = data[2]; * }); */ createImage(name, disk, options, callback) { diff --git a/system-test/compute.js b/system-test/compute.js index 0a754c26..6bd6ace0 100644 --- a/system-test/compute.js +++ b/system-test/compute.js @@ -384,6 +384,12 @@ describe('Compute', () => { assert.strictEqual(exists, true); }); + it('should create an image from an image object', async () => { + const image = compute.image(generateName('image')); + const [, operation] = await image.create(DISK, {labels: {a: 'b'}}); + await operation.promise(); + }); + it('should list images', async () => { const [images] = await compute.getImages(); assert(images.length > 0); diff --git a/test/image.js b/test/image.js index 9196a85c..490de9d2 100644 --- a/test/image.js +++ b/test/image.js @@ -41,7 +41,6 @@ describe('Image', () => { const COMPUTE = { projectId: 'project-id', - createImage: util.noop, operation: util.noop, }; const IMAGE_NAME = 'image-name'; @@ -66,13 +65,7 @@ describe('Image', () => { }); it('should inherit from ServiceObject', () => { - const computeInstance = Object.assign({}, COMPUTE, { - createImage: { - bind: function (context) { - assert.strictEqual(context, computeInstance); - }, - }, - }); + const computeInstance = Object.assign({}, COMPUTE); const image = new Image(computeInstance, IMAGE_NAME); assert(image instanceof ServiceObject); @@ -83,7 +76,6 @@ describe('Image', () => { assert.strictEqual(calledWith.baseUrl, '/global/images'); assert.strictEqual(calledWith.id, IMAGE_NAME); assert.deepStrictEqual(calledWith.methods, { - create: true, exists: true, get: true, getMetadata: true, @@ -91,6 +83,22 @@ describe('Image', () => { }); }); + describe('create', () => { + it('should pass correct arguments to compute.createImage method', done => { + const disk = {}; + const options = {}; + + image.parent.createImage = (id, disk_, options_, callback) => { + assert.strictEqual(id, image.id); + assert.strictEqual(disk_, disk); + assert.strictEqual(options_, options); + callback(); // done() + }; + + image.create(disk, options, done); + }); + }); + describe('delete', () => { it('should call ServiceObject.delete', done => { FakeServiceObject.prototype.delete = function () {