forked from actions/setup-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sandbox-svc.ts
182 lines (155 loc) · 5.24 KB
/
sandbox-svc.ts
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import * as envUtils from '../utils/env-utils';
import * as cliUtils from '../utils/cli-utils';
var accountKey: string = null;
const SANDBOX_API_BASE = '/sandbox-api/v1';
export function setAccountKey(account: string) {
accountKey = account;
}
function isOkStatus(code) {
return code >= 200 && code < 300;
}
function sendEdgeRequest(pth: string, method: string, body, headers) {
const edge = envUtils.getEdgeGrid();
var path = pth;
if (accountKey) {
path += `?accountSwitchKey=${accountKey}`;
}
return new Promise<any>(
(resolve, reject) => {
edge.auth({
path,
method,
headers,
body
});
edge.send(function (error, response, body) {
if (error) {
reject(error);
} else if (isOkStatus(response.statusCode)) {
var obj: any = {
response,
body: !!body ? JSON.parse(body) : undefined
};
resolve(obj);
} else {
try {
var errorObj = JSON.parse(body);
reject(cliUtils.toJsonPretty(errorObj));
} catch (ex) {
console.error(`got error code: ${response.statusCode} calling ${method} ${path}\n${body}`);
reject(body);
}
}
});
});
}
function postJson(path: string, body) {
return sendEdgeRequest(path, 'POST', body, {
'Content-Type': 'application/json'
});
}
function putJson(path: string, body) {
return sendEdgeRequest(path, 'PUT', body, {
'Content-Type': 'application/json'
});
}
function getJson(path: string) {
return sendEdgeRequest(path, 'GET', '', {});
}
function del(path: string) {
return sendEdgeRequest(path, 'DELETE', '', {});
}
export function deleteSandbox(sandboxId: string) {
return sendEdgeRequest(`${SANDBOX_API_BASE}/sandboxes/${sandboxId}`, 'DELETE', '', {}).then(r => r.body);
}
export function cloneSandbox(sandboxId: string, name: string, clonable = false) {
const body = {
name,
isClonable: clonable
};
return postJson(`${SANDBOX_API_BASE}/sandboxes/${sandboxId}/clone`, body).then(r => r.body);
}
export function getAllSandboxes() {
return getJson(`${SANDBOX_API_BASE}/sandboxes`).then(r => {
const limit = parseInt(r.response.headers['x-limit-sandboxes-limit']);
const remaining = parseInt(r.response.headers['x-limit-sandboxes-remaining']);
return {
quota: {
max: limit,
used: limit - remaining
},
result: r.body
}
});
}
export function getSandbox(sandboxId: string) {
return getJson(`${SANDBOX_API_BASE}/sandboxes/${sandboxId}`).then(r => r.body);
}
export function updateSandbox(sandbox) {
return putJson(`${SANDBOX_API_BASE}/sandboxes/${sandbox.sandboxId}`, sandbox).then(r => r.body);
}
export function createFromRules(papiRules, requestHostnames, name, isClonable, cpcode) {
var bodyObj = {
name: name,
requestHostnames: requestHostnames,
createFromRules: papiRules,
isClonable: isClonable
};
if(cpcode) {
bodyObj['cpcode'] = cpcode;
}
return postJson(`${SANDBOX_API_BASE}/sandboxes`, bodyObj).then(r => r.body);
}
export function addPropertyFromRules(sandboxId: string, requestHostnames, papiRules) {
var bodyObj = {
requestHostnames: requestHostnames,
createFromRules: papiRules
};
return postJson(`${SANDBOX_API_BASE}/sandboxes/${sandboxId}/properties`, bodyObj).then(r => r.body);
}
export function addPropertyFromProperty(sandboxId: string, requestHostnames, fromPropertyObj) {
var bodyObj = {
createFromProperty: fromPropertyObj,
};
if (requestHostnames) {
bodyObj['requestHostnames'] = requestHostnames;
}
return postJson(`${SANDBOX_API_BASE}/sandboxes/${sandboxId}/properties`, bodyObj).then(r => r.body);
}
export function createFromProperty(requestHostnames, name, isClonable, fromPropertyObj, cpcode) {
var bodyObj = {
name: name,
createFromProperty: fromPropertyObj,
isClonable: isClonable
};
if (requestHostnames) {
bodyObj['requestHostnames'] = requestHostnames;
}
if(cpcode) {
bodyObj['cpcode'] = cpcode;
}
return postJson(`${SANDBOX_API_BASE}/sandboxes`, bodyObj).then(r => r.body);
}
export function getRules(sandboxId: string, sandboxPropertyId: string) {
var endpoint = `${SANDBOX_API_BASE}/sandboxes/${sandboxId}/properties/${sandboxPropertyId}/rules`;
return getJson(endpoint).then(r => r.body);
}
export function updateRules(sandboxId: string, sandboxPropertyId: string, rules) {
var endpoint = `${SANDBOX_API_BASE}/sandboxes/${sandboxId}/properties/${sandboxPropertyId}/rules`;
var body = {
rules: rules.rules ? rules.rules : rules
};
return putJson(endpoint, body).then(r => r.body);
}
export function getProperty(sandboxId: string, sandboxPropertyId: string) {
var endpoint = `${SANDBOX_API_BASE}/sandboxes/${sandboxId}/properties/${sandboxPropertyId}`;
return getJson(endpoint).then(r => r.body);
}
export function updateProperty(sandboxId, propertyObj) {
var endpoint = `${SANDBOX_API_BASE}/sandboxes/${sandboxId}/properties/${propertyObj.sandboxPropertyId}`;
return putJson(endpoint, propertyObj).then(r => r.body);
}
export function deleteProperty(sandboxId, sandboxPropertyId) {
const endpoint = `${SANDBOX_API_BASE}/sandboxes/${sandboxId}/properties/${sandboxPropertyId}`;
return del(endpoint).then(r => r.body);
}