Skip to content

Commit c3ed87e

Browse files
committed
typescriptifys ensureApiEnabled
1 parent e4ff1fb commit c3ed87e

File tree

2 files changed

+92
-86
lines changed

2 files changed

+92
-86
lines changed

src/ensureApiEnabled.js

Lines changed: 0 additions & 86 deletions
This file was deleted.

src/ensureApiEnabled.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import * as _ from "lodash";
2+
3+
import * as api from "./api";
4+
import * as utils from "./utils";
5+
6+
const POLL_INTERVAL = 10000; // 10 seconds
7+
const POLLS_BEFORE_RETRY = 12; // Retry enabling the API after 2 minutes
8+
9+
export async function check(
10+
projectId: string,
11+
apiName: string,
12+
prefix: string,
13+
silent: boolean
14+
): Promise<boolean> {
15+
const response = await api.request("GET", `/v1/projects/${projectId}/services/${apiName}`, {
16+
auth: true,
17+
origin: api.serviceUsageOrigin,
18+
});
19+
20+
const isEnabled = _.get(response.body, "state") === "ENABLED";
21+
if (isEnabled && !silent) {
22+
utils.logLabeledSuccess(prefix, "all necessary APIs are enabled");
23+
}
24+
return isEnabled;
25+
}
26+
27+
export async function enable(projectId: string, apiName: string): Promise<void> {
28+
return api.request("POST", `/v1/projects/${projectId}/services/${apiName}:enable`, {
29+
auth: true,
30+
origin: api.serviceUsageOrigin,
31+
});
32+
}
33+
34+
export async function ensure(
35+
projectId: string,
36+
apiName: string,
37+
prefix: string,
38+
silent: boolean
39+
): Promise<void> {
40+
if (!silent) {
41+
utils.logLabeledBullet(prefix, "ensuring necessary APIs are enabled...");
42+
}
43+
const isEnabled = await check(projectId, apiName, prefix, silent);
44+
if (isEnabled) {
45+
return;
46+
}
47+
if (!silent) {
48+
utils.logLabeledWarning(prefix, "missing necessary APIs. Enabling now...");
49+
}
50+
return enableApiWithRetries(projectId, apiName, prefix, silent);
51+
}
52+
53+
const pollCheckEnabled = async (
54+
projectId: string,
55+
apiName: string,
56+
prefix: string,
57+
silent: boolean,
58+
enablementRetries: number,
59+
pollRetries = 0
60+
): Promise<void> => {
61+
if (pollRetries > POLLS_BEFORE_RETRY) {
62+
return enableApiWithRetries(projectId, apiName, prefix, silent, enablementRetries + 1);
63+
}
64+
65+
await new Promise((resolve) => {
66+
setTimeout(resolve, POLL_INTERVAL);
67+
});
68+
const isEnabled = await check(projectId, apiName, prefix, silent);
69+
if (isEnabled) {
70+
return;
71+
}
72+
if (!silent) {
73+
utils.logLabeledBullet(prefix, "waiting for APIs to activate...");
74+
}
75+
return pollCheckEnabled(projectId, apiName, prefix, silent, enablementRetries, pollRetries + 1);
76+
};
77+
78+
const enableApiWithRetries = async (
79+
projectId: string,
80+
apiName: string,
81+
prefix: string,
82+
silent: boolean,
83+
enablementRetries = 0
84+
) => {
85+
if (enablementRetries > 1) {
86+
return utils.reject(
87+
"Timed out while waiting for APIs to enable. Please try again in a few minutes."
88+
);
89+
}
90+
await enable(projectId, apiName);
91+
return pollCheckEnabled(projectId, apiName, prefix, silent, enablementRetries);
92+
};

0 commit comments

Comments
 (0)