This repository has been archived by the owner on Jan 14, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
431 additions
and
144 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package cloudformation | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/KablamoOSS/kombustion/internal/manifest" | ||
"github.com/aws/aws-sdk-go/aws" | ||
awsCF "github.com/aws/aws-sdk-go/service/cloudformation" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
// GetParamMap retrives the --param if any, for the map of | ||
// Parameters in the template | ||
func GetParamMap(c *cli.Context) map[string]string { | ||
paramMap := make(map[string]string) | ||
params := c.StringSlice("param") | ||
for _, param := range params { | ||
parts := strings.Split(param, "=") | ||
if len(parts) > 1 { | ||
paramMap[parts[0]] = strings.Join(parts[1:], "=") | ||
} | ||
} | ||
return paramMap | ||
} | ||
|
||
// ResolveParameters for the template | ||
func ResolveParameters( | ||
c *cli.Context, | ||
cfYaml YamlCloudformation, | ||
manifestFile *manifest.Manifest, | ||
) []*awsCF.Parameter { | ||
results := []*awsCF.Parameter{} | ||
|
||
env := resolveEnvironmentParameters(manifestFile, c.String("environment")) | ||
|
||
// override envFile values with optional --param values | ||
params := GetParamMap(c) | ||
for key, value := range params { | ||
env[key] = value | ||
} | ||
|
||
// convert to aws Parameter list | ||
for paramKey := range cfYaml.Parameters { | ||
for key, value := range env { | ||
if paramKey == key { | ||
// Filter to params in the stack | ||
results = append(results, &awsCF.Parameter{ | ||
ParameterKey: aws.String(key), | ||
ParameterValue: aws.String(value), | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return results | ||
} | ||
|
||
// ResolveParametersS3 for an S3 based template | ||
func ResolveParametersS3( | ||
c *cli.Context, | ||
manifestFile *manifest.Manifest, | ||
) []*awsCF.Parameter { | ||
|
||
results := []*awsCF.Parameter{} | ||
|
||
params := make(map[string]string) | ||
|
||
env := resolveEnvironmentParameters(manifestFile, c.String("environment")) | ||
for key, value := range params { | ||
env[key] = value | ||
} | ||
|
||
// convert to aws Parameter list | ||
for key, value := range params { | ||
// Filter to params in the stack | ||
results = append(results, &awsCF.Parameter{ | ||
ParameterKey: aws.String(key), | ||
ParameterValue: aws.String(value), | ||
}) | ||
} | ||
|
||
return results | ||
} | ||
|
||
func resolveEnvironmentParameters(manifestFile *manifest.Manifest, environment string) (parameters map[string]string) { | ||
if manifestFile.Environments[environment].Parameters != nil { | ||
envParams := manifestFile.Environments[environment].Parameters | ||
if envParams != nil { | ||
parameters = envParams | ||
} | ||
} | ||
return | ||
} |
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,190 @@ | ||
package cloudformation | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/KablamoOSS/kombustion/internal/manifest" | ||
"github.com/KablamoOSS/kombustion/types" | ||
awsCF "github.com/aws/aws-sdk-go/service/cloudformation" | ||
"github.com/aws/aws-sdk-go/service/cloudformation/cloudformationiface" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
type mockCloudFormationClient struct { | ||
cloudformationiface.CloudFormationAPI | ||
} | ||
|
||
func TestResolveEnvironmentParameters(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
environment string | ||
manifest manifest.Manifest | ||
output map[string]string | ||
}{ | ||
{ | ||
name: "Returns map of env vars", | ||
environment: "development", | ||
manifest: manifest.Manifest{ | ||
Name: "TestManifestWithEnvironment", | ||
Plugins: nil, | ||
Architectures: []string(nil), | ||
HideDefaultExports: false, | ||
Environments: map[string]manifest.Environment{ | ||
"development": { | ||
AccountIDs: nil, | ||
Parameters: map[string]string{ | ||
"parameterOneName": "parameterOneValue", | ||
"parameterTwoName": "8654238642489624862", | ||
"parameterThreeName": "3so87tg4y98n7y34ts3t4sh st34y79p4y3t7 8s", | ||
"parameterFourName": "hhh:://asdfasdf.sadfasdf:3452345@f][a;v-][0[-", | ||
}, | ||
}, | ||
}, | ||
}, | ||
output: map[string]string{ | ||
"parameterOneName": "parameterOneValue", | ||
"parameterTwoName": "8654238642489624862", | ||
"parameterThreeName": "3so87tg4y98n7y34ts3t4sh st34y79p4y3t7 8s", | ||
"parameterFourName": "hhh:://asdfasdf.sadfasdf:3452345@f][a;v-][0[-", | ||
}, | ||
}, | ||
{ | ||
name: "Returns emtpy map", | ||
environment: "production", | ||
manifest: manifest.Manifest{ | ||
Name: "TestManifestWithEnvironment", | ||
Plugins: nil, | ||
Architectures: []string(nil), | ||
HideDefaultExports: false, | ||
Environments: map[string]manifest.Environment{ | ||
"development": { | ||
AccountIDs: nil, | ||
Parameters: map[string]string{ | ||
"parameterOneName": "parameterOneValue", | ||
"parameterTwoName": "8654238642489624862", | ||
"parameterThreeName": "3so87tg4y98n7y34ts3t4sh st34y79p4y3t7 8s", | ||
"parameterFourName": "hhh:://asdfasdf.sadfasdf:3452345@f][a;v-][0[-", | ||
}, | ||
}, | ||
}, | ||
}, | ||
output: nil, | ||
}, | ||
} | ||
|
||
for i, test := range tests { | ||
assert := assert.New(t) | ||
testOutput := resolveEnvironmentParameters(&test.manifest, test.environment) | ||
assert.Equal(testOutput, test.output, fmt.Sprintf("Test %d: %s", i, test.name)) | ||
} | ||
} | ||
|
||
func TestResolveParameters(t *testing.T) { | ||
type input struct { | ||
ctx *cli.Context | ||
cfYaml YamlCloudformation | ||
cfClient *awsCF.CloudFormation | ||
manifestFile *manifest.Manifest | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
input input | ||
output []*awsCF.Parameter | ||
}{ | ||
{ | ||
name: "Dev", | ||
input: input{ | ||
ctx: func() *cli.Context { | ||
set := flag.NewFlagSet("test", 0) | ||
set.String("environment", "development", "development") | ||
context := cli.NewContext(nil, set, nil) | ||
return context | ||
}(), | ||
cfYaml: YamlCloudformation{ | ||
AWSTemplateFormatVersion: "version", | ||
Description: "Test Template", | ||
Parameters: types.TemplateObject{ | ||
"parameterOneName": "parameterOneValue", | ||
"parameterTwoName": "8654238642489624862", | ||
"parameterThreeName": "3so87tg4y98n7y34ts3t4sh st34y79p4y3t7 8s", | ||
"parameterFourName": "hhh:://asdfasdf.sadfasdf:3452345@f][a;v-][0[-", | ||
}, | ||
Mappings: types.TemplateObject{}, | ||
Conditions: types.TemplateObject{}, | ||
Transform: types.TemplateObject{}, | ||
Resources: types.TemplateObject{}, | ||
Outputs: types.TemplateObject{}, | ||
}, | ||
manifestFile: &manifest.Manifest{ | ||
Name: "TestManifestWithEnvironment", | ||
Plugins: nil, | ||
Architectures: []string(nil), | ||
HideDefaultExports: false, | ||
Environments: map[string]manifest.Environment{ | ||
"development": { | ||
AccountIDs: nil, | ||
Parameters: map[string]string{ | ||
"parameterOneName": "parameterOneValue", | ||
"parameterTwoName": "8654238642489624862", | ||
"parameterThreeName": "3so87tg4y98n7y34ts3t4sh st34y79p4y3t7 8s", | ||
"parameterFourName": "hhh:://asdfasdf.sadfasdf:3452345@f][a;v-][0[-", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
output: []*awsCF.Parameter{}, | ||
}, | ||
} | ||
|
||
for i, test := range tests { | ||
assert := assert.New(t) | ||
testOutput := ResolveParameters( | ||
test.input.ctx, | ||
test.input.cfYaml, | ||
test.input.manifestFile, | ||
) | ||
assert.Equal(testOutput, test.output, fmt.Sprintf("Test %d: %s", i, test.name)) | ||
} | ||
} | ||
|
||
// func ResolveParameters(t *testing.T) { | ||
// tests := []struct { | ||
// name string | ||
// input struct { | ||
// ctx *cli.Context | ||
// cfYaml YamlCloudformation | ||
// cfClient *awsCF.CloudFormation | ||
// manifestFile *manifest.Manifest | ||
// } | ||
// output []*awsCF.Parameter | ||
// throws bool | ||
// }{ | ||
// { | ||
// name: "Dev", | ||
// environment: "development", | ||
// output: map[string]string{}, | ||
// throws: false, | ||
// }, | ||
// } | ||
|
||
// for i, test := range tests { | ||
// assert := assert.New(t) | ||
// testOutput, err := ResolveParameters( | ||
// test.input.ctx, | ||
// test.input.cfYaml, | ||
// test.input.cfClient, | ||
// test.input.manifestFile, | ||
// ) | ||
// if test.throws { | ||
// assert.NotNil(err) | ||
// } else { | ||
// assert.Nil(err) | ||
// assert.Equal(testOutput, test.output, fmt.Sprintf("Test %d: %s", i, test.name)) | ||
// } | ||
// } | ||
// } |
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.