-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.js
62 lines (50 loc) · 1.56 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const HTTP_CODE_UNAUTHORIZED = 401;
const UNAUTHORIZED_ERROR_MESSAGE = "Please ensure the Service Account Token is vaulted in the correct format and that your service account has sufficient privileges to perform this operation. Consult the plugin documentation for more details.";
function validateNamespace(namespace, deleteFunction, objectType) {
const namespaced = deleteFunction.name.includes("Namespaced");
if (namespaced && !namespace) {
throw new Error(`Must specify namespace to delete object of type '${objectType}`);
}
}
async function applySpec(client, spec) {
try {
await client.read(spec);
} catch (error) {
return client.create(spec);
}
return client.patch(spec);
}
function extractResponseData(response) {
const data = response?.body?.items;
if (!data) {
throw new Error("Response is missing data.");
}
return data;
}
function parseError(err) {
if (err?.body?.code === HTTP_CODE_UNAUTHORIZED) {
return `${UNAUTHORIZED_ERROR_MESSAGE} ${err.body}`;
}
return err.body ? err.body : err;
}
function decodeBase64(content) {
return Buffer.from(content, "base64").toString("utf-8");
}
function generateRandomString() {
return Math.random().toString(36).slice(2);
}
function convertCertificateToBase64(certificate) {
if (certificate.startsWith("-----BEGIN CERTIFICATE-----")) {
return Buffer.from(certificate).toString("base64");
}
return certificate;
}
module.exports = {
validateNamespace,
applySpec,
extractResponseData,
parseError,
decodeBase64,
generateRandomString,
convertCertificateToBase64,
};