|
| 1 | +import cdk = require('@aws-cdk/cdk'); |
| 2 | +import { cloudformation } from './codedeploy.generated'; |
| 3 | + |
| 4 | +/** |
| 5 | + * The Deployment Configuration of an EC2/on-premise Deployment Group. |
| 6 | + * The default, pre-defined Configurations are available as constants on the {@link ServerDeploymentConfig} class |
| 7 | + * (`ServerDeploymentConfig.HalfAtATime`, `ServerDeploymentConfig.AllAtOnce`, etc.). |
| 8 | + * To create a custom Deployment Configuration, |
| 9 | + * instantiate the {@link ServerDeploymentConfig} Construct. |
| 10 | + */ |
| 11 | +export interface IServerDeploymentConfig { |
| 12 | + readonly deploymentConfigName: string; |
| 13 | + readonly deploymentConfigArn: string; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Properties of a reference to a CodeDeploy EC2/on-premise Deployment Configuration. |
| 18 | + * |
| 19 | + * @see ServerDeploymentConfigRef#import |
| 20 | + * @see ServerDeploymentConfigRef#export |
| 21 | + */ |
| 22 | +export interface ServerDeploymentConfigRefProps { |
| 23 | + /** |
| 24 | + * The physical, human-readable name of the custom CodeDeploy EC2/on-premise Deployment Configuration |
| 25 | + * that we are referencing. |
| 26 | + */ |
| 27 | + deploymentConfigName: string; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Reference to a custom Deployment Configuration for an EC2/on-premise Deployment Group. |
| 32 | + */ |
| 33 | +export abstract class ServerDeploymentConfigRef extends cdk.Construct implements IServerDeploymentConfig { |
| 34 | + /** |
| 35 | + * Import a custom Deployment Configuration for an EC2/on-premise Deployment Group defined either outside the CDK, |
| 36 | + * or in a different CDK Stack and exported using the {@link #export} method. |
| 37 | + * |
| 38 | + * @param parent the parent Construct for this new Construct |
| 39 | + * @param id the logical ID of this new Construct |
| 40 | + * @param props the properties of the referenced custom Deployment Configuration |
| 41 | + * @returns a Construct representing a reference to an existing custom Deployment Configuration |
| 42 | + */ |
| 43 | + public static import(parent: cdk.Construct, id: string, props: ServerDeploymentConfigRefProps): |
| 44 | + ServerDeploymentConfigRef { |
| 45 | + return new ImportedServerDeploymentConfigRef(parent, id, props); |
| 46 | + } |
| 47 | + |
| 48 | + public abstract readonly deploymentConfigName: string; |
| 49 | + public abstract readonly deploymentConfigArn: string; |
| 50 | + |
| 51 | + public export(): ServerDeploymentConfigRefProps { |
| 52 | + return { |
| 53 | + deploymentConfigName: new cdk.Output(this, 'DeploymentConfigName', { |
| 54 | + value: this.deploymentConfigName, |
| 55 | + }).makeImportValue().toString(), |
| 56 | + }; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +class ImportedServerDeploymentConfigRef extends ServerDeploymentConfigRef { |
| 61 | + public readonly deploymentConfigName: string; |
| 62 | + public readonly deploymentConfigArn: string; |
| 63 | + |
| 64 | + constructor(parent: cdk.Construct, id: string, props: ServerDeploymentConfigRefProps) { |
| 65 | + super(parent, id); |
| 66 | + |
| 67 | + this.deploymentConfigName = props.deploymentConfigName; |
| 68 | + this.deploymentConfigArn = arnForDeploymentConfigName(this.deploymentConfigName); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +class DefaultServerDeploymentConfig implements IServerDeploymentConfig { |
| 73 | + public readonly deploymentConfigName: string; |
| 74 | + public readonly deploymentConfigArn: string; |
| 75 | + |
| 76 | + constructor(deploymentConfigName: string) { |
| 77 | + this.deploymentConfigName = deploymentConfigName; |
| 78 | + this.deploymentConfigArn = arnForDeploymentConfigName(this.deploymentConfigName); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Construction properties of {@link ServerDeploymentConfig}. |
| 84 | + */ |
| 85 | +export interface ServerDeploymentConfigProps { |
| 86 | + /** |
| 87 | + * The physical, human-readable name of the Deployment Configuration. |
| 88 | + * |
| 89 | + * @default a name will be auto-generated |
| 90 | + */ |
| 91 | + deploymentConfigName?: string; |
| 92 | + |
| 93 | + /** |
| 94 | + * The minimum healhty hosts threshold expressed as an absolute number. |
| 95 | + * If you've specified this value, |
| 96 | + * you can't specify {@link #minHealthyHostPercentage}, |
| 97 | + * however one of this or {@link #minHealthyHostPercentage} is required. |
| 98 | + */ |
| 99 | + minHealthyHostCount?: number; |
| 100 | + |
| 101 | + /** |
| 102 | + * The minmum healhty hosts threshold expressed as a percentage of the fleet. |
| 103 | + * If you've specified this value, |
| 104 | + * you can't specify {@link #minHealthyHostCount}, |
| 105 | + * however one of this or {@link #minHealthyHostCount} is required. |
| 106 | + */ |
| 107 | + minHealthyHostPercentage?: number; |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * A custom Deployment Configuration for an EC2/on-premise Deployment Group. |
| 112 | + */ |
| 113 | +export class ServerDeploymentConfig extends ServerDeploymentConfigRef { |
| 114 | + public static readonly OneAtATime: IServerDeploymentConfig = new DefaultServerDeploymentConfig('CodeDeployDefault.OneAtATime'); |
| 115 | + public static readonly HalfAtATime: IServerDeploymentConfig = new DefaultServerDeploymentConfig('CodeDeployDefault.HalfAtATime'); |
| 116 | + public static readonly AllAtOnce: IServerDeploymentConfig = new DefaultServerDeploymentConfig('CodeDeployDefault.AllAtOnce'); |
| 117 | + |
| 118 | + public readonly deploymentConfigName: string; |
| 119 | + public readonly deploymentConfigArn: string; |
| 120 | + |
| 121 | + constructor(parent: cdk.Construct, id: string, props: ServerDeploymentConfigProps) { |
| 122 | + super(parent, id); |
| 123 | + |
| 124 | + const resource = new cloudformation.DeploymentConfigResource(this, 'Resource', { |
| 125 | + deploymentConfigName: props.deploymentConfigName, |
| 126 | + minimumHealthyHosts: this.minimumHealthyHosts(props), |
| 127 | + }); |
| 128 | + |
| 129 | + this.deploymentConfigName = resource.ref.toString(); |
| 130 | + this.deploymentConfigArn = arnForDeploymentConfigName(this.deploymentConfigName); |
| 131 | + } |
| 132 | + |
| 133 | + private minimumHealthyHosts(props: ServerDeploymentConfigProps): |
| 134 | + cloudformation.DeploymentConfigResource.MinimumHealthyHostsProperty { |
| 135 | + if (props.minHealthyHostCount === undefined && props.minHealthyHostPercentage === undefined) { |
| 136 | + throw new Error('At least one of minHealthyHostCount or minHealthyHostPercentage must be specified when creating ' + |
| 137 | + 'a custom Server DeploymentConfig'); |
| 138 | + } |
| 139 | + if (props.minHealthyHostCount !== undefined && props.minHealthyHostPercentage !== undefined) { |
| 140 | + throw new Error('Both minHealthyHostCount and minHealthyHostPercentage cannot be specified when creating ' + |
| 141 | + 'a custom Server DeploymentConfig'); |
| 142 | + } |
| 143 | + |
| 144 | + return { |
| 145 | + type: props.minHealthyHostCount !== undefined ? 'HOST_COUNT' : 'FLEET_PERCENT', |
| 146 | + value: props.minHealthyHostCount !== undefined ? props.minHealthyHostCount : props.minHealthyHostPercentage!, |
| 147 | + }; |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +function arnForDeploymentConfigName(name: string): string { |
| 152 | + return cdk.ArnUtils.fromComponents({ |
| 153 | + service: 'codedeploy', |
| 154 | + resource: 'deploymentconfig', |
| 155 | + resourceName: name, |
| 156 | + sep: ':', |
| 157 | + }); |
| 158 | +} |
0 commit comments