From 49c53847b3e5dd993a3996a80ad0fce798ba092c Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Fri, 7 Jun 2019 08:00:57 -0700 Subject: [PATCH] feat: support apiEndpoint override (#322) --- package.json | 8 ++++---- src/index.js | 8 +++++--- src/service.js | 2 +- src/snapshot.js | 8 +++++--- src/vm.js | 4 ++-- test/service.js | 1 + test/snapshot.js | 4 +++- test/vm.js | 1 + 8 files changed, 22 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 50e2d13e..c64f5281 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "predocs-test": "npm run docs" }, "dependencies": { - "@google-cloud/common": "^1.0.0", + "@google-cloud/common": "^2.0.0", "@google-cloud/paginator": "^1.0.0", "@google-cloud/projectify": "^1.0.0", "@google-cloud/promisify": "^1.0.0", @@ -54,15 +54,15 @@ "eslint-config-prettier": "^4.0.0", "eslint-plugin-node": "^9.0.0", "eslint-plugin-prettier": "^3.0.0", - "jsdoc-baseline": "^0.1.0", "intelli-espower-loader": "^1.0.1", "jsdoc": "^3.6.2", + "jsdoc-baseline": "^0.1.0", + "linkinator": "^1.4.2", "mocha": "^6.0.0", "nyc": "^14.0.0", "power-assert": "^1.5.0", "prettier": "^1.13.5", "proxyquire": "^2.0.1", - "uuid": "^3.2.1", - "linkinator": "^1.1.2" + "uuid": "^3.2.1" } } diff --git a/src/index.js b/src/index.js index 4077d165..7d3557ce 100644 --- a/src/index.js +++ b/src/index.js @@ -58,6 +58,7 @@ const Image = require('./image.js'); * attempted before returning the error. * @property {Constructor} [promise] Custom promise module to use instead of * native Promises. + * @property {string} [apiEndpoint] The API endpoint of the service used to make requests. Defaults to `www.googleapis.com` */ /** * @see [What is Google Compute Engine?]{@link https://cloud.google.com/compute/docs} @@ -78,10 +79,11 @@ const Image = require('./image.js'); * }); */ class Compute extends common.Service { - constructor(options) { - options = options || {}; + constructor(options = {}) { + options.apiEndpoint = options.apiEndpoint || 'www.googleapis.com'; const config = { - baseUrl: 'https://www.googleapis.com/compute/v1', + apiEndpoint: options.apiEndpoint, + baseUrl: `https://${options.apiEndpoint}/compute/v1`, scopes: ['https://www.googleapis.com/auth/compute'], packageJson: require('../package.json'), }; diff --git a/src/service.js b/src/service.js index 1c6263c8..e1a281eb 100644 --- a/src/service.js +++ b/src/service.js @@ -286,7 +286,7 @@ class Service extends common.ServiceObject { getHealth(group, callback) { if (!is.string(group)) { group = format('{baseUrl}/projects/{p}/zones/{z}/instanceGroups/{n}', { - baseUrl: 'https://www.googleapis.com/compute/v1', + baseUrl: `https://${this.compute.apiEndpoint}/compute/v1`, p: this.parent.projectId, z: group.zone.name || group.zone, n: group.name, diff --git a/src/snapshot.js b/src/snapshot.js index 832d1b7c..36a6bb8d 100644 --- a/src/snapshot.js +++ b/src/snapshot.js @@ -124,10 +124,12 @@ class Snapshot extends common.ServiceObject { */ getMetadata: true, }; + const compute = isDisk ? scope.zone.compute : scope; const config = { parent: scope, - baseUrl: - 'https://www.googleapis.com/compute/v1/projects/{{projectId}}/global/snapshots', + baseUrl: `https://${ + compute.apiEndpoint + }/compute/v1/projects/{{projectId}}/global/snapshots`, /** * @name Snapshot#id * @type {string} @@ -174,7 +176,7 @@ class Snapshot extends common.ServiceObject { * @name Snapshot#compute * @type {Compute} */ - this.compute = isDisk ? scope.zone.compute : scope; + this.compute = compute; /** * @name Snapshot#name * @type {string} diff --git a/src/vm.js b/src/vm.js index 0fbd66a7..e4377468 100644 --- a/src/vm.js +++ b/src/vm.js @@ -240,7 +240,7 @@ class VM extends common.ServiceObject { this.hasActiveWaiters = false; this.waiters = []; this.url = format('{base}/{project}/zones/{zone}/instances/{name}', { - base: 'https://www.googleapis.com/compute/v1/projects', + base: `https://${this.zone.compute.apiEndpoint}/compute/v1/projects`, project: zone.compute.projectId, zone: zone.name, name: this.name, @@ -422,7 +422,7 @@ class VM extends common.ServiceObject { } const diskName = replaceProjectIdToken(disk.formattedName, projectId); let deviceName; - const baseUrl = 'https://www.googleapis.com/compute/v1/'; + const baseUrl = `https://${self.zone.compute.apiEndpoint}/compute/v1/`; const disks = metadata.disks || []; // Try to find the deviceName by matching the source of the attached disks // to the name of the disk provided by the user. diff --git a/test/service.js b/test/service.js index 0bea16fb..845648cc 100644 --- a/test/service.js +++ b/test/service.js @@ -46,6 +46,7 @@ describe('Service', function() { const COMPUTE = { projectId: 'project-id', createService: util.noop, + apiEndpoint: 'www.googleapis.com', }; before(function() { diff --git a/test/snapshot.js b/test/snapshot.js index c9eb7b62..e5dc912e 100644 --- a/test/snapshot.js +++ b/test/snapshot.js @@ -41,7 +41,9 @@ describe('Snapshot', function() { let Snapshot; let snapshot; - const COMPUTE = {}; + const COMPUTE = { + apiEndpoint: 'www.googleapis.com', + }; const SNAPSHOT_NAME = 'snapshot-name'; before(function() { diff --git a/test/vm.js b/test/vm.js index 97556577..54280225 100644 --- a/test/vm.js +++ b/test/vm.js @@ -62,6 +62,7 @@ describe('VM', function() { }, }, projectId: 'project-id', + apiEndpoint: 'www.googleapis.com', }; const ZONE = { compute: COMPUTE,