Skip to content

Commit

Permalink
refactor(appconfig): refactoring deployTo and contentType properties (#…
Browse files Browse the repository at this point in the history
…26768)

Refactoring deployTo property to only create a deployment if specified.

Refactoring contentType to be passed in by ConfigurationContent.

**Includes breaking change. Deployments will not be created anymore if deployTo is not specified and contentType cannot be passed in as a HostedConfiguration prop, only can be passed directly to ConfigurationContent

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
chenjane-dev authored Aug 18, 2023
1 parent 89393a2 commit 58e87fb
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 158 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
"@aws-cdk/assertions-alpha/fs-extra/**",
"@aws-cdk/assertions/fs-extra",
"@aws-cdk/assertions/fs-extra/**",
"@aws-cdk/aws-appconfig-alpha/mime-types",
"@aws-cdk/aws-appconfig-alpha/mime-types/**",
"@aws-cdk/aws-codebuild/yaml",
"@aws-cdk/aws-codebuild/yaml/**",
"@aws-cdk/aws-codepipeline-actions/case",
Expand Down
32 changes: 32 additions & 0 deletions packages/@aws-cdk/aws-appconfig-alpha/NOTICE
Original file line number Diff line number Diff line change
@@ -1,2 +1,34 @@
AWS Cloud Development Kit (AWS CDK)
Copyright 2018-2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.

-------------------------------------------------------------------------------

The AWS CDK includes the following third-party software/licensing:

** mime-db - https://www.npmjs.com/package/mime-db
** mime-types - https://www.npmjs.com/package/mime-types
(The MIT License)

Copyright (c) 2014 Jonathan Ong <[email protected]>
Copyright (c) 2015-2022 Douglas Christopher Wilson <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

----------------
14 changes: 7 additions & 7 deletions packages/@aws-cdk/aws-appconfig-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ declare const application: appconfig.Application;

new appconfig.HostedConfiguration(this, 'MyHostedConfiguration', {
application,
content: appconfig.ConfigurationContent.fromInline('This is my configuration content.'),
content: appconfig.ConfigurationContent.fromInlineText('This is my configuration content.'),
});
```

Expand All @@ -95,7 +95,7 @@ declare const application: appconfig.Application;

new appconfig.HostedConfiguration(this, 'MyHostedConfiguration', {
application,
content: appconfig.ConfigurationContent.fromInline('This is my configuration content.'),
content: appconfig.ConfigurationContent.fromInlineText('This is my configuration content.'),
type: appconfig.ConfigurationType.FEATURE_FLAGS,
});
```
Expand All @@ -111,7 +111,7 @@ declare const fn: lambda.Function;

new appconfig.HostedConfiguration(this, 'MyHostedConfiguration', {
application,
content: appconfig.ConfigurationContent.fromInline('This is my configuration content.'),
content: appconfig.ConfigurationContent.fromInlineText('This is my configuration content.'),
validators: [
appconfig.JsonSchemaValidator.fromFile('schema.json'),
appconfig.LambdaValidator.fromFunction(fn),
Expand All @@ -128,7 +128,7 @@ declare const application: appconfig.Application;

new appconfig.HostedConfiguration(this, 'MyHostedConfiguration', {
application,
content: appconfig.ConfigurationContent.fromInline('This is my configuration content.'),
content: appconfig.ConfigurationContent.fromInlineText('This is my configuration content.'),
deploymentStrategy: new appconfig.DeploymentStrategy(this, 'MyDeploymentStrategy', {
rolloutStrategy: appconfig.RolloutStrategy.linear({
growthFactor: 15,
Expand All @@ -139,7 +139,7 @@ new appconfig.HostedConfiguration(this, 'MyHostedConfiguration', {
});
```

The `deployTo` parameter is used to specify which environments to deploy the configuration to. If this parameter is not specified, there will only be a deployment if there is one environment associated to the AWS AppConfig application.
The `deployTo` parameter is used to specify which environments to deploy the configuration to. If this parameter is not specified, there will not be a deployment.

A hosted configuration with `deployTo`:

Expand All @@ -149,7 +149,7 @@ declare const env: appconfig.Environment;

new appconfig.HostedConfiguration(this, 'MyHostedConfiguration', {
application,
content: appconfig.ConfigurationContent.fromInline('This is my configuration content.'),
content: appconfig.ConfigurationContent.fromInlineText('This is my configuration content.'),
deployTo: [env],
});
```
Expand Down Expand Up @@ -301,7 +301,7 @@ new appconfig.SourcedConfiguration(this, 'MySourcedConfiguration', {
});
```

The `deployTo` parameter is used to specify which environments to deploy the configuration to. If this parameter is not specified, there will only be a deployment if there is one environment associated to the AWS AppConfig application.
The `deployTo` parameter is used to specify which environments to deploy the configuration to. If this parameter is not specified, there will not be a deployment.

A sourced configuration with `deployTo`:

Expand Down
67 changes: 41 additions & 26 deletions packages/@aws-cdk/aws-appconfig-alpha/lib/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fs from 'fs';
import * as mimeTypes from 'mime-types';
import { PhysicalName, Stack, ArnFormat, Names, RemovalPolicy } from 'aws-cdk-lib';
import { CfnConfigurationProfile, CfnDeployment, CfnHostedConfigurationVersion } from 'aws-cdk-lib/aws-appconfig';
import * as cp from 'aws-cdk-lib/aws-codepipeline';
Expand Down Expand Up @@ -55,9 +56,8 @@ export interface ConfigurationOptions {
/**
* The list of environments to deploy the configuration to.
*
* If this parameter is not specified and there is only one environment
* associated to the application, then we will deploy to that one. Otherwise,
* there will be no deployment.
* If this parameter is not specified, then there will be no
* deployment.
*
* @default - None.
*/
Expand Down Expand Up @@ -305,13 +305,7 @@ abstract class ConfigurationBase extends Construct implements IConfiguration, IE
}

protected deployConfigToEnvironments() {
if (this.application.environments.length == 0) {
this.application.addEnvironment('Environment', {
description: this.description,
});
}

if ((!this.deployTo && this.application.environments.length > 1) || !this.versionNumber) {
if (!this.deployTo || !this.versionNumber) {
return;
}

Expand All @@ -320,9 +314,6 @@ abstract class ConfigurationBase extends Construct implements IConfiguration, IE
return;
}
const logicalId = `Deployment${this.getDeploymentHash(environment)}`;
if (this.node.tryFindChild(logicalId)) {
return;
}
new CfnDeployment(this, logicalId, {
applicationId: this.application.applicationId,
configurationProfileId: this.configurationProfileId,
Expand All @@ -342,11 +333,6 @@ export interface HostedConfigurationOptions extends ConfigurationOptions {
*/
readonly content: ConfigurationContent;

/**
* The content type of the hosted configuration.
*/
readonly contentType?: string;

/**
* The latest version number of the hosted configuration.
*/
Expand All @@ -364,11 +350,6 @@ export interface HostedConfigurationProps extends ConfigurationProps {
*/
readonly content: ConfigurationContent;

/**
* The content type of the hosted configuration.
*/
readonly contentType?: string;

/**
* The latest version number of the hosted configuration.
*/
Expand Down Expand Up @@ -444,7 +425,7 @@ export class HostedConfiguration extends ConfigurationBase {
this.extensible = new ExtensibleBase(scope, this.configurationProfileArn, this.name);

this.content = props.content.content;
this.contentType = props.contentType || 'application/json';
this.contentType = props.content.contentType;
this.latestVersionNumber = props.latestVersionNumber;
this.versionLabel = props.versionLabel;
this._cfnHostedConfigurationVersion = new CfnHostedConfigurationVersion(this, 'Resource', {
Expand Down Expand Up @@ -792,28 +773,62 @@ export abstract class ConfigurationContent {
* Defines the hosted configuration content from a file.
*
* @param path The path to the file that defines configuration content
* @param contentType The content type of the configuration
*/
public static fromFile(path: string): ConfigurationContent {
public static fromFile(path: string, contentType?: string): ConfigurationContent {
return {
content: fs.readFileSync(path).toString(),
contentType: contentType || mimeTypes.lookup(path) || 'application/json',
};
}

/**
* Defines the hosted configuration content from inline code.
*
* @param content The inline code that defines the configuration content
* @param contentType The content type of the configuration
*/
public static fromInline(content: string): ConfigurationContent {
public static fromInline(content: string, contentType?: string): ConfigurationContent {
return {
content,
contentType: contentType || 'application/octet-stream',
};
}

/**
* Defines the hosted configuration content as JSON from inline code.
*
* @param content The inline code that defines the configuration content
* @param contentType The content type of the configuration
*/
public static fromInlineJson(content: string, contentType?: string): ConfigurationContent {
return {
content,
contentType: contentType || 'application/json',
};
}

/**
* Defines the hosted configuration content as text from inline code.
*
* @param content The inline code that defines the configuration content
*/
public static fromInlineText(content: string): ConfigurationContent {
return {
content,
contentType: 'text/plain',
};
}

/**
* The configuration content.
*/
public abstract readonly content: string;

/**
* The configuration content type.
*/
public abstract readonly contentType: string;
}

/**
Expand Down
15 changes: 10 additions & 5 deletions packages/@aws-cdk/aws-appconfig-alpha/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,20 @@
},
"license": "Apache-2.0",
"devDependencies": {
"aws-cdk-lib": "0.0.0",
"@aws-cdk/cdk-build-tools": "0.0.0",
"@aws-cdk/integ-runner": "0.0.0",
"@aws-cdk/pkglint": "0.0.0",
"@aws-cdk/integ-tests-alpha": "0.0.0",
"@aws-cdk/pkglint": "0.0.0",
"@types/jest": "^29.5.3",
"@types/mime-types": "^2.1.1",
"aws-cdk-lib": "0.0.0",
"constructs": "^10.0.0",
"jest": "^29.6.2"
"jest": "^29.6.1"
},
"dependencies": {
"aws-cdk-lib": "0.0.0",
"constructs": "^10.0.0"
"constructs": "^10.0.0",
"mime-types": "^2.1.35"
},
"homepage": "https://github.com/aws/aws-cdk",
"peerDependencies": {
Expand Down Expand Up @@ -115,5 +117,8 @@
"env": {
"AWSLINT_BASE_CONSTRUCT": true
}
}
},
"bundleDependencies": [
"mime-types"
]
}
Loading

0 comments on commit 58e87fb

Please sign in to comment.