Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(servicecatalog): Make possible to have assets in different products in the same portfolio #26039

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const portfolio = new servicecatalog.Portfolio(stack, 'TestPortfolio', {
messageLanguage: servicecatalog.MessageLanguage.EN,
});

class TestAssetProductStack extends servicecatalog.ProductStack {
class TestAssetProductOneStack extends servicecatalog.ProductStack {
constructor(scope: any, id: string, props?: ProductStackProps) {
super(scope, id, props);

Expand All @@ -101,7 +101,25 @@ class TestAssetProductStack extends servicecatalog.ProductStack {
}
}

const productStackHistory = new ProductStackHistory(stack, 'ProductStackHistory', {
class TestAssetProductTwoStack extends servicecatalog.ProductStack {
constructor(scope: any, id: string, props?: ProductStackProps) {
super(scope, id, props);

new lambda.Function(this, 'HelloHandler', {
runtime: lambda.Runtime.PYTHON_3_9,
code: lambda.Code.fromAsset('./assets'),
handler: 'index.handler',
});

new lambda.Function(this, 'HelloHandler2', {
runtime: lambda.Runtime.PYTHON_3_9,
code: lambda.Code.fromAsset('./assetsv2'),
handler: 'index.handler',
});
}
}

const productStackHistory = new ProductStackHistory(stack, 'ProductStackOneHistory', {
productStack: new TestProductStack(stack, 'SNSTopicProduct3'),
currentVersionName: 'v1',
currentVersionLocked: false,
Expand Down Expand Up @@ -135,9 +153,16 @@ const product = new servicecatalog.CloudFormationProduct(stack, 'TestProduct', {
cloudFormationTemplate: servicecatalog.CloudFormationTemplate.fromProductStack(new TestProductStack(stack, 'SNSTopicProduct2')),
},
{
productVersionName: 'testAssetProduct',
productVersionName: 'testAssetProductOne',
validateTemplate: false,
cloudFormationTemplate: servicecatalog.CloudFormationTemplate.fromProductStack(new TestAssetProductOneStack(stack, 'S3AssetProductOne', {
assetBucket: testAssetBucket,
})),
},
{
productVersionName: 'testAssetProductTwo',
validateTemplate: false,
cloudFormationTemplate: servicecatalog.CloudFormationTemplate.fromProductStack(new TestAssetProductStack(stack, 'S3AssetProduct', {
cloudFormationTemplate: servicecatalog.CloudFormationTemplate.fromProductStack(new TestAssetProductTwoStack(stack, 'S3AssetProductTwo', {
assetBucket: testAssetBucket,
})),
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Resources": {
"TopicProductD757E287": {
"Type": "AWS::SNS::Topic"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class ProductStackSynthesizer extends cdk.StackSynthesizer {
cdk.Annotations.of(parentStack).addWarning('[WARNING] Bucket Policy Permissions cannot be added to' +
' referenced Bucket. Please make sure your bucket has the correct permissions');
}
this.bucketDeployment = new BucketDeployment(parentStack, 'AssetsBucketDeployment', {
this.bucketDeployment = new BucketDeployment(parentStack, `${cdk.Names.uniqueId(this.boundStack)}-AssetsBucketDeployment`, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the bucket deployment can be shared within the same stack what if we did something like this:

this.bucketDeployment = parentStack.tryFindChild('AssetsBucketDeployment') ?? new BucketDeployment(...)

sources: [Source.asset(assetPath)],
destinationBucket: this.assetBucket,
extract: false,
Expand Down
39 changes: 39 additions & 0 deletions packages/aws-cdk-lib/aws-servicecatalog/test/product-stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,45 @@ describe('ProductStack', () => {
}).toThrow('A bucketName must be provided to use Assets');
});

test('can be deployed with multiple assets bucket', () => {
// GIVEN
const app = new cdk.App(
{ outdir: 'cdk.out' },
);
const mainStack = new cdk.Stack(app, 'MyStack');
const testAssetBucket = new s3.Bucket(mainStack, 'TestAssetBucket', {
bucketName: 'test-asset-bucket',
});
const productStack = new servicecatalog.ProductStack(mainStack, 'MyProductStack', {
assetBucket: testAssetBucket,
});

new lambda.Function(productStack, 'HelloHandler', {
runtime: lambda.Runtime.PYTHON_3_9,
code: lambda.Code.fromAsset(path.join(__dirname, 'assets')),
handler: 'index.handler',
});

const productStack2 = new servicecatalog.ProductStack(mainStack, 'MyProductStack2', {
assetBucket: testAssetBucket,
});

new lambda.Function(productStack2, 'HelloHandler2', {
runtime: lambda.Runtime.PYTHON_3_9,
code: lambda.Code.fromAsset(path.join(__dirname, 'assetsv2')),
handler: 'index.handler',
});

// WHEN
const whenAction = () => {
app.synth();
};

// THEN
expect(whenAction).not.toThrow(/There is already a Construct with name/);
Template.fromStack(mainStack).resourcePropertiesCountIs('Custom::CDKBucketDeployment', {}, 2);
});

test('fails if Asset bucket is not defined in product stack with assets', () => {
// GIVEN
const app = new cdk.App();
Expand Down
Loading