From ffe7e425e605144a465cea9befa68d4fe19f9d8c Mon Sep 17 00:00:00 2001 From: Niranjan Jayakar Date: Thu, 28 Jan 2021 18:50:19 +0000 Subject: [PATCH] fix(apigateway): stack update fails to replace api key (#12745) This reverts commit 96cbe32d2399d82a2ad6c3bf6dc1fd65396882d4. The above commit changed the logical id layout of API keys. It turns out that ApiKey resource types cannot be replaced without explicitly specifying, and changing, the API key name. See https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html#cfn-apigateway-apikey-name fixes #12698 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../@aws-cdk/aws-apigateway/lib/usage-plan.ts | 4 ++- .../test/integ.restapi.expected.json | 2 +- .../integ.usage-plan.multikey.expected.json | 2 +- .../aws-apigateway/test/usage-plan.test.ts | 28 ------------------- 4 files changed, 5 insertions(+), 31 deletions(-) diff --git a/packages/@aws-cdk/aws-apigateway/lib/usage-plan.ts b/packages/@aws-cdk/aws-apigateway/lib/usage-plan.ts index 6a1c5a5091bda..ad807d4a7d2d0 100644 --- a/packages/@aws-cdk/aws-apigateway/lib/usage-plan.ts +++ b/packages/@aws-cdk/aws-apigateway/lib/usage-plan.ts @@ -179,8 +179,10 @@ export class UsagePlan extends Resource { * @param apiKey */ public addApiKey(apiKey: IApiKey): void { + const prefix = 'UsagePlanKeyResource'; + // Postfixing apikey id only from the 2nd child, to keep physicalIds of UsagePlanKey for existing CDK apps unmodifed. - const id = `UsagePlanKeyResource:${Names.nodeUniqueId(apiKey.node)}`; + const id = this.node.tryFindChild(prefix) ? `${prefix}:${Names.nodeUniqueId(apiKey.node)}` : prefix; new CfnUsagePlanKey(this, id, { keyId: apiKey.keyId, diff --git a/packages/@aws-cdk/aws-apigateway/test/integ.restapi.expected.json b/packages/@aws-cdk/aws-apigateway/test/integ.restapi.expected.json index a0fb6357db3c7..91af3471593eb 100644 --- a/packages/@aws-cdk/aws-apigateway/test/integ.restapi.expected.json +++ b/packages/@aws-cdk/aws-apigateway/test/integ.restapi.expected.json @@ -602,7 +602,7 @@ "UsagePlanName": "Basic" } }, - "myapiUsagePlanUsagePlanKeyResourcetestapigatewayrestapimyapiApiKeyC43601CB600D112D": { + "myapiUsagePlanUsagePlanKeyResource050D133F": { "Type": "AWS::ApiGateway::UsagePlanKey", "Properties": { "KeyId": { diff --git a/packages/@aws-cdk/aws-apigateway/test/integ.usage-plan.multikey.expected.json b/packages/@aws-cdk/aws-apigateway/test/integ.usage-plan.multikey.expected.json index 9dee2e7aa07b0..8e761f40e2a26 100644 --- a/packages/@aws-cdk/aws-apigateway/test/integ.usage-plan.multikey.expected.json +++ b/packages/@aws-cdk/aws-apigateway/test/integ.usage-plan.multikey.expected.json @@ -3,7 +3,7 @@ "myusageplan4B391740": { "Type": "AWS::ApiGateway::UsagePlan" }, - "myusageplanUsagePlanKeyResourcetestapigatewayusageplanmultikeymyapikey1DDABC389A2809A73": { + "myusageplanUsagePlanKeyResource095B4EA9": { "Type": "AWS::ApiGateway::UsagePlanKey", "Properties": { "KeyId": { diff --git a/packages/@aws-cdk/aws-apigateway/test/usage-plan.test.ts b/packages/@aws-cdk/aws-apigateway/test/usage-plan.test.ts index 854c0a65a6562..f183d08796388 100644 --- a/packages/@aws-cdk/aws-apigateway/test/usage-plan.test.ts +++ b/packages/@aws-cdk/aws-apigateway/test/usage-plan.test.ts @@ -205,32 +205,4 @@ describe('usage plan', () => { }, }, ResourcePart.Properties); }); - - test('UsagePlanKeys have unique logical ids', () => { - // GIVEN - const app = new cdk.App(); - const stack = new cdk.Stack(app, 'my-stack'); - const usagePlan = new apigateway.UsagePlan(stack, 'my-usage-plan'); - const apiKey1 = new apigateway.ApiKey(stack, 'my-api-key-1', { - apiKeyName: 'my-api-key-1', - }); - const apiKey2 = new apigateway.ApiKey(stack, 'my-api-key-2', { - apiKeyName: 'my-api-key-2', - }); - - // WHEN - usagePlan.addApiKey(apiKey1); - usagePlan.addApiKey(apiKey2); - - // THEN - const template = app.synth().getStackByName(stack.stackName).template; - const logicalIds = Object.entries(template.Resources) - .filter(([_, v]) => (v as any).Type === 'AWS::ApiGateway::UsagePlanKey') - .map(([k, _]) => k); - - expect(logicalIds).toEqual([ - 'myusageplanUsagePlanKeyResourcemystackmyapikey1EE9AA1B359121274', - 'myusageplanUsagePlanKeyResourcemystackmyapikey2B4E8EB1456DC88E9', - ]); - }); });