-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Step Function] 3.2 Extract class ConfigLoader and LambdaConfigLoader #150
Merged
+185
−170
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { InputEvent } from "types"; | ||
import log from "loglevel"; | ||
|
||
const DATADOG = "Datadog"; | ||
const PARAMETERS = "Parameters"; | ||
|
||
export abstract class ConfigLoader<TConfig> { | ||
abstract readonly defaultConfiguration: TConfig; | ||
/** | ||
* Returns the default configuration with any values overwritten by environment variables. | ||
*/ | ||
abstract getConfigFromEnvVars(): TConfig; | ||
|
||
/** | ||
* Returns the configuration. | ||
* If DatadogServerless transform params are set, then the priority order is: | ||
* 1. CloudFormation Macro params | ||
* 2. Environment variables | ||
* 3. Default configuration | ||
* Otherwise, if CloudFormation Mappings for Datadog are set, then the priority order is: | ||
* 1. CloudFormation Mappings params | ||
* 2. Environment variables | ||
* 3. Default configuration | ||
* Otherwise, the priority order is: | ||
* 1. Environment variables | ||
* 2. Default configuration | ||
*/ | ||
public getConfig(event: InputEvent): TConfig { | ||
let config: TConfig; | ||
// Use the parameters given for this specific transform/macro if it exists | ||
const transformParams = event.params ?? {}; | ||
if (Object.keys(transformParams).length > 0) { | ||
log.debug("Parsing config from CloudFormation transform/macro parameters"); | ||
config = this.getConfigFromCfnParams(transformParams as Partial<TConfig>); | ||
} else { | ||
// If not, check the Mappings section for Datadog config parameters as well | ||
log.debug("Parsing config from CloudFormation template mappings"); | ||
config = this.getConfigFromCfnMappings(event.fragment.Mappings); | ||
} | ||
return config; | ||
} | ||
|
||
/** | ||
* Parses the Mappings section for Datadog config parameters. | ||
* Assumes that the parameters live under the Mappings section in this format: | ||
* | ||
* Mappings: | ||
* Datadog: | ||
* Parameters: | ||
* addLayers: true | ||
* ... | ||
*/ | ||
public getConfigFromCfnMappings(mappings: any): TConfig { | ||
if (mappings === undefined || mappings[DATADOG] === undefined) { | ||
log.debug("No Datadog mappings found in the CloudFormation template, using the default config"); | ||
return this.getConfigFromEnvVars(); | ||
} | ||
return this.getConfigFromCfnParams(mappings[DATADOG][PARAMETERS]); | ||
} | ||
|
||
/** | ||
* Takes a set of parameters from the CloudFormation template. This could come from either | ||
* the Mappings section of the template, or directly from the Parameters under the transform/macro | ||
* as the 'params' property under the original InputEvent to the handler in src/index.ts | ||
* | ||
* Uses these parameters as the Datadog configuration, and for values that are required in the | ||
* configuration but not provided in the parameters, uses the default values from | ||
* the defaultConfiguration above. | ||
*/ | ||
public getConfigFromCfnParams(datadogConfig: Partial<TConfig> | undefined) { | ||
if (datadogConfig === undefined) { | ||
log.debug("No Datadog config found, using the default config"); | ||
datadogConfig = {}; | ||
} | ||
return { | ||
...this.getConfigFromEnvVars(), | ||
...datadogConfig, | ||
}; | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⚪ Code Quality Violation
Unexpected any. Specify a different type. (...read more)
Do not use the
any
type, as it is too broad and can lead to unexpected behavior.