forked from aws/aws-cdk-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappsync-mapping-templates.ts
More file actions
195 lines (181 loc) · 7.94 KB
/
appsync-mapping-templates.ts
File metadata and controls
195 lines (181 loc) · 7.94 KB
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
import type {
GetSchemaCreationStatusCommandOutput,
GetSchemaCreationStatusCommandInput,
FunctionConfiguration,
} from '@aws-sdk/client-appsync';
import {
type ChangeHotswapResult,
classifyChanges,
type HotswappableChangeCandidate,
lowerCaseFirstCharacter,
transformObjectKeys,
} from './common';
import { ToolkitError } from '../../toolkit/error';
import type { SDK } from '../aws-auth';
import type { EvaluateCloudFormationTemplate } from '../evaluate-cloudformation-template';
type ApiId = string;
export async function isHotswappableAppSyncChange(
logicalId: string,
change: HotswappableChangeCandidate,
evaluateCfnTemplate: EvaluateCloudFormationTemplate,
): Promise<ChangeHotswapResult> {
const isResolver = change.newValue.Type === 'AWS::AppSync::Resolver';
const isFunction = change.newValue.Type === 'AWS::AppSync::FunctionConfiguration';
const isGraphQLSchema = change.newValue.Type === 'AWS::AppSync::GraphQLSchema';
const isAPIKey = change.newValue.Type === 'AWS::AppSync::ApiKey';
if (!isResolver && !isFunction && !isGraphQLSchema && !isAPIKey) {
return [];
}
const ret: ChangeHotswapResult = [];
const classifiedChanges = classifyChanges(change, [
'RequestMappingTemplate',
'RequestMappingTemplateS3Location',
'ResponseMappingTemplate',
'ResponseMappingTemplateS3Location',
'Code',
'CodeS3Location',
'Definition',
'DefinitionS3Location',
'Expires',
]);
classifiedChanges.reportNonHotswappablePropertyChanges(ret);
const namesOfHotswappableChanges = Object.keys(classifiedChanges.hotswappableProps);
if (namesOfHotswappableChanges.length > 0) {
let physicalName: string | undefined = undefined;
const arn = await evaluateCfnTemplate.establishResourcePhysicalName(
logicalId,
isFunction ? change.newValue.Properties?.Name : undefined,
);
if (isResolver) {
const arnParts = arn?.split('/');
physicalName = arnParts ? `${arnParts[3]}.${arnParts[5]}` : undefined;
} else {
physicalName = arn;
}
const functions: Record<ApiId, FunctionConfiguration[]> = {};
ret.push({
hotswappable: true,
resourceType: change.newValue.Type,
propsChanged: namesOfHotswappableChanges,
service: 'appsync',
resourceNames: [`${change.newValue.Type} '${physicalName}'`],
apply: async (sdk: SDK) => {
if (!physicalName) {
return;
}
const sdkProperties: { [name: string]: any } = {
...change.oldValue.Properties,
Definition: change.newValue.Properties?.Definition,
DefinitionS3Location: change.newValue.Properties?.DefinitionS3Location,
requestMappingTemplate: change.newValue.Properties?.RequestMappingTemplate,
requestMappingTemplateS3Location: change.newValue.Properties?.RequestMappingTemplateS3Location,
responseMappingTemplate: change.newValue.Properties?.ResponseMappingTemplate,
responseMappingTemplateS3Location: change.newValue.Properties?.ResponseMappingTemplateS3Location,
code: change.newValue.Properties?.Code,
codeS3Location: change.newValue.Properties?.CodeS3Location,
expires: change.newValue.Properties?.Expires,
};
const evaluatedResourceProperties = await evaluateCfnTemplate.evaluateCfnExpression(sdkProperties);
const sdkRequestObject = transformObjectKeys(evaluatedResourceProperties, lowerCaseFirstCharacter);
// resolve s3 location files as SDK doesn't take in s3 location but inline code
if (sdkRequestObject.requestMappingTemplateS3Location) {
sdkRequestObject.requestMappingTemplate = await fetchFileFromS3(
sdkRequestObject.requestMappingTemplateS3Location,
sdk,
);
delete sdkRequestObject.requestMappingTemplateS3Location;
}
if (sdkRequestObject.responseMappingTemplateS3Location) {
sdkRequestObject.responseMappingTemplate = await fetchFileFromS3(
sdkRequestObject.responseMappingTemplateS3Location,
sdk,
);
delete sdkRequestObject.responseMappingTemplateS3Location;
}
if (sdkRequestObject.definitionS3Location) {
sdkRequestObject.definition = await fetchFileFromS3(sdkRequestObject.definitionS3Location, sdk);
delete sdkRequestObject.definitionS3Location;
}
if (sdkRequestObject.codeS3Location) {
sdkRequestObject.code = await fetchFileFromS3(sdkRequestObject.codeS3Location, sdk);
delete sdkRequestObject.codeS3Location;
}
if (isResolver) {
await sdk.appsync().updateResolver(sdkRequestObject);
} else if (isFunction) {
// Function version is only applicable when using VTL and mapping templates
// Runtime only applicable when using code (JS mapping templates)
if (sdkRequestObject.code) {
delete sdkRequestObject.functionVersion;
} else {
delete sdkRequestObject.runtime;
}
if (!functions.hasOwnProperty(sdkRequestObject.apiId)) {
functions[sdkRequestObject.apiId] = await sdk.appsync().listFunctions({ apiId: sdkRequestObject.apiId });
}
const { functionId } = functions[sdkRequestObject.apiId].find((fn) => fn.name === physicalName) ?? {};
// Updating multiple functions at the same time or along with graphql schema results in `ConcurrentModificationException`
await exponentialBackOffRetry(
() =>
sdk.appsync().updateFunction({
...sdkRequestObject,
functionId: functionId,
}),
6,
1000,
'ConcurrentModificationException',
);
} else if (isGraphQLSchema) {
let schemaCreationResponse: GetSchemaCreationStatusCommandOutput = await sdk
.appsync()
.startSchemaCreation(sdkRequestObject);
while (
schemaCreationResponse.status &&
['PROCESSING', 'DELETING'].some((status) => status === schemaCreationResponse.status)
) {
await sleep(1000); // poll every second
const getSchemaCreationStatusRequest: GetSchemaCreationStatusCommandInput = {
apiId: sdkRequestObject.apiId,
};
schemaCreationResponse = await sdk.appsync().getSchemaCreationStatus(getSchemaCreationStatusRequest);
}
if (schemaCreationResponse.status === 'FAILED') {
throw new ToolkitError(schemaCreationResponse.details ?? 'Schema creation has failed.');
}
} else {
// isApiKey
if (!sdkRequestObject.id) {
// ApiKeyId is optional in CFN but required in SDK. Grab the KeyId from physicalArn if not available as part of CFN template
const arnParts = physicalName?.split('/');
if (arnParts && arnParts.length === 4) {
sdkRequestObject.id = arnParts[3];
}
}
await sdk.appsync().updateApiKey(sdkRequestObject);
}
},
});
}
return ret;
}
async function fetchFileFromS3(s3Url: string, sdk: SDK) {
const s3PathParts = s3Url.split('/');
const s3Bucket = s3PathParts[2]; // first two are "s3:" and "" due to s3://
const s3Key = s3PathParts.splice(3).join('/'); // after removing first three we reconstruct the key
return (await sdk.s3().getObject({ Bucket: s3Bucket, Key: s3Key })).Body?.transformToString();
}
async function exponentialBackOffRetry(fn: () => Promise<any>, numOfRetries: number, backOff: number, errorCodeToRetry: string) {
try {
await fn();
} catch (error: any) {
if (error && error.name === errorCodeToRetry && numOfRetries > 0) {
await sleep(backOff); // time to wait doubles everytime function fails, starts at 1 second
await exponentialBackOffRetry(fn, numOfRetries - 1, backOff * 2, errorCodeToRetry);
} else {
throw error;
}
}
}
async function sleep(ms: number) {
return new Promise((ok) => setTimeout(ok, ms));
}