-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathvariables.ts
274 lines (255 loc) · 7.61 KB
/
variables.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/** @module @twilio-labs/serverless-api/dist/api */
import debug from 'debug';
import { TwilioServerlessApiClient } from '../client';
import {
EnvironmentVariables,
Sid,
Variable,
VariableList,
VariableResource,
} from '../types';
import { ClientApiError } from '../utils/error';
import { getPaginatedResource } from './utils/pagination';
import { isSid } from './utils/type-checks';
const log = debug('twilio-serverless-api:variables');
/**
* Creates a new environment variable for a given environment
*
* @param {string} key the name of the variable
* @param {string} value the value of the variable
* @param {string} environmentSid the environment the variable should be created for
* @param {string} serviceSid the service that the environment belongs to
* @param {TwilioServerlessApiClient} client API client
* @returns {Promise<VariableResource>}
*/
async function registerVariableInEnvironment(
key: string,
value: string,
environmentSid: string,
serviceSid: string,
client: TwilioServerlessApiClient
): Promise<VariableResource> {
try {
const resp = await client.request(
'post',
`Services/${serviceSid}/Environments/${environmentSid}/Variables`,
{
form: {
Key: key,
Value: value,
},
}
);
return (resp.body as unknown) as VariableResource;
} catch (err) {
log('%O', new ClientApiError(err));
throw err;
}
}
/**
* Given the SID of a variable it will update the name and value of the variable
*
* @param {string} key the name of the variable
* @param {string} value the value of the variable
* @param {string} variableSid the SID of the existing variable
* @param {string} environmentSid the environment the variable belongs to
* @param {string} serviceSid the service the environment belongs to
* @param {TwilioServerlessApiClient} client API client
* @returns {Promise<VariableResource>}
*/
async function updateVariableInEnvironment(
key: string,
value: string,
variableSid: string,
environmentSid: string,
serviceSid: string,
client: TwilioServerlessApiClient
): Promise<VariableResource> {
try {
const resp = await client.request(
'post',
`Services/${serviceSid}/Environments/${environmentSid}/Variables/${variableSid}`,
{
form: {
Key: key,
Value: value,
},
}
);
return (resp.body as unknown) as VariableResource;
} catch (err) {
log('%O', new ClientApiError(err));
throw err;
}
}
/**
* Lists all variables for a given environment
*
* @export
* @param {string} environmentSid the environment to get the variables for
* @param {string} serviceSid the service the environment belongs to
* @param {TwilioServerlessApiClient} client API client
* @returns {Promise<VariableResource[]>}
*/
export async function listVariablesForEnvironment(
environmentSid: string,
serviceSid: string,
client: TwilioServerlessApiClient
): Promise<VariableResource[]> {
try {
return getPaginatedResource<VariableList, VariableResource>(
client,
`Services/${serviceSid}/Environments/${environmentSid}/Variables`
);
} catch (err) {
log('%O', new ClientApiError(err));
throw err;
}
}
/**
* Convers an object of environment variables into an array of key-value pairs
*
* @param {EnvironmentVariables} env the object of environment variables
* @returns {Variable[]}
*/
function convertToVariableArray(env: EnvironmentVariables): Variable[] {
const output: Variable[] = [];
Object.keys(env).forEach((key) => {
const value = env[key];
if (typeof value === 'string' || typeof value === 'number') {
output.push({ key, value: `${value}` });
}
});
return output;
}
/**
* Sets or updates the values passed in an object of environment variables for a specfic environment
*
* @export
* @param {EnvironmentVariables} envVariables the object of variables
* @param {string} environmentSid the environment the varibales should be set for
* @param {string} serviceSid the service the environment belongs to
* @param {TwilioServerlessApiClient} client API client
* @param {boolean} [removeRedundantOnes=false] whether to remove variables that are not passed but are currently set
* @returns {Promise<void>}
*/
export async function setEnvironmentVariables(
envVariables: EnvironmentVariables,
environmentSid: string,
serviceSid: string,
client: TwilioServerlessApiClient,
removeRedundantOnes: boolean = false
): Promise<void> {
const existingVariables = await listVariablesForEnvironment(
environmentSid,
serviceSid,
client
);
const variables = convertToVariableArray(envVariables);
const variableResources = variables.map((variable) => {
const existingResource = existingVariables.find(
(res) => res.key === variable.key
);
if (!existingResource) {
return registerVariableInEnvironment(
variable.key,
variable.value,
environmentSid,
serviceSid,
client
);
}
if (existingResource.value === variable.value) {
return Promise.resolve(existingResource);
}
return updateVariableInEnvironment(
variable.key,
variable.value,
existingResource.sid,
environmentSid,
serviceSid,
client
);
});
await Promise.all(variableResources);
if (removeRedundantOnes) {
const removeVariablePromises = existingVariables.map(async (variable) => {
if (typeof envVariables[variable.key] === 'undefined') {
return deleteEnvironmentVariable(
variable.sid,
environmentSid,
serviceSid,
client
);
}
});
await Promise.all(removeVariablePromises);
}
}
/**
* Deletes a given variable from a given environment
*
* @export
* @param {string} variableSid the SID of the variable to delete
* @param {string} environmentSid the environment the variable belongs to
* @param {string} serviceSid the service the environment belongs to
* @param {TwilioServerlessApiClient} client API client instance
* @returns {Promise<boolean>}
*/
export async function deleteEnvironmentVariable(
variableSid: string,
environmentSid: string,
serviceSid: string,
client: TwilioServerlessApiClient
): Promise<boolean> {
try {
const resp = await client.request(
'delete',
`Services/${serviceSid}/Environments/${environmentSid}/Variables/${variableSid}`
);
return true;
} catch (err) {
log('%O', new ClientApiError(err));
throw err;
}
}
/**
* Deletes all variables matching the passed keys from an environment
*
* @export
* @param {string[]} keys the keys of the variables to delete
* @param {string} environmentSid the environment the variables belong to
* @param {string} serviceSid the service the environment belongs to
* @param {TwilioServerlessApiClient} client API client instance
* @returns {Promise<boolean>}
*/
export async function removeEnvironmentVariables(
keys: string[],
environmentSid: string,
serviceSid: string,
client: TwilioServerlessApiClient
): Promise<boolean> {
const existingVariables = await listVariablesForEnvironment(
environmentSid,
serviceSid,
client
);
const variableSidMap = new Map<string, Sid>();
existingVariables.forEach((variableResource) => {
variableSidMap.set(variableResource.key, variableResource.sid);
});
const requests: Promise<boolean>[] = keys.map((key) => {
const variableSid = variableSidMap.get(key);
if (isSid(variableSid)) {
return deleteEnvironmentVariable(
variableSid,
environmentSid,
serviceSid,
client
);
}
return Promise.resolve(true);
});
await Promise.all(requests);
return true;
}