-
Notifications
You must be signed in to change notification settings - Fork 818
/
storeResources.ts
320 lines (287 loc) · 13.1 KB
/
storeResources.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import { JSONUtilities, pathManager, stateManager, $TSAny, $TSContext, $TSObject } from 'amplify-cli-core';
import { FunctionParameters, FunctionTriggerParameters, FunctionBreadcrumbs } from 'amplify-function-plugin-interface';
import _ from 'lodash';
import fs from 'fs-extra';
import path from 'path';
import { functionParametersFileName, layerParametersFileName, parametersFileName, provider, ServiceName } from './constants';
import { category as categoryName } from '../../../constants';
import { generateLayerCfnObj } from './lambda-layer-cloudformation-template';
import { isMultiEnvLayer, LayerParameters, StoredLayerParameters } from './layerParams';
import { convertLambdaLayerMetaToLayerCFNArray } from './layerArnConverter';
import { saveLayerRuntimes } from './layerRuntimes';
// handling both FunctionParameters and FunctionTriggerParameters here is a hack
// ideally we refactor the auth trigger flows to use FunctionParameters directly and get rid of FunctionTriggerParameters altogether
export function createFunctionResources(context: $TSContext, parameters: FunctionParameters | FunctionTriggerParameters) {
context.amplify.updateamplifyMetaAfterResourceAdd(
categoryName,
parameters.resourceName || parameters.functionName,
translateFuncParamsToResourceOpts(parameters),
);
// copy template, CFN and parameter files
copyTemplateFiles(context, parameters);
saveMutableState(parameters);
saveCFNParameters(parameters);
context.amplify.leaveBreadcrumbs(context, categoryName, parameters.resourceName, createBreadcrumbs(parameters));
}
export const createLayerArtifacts = (context: $TSContext, parameters: LayerParameters, latestVersion: number = 1): string => {
const layerDirPath = ensureLayerFolders(parameters);
updateLayerState(context, parameters, layerDirPath);
createParametersFile({ layerVersion: latestVersion }, parameters.layerName, parametersFileName);
createLayerCfnFile(context, parameters, layerDirPath);
addLayerToAmplifyMeta(context, parameters);
return layerDirPath;
};
// updates the layer resources and returns the resource directory
const defaultOpts = {
layerParams: true,
cfnFile: true,
amplifyMeta: true,
};
export const updateLayerArtifacts = (
context: $TSContext,
parameters: LayerParameters,
latestVersion?: number,
options: Partial<typeof defaultOpts> = {},
): string => {
options = _.assign(defaultOpts, options);
const layerDirPath = ensureLayerFolders(parameters);
if (options.layerParams) {
updateLayerState(context, parameters, layerDirPath);
}
if (options.cfnFile) {
if (latestVersion !== undefined) {
createParametersFile({ layerVersion: latestVersion }, parameters.layerName, parametersFileName);
}
updateLayerCfnFile(context, parameters, layerDirPath);
}
if (options.amplifyMeta) {
updateLayerInAmplifyMeta(context, parameters);
}
return layerDirPath;
};
export function removeLayerArtifacts(context: $TSContext, layerName: string) {
if (isMultiEnvLayer(context, layerName)) {
removeLayerFromTeamProviderInfo(context, layerName);
}
}
// ideally function update should be refactored so this function does not need to be exported
export function saveMutableState(
parameters:
| Partial<Pick<FunctionParameters, 'mutableParametersState' | 'resourceName' | 'lambdaLayers' | 'functionName'>>
| FunctionTriggerParameters,
) {
createParametersFile(buildParametersFileObj(parameters), parameters.resourceName || parameters.functionName, functionParametersFileName);
}
// ideally function update should be refactored so this function does not need to be exported
export function saveCFNParameters(
parameters: Partial<Pick<FunctionParameters, 'cloudwatchRule' | 'resourceName'>> | FunctionTriggerParameters,
) {
if ('trigger' in parameters) {
const params = {
modules: parameters.modules.join(),
resourceName: parameters.resourceName,
};
createParametersFile(params, parameters.resourceName, parametersFileName);
}
if ('cloudwatchRule' in parameters) {
const params = {
CloudWatchRule: parameters.cloudwatchRule,
};
createParametersFile(params, parameters.resourceName, parametersFileName);
}
}
function updateLayerState(context: $TSContext, parameters: LayerParameters, layerDirPath: string) {
if (isMultiEnvLayer(context, parameters.layerName)) {
updateLayerTeamProviderInfo(context, parameters, layerDirPath);
saveLayerRuntimes(layerDirPath, parameters.layerName, parameters.runtimes);
} else {
createLayerParametersFile(parameters, layerDirPath, isMultiEnvLayer(context, parameters.layerName));
}
}
function copyTemplateFiles(context: $TSContext, parameters: FunctionParameters | FunctionTriggerParameters) {
// copy function template files
const destDir = pathManager.getBackendDirPath();
const copyJobs = parameters.functionTemplate.sourceFiles.map(file => {
return {
dir: parameters.functionTemplate.sourceRoot,
template: file,
target: path.join(
destDir,
categoryName,
parameters.resourceName,
_.get(parameters.functionTemplate.destMap, file, file.replace(/\.ejs$/, '')),
),
};
});
// this is a hack to reuse some old code
let templateParams: $TSAny = parameters;
if ('trigger' in parameters) {
let triggerEnvs = context.amplify.loadEnvResourceParameters(context, 'function', parameters.resourceName);
parameters.triggerEnvs = JSONUtilities.parse(parameters.triggerEnvs) || [];
parameters.triggerEnvs.forEach(c => {
triggerEnvs[c.key] = c.value;
});
templateParams = _.assign(templateParams, triggerEnvs);
}
templateParams = _.assign(templateParams, {
enableCors: process.env.AMPLIFY_CLI_LAMBDA_CORS_HEADER === 'true',
});
context.amplify.copyBatch(context, copyJobs, templateParams, false);
// copy cloud resource template
const cloudTemplateJob = {
dir: '',
template: parameters.cloudResourceTemplatePath,
target: path.join(destDir, categoryName, parameters.resourceName, `${parameters.resourceName}-cloudformation-template.json`),
};
const copyJobParams: $TSAny = parameters;
if ('lambdaLayers' in parameters) {
const layerCFNValues = convertLambdaLayerMetaToLayerCFNArray(context, parameters.lambdaLayers, context.amplify.getEnvInfo().envName);
copyJobParams.lambdaLayersCFNArray = layerCFNValues;
}
context.amplify.copyBatch(context, [cloudTemplateJob], copyJobParams, false);
}
function ensureLayerFolders(parameters: $TSAny) {
const projectBackendDirPath = pathManager.getBackendDirPath();
const layerDirPath = path.join(projectBackendDirPath, categoryName, parameters.layerName);
fs.ensureDirSync(path.join(layerDirPath, 'opt'));
parameters.runtimes.forEach(runtime => ensureLayerRuntimeFolder(layerDirPath, runtime));
return layerDirPath;
}
// Default files are only created if the path does not exist
function ensureLayerRuntimeFolder(layerDirPath: string, runtime: $TSAny) {
const runtimeDirPath = path.join(layerDirPath, 'lib', runtime.layerExecutablePath);
if (!fs.pathExistsSync(runtimeDirPath)) {
fs.ensureDirSync(runtimeDirPath);
fs.writeFileSync(path.join(runtimeDirPath, 'README.txt'), 'Replace this file with your layer files');
(runtime.layerDefaultFiles || []).forEach(defaultFile =>
fs.writeFileSync(path.join(layerDirPath, 'lib', defaultFile.path, defaultFile.filename), defaultFile.content),
);
}
}
function createLayerCfnFile(context: $TSContext, parameters: LayerParameters, layerDirPath: string) {
JSONUtilities.writeJson(
path.join(layerDirPath, parameters.layerName + '-awscloudformation-template.json'),
generateLayerCfnObj(context, parameters),
);
}
function updateLayerCfnFile(context: $TSContext, parameters: LayerParameters, layerDirPath: string) {
JSONUtilities.writeJson(
path.join(layerDirPath, parameters.layerName + '-awscloudformation-template.json'),
generateLayerCfnObj(context, parameters),
);
}
const writeParametersToAmplifyMeta = (context: $TSContext, layerName: string, parameters) => {
const amplifyMeta = context.amplify.getProjectMeta();
_.set(amplifyMeta, ['function', layerName], parameters);
JSONUtilities.writeJson(pathManager.getAmplifyMetaFilePath(), amplifyMeta);
};
const addLayerToAmplifyMeta = (context: $TSContext, parameters: LayerParameters) => {
context.amplify.updateamplifyMetaAfterResourceAdd(categoryName, parameters.layerName, amplifyMetaAndBackendParams(parameters));
writeParametersToAmplifyMeta(
context,
parameters.layerName,
layerParamsToAmplifyMetaParams(parameters, isMultiEnvLayer(context, parameters.layerName)),
);
};
const updateLayerInAmplifyMeta = (context: $TSContext, parameters: LayerParameters) => {
writeParametersToAmplifyMeta(
context,
parameters.layerName,
layerParamsToAmplifyMetaParams(parameters, isMultiEnvLayer(context, parameters.layerName)),
);
};
const createLayerParametersFile = (parameters: LayerParameters | StoredLayerParameters, layerDirPath: string, isMultiEnv: boolean) => {
fs.ensureDirSync(layerDirPath);
const parametersFilePath = path.join(layerDirPath, layerParametersFileName);
JSONUtilities.writeJson(parametersFilePath, layerParamsToStoredParams(parameters, isMultiEnv));
};
const updateLayerTeamProviderInfo = (context: $TSContext, parameters: LayerParameters, layerDirPath: string) => {
fs.ensureDirSync(layerDirPath);
const { envName } = context.amplify.getEnvInfo();
const teamProviderInfo = stateManager.getTeamProviderInfo();
_.set(
teamProviderInfo,
[envName, 'nonCFNdata', categoryName, parameters.layerName],
layerParamsToStoredParams(parameters, isMultiEnvLayer(context, parameters.layerName)),
);
stateManager.setTeamProviderInfo(undefined, teamProviderInfo);
};
const removeLayerFromTeamProviderInfo = (context: $TSContext, layerName: string) => {
const { envName } = context.amplify.getEnvInfo();
const teamProviderInfo = stateManager.getTeamProviderInfo();
_.unset(teamProviderInfo, [envName, 'nonCFNdata', categoryName, layerName]);
if (_.isEmpty(_.get(teamProviderInfo, [envName, 'nonCFNdata', categoryName]))) {
_.unset(teamProviderInfo, [envName, 'nonCFNdata', categoryName]);
if (_.isEmpty(_.get(teamProviderInfo, [envName, 'nonCFNdata']))) {
_.unset(teamProviderInfo, [envName, 'nonCFNdata']);
}
}
stateManager.setTeamProviderInfo(undefined, teamProviderInfo);
};
interface LayerMetaAndBackendConfigParams {
providerPlugin: string;
service: string;
build: boolean;
}
const amplifyMetaAndBackendParams = (parameters: LayerParameters): LayerMetaAndBackendConfigParams => ({
providerPlugin: parameters.providerContext.provider,
service: parameters.providerContext.service,
build: parameters.build,
});
const layerParamsToAmplifyMetaParams = (
parameters: LayerParameters,
isMultiEnv: boolean,
): LayerMetaAndBackendConfigParams & StoredLayerParameters => {
const amplifyMetaBackendParams = amplifyMetaAndBackendParams(parameters);
return _.assign(layerParamsToStoredParams(parameters, isMultiEnv), amplifyMetaBackendParams);
};
const layerParamsToStoredParams = (parameters: LayerParameters | StoredLayerParameters, isMultiEnv: boolean): StoredLayerParameters => {
const storedParams: StoredLayerParameters = { layerVersionMap: parameters.layerVersionMap };
if (!isMultiEnv) {
storedParams.runtimes = (parameters.runtimes || []).map(runtime =>
_.pick(runtime, 'value', 'name', 'layerExecutablePath', 'cloudTemplateValue'),
);
}
return storedParams;
};
function createParametersFile(parameters: $TSObject, resourceName: string, parametersFileName: string) {
const parametersFilePath = path.join(pathManager.getBackendDirPath(), categoryName, resourceName, parametersFileName);
const currentParameters: $TSAny = JSONUtilities.readJson(parametersFilePath, { throwIfNotExist: false }) || {};
delete currentParameters.mutableParametersState; // this field was written in error in a previous version of the cli
JSONUtilities.writeJson(parametersFilePath, { ...currentParameters, ...parameters });
}
function buildParametersFileObj(
parameters: Partial<Pick<FunctionParameters, 'mutableParametersState' | 'lambdaLayers'>> | FunctionTriggerParameters,
): any {
if ('trigger' in parameters) {
return _.omit(parameters, ['functionTemplate', 'cloudResourceTemplatePath']);
}
return { ...parameters.mutableParametersState, ..._.pick(parameters, ['lambdaLayers']) };
}
function translateFuncParamsToResourceOpts(params: FunctionParameters | FunctionTriggerParameters): $TSAny {
let result: $TSObject = {
build: true,
providerPlugin: provider,
service: ServiceName.LambdaFunction,
};
if (!('trigger' in params)) {
result.dependsOn = params.dependsOn;
}
return result;
}
function createBreadcrumbs(params: FunctionParameters | FunctionTriggerParameters): FunctionBreadcrumbs {
if ('trigger' in params) {
return {
pluginId: 'amplify-nodejs-function-runtime-provider',
functionRuntime: 'nodejs',
useLegacyBuild: true,
defaultEditorFile: 'src/index.js',
};
}
return {
pluginId: params.runtimePluginId,
functionRuntime: params.runtime.value,
useLegacyBuild: params.runtime.value === 'nodejs' ? true : false, // so we can update node builds in the future
defaultEditorFile: params.functionTemplate.defaultEditorFile,
};
}