|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +var _ = require("lodash"); |
| 4 | +var clc = require("cli-color"); |
| 5 | + |
| 6 | +var api = require("./api"); |
| 7 | +var utils = require("./utils"); |
| 8 | + |
| 9 | +var POLL_INTERVAL = 10000; // 10 seconds |
| 10 | +var POLLS_BEFORE_RETRY = 12; // Retry enabling the API after 2 minutes |
| 11 | +var _enableApiWithRetries; |
| 12 | + |
| 13 | +var _checkEnabled = function(projectId, apiName, prefix, silent) { |
| 14 | + return api |
| 15 | + .request("GET", `/v1/projects/${projectId}/services/${apiName}`, { |
| 16 | + auth: true, |
| 17 | + origin: api.serviceUsageOrigin, |
| 18 | + }) |
| 19 | + .then(function(response) { |
| 20 | + var isEnabled = _.get(response.body, "state") === "ENABLED"; |
| 21 | + if (isEnabled && !silent) { |
| 22 | + utils.logSuccess(clc.bold.green(prefix + ":") + " all necessary APIs are enabled"); |
| 23 | + } |
| 24 | + return isEnabled; |
| 25 | + }); |
| 26 | +}; |
| 27 | + |
| 28 | +var _enableApi = function(projectId, apiName) { |
| 29 | + return api.request("POST", `/v1/projects/${projectId}/services/${apiName}:enable`, { |
| 30 | + auth: true, |
| 31 | + origin: api.serviceUsageOrigin, |
| 32 | + }); |
| 33 | +}; |
| 34 | + |
| 35 | +var _pollCheckEnabled = function(projectId, apiName, prefix, enablementRetries, pollRetries) { |
| 36 | + pollRetries = pollRetries || 0; |
| 37 | + if (pollRetries > POLLS_BEFORE_RETRY) { |
| 38 | + return _enableApiWithRetries(projectId, apiName, prefix, enablementRetries + 1); |
| 39 | + } |
| 40 | + |
| 41 | + return new Promise(function(resolve) { |
| 42 | + setTimeout(function() { |
| 43 | + resolve(); |
| 44 | + }, POLL_INTERVAL); |
| 45 | + }).then(function() { |
| 46 | + return _checkEnabled(projectId, apiName, prefix).then(function(isEnabled) { |
| 47 | + if (isEnabled) { |
| 48 | + return true; |
| 49 | + } |
| 50 | + utils.logBullet(clc.bold.cyan(prefix + ":") + " waiting for APIs to activate..."); |
| 51 | + return _pollCheckEnabled(projectId, apiName, prefix, enablementRetries, pollRetries + 1); |
| 52 | + }); |
| 53 | + }); |
| 54 | +}; |
| 55 | + |
| 56 | +_enableApiWithRetries = function(projectId, apiName, prefix, enablementRetries) { |
| 57 | + enablementRetries = enablementRetries || 0; |
| 58 | + if (enablementRetries > 1) { |
| 59 | + return utils.reject("Timed out waiting for APIs to enable. Please try again in a few minutes."); |
| 60 | + } |
| 61 | + return _enableApi(projectId, apiName, prefix, enablementRetries).then(function() { |
| 62 | + return _pollCheckEnabled(projectId, apiName, prefix, enablementRetries); |
| 63 | + }); |
| 64 | +}; |
| 65 | + |
| 66 | +var _ensureApi = function(projectId, apiName, prefix, silent) { |
| 67 | + if (!silent) { |
| 68 | + utils.logBullet(clc.bold.cyan(prefix + ":") + " ensuring necessary APIs are enabled..."); |
| 69 | + } |
| 70 | + return _checkEnabled(projectId, apiName, prefix, silent).then(function(isEnabled) { |
| 71 | + if (isEnabled) { |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
| 75 | + if (!silent) { |
| 76 | + utils.logWarning(clc.bold.yellow(prefix + ":") + " missing necessary APIs. Enabling now..."); |
| 77 | + } |
| 78 | + return _enableApiWithRetries(projectId, apiName, prefix); |
| 79 | + }); |
| 80 | +}; |
| 81 | + |
| 82 | +module.exports = { |
| 83 | + ensure: _ensureApi, |
| 84 | + check: _checkEnabled, |
| 85 | + enable: _enableApi, |
| 86 | +}; |
0 commit comments