-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddRemoveAssets.js
178 lines (168 loc) · 5.18 KB
/
AddRemoveAssets.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
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/**
* This script adds ore removes additional assets for the example after the
* primary resources have been deployed by running the setup.yaml AWS CloudFormation
* script.
*
* Before running this script, deploy the CloudFormation stack by running the following
* at a command prompt:
*
* aws cloudformation create-stack --stack-name <stack-name> --template-body
* file://setup.yaml --capabilities CAPABILITY_IAM
*
* ** Add assets **
* Add additional demo assets by running the following at a command prompt:
*
* node AddRemoveAssets.js add <stack-name>
*
* Remember to substitute the name of the CloudFormation stack for `<stack-name>`.
*
* ** Remove assets **
* Remove the additional demo assets by running the following at a command prompt:
*
* node AddRemoveAssets.js remove
*
* Remember to destroy the CloudFormation stack after running this script to completely
* remove resources created for the demo.
*/
import fs from "fs";
import { Config } from "./src/Config.js";
import {
S3Client,
PutObjectCommand,
DeleteObjectCommand,
} from "@aws-sdk/client-s3";
import {
CognitoIdentityProviderClient,
AdminDeleteUserCommand,
ListUsersCommand,
} from "@aws-sdk/client-cognito-identity-provider";
import {
CognitoIdentityClient,
DeleteIdentitiesCommand,
ListIdentitiesCommand,
} from "@aws-sdk/client-cognito-identity";
import {
CloudFormationClient,
DescribeStacksCommand,
} from "@aws-sdk/client-cloudformation";
const ConfigError =
"Demo resources not initialized. You must deploy AWS resources " +
"and demo elements before running this application. See the README for details.";
const DefaultImageName = "default_document_3.png";
const mode = process.argv[2];
const stackName = process.argv[3];
const s3 = new S3Client({});
if (mode === "add") {
console.log(`Updating Config.js with outputs from stack ${stackName}.`);
const cform = new CloudFormationClient({});
(async () => {
try {
const { Stacks } = await cform.send(
new DescribeStacksCommand({
StackName: stackName,
}),
);
let configOutputs =
` StackName: '${stackName}',\n` +
` DefaultImageName: '${DefaultImageName}',\n`;
Stacks[0].Outputs.forEach((current) => {
configOutputs += ` ${current.OutputKey}: '${current.OutputValue}',\n`;
Config[current.OutputKey] = current.OutputValue;
});
fs.writeFileSync(
"src/Config.js",
`export const Config = {\n${configOutputs}};`,
);
console.log(
`Uploading demo image ${DefaultImageName} to ` +
`bucket ${Config.DefaultBucketName}.`,
);
const imageFile = fs.readFileSync(`src/.media/${DefaultImageName}`);
await s3.send(
new PutObjectCommand({
Bucket: Config.DefaultBucketName,
Key: DefaultImageName,
Body: imageFile,
}),
);
console.log("Demo assets successfully added!");
} catch (error) {
console.log(error.message);
}
})();
} else if (mode === "remove") {
console.log(`Emptying bucket ${Config.DefaultBucketName}.`);
(async () => {
try {
await s3.send(
new DeleteObjectCommand({
Bucket: Config.DefaultBucketName,
Key: Config.DefaultImageName,
}),
);
} catch (error) {
console.log(error.message);
}
})();
console.log(`Removing users from user pool ${Config.CognitoUserPoolId}.`);
const cogProvider = new CognitoIdentityProviderClient({});
(async () => {
try {
const { Users } = await cogProvider.send(
new ListUsersCommand({
UserPoolId: Config.CognitoUserPoolId,
}),
);
Users.forEach((user) => {
console.log(`Deleting user ${user.Username}`);
cogProvider.send(
new AdminDeleteUserCommand({
UserPoolId: Config.CognitoUserPoolId,
Username: user.Username,
}),
);
});
} catch (error) {
console.log(error.message);
}
})();
const cogIdentity = new CognitoIdentityClient({});
(async () => {
try {
const { Identities } = await cogIdentity.send(
new ListIdentitiesCommand({
IdentityPoolId: Config.CognitoIdentityPoolId,
MaxResults: 10,
}),
);
const idList = Identities.map((id) => id.IdentityId);
console.log(
`Removing identities ${idList} from identity` +
`pool ${Config.CognitoIdentityPoolId}.`,
);
await cogIdentity.send(
new DeleteIdentitiesCommand({
IdentityIdsToDelete: idList,
}),
);
} catch (error) {
console.log(error.message);
}
let configOutputs = ` ConfigError: '${ConfigError}'\n`;
fs.writeFileSync(
"src/Config.js",
`export const Config = {\n${configOutputs}};`,
);
console.log(
"Demo assets removed. Now run `aws cloudformation delete-stack " +
"stack-name <stack-name>` to delete resources deployed for the demo.",
);
})();
} else {
console.log(
"Run with `add <stack-name>` to add demo assets.\n" +
"Run with `remove` to remove them.",
);
}