Skip to content

Commit

Permalink
fix(amplify-category-api): support delete all tags
Browse files Browse the repository at this point in the history
  • Loading branch information
sundersc committed Jul 16, 2024
1 parent f934d8b commit dbb82cb
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as path from 'path';
import { createNewProjectDir, deleteProjectDir } from 'amplify-category-api-e2e-core';
import { createNewProjectDir, deleteProjectDir, getDDBTable, getDDBTableTags } from 'amplify-category-api-e2e-core';
import { cdkDestroy, initCDKProject, cdkDeploy, updateCDKAppWithTemplate } from '../commands';
import { DURATION_1_HOUR } from '../utils/duration-constants';

Expand Down Expand Up @@ -29,14 +29,34 @@ describe('CDK amplify table 4', () => {
await initCDKProject(projRoot, templatePath);
await expect(cdkDeploy(projRoot, '--all')).resolves.not.toThrow();
});

test('should not throw limit exceed error when creating a large number of tables with datastore disabled at first and enabled in second deployment', async () => {
const templatePath = path.resolve(path.join(__dirname, 'backends', 'amplify-table', 'rate-limit', 'updateTableTtl', 'disabled'));
await initCDKProject(projRoot, templatePath);
await expect(cdkDeploy(projRoot, '--all')).resolves.not.toThrow();
const name = await initCDKProject(projRoot, templatePath);

const outputs = await cdkDeploy(projRoot, '--all');
const { awsAppsyncApiId: apiId, awsAppsyncRegion: region } = outputs[name];
const tableName = `Todo1-${apiId}-NONE`;
const table = await getDDBTable(tableName, region);
expect(table).toBeDefined();

// Verify the tags on the table
const tableTags = await getDDBTableTags(table.Table.TableArn, region);
expect(tableTags.Tags).toBeDefined();
expect(tableTags.Tags.length).toBe(1);
expect(tableTags.Tags).toEqual(expect.arrayContaining([{ Key: 'created-by', Value: 'amplify-original' }]));

// deploy with datastore enabled
let updateTemplatePath;
updateTemplatePath = path.resolve(path.join(__dirname, 'backends', 'amplify-table', 'rate-limit', 'updateTableTtl', 'enabled'));
const updateTemplatePath = path.resolve(path.join(__dirname, 'backends', 'amplify-table', 'rate-limit', 'updateTableTtl', 'enabled'));
updateCDKAppWithTemplate(projRoot, updateTemplatePath);
await expect(cdkDeploy(projRoot, '--all')).resolves.not.toThrow();

const updatedTable = await getDDBTable(tableName, region);
expect(updatedTable).toBeDefined();

// Verify the tags on the table after update
const updatedTableTags = await getDDBTableTags(updatedTable.Table.TableArn, region);
expect(updatedTableTags.Tags).toBeDefined();
expect(updatedTableTags.Tags.length).toBe(0);
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
import 'source-map-support/register';
import { App, Stack, Duration } from 'aws-cdk-lib';
import { App, Stack, Duration, Tags } from 'aws-cdk-lib';
// @ts-ignore
import { AmplifyGraphqlApi, AmplifyGraphqlDefinition } from '@aws-amplify/graphql-api-construct';

Expand Down Expand Up @@ -37,3 +37,5 @@ new AmplifyGraphqlApi(stack, 'GraphqlApi', {
apiKeyConfig: { expires: Duration.days(7) },
},
});

Tags.of(stack).add('created-by', 'amplify-original');
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
import 'source-map-support/register';
import { App, Stack, Duration } from 'aws-cdk-lib';
import { App, Stack, Duration, Tags } from 'aws-cdk-lib';
// @ts-ignore
import { AmplifyGraphqlApi, AmplifyGraphqlDefinition } from '@aws-amplify/graphql-api-construct';

Expand Down Expand Up @@ -31,3 +31,5 @@ new AmplifyGraphqlApi(stack, 'GraphqlApi', {
apiKeyConfig: { expires: Duration.days(7) },
},
});

Tags.of(stack).add('created-by', 'amplify-original');
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class AmplifyDynamoModelResourceGenerator extends DynamoModelResourceGene
'dynamodb:UpdateContinuousBackups',
'dynamodb:UpdateTimeToLive',
'dynamodb:TagResource',
'dynamodb:UntagResource',
'dynamodb:ListTagsOfResource',
],
resources: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,17 @@ const processOnEvent = async (
Object.values(tableDef.tags ?? []).forEach((tag) => {
newTags.push({ Key: tag.key, Value: tag.value });
});
if (requiresTagsUpdate(currentTableTags, newTags)) {
if (currentTableTags && currentTableTags.length > 0 && (!newTags || newTags.length === 0)) {
await ddbClient.untagResource({
ResourceArn: describeTableResult.Table.TableArn,
TagKeys: currentTableTags.map((tag) => tag.Key!),
});
await retry(
async () => await isTableReady(event.PhysicalResourceId!),
(res) => res === true,
);
console.log(`Table '${event.PhysicalResourceId}' is ready after the deletion of Tags.`);
} else if (requiresTagsUpdate(currentTableTags, newTags)) {
console.log('Detected tag changes: ', tableDef.tags);
await ddbClient.tagResource({
ResourceArn: describeTableResult.Table.TableArn,
Expand Down Expand Up @@ -806,7 +816,7 @@ export const getSseUpdate = (currentState: TableDescription, endState: CustomDDB
* @returns Boolean indicating if the tags are updated
*/
export const requiresTagsUpdate = (currentTags: DynamoDBTag[], newTags?: DynamoDBTag[]): boolean => {
if (!newTags) {
if (!newTags || newTags.length === 0) {
return false;
}
if (currentTags.length !== newTags.length) {
Expand Down

0 comments on commit dbb82cb

Please sign in to comment.