-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(servicecatalog): ProductStackHistory can retain old ProductStack…
… iterations (#20244) Adding enhancement to ProductStack to allow the specification of a VersioningStrategy. VersioningStrategy `RetainPreviousVersions` added to save previously deployed ProductStacks templates in a local context directory. These productVersions can then be easily be deployed using the stored templates. --- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
18 changed files
with
499 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
*.d.ts | ||
tsconfig.json | ||
node_modules | ||
product-stack-snapshots | ||
*.generated.ts | ||
dist | ||
.jsii | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
packages/@aws-cdk/aws-servicecatalog/lib/product-stack-history.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { Names } from '@aws-cdk/core'; | ||
import { Construct } from 'constructs'; | ||
import { CloudFormationTemplate } from './cloudformation-template'; | ||
import { DEFAULT_PRODUCT_STACK_SNAPSHOT_DIRECTORY } from './common'; | ||
import { CloudFormationProductVersion } from './product'; | ||
import { ProductStack } from './product-stack'; | ||
|
||
// 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 | ||
import { Construct as CoreConstruct } from '@aws-cdk/core'; | ||
|
||
/** | ||
* Properties for a ProductStackHistory. | ||
*/ | ||
export interface ProductStackHistoryProps { | ||
/** | ||
* The ProductStack whose history will be retained as a snapshot | ||
*/ | ||
readonly productStack: ProductStack; | ||
|
||
/** | ||
* The current version name of the ProductStack. | ||
*/ | ||
readonly currentVersionName: string; | ||
|
||
/** | ||
* If this is set to true, the ProductStack will not be overwritten if a snapshot is found for the currentVersionName. | ||
*/ | ||
readonly currentVersionLocked: boolean | ||
|
||
/** | ||
* The description of the product version | ||
* @default - No description provided | ||
*/ | ||
readonly description?: string; | ||
|
||
/** | ||
* Whether the specified product template will be validated by CloudFormation. | ||
* If turned off, an invalid template configuration can be stored. | ||
* @default true | ||
*/ | ||
readonly validateTemplate?: boolean; | ||
|
||
/** | ||
* The directory where template snapshots will be stored | ||
* @default 'product-stack-snapshots' | ||
*/ | ||
readonly directory?: string | ||
} | ||
|
||
/** | ||
* A Construct that contains a Service Catalog product stack with its previous deployments maintained. | ||
*/ | ||
export class ProductStackHistory extends CoreConstruct { | ||
private readonly props: ProductStackHistoryProps | ||
constructor(scope: Construct, id: string, props: ProductStackHistoryProps) { | ||
super(scope, id); | ||
props.productStack._setParentProductStackHistory(this); | ||
this.props = props; | ||
} | ||
|
||
/** | ||
* Retains product stack template as a snapshot when deployed and | ||
* retrieves a CloudFormationProductVersion for the current product version. | ||
*/ | ||
public currentVersion() : CloudFormationProductVersion { | ||
return { | ||
cloudFormationTemplate: CloudFormationTemplate.fromProductStack(this.props.productStack), | ||
productVersionName: this.props.currentVersionName, | ||
description: this.props.description, | ||
}; | ||
} | ||
|
||
/** | ||
* Retrieves a CloudFormationProductVersion from a previously deployed productVersionName. | ||
*/ | ||
public versionFromSnapshot(productVersionName: string) : CloudFormationProductVersion { | ||
const productStackSnapshotDirectory = this.props.directory || DEFAULT_PRODUCT_STACK_SNAPSHOT_DIRECTORY; | ||
const templateFileKey = `${Names.uniqueId(this)}.${this.props.productStack.artifactId}.${productVersionName}.product.template.json`; | ||
const templateFilePath = path.join(productStackSnapshotDirectory, templateFileKey); | ||
if (!fs.existsSync(templateFilePath)) { | ||
throw new Error(`Template ${templateFileKey} cannot be found in ${productStackSnapshotDirectory}`); | ||
} | ||
return { | ||
cloudFormationTemplate: CloudFormationTemplate.fromAsset(templateFilePath), | ||
productVersionName: productVersionName, | ||
description: this.props.description, | ||
}; | ||
} | ||
|
||
/** | ||
* Writes current template generated from Product Stack to a snapshot directory. | ||
* | ||
* @internal | ||
*/ | ||
public _writeTemplateToSnapshot(cfn: string) { | ||
const productStackSnapshotDirectory = this.props.directory || DEFAULT_PRODUCT_STACK_SNAPSHOT_DIRECTORY; | ||
if (!fs.existsSync(productStackSnapshotDirectory)) { | ||
fs.mkdirSync(productStackSnapshotDirectory); | ||
} | ||
const templateFileKey = `${Names.uniqueId(this)}.${this.props.productStack.artifactId}.${this.props.currentVersionName}.product.template.json`; | ||
const templateFilePath = path.join(productStackSnapshotDirectory, templateFileKey); | ||
if (fs.existsSync(templateFilePath)) { | ||
const previousCfn = fs.readFileSync(templateFilePath).toString(); | ||
if (previousCfn !== cfn && this.props.currentVersionLocked) { | ||
throw new Error(`Template has changed for ProductStack Version ${this.props.currentVersionName}. | ||
${this.props.currentVersionName} already exist in ${productStackSnapshotDirectory}. | ||
Since locked has been set to ${this.props.currentVersionLocked}, | ||
Either update the currentVersionName to deploy a new version or deploy the existing ProductStack snapshot. | ||
If ${this.props.currentVersionName} was unintentionally synthesized and not deployed, | ||
delete the corresponding version from ${productStackSnapshotDirectory} and redeploy.`); | ||
} | ||
} | ||
fs.writeFileSync(templateFilePath, cfn); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/@aws-cdk/aws-servicecatalog/test/portfolio.integ.snapshot/cdk.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"version":"17.0.0"} | ||
{"version":"18.0.0"} |
2 changes: 1 addition & 1 deletion
2
packages/@aws-cdk/aws-servicecatalog/test/portfolio.integ.snapshot/integ.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/@aws-cdk/aws-servicecatalog/test/portfolio.integ.snapshot/manifest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"version": "17.0.0", | ||
"version": "18.0.0", | ||
"artifacts": { | ||
"Tree": { | ||
"type": "cdk:tree", | ||
|
2 changes: 1 addition & 1 deletion
2
packages/@aws-cdk/aws-servicecatalog/test/product.integ.snapshot/cdk.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"version":"17.0.0"} | ||
{"version":"18.0.0"} |
Oops, something went wrong.