Skip to content

Commit

Permalink
Addressed suggestions - imported env coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Hawkins committed Apr 18, 2024
1 parent 4d83c94 commit 54b8341
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-appconfig/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ new appconfig.HostedConfiguration(this, 'MyHostedConfiguration', {

When more than one configuration is set to deploy to the same environment, the
deployments will occur one at a time. This is done to satisfy
[AppConfig's constraint:](https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-deploying.html)
[AppConfig's constraint](https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-deploying.html):
> [!NOTE]
> You can only deploy one configuration at a time to an environment.
> However, you can deploy one configuration each to different environments at the same time.
Expand Down
3 changes: 2 additions & 1 deletion packages/aws-cdk-lib/aws-appconfig/lib/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export interface ConfigurationOptions {
* If this parameter is not specified, then there will be no
* deployment created alongside this configuration.
*
* A deployment can be added later via IEnvironment.
* Deployments can be added later using the `IEnvironment.addDeployment` or
* `IEnvironment.addDeployments` methods.
*
* @default - None.
*/
Expand Down
10 changes: 7 additions & 3 deletions packages/aws-cdk-lib/aws-appconfig/lib/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ abstract class EnvironmentBase extends Resource implements IEnvironment, IExtens
public abstract applicationId: string;
public abstract environmentId: string;
public abstract environmentArn: string;
public abstract name?: string | undefined;
public abstract name?: string;
protected extensible!: ExtensibleBase;
protected deploymentQueue: Array<CfnDeployment> = [];

public addDeployment(configuration: IConfiguration): void {
if (this.name === undefined) {
throw new Error('Environment name must be known to add a Deployment');
}

const queueSize = this.deploymentQueue.push(
new CfnDeployment(configuration, `Deployment${getHash(this.name!)}`, {
new CfnDeployment(configuration, `Deployment${getHash(this.name)}`, {
applicationId: configuration.application.applicationId,
configurationProfileId: configuration.configurationProfileId,
deploymentStrategyId: configuration.deploymentStrategy!.deploymentStrategyId,
Expand Down Expand Up @@ -179,7 +183,7 @@ export class Environment extends EnvironmentBase {
public readonly applicationId = applicationId;
public readonly environmentId = environmentId;
public readonly environmentArn = environmentArn;
public readonly name?: string | undefined;
public readonly name? = undefined;
}

return new Import(scope, id, {
Expand Down
83 changes: 83 additions & 0 deletions packages/aws-cdk-lib/aws-appconfig/test/environment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,20 @@ describe('environment', () => {
expect(env.env.region).toEqual('us-west-2');
});

test('from environment arn; cannot add new deployment', () => {
const stack = new cdk.Stack();
const application = new Application(stack, 'MyAppConfig');
const env = Environment.fromEnvironmentArn(stack, 'MyEnvironment',
'arn:aws:appconfig:us-west-2:123456789012:application/abc123/environment/def456');

expect(() => {
env.addDeployment(new HostedConfiguration(stack, 'FirstConfig', {
application,
content: ConfigurationContent.fromInlineText('This is my content 1'),
}));
}).toThrow('Environment name must be known to add a Deployment');
});

test('from environment arn with no resource name', () => {
const stack = new cdk.Stack();
expect(() => {
Expand Down Expand Up @@ -746,4 +760,73 @@ describe('environment', () => {
expect(env.env.account).toEqual('123456789012');
expect(env.env.region).toEqual('us-west-2');
});

test('from environment attributes; cannot add new deployment without name', () => {
const stack = new cdk.Stack();
const application = new Application(stack, 'MyAppConfig');
const env = Environment.fromEnvironmentAttributes(stack, 'MyEnvironment', {
application,
environmentId: 'def456',
});

expect(() => {
env.addDeployment(new HostedConfiguration(stack, 'FirstConfig', {
application,
content: ConfigurationContent.fromInlineText('This is my content 1'),
}));
}).toThrow('Environment name must be known to add a Deployment');
});

test('from environment attributes with name; can add new deployment', () => {
const stack = new cdk.Stack();
const application = new Application(stack, 'MyAppConfig');
const env = Environment.fromEnvironmentAttributes(stack, 'MyEnvironment', {
application,
environmentId: 'def456',
name: 'NamedEnv',
});
env.addDeployment(new HostedConfiguration(stack, 'FirstConfig', {
application,
content: ConfigurationContent.fromInlineText('This is my content 1'),
}));

const actual = Template.fromStack(stack);

actual.hasResourceProperties('AWS::AppConfig::ConfigurationProfile', {
Name: 'FirstConfig',
ApplicationId: {
Ref: 'MyAppConfigB4B63E75',
},
LocationUri: 'hosted',
});
actual.hasResourceProperties('AWS::AppConfig::HostedConfigurationVersion', {
ApplicationId: {
Ref: 'MyAppConfigB4B63E75',
},
ConfigurationProfileId: {
Ref: 'FirstConfigConfigurationProfileDEF37C63',
},
Content: 'This is my content 1',
ContentType: 'text/plain',
});
actual.hasResource('AWS::AppConfig::Deployment', {
Properties: {
ApplicationId: {
Ref: 'MyAppConfigB4B63E75',
},
EnvironmentId: 'def456',
ConfigurationVersion: {
Ref: 'FirstConfigC35E996C',
},
ConfigurationProfileId: {
Ref: 'FirstConfigConfigurationProfileDEF37C63',
},
DeploymentStrategyId: {
Ref: 'FirstConfigDeploymentStrategy863BBA9A',
},
},
});

actual.resourceCountIs('AWS::AppConfig::Deployment', 1);
});
});

0 comments on commit 54b8341

Please sign in to comment.