Skip to content

Commit

Permalink
fix(aws-servicecatalog): Allow users to create multiple product versi…
Browse files Browse the repository at this point in the history
…on from assets

This small PR should fix an issue brought up that we used a static `Template` as the resource
name for assets which causes a collision if you have multiple versions.  The correct configuration
should be that the asset name is unique for each unique template file uploaded for a product version.

Fixes: [aws#16892](aws#16892)

Testing done
------------------
* `yarn build && yarn test`
  • Loading branch information
Aidan Crank committed Oct 11, 2021
1 parent ad7288f commit b679364
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as s3_assets from '@aws-cdk/aws-s3-assets';
import { hashValues } from './private/util';

// keep this import separate from other imports to reduce chance for merge conflicts with v2-main
// eslint-disable-next-line no-duplicate-imports, import/order
Expand Down Expand Up @@ -72,11 +73,12 @@ class CloudFormationAssetTemplate extends CloudFormationTemplate {
constructor(public readonly path: string, private readonly options: s3_assets.AssetOptions = { }) {
super();
}
/* eslint-disable no-debugger, no-console */

public bind(scope: Construct): CloudFormationTemplateConfig {
// If the same AssetCode is used multiple times, retain only the first instantiation.
if (!this.asset) {
this.asset = new s3_assets.Asset(scope, 'Template', {
this.asset = new s3_assets.Asset(scope, `Template${hashValues(this.path)}`, {
path: this.path,
...this.options,
});
Expand Down
5 changes: 4 additions & 1 deletion packages/@aws-cdk/aws-servicecatalog/test/integ.product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ new servicecatalog.CloudFormationProduct(stack, 'TestProduct', {
'https://awsdocs.s3.amazonaws.com/servicecatalog/development-environment.template'),
},
{
cloudFormationTemplate: servicecatalog.CloudFormationTemplate.fromAsset(path.join(__dirname, 'development-environment.template.json')),
cloudFormationTemplate: servicecatalog.CloudFormationTemplate.fromAsset(path.join(__dirname, 'product1.template.json')),
},
{
cloudFormationTemplate: servicecatalog.CloudFormationTemplate.fromAsset(path.join(__dirname, 'product2.template.json')),
},
],
});
Expand Down
25 changes: 23 additions & 2 deletions packages/@aws-cdk/aws-servicecatalog/test/product.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,28 @@ describe('Product', () => {
owner: 'testOwner',
productVersions: [
{
cloudFormationTemplate: servicecatalog.CloudFormationTemplate.fromAsset(path.join(__dirname, 'development-environment.template.json')),
cloudFormationTemplate: servicecatalog.CloudFormationTemplate.fromAsset(path.join(__dirname, 'product1.template.json')),
},
],
});

const assembly = app.synth();
const synthesized = assembly.stacks[0];
expect(synthesized.assets.length).toEqual(1);
}),

test('multiple products test from Asset', () => {
new servicecatalog.CloudFormationProduct(stack, 'MyProduct', {
productName: 'testProduct',
owner: 'testOwner',
productVersions: [
{
productVersionName: 'v1',
cloudFormationTemplate: servicecatalog.CloudFormationTemplate.fromAsset(path.join(__dirname, 'product1.template.json')),
},
{
productVersionName: 'v2',
cloudFormationTemplate: servicecatalog.CloudFormationTemplate.fromAsset(path.join(__dirname, 'product2.template.json')),
},
],
});
Expand All @@ -95,7 +116,7 @@ describe('Product', () => {
},
{
productVersionName: 'v3',
cloudFormationTemplate: servicecatalog.CloudFormationTemplate.fromAsset(path.join(__dirname, 'development-environment.template.json')),
cloudFormationTemplate: servicecatalog.CloudFormationTemplate.fromAsset(path.join(__dirname, 'product1.template.json')),
},
],
});
Expand Down
98 changes: 98 additions & 0 deletions packages/@aws-cdk/aws-servicecatalog/test/product2.template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"AWSTemplateFormatVersion" : "2010-09-09",

"Description" : "AWS Service Catalog sample template. Creates an Amazon EC2 instance running the Amazon Linux AMI. The AMI is chosen based on the region in which the stack is run. This example creates an EC2 security group for the instance to give you SSH access. **WARNING** This template creates an Amazon EC2 instance. You will be billed for the AWS resources used if you create a stack from this template.",

"Parameters" : {
"KeyName": {
"Description" : "Name of an existing EC2 key pair for SSH access to the EC2 instance.",
"Type": "AWS::EC2::KeyPair::KeyName"
},

"InstanceType" : {
"Description" : "EC2 instance type.",
"Type" : "String",
"Default" : "t2.micro",
"AllowedValues" : [ "t2.micro", "t2.small", "t2.medium", "m3.medium", "m3.large", "m3.xlarge", "m3.2xlarge" ]
},

"SSHLocation" : {
"Description" : "The IP address range that can SSH to the EC2 instance.",
"Type": "String",
"MinLength": "9",
"MaxLength": "18",
"Default": "0.0.0.0/0",
"AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
"ConstraintDescription": "Must be a valid IP CIDR range of the form x.x.x.x/x."
}
},

"Metadata" : {
"AWS::CloudFormation::Interface" : {
"ParameterGroups" : [{
"Label" : {"default": "Instance configuration"},
"Parameters" : ["InstanceType"]
},{
"Label" : {"default": "Security configuration"},
"Parameters" : ["KeyName", "SSHLocation"]
}],
"ParameterLabels" : {
"InstanceType": {"default": "Server size:"},
"KeyName": {"default": "Key pair:"},
"SSHLocation": {"default": "CIDR range:"}
}
}
},

"Mappings" : {
"AWSRegionArch2AMI" : {
"us-east-1" : { "HVM64" : "ami-08842d60" },
"us-west-2" : { "HVM64" : "ami-8786c6b7" },
"us-west-1" : { "HVM64" : "ami-cfa8a18a" },
"eu-west-1" : { "HVM64" : "ami-748e2903" },
"ap-southeast-1" : { "HVM64" : "ami-d6e1c584" },
"ap-northeast-1" : { "HVM64" : "ami-35072834" },
"ap-southeast-2" : { "HVM64" : "ami-fd4724c7" },
"sa-east-1" : { "HVM64" : "ami-956cc688" },
"cn-north-1" : { "HVM64" : "ami-ac57c595" },
"eu-central-1" : { "HVM64" : "ami-b43503a9" }
}

},

"Resources" : {
"EC2Instance" : {
"Type" : "AWS::EC2::Instance",
"Properties" : {
"InstanceType" : { "Ref" : "InstanceType" },
"SecurityGroups" : [ { "Ref" : "InstanceSecurityGroup" } ],
"KeyName" : { "Ref" : "KeyName" },
"ImageId" : { "Fn::FindInMap" : [ "AWSRegionArch2AMI", { "Ref" : "AWS::Region" }, "HVM64" ] }
}
},

"InstanceSecurityGroup" : {
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"GroupDescription" : "Enable SSH access via port 22",
"SecurityGroupIngress" : [ {
"IpProtocol" : "tcp",
"FromPort" : "22",
"ToPort" : "22",
"CidrIp" : { "Ref" : "SSHLocation"}
} ]
}
}
},

"Outputs" : {
"PublicDNSName" : {
"Description" : "Public DNS name of the new EC2 instance",
"Value" : { "Fn::GetAtt" : [ "EC2Instance", "PublicDnsName" ] }
},
"PublicIPAddress" : {
"Description" : "Public IP address of the new EC2 instance",
"Value" : { "Fn::GetAtt" : [ "EC2Instance", "PublicIp" ] }
}
}
}

0 comments on commit b679364

Please sign in to comment.