Skip to content

Commit 0400f26

Browse files
kaustavghosh06yuth
authored andcommitted
feat: multiple evironment support for analytics and s3 storage
1 parent 4abaccc commit 0400f26

File tree

6 files changed

+113
-34
lines changed

6 files changed

+113
-34
lines changed

packages/amplify-category-analytics/provider-utils/awscloudformation/cloudformation-templates/pinpoint-cloudformation-template.json

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030
"unauthRoleName": {
3131
"Type": "String"
3232
},
33-
"IAMPrefix": {
33+
"authRoleArn": {
34+
"Type": "String"
35+
},
36+
"env": {
3437
"Type": "String"
3538
}
3639
},
@@ -56,6 +59,14 @@
5659
},
5760
"NONE"
5861
]
62+
},
63+
"ShouldNotCreateEnvResources": {
64+
"Fn::Equals": [
65+
{
66+
"Ref": "env"
67+
},
68+
"NONE"
69+
]
5970
}
6071
},
6172
"Resources": {
@@ -190,7 +201,27 @@
190201
]
191202
},
192203
"appName": {
193-
"Ref": "appName"
204+
"Fn::If": [
205+
"ShouldNotCreateEnvResources",
206+
{
207+
"Ref": "appName"
208+
},
209+
{
210+
211+
"Fn::Join": [
212+
"",
213+
[
214+
{
215+
"Ref": "appName"
216+
},
217+
"-",
218+
{
219+
"Ref": "env"
220+
}
221+
]
222+
]
223+
},
224+
]
194225
}
195226
}
196227
},
@@ -222,9 +253,7 @@
222253
"",
223254
[
224255
"arn:aws:mobiletargeting:*:",
225-
{
226-
"Ref": "IAMPrefix"
227-
},
256+
{ "Fn::Select" : [ "4", { "Fn::Split": [":", {"Ref": "authRoleArn"}]}]},
228257
":apps/",
229258
{
230259
"Fn::GetAtt": [
@@ -242,9 +271,7 @@
242271
"",
243272
[
244273
"arn:aws:mobiletargeting:*:",
245-
{
246-
"Ref": "IAMPrefix"
247-
},
274+
{ "Fn::Select" : [ "4", { "Fn::Split": [":", {"Ref": "authRoleArn"}]}]},
248275
":apps/",
249276
{
250277
"Ref": "appId"
@@ -290,9 +317,7 @@
290317
"",
291318
[
292319
"arn:aws:mobiletargeting:*:",
293-
{
294-
"Ref": "IAMPrefix"
295-
},
320+
{ "Fn::Select" : [ "4", { "Fn::Split": [":", {"Ref": "authRoleArn"}]}]},
296321
":apps/",
297322
{
298323
"Fn::GetAtt": [
@@ -310,9 +335,7 @@
310335
"",
311336
[
312337
"arn:aws:mobiletargeting:*:",
313-
{
314-
"Ref": "IAMPrefix"
315-
},
338+
{ "Fn::Select" : [ "4", { "Fn::Split": [":", {"Ref": "authRoleArn"}]}]},
316339
":apps/",
317340
{
318341
"Ref": "appId"

packages/amplify-category-analytics/provider-utils/awscloudformation/default-values/pinpoint-defaults.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@ const getAllDefaults = (project) => {
44
const appName = project.projectConfig.projectName.toLowerCase();
55
const [shortId] = uuid().split('-');
66

7-
const { amplifyMeta } = project;
8-
const providerInfo = amplifyMeta.providers.awscloudformation;
9-
10-
const authRoleName = providerInfo.AuthRoleName;
11-
const unauthRoleName = providerInfo.UnauthRoleName;
7+
const authRoleName = {
8+
Ref: 'AuthRoleName',
9+
};
1210

13-
const authRoleArn = providerInfo.AuthRoleArn;
11+
const unauthRoleName = {
12+
Ref: 'UnauthRoleName',
13+
};
1414

15-
const splitArn = authRoleArn.split(':');
16-
const IAMPrefix = splitArn[4];
15+
const authRoleArn = {
16+
'Fn::GetAtt': [
17+
'AuthRole',
18+
'Arn',
19+
],
20+
};
1721

1822
const defaults = {
1923
appName,
@@ -25,7 +29,7 @@ const getAllDefaults = (project) => {
2529
unauthPolicyName: `pinpoint_amplify_${shortId}`,
2630
authRoleName,
2731
unauthRoleName,
28-
IAMPrefix,
32+
authRoleArn,
2933
};
3034

3135
return defaults;

packages/amplify-category-storage/provider-utils/awscloudformation/cloudformation-templates/s3-cloudformation-template.json

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,21 @@
2222
},
2323
"authPermissions": {
2424
"Type": "String"
25-
}
25+
},
26+
"env": {
27+
"Type": "String"
28+
}
29+
2630
},
2731
"Conditions": {
32+
"ShouldNotCreateEnvResources": {
33+
"Fn::Equals": [
34+
{
35+
"Ref": "env"
36+
},
37+
"NONE"
38+
]
39+
},
2840
"EnableUnauthReadWrite": {
2941
"Fn::Equals": [
3042
{
@@ -80,8 +92,27 @@
8092
"DeletionPolicy" : "Retain",
8193
"Properties": {
8294
"BucketName": {
83-
"Ref": "bucketName"
84-
},
95+
"Fn::If": [
96+
"ShouldNotCreateEnvResources",
97+
{
98+
"Ref": "bucketName"
99+
},
100+
{
101+
"Fn::Join": [
102+
"",
103+
[
104+
{
105+
"Ref": "bucketName"
106+
},
107+
"-",
108+
{
109+
"Ref": "env"
110+
}
111+
]
112+
]
113+
}
114+
]
115+
},
85116
"CorsConfiguration": {
86117
"CorsRules": [
87118
{

packages/amplify-category-storage/provider-utils/awscloudformation/default-values/s3-defaults.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ const getAllDefaults = (project) => {
44
const name = project.projectConfig.projectName.toLowerCase();
55
const [shortId] = uuid().split('-');
66

7-
const { amplifyMeta } = project;
8-
const providerInfo = amplifyMeta.providers.awscloudformation;
7+
const authRoleName = {
8+
Ref: 'AuthRoleName',
9+
};
910

10-
const authRoleName = providerInfo.AuthRoleName;
11-
const unauthRoleName = providerInfo.UnauthRoleName;
11+
const unauthRoleName = {
12+
Ref: 'UnauthRoleName',
13+
};
1214

1315
const defaults = {
1416
resourceName: `s3${shortId}`,

packages/amplify-cli/src/commands/env/add.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
const fs = require('fs-extra');
2+
const path = require('path');
23

34
module.exports = {
45
name: 'add',
56
run: async (context) => {
67
const envName = context.parameters.options.name;
78
const config = JSON.parse(context.parameters.options.config);
9+
const awsInfo = JSON.parse(context.parameters.options.awsInfo);
10+
811

912
if (!envName) {
1013
context.print.error('You must pass in the name of the environment using the --name flag');
@@ -28,13 +31,28 @@ module.exports = {
2831
const addNewEnvConfig = () => {
2932
const envProviderFilepath = context.amplify.pathManager.getProviderInfoFilePath();
3033
allEnvs[envName] = config;
31-
const jsonString = JSON.stringify(allEnvs, null, '\t');
34+
let jsonString = JSON.stringify(allEnvs, null, '\t');
3235
fs.writeFileSync(envProviderFilepath, jsonString, 'utf8');
36+
37+
const dotConfigDirPath = context.amplify.pathManager.getDotConfigDirPath();
38+
const configInfoFilePath = path.join(dotConfigDirPath, 'local-aws-info.json');
39+
40+
let envAwsInfo = {};
41+
if (fs.existsSync(configInfoFilePath)) {
42+
envAwsInfo = JSON.parse(fs.readFileSync(configInfoFilePath));
43+
}
44+
45+
envAwsInfo[envName] = awsInfo;
46+
jsonString = JSON.stringify(envAwsInfo, null, 4);
47+
fs.writeFileSync(configInfoFilePath, jsonString, 'utf8');
48+
3349
context.print.success('Successfully added environment from your project');
3450
};
3551

3652
if (envFound) {
37-
if (await context.prompt.confirm('We found an environment with the same name. Do you want to overwrite existing enviornment config?')) {
53+
if (context.parameters.options.yes) {
54+
addNewEnvConfig();
55+
} else if (await context.prompt.confirm('We found an environment with the same name. Do you want to overwrite existing enviornment config?')) {
3856
addNewEnvConfig();
3957
}
4058
} else {

packages/amplify-provider-awscloudformation/lib/configuration-manager.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function carryOutConfigAction(context) {
135135

136136
async function initialize(context) {
137137
const { awsConfigInfo } = context.exeInfo;
138-
if (context.exeInfo.inputParams && context.exeInfo.inputParams[constants.Label]) {
138+
if (context.exeInfo.inputParams && context.exeInfo.inputParams.providers[constants.Label]) {
139139
const inputParams = context.exeInfo.inputParams[constants.Label];
140140
Object.assign(awsConfigInfo, inputParams);
141141
} else if (awsConfigInfo.configLevel === 'project' &&
@@ -214,6 +214,7 @@ function printInfo(context) {
214214
async function setProjectConfigAction(context) {
215215
if (context.exeInfo.inputParams[constants.Label]) {
216216
const inputParams = context.exeInfo.inputParams[constants.Label];
217+
217218
if (context.exeInfo.awsConfigInfo.configLevel === 'project') {
218219
if (inputParams.configLevel === 'project') {
219220
context.exeInfo.awsConfigInfo.action = 'update';
@@ -512,7 +513,7 @@ function logProjectSpecificConfg(context, awsClient) {
512513
if (fs.existsSync(configInfoFilePath)) {
513514
const { envName } = context.amplify.getEnvInfo();
514515
const configInfo = JSON.parse(fs.readFileSync(configInfoFilePath, 'utf8'))[envName];
515-
if (configInfo.useProfile && configInfo.profileName) {
516+
if (configInfo && configInfo.useProfile && configInfo.profileName) {
516517
process.env.AWS_PROFILE = configInfo.profileName;
517518

518519
const credentials = new awsClient.SharedIniFileCredentials({

0 commit comments

Comments
 (0)