Skip to content

Commit

Permalink
feat: provide tags on create app (#6381)
Browse files Browse the repository at this point in the history
  • Loading branch information
ammarkarachi committed Jan 21, 2021
1 parent 80fbd13 commit 0530d1a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
26 changes: 15 additions & 11 deletions packages/amplify-e2e-tests/src/__tests__/tags.test.ts
Expand Up @@ -3,12 +3,12 @@ import {
createNewProjectDir,
deleteProject,
deleteProjectDir,
amplifyPushWithoutCodegen,
getProjectMeta,
getProjectTags,
describeCloudFormationStack,
addDEVHosting,
} from 'amplify-e2e-core';
import _ from 'lodash';
import { Amplify } from 'aws-sdk';

describe('generated tags test', () => {
let projRoot: string;
Expand All @@ -23,22 +23,26 @@ describe('generated tags test', () => {
});

it('should compare the nested stack tags key with the tags.json file and return true', async () => {
const projName = 'tagsTest';
const envName = 'devtag';
await initJSProjectWithProfile(projRoot, { name: projName, envName });
await addDEVHosting(projRoot);
await amplifyPushWithoutCodegen(projRoot);
await initJSProjectWithProfile(projRoot, { disableAmplifyAppCreation: false });

// This block of code gets the necessary info to compare the values of both the local tags from the JSON file and tags on the stack
const amplifyMeta = getProjectMeta(projRoot);
const meta = amplifyMeta.providers.awscloudformation;
const rootStackInfo = await describeCloudFormationStack(meta.StackName, meta.Region);
const localTags = getProjectTags(projRoot);

// Currently only checks to make sure that thhe pushed tags have the same amount and name of keys than the ones added locally on the tags.json file
expect(checkEquality(localTags, rootStackInfo.Tags)).toBe(true);
expect(rootStackInfo.Tags.filter(r => r.Key === 'user:Stack')[0].Value).toEqual(envName);
expect(rootStackInfo.Tags.filter(r => r.Key === 'user:Application')[0].Value).toEqual(projName);
expect(checkEquality(localTags, rootStackInfo.Tags)).toBeTruthy();
const { AmplifyAppId, Region } = meta;
const amplify = new Amplify({ region: Region });
const amplifyApp = await amplify.getApp({ appId: AmplifyAppId }).promise();
const tagMap = amplifyApp.app.tags;
const amplifyTags = Object.keys(tagMap).map(key => {
return {
Key: key,
Value: tagMap[key],
};
});
expect(checkEquality(localTags, amplifyTags)).toBeTruthy();
});
});

Expand Down
Expand Up @@ -18,6 +18,7 @@ async function init(amplifyServiceParams) {
let deploymentBucketName = `${stackName}-deployment`;

const amplifyClient = await getConfiguredAmplifyClient(context, awsConfig);
logger('init.getConfiguredAmplifyClient', [!!amplifyClient])();
if (!amplifyClient) {
// This happens when the Amplify service is not available in the region
return {
Expand All @@ -28,6 +29,7 @@ async function init(amplifyServiceParams) {
}

const hasPermission = await checkAmplifyServiceIAMPermission(context, amplifyClient);
logger('init.checkAmplifyServiceIAMPermission', [hasPermission])();
if (!hasPermission) {
return {
amplifyAppId,
Expand Down Expand Up @@ -115,13 +117,18 @@ async function init(amplifyServiceParams) {
}

if (!amplifyAppId) {
logger('in if', [amplifyAppId])();

const createAppParams = {
name: projectName,
tags: getTagMap(context),
environmentVariables: { _LIVE_PACKAGE_UPDATES: '[{"pkg":"@aws-amplify/cli","type":"npm","version":"latest"}]' },
};
logger('after create params', [amplifyAppId])();
const log = logger('init.amplifyClient.createApp', [createAppParams]);

try {
logger('amplifyAppCreationEnabled', [amplifyAppCreationEnabled()])();
if (amplifyAppCreationEnabled()) {
log();
const createAppResponse = await amplifyClient.createApp(createAppParams).promise();
Expand Down Expand Up @@ -202,6 +209,14 @@ async function init(amplifyServiceParams) {
deploymentBucketName,
};
}
function getTagMap(context) {
const tags = context.amplify.getTags(context);
const tagMap = tags.reduce((obj, tag) => {
obj[tag.Key] = tag.Value;
return obj;
}, {});
return tagMap;
}

async function deleteEnv(context, envName, awsConfig) {
if (stateManager.teamProviderInfoExists()) {
Expand Down Expand Up @@ -291,6 +306,7 @@ async function postPushCheck(context) {
if (!amplifyAppId) {
const createAppParams = {
name: projectConfig.projectName,
tags: getTagMap(context),
environmentVariables: { _LIVE_PACKAGE_UPDATES: '[{"pkg":"@aws-amplify/cli","type":"npm","version":"latest"}]' },
};
const log = logger('postPushCheck.amplifyClient.createApp', [createAppParams]);
Expand Down

0 comments on commit 0530d1a

Please sign in to comment.