-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(aws-codedeploy): Deployment Configuration Construct.
Part of the work on a AWS Construct Library for CodeDeploy.
- Loading branch information
Showing
9 changed files
with
296 additions
and
10 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
150 changes: 150 additions & 0 deletions
150
packages/@aws-cdk/aws-codedeploy/lib/deployment-config.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,150 @@ | ||
import cdk = require('@aws-cdk/cdk'); | ||
import { cloudformation } from './codedeploy.generated'; | ||
|
||
export class DeploymentConfigName extends cdk.CloudFormationToken {} | ||
|
||
export class DeploymentConfigArn extends cdk.Arn {} | ||
|
||
export interface IServerDeploymentConfig { | ||
readonly deploymentConfigName: DeploymentConfigName; | ||
readonly deploymentConfigArn: DeploymentConfigArn; | ||
} | ||
|
||
export interface ServerDeploymentConfigRefProps { | ||
/** | ||
* The physical, human-readable name of the custom CodeDeploy EC2/on-premise Deployment Configuration | ||
* that we are referencing. | ||
*/ | ||
deploymentConfigName: DeploymentConfigName; | ||
} | ||
|
||
/** | ||
* Reference to a custom Deployment Configuration for an EC2/on-premise Deployment Group. | ||
*/ | ||
export abstract class ServerDeploymentConfigRef extends cdk.Construct implements IServerDeploymentConfig { | ||
/** | ||
* Import a custom Deployment Configuration for an EC2/on-premise Deployment Group defined either outside the CDK, | ||
* or in a different CDK Stack and exported using the {@link #export} method. | ||
* | ||
* @param parent the parent Construct for this new Construct | ||
* @param id the logical ID of this new Construct | ||
* @param props the properties of the referenced custom Deployment Configuration | ||
* @returns a Construct representing a reference to an existing custom Deployment Configuration | ||
*/ | ||
public static import(parent: cdk.Construct, id: string, props: ServerDeploymentConfigRefProps): | ||
ServerDeploymentConfigRef { | ||
return new ImportedServerDeploymentConfigRef(parent, id, props); | ||
} | ||
|
||
public abstract readonly deploymentConfigName: DeploymentConfigName; | ||
public abstract readonly deploymentConfigArn: DeploymentConfigArn; | ||
|
||
public export(): ServerDeploymentConfigRefProps { | ||
return { | ||
deploymentConfigName: new cdk.Output(this, 'DeploymentConfigName', { | ||
value: this.deploymentConfigName, | ||
}).makeImportValue(), | ||
}; | ||
} | ||
} | ||
|
||
class ImportedServerDeploymentConfigRef extends ServerDeploymentConfigRef { | ||
public readonly deploymentConfigName: DeploymentConfigName; | ||
public readonly deploymentConfigArn: DeploymentConfigArn; | ||
|
||
constructor(parent: cdk.Construct, id: string, props: ServerDeploymentConfigRefProps) { | ||
super(parent, id); | ||
|
||
this.deploymentConfigName = props.deploymentConfigName; | ||
this.deploymentConfigArn = arnForDeploymentConfigName(this.deploymentConfigName); | ||
} | ||
} | ||
|
||
class DefaultServerDeploymentConfig implements IServerDeploymentConfig { | ||
public readonly deploymentConfigName: DeploymentConfigName; | ||
public readonly deploymentConfigArn: DeploymentConfigArn; | ||
|
||
constructor(deploymentConfigName: string) { | ||
this.deploymentConfigName = new DeploymentConfigName(deploymentConfigName); | ||
this.deploymentConfigArn = arnForDeploymentConfigName(this.deploymentConfigName); | ||
} | ||
} | ||
|
||
/** | ||
* Construction properties of {@link ServerDeploymentConfig}. | ||
*/ | ||
export interface ServerDeploymentConfigProps { | ||
/** | ||
* The physical, human-readable name of the Deployment Configuration. | ||
* | ||
* @default a name will be auto-generated | ||
*/ | ||
deploymentConfigName?: string; | ||
|
||
/** | ||
* The minimum healhty hosts threshold expressed as an absolute number. | ||
* If you've specified this value, | ||
* you can't specify {@link #minHealthyHostPercentage}, | ||
* however one of this or {@link #minHealthyHostPercentage} is required. | ||
*/ | ||
minHealthyHostCount?: number; | ||
|
||
/** | ||
* The minmum healhty hosts threshold expressed as a percentage of the fleet. | ||
* If you've specified this value, | ||
* you can't specify {@link #minHealthyHostCount}, | ||
* however one of this or {@link #minHealthyHostCount} is required. | ||
*/ | ||
minHealthyHostPercentage?: number; | ||
} | ||
|
||
/** | ||
* A custom Deployment Configuration for an EC2/on-premise Deployment Group. | ||
*/ | ||
export class ServerDeploymentConfig extends ServerDeploymentConfigRef { | ||
public static readonly OneAtATime: IServerDeploymentConfig = new DefaultServerDeploymentConfig('CodeDeployDefault.OneAtATime'); | ||
public static readonly HalfAtATime: IServerDeploymentConfig = new DefaultServerDeploymentConfig('CodeDeployDefault.HalfAtATime'); | ||
public static readonly AllAtOnce: IServerDeploymentConfig = new DefaultServerDeploymentConfig('CodeDeployDefault.AllAtOnce'); | ||
|
||
public readonly deploymentConfigName: DeploymentConfigName; | ||
public readonly deploymentConfigArn: DeploymentConfigArn; | ||
|
||
constructor(parent: cdk.Construct, id: string, props: ServerDeploymentConfigProps) { | ||
super(parent, id); | ||
|
||
const resource = new cloudformation.DeploymentConfigResource(this, 'Resource', { | ||
deploymentConfigName: props.deploymentConfigName, | ||
minimumHealthyHosts: this.minimumHealthyHosts(props), | ||
}); | ||
|
||
this.deploymentConfigName = resource.ref; | ||
this.deploymentConfigArn = arnForDeploymentConfigName(this.deploymentConfigName); | ||
} | ||
|
||
private minimumHealthyHosts(props: ServerDeploymentConfigProps): | ||
cloudformation.DeploymentConfigResource.MinimumHealthyHostsProperty { | ||
if (props.minHealthyHostCount === undefined && props.minHealthyHostPercentage === undefined) { | ||
throw new Error('At least one of minHealthyHostCount or minHealthyHostPercentage must be specified when creating ' + | ||
'a custom Server DeploymentConfig'); | ||
} | ||
if (props.minHealthyHostCount !== undefined && props.minHealthyHostPercentage !== undefined) { | ||
throw new Error('Both minHealthyHostCount and minHealthyHostPercentage cannot be specified when creating ' + | ||
'a custom Server DeploymentConfig'); | ||
} | ||
|
||
return { | ||
type: props.minHealthyHostCount !== undefined ? 'HOST_COUNT' : 'FLEET_PERCENT', | ||
value: props.minHealthyHostCount !== undefined ? props.minHealthyHostCount : props.minHealthyHostPercentage!, | ||
}; | ||
} | ||
} | ||
|
||
function arnForDeploymentConfigName(name: DeploymentConfigName): | ||
DeploymentConfigArn { | ||
return cdk.Arn.fromComponents({ | ||
service: 'codedeploy', | ||
resource: 'deploymentconfig', | ||
resourceName: name, | ||
sep: ':', | ||
}); | ||
} |
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 was deleted.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
packages/@aws-cdk/aws-codedeploy/test/test.deployment-config.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,68 @@ | ||
import { expect, haveResource } from '@aws-cdk/assert'; | ||
import cdk = require('@aws-cdk/cdk'); | ||
import { Test } from 'nodeunit'; | ||
import codedeploy = require('../lib'); | ||
|
||
// tslint:disable:object-literal-key-quotes | ||
|
||
export = { | ||
'CodeDeploy DeploymentConfig': { | ||
"cannot be created without specifying minHealthyHostCount or minHealthyHostPercentage"(test: Test) { | ||
const stack = new cdk.Stack(); | ||
|
||
test.throws(() => { | ||
new codedeploy.ServerDeploymentConfig(stack, 'DeploymentConfig', { | ||
}); | ||
}, /minHealthyHost/i); | ||
|
||
test.done(); | ||
}, | ||
|
||
"cannot be created specifying both minHealthyHostCount and minHealthyHostPercentage"(test: Test) { | ||
const stack = new cdk.Stack(); | ||
|
||
test.throws(() => { | ||
new codedeploy.ServerDeploymentConfig(stack, 'DeploymentConfig', { | ||
minHealthyHostCount: 1, | ||
minHealthyHostPercentage: 1, | ||
}); | ||
}, /minHealthyHost/i); | ||
|
||
test.done(); | ||
}, | ||
|
||
"can be created by specifying only minHealthyHostCount"(test: Test) { | ||
const stack = new cdk.Stack(); | ||
|
||
new codedeploy.ServerDeploymentConfig(stack, 'DeploymentConfig', { | ||
minHealthyHostCount: 1, | ||
}); | ||
|
||
expect(stack).to(haveResource('AWS::CodeDeploy::DeploymentConfig', { | ||
"MinimumHealthyHosts": { | ||
"Type": "HOST_COUNT", | ||
"Value": 1, | ||
}, | ||
})); | ||
|
||
test.done(); | ||
}, | ||
|
||
"can be created by specifying only minHealthyHostPercentage"(test: Test) { | ||
const stack = new cdk.Stack(); | ||
|
||
new codedeploy.ServerDeploymentConfig(stack, 'DeploymentConfig', { | ||
minHealthyHostPercentage: 75, | ||
}); | ||
|
||
expect(stack).to(haveResource('AWS::CodeDeploy::DeploymentConfig', { | ||
"MinimumHealthyHosts": { | ||
"Type": "FLEET_PERCENT", | ||
"Value": 75, | ||
}, | ||
})); | ||
|
||
test.done(); | ||
}, | ||
}, | ||
}; |
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
Oops, something went wrong.