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

Bugfix for issue #24561 #32247

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions packages/aws-cdk-lib/aws-servicecatalog/lib/product-stack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as crypto from 'crypto';
import * as fs from 'fs';
import * as path from 'path';
import { Construct } from 'constructs';
Expand All @@ -7,6 +6,7 @@ import { ProductStackHistory } from './product-stack-history';
import { IBucket } from '../../aws-s3';
import { ServerSideEncryption } from '../../aws-s3-deployment';
import * as cdk from '../../core';
import { fingerprint } from '../../core/lib/fs/fingerprint';

/**
* Product stack props.
Expand Down Expand Up @@ -144,7 +144,9 @@ export class ProductStack extends cdk.Stack {
*/
public _synthesizeTemplate(session: cdk.ISynthesisSession): void {
const cfn = JSON.stringify(this._toCloudFormation(), undefined, 2);
const templateHash = crypto.createHash('sha256').update(cfn).digest('hex');
const templateFilePath = path.join(session.assembly.outdir, this.templateFile);
fs.writeFileSync(templateFilePath, cfn);
const templateHash = fingerprint(templateFilePath);

this._templateUrl = this._parentStack.synthesizer.addFileAsset({
packaging: cdk.FileAssetPackaging.FILE,
Expand All @@ -155,8 +157,6 @@ export class ProductStack extends cdk.Stack {
if (this._parentProductStackHistory) {
this._parentProductStackHistory._writeTemplateToSnapshot(cfn);
}

fs.writeFileSync(path.join(session.assembly.outdir, this.templateFile), cfn);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { Construct } from 'constructs';
import { Match, Template } from '../../assertions';
Expand Down Expand Up @@ -500,4 +501,47 @@ class ProductWithAnAsset extends servicecatalog.ProductStack {
handler: 'index.handler',
});
}
}
}

test('Adding new version to product stack history', () => {
const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'cdk-product-stack-snapshots'));
const createProduct = (currentVersion: string, previousVersion?: string) => {
const app = new cdk.App();
const mainStack = new cdk.Stack(app, 'MainStack');
const productStack = new servicecatalog.ProductStack(mainStack, 'ProductStack');
new s3.Bucket(productStack, `Bucket${currentVersion}`);
const productStackHistory = new servicecatalog.ProductStackHistory(mainStack, 'ProductStackHistory', {
currentVersionLocked: true,
currentVersionName: currentVersion,
productStack,
directory: tmpdir,
});
const productVersions = new Array<servicecatalog.CloudFormationProductVersion>();
if (previousVersion) {
productVersions.push(productStackHistory.versionFromSnapshot(previousVersion));
}
productVersions.push(productStackHistory.currentVersion());
const product = new servicecatalog.CloudFormationProduct(mainStack, 'Product', {
productName: 'Product',
owner: 'Owner',
productVersions,
});
app.synth();
const template = Template.fromStack(mainStack);
const productsInTemplate = template.findResources('AWS::ServiceCatalog::CloudFormationProduct');
expect(Object.values(productsInTemplate).length).toEqual(1);
return Object.values(productsInTemplate)[0]
.Properties
.ProvisioningArtifactParameters[0]
.Info
.LoadTemplateFromURL['Fn::Sub']
.replace(/^.*\//, '');
};
try {
const assetHash1 = createProduct('V1');
const assetHash2 = createProduct('V2', 'V1');
expect(assetHash1).toEqual(assetHash2);
} finally {
fs.rmSync(tmpdir, { recursive: true });
}
});
Loading