Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
Merge 82840d0 into 0d5ca27
Browse files Browse the repository at this point in the history
  • Loading branch information
ojkelly committed Jun 11, 2018
2 parents 0d5ca27 + 82840d0 commit 48e6b5d
Show file tree
Hide file tree
Showing 18 changed files with 431 additions and 144 deletions.
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions config/errors.go
Expand Up @@ -4,8 +4,7 @@ package config
var ErrorHelpInfo string

func init() {
ErrorHelpInfo = `
If this may be an issue with Kombustion, or happens repeatedly
please file a bug report https://github.com/KablamoOSS/kombustion/issues/new?template=bug_report.md
`
ErrorHelpInfo = `--
If this may be an issue with Kombustion, or happens repeatedly please file a bug report:
https://github.com/KablamoOSS/kombustion/issues/new?template=bug_report.md`
}
93 changes: 93 additions & 0 deletions internal/cloudformation/parameters.go
@@ -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
}
190 changes: 190 additions & 0 deletions internal/cloudformation/parameters_test.go
@@ -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))
// }
// }
// }
12 changes: 6 additions & 6 deletions internal/cloudformation/tasks/upsert.go
Expand Up @@ -11,14 +11,14 @@ import (
awsCF "github.com/aws/aws-sdk-go/service/cloudformation"
)

// Upsert a stack
// UpsertStack -
func UpsertStack(
templateBody []byte,
parameters []*awsCF.Parameter,
capabilities []*string,
stackName, profile, region string,
stackName string,
cf *awsCF.CloudFormation,
) {
cf := GetCloudformationClient(profile, region)

var err error
var status *awsCF.DescribeStacksOutput
Expand Down Expand Up @@ -69,14 +69,14 @@ func UpsertStack(
}
}

// Upsert a stack
// UpsertStackViaS3 -
func UpsertStackViaS3(
templateURL string,
parameters []*awsCF.Parameter,
capabilities []*string,
stackName, profile, region string,
stackName string,
cf *awsCF.CloudFormation,
) {
cf := GetCloudformationClient(profile, region)

var err error
var status *awsCF.DescribeStacksOutput
Expand Down

0 comments on commit 48e6b5d

Please sign in to comment.