Skip to content

Commit

Permalink
Merge pull request #10 from janritter/refactor/remove-dot-terraform-f…
Browse files Browse the repository at this point in the history
…older

Refactor/remove dot terraform folder
  • Loading branch information
janritter authored Jun 2, 2019
2 parents 4327ae7 + 6f7ac75 commit cd96d7a
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 99 deletions.
16 changes: 2 additions & 14 deletions Gopkg.lock

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

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[![Build Status](https://travis-ci.com/janritter/terrastate.svg?token=fPhMwJC3SnTkQrfzte44&branch=master)](https://travis-ci.com/janritter/terrastate)
[![Maintainability](https://api.codeclimate.com/v1/badges/235b50a37a1d73929d5c/maintainability)](https://codeclimate.com/github/janritter/terrastate/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/235b50a37a1d73929d5c/test_coverage)](https://codeclimate.com/github/janritter/terrastate/test_coverage)

> Tool to manage multiple states in Terraform - Allows Multi account setups
Expand Down
8 changes: 4 additions & 4 deletions backend/s3/parsercall.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import (
func callParserForBackendParameters(in interface{}, out *stateConfig) error {
varParser := parser.NewParser(in)

err, bucket := varParser.GetBackendParameterString("state_bucket", false)
bucket, _, err := varParser.GetBackendParameterString("state_bucket", false)
if err != nil {
return err
}
out.Bucket = bucket

err, dynamodbTable := varParser.GetBackendParameterString("state_dynamodb_table", false)
dynamodbTable, _, err := varParser.GetBackendParameterString("state_dynamodb_table", false)
if err != nil {
return err
}
out.DynamoDBTable = dynamodbTable

err, stateKey := varParser.GetBackendParameterString("state_key", false)
stateKey, _, err := varParser.GetBackendParameterString("state_key", false)
if err != nil {
return err
}
Expand All @@ -31,7 +31,7 @@ func callParserForBackendParameters(in interface{}, out *stateConfig) error {
}
out.Key = stateKey

err, region := varParser.GetBackendParameterString("region", false)
region, _, err := varParser.GetBackendParameterString("region", false)
if err != nil {
return err
}
Expand Down
29 changes: 5 additions & 24 deletions helper/backend.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
package helper

import (
"errors"
"log"
"reflect"
"github.com/janritter/terrastate/helper/parser"
)

func GetBackendType(in interface{}) (string, error) {
var stateBucket string
switch in.(type) {
case map[string]interface{}:
mapped := in.(map[string]interface{})
if mapped["state_backend"] == nil {
err := errors.New("state_backend must be defined")
log.Println(err)
return "", err
}
if reflect.TypeOf(mapped["state_backend"]).String() != "string" {
err := errors.New("state_backend must be of type string, was " + reflect.TypeOf(mapped["state_backend"]).String())
log.Println(err)
return "", err
}
stateBucket = mapped["state_backend"].(string)

default:
err := errors.New("Unknown var-file format")
log.Println(err)
varParser := parser.NewParser(in)
backend, _, err := varParser.GetBackendParameterString("state_backend", false)
if err != nil {
return "", err
}

return stateBucket, nil
return backend, nil
}
55 changes: 35 additions & 20 deletions helper/parser/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,66 @@ import (
"github.com/fatih/color"
)

func (parser *Parser) GetBackendParameterString(key string, optional bool) (error, string) {
err, iface := parser.getSingleBackendParameterInterface(key, optional, "string")
func (parser *Parser) GetBackendParameterString(key string, optional bool) (string, bool, error) {
iface, valueSet, err := parser.getSingleBackendParameterInterface(key, optional, "string")
if err != nil {
return err, ""
return "", valueSet, err
}
return nil, iface.(string)
if valueSet == false {
return "", valueSet, nil
}
return iface.(string), valueSet, nil
}

func (parser *Parser) GetBackendParameterInt(key string, optional bool) (error, int) {
err, iface := parser.getSingleBackendParameterInterface(key, optional, "int")
func (parser *Parser) GetBackendParameterInt(key string, optional bool) (int, bool, error) {
iface, valueSet, err := parser.getSingleBackendParameterInterface(key, optional, "int")
if err != nil {
return err, 0
return 0, valueSet, err
}
if valueSet == false {
return 0, valueSet, nil
}
return nil, iface.(int)
return iface.(int), valueSet, nil
}

func (parser *Parser) GetBackendParameterBool(key string, optional bool) (error, bool) {
err, iface := parser.getSingleBackendParameterInterface(key, optional, "bool")
// Return values are: an error, the parsed bool value and if the value in the var-file was set.
func (parser *Parser) GetBackendParameterBool(key string, optional bool) (bool, bool, error) {
iface, valueSet, err := parser.getSingleBackendParameterInterface(key, optional, "bool")
if err != nil {
return err, false
return false, valueSet, err
}
if valueSet == false {
return false, valueSet, nil
}
return nil, iface.(bool)
return iface.(bool), valueSet, nil
}

func (parser *Parser) getSingleBackendParameterInterface(key string, optional bool, expectedType string) (error, interface{}) {
func (parser *Parser) getSingleBackendParameterInterface(key string, optional bool, expectedType string) (interface{}, bool, error) {
switch parser.VarFileContent.(type) {
case map[string]interface{}:
mapped := parser.VarFileContent.(map[string]interface{})

if mapped[key] == nil && optional == false {
err := errors.New(key + " must be defined, was not found var-file")
color.Red(err.Error())
return err, nil
valueSet := mapped[key] != nil

if valueSet == false {
if optional == false {
err := errors.New(key + " must be defined, was not found var-file")
color.Red(err.Error())
return nil, valueSet, err
}
return nil, valueSet, nil
}

if reflect.TypeOf(mapped[key]).String() != expectedType {
err := errors.New("Expected " + key + " to be " + expectedType + ", was " + reflect.TypeOf(mapped[key]).String())
color.Red(err.Error())
return err, nil
return nil, false, err
}
return nil, mapped[key]
return mapped[key], valueSet, nil

default:
err := errors.New("Unknown var-file format")
color.Red(err.Error())
return err, nil
return nil, false, err
}
}
177 changes: 177 additions & 0 deletions helper/parser/parameter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package parser

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

//
// GetBackendParameterString
//
func TestGetBackendParameterString(t *testing.T) {
testMap := make(map[string]interface{})
testMap["testKey"] = "testValue"

parser := NewParser(testMap)
result, valueSet, err := parser.GetBackendParameterString("testKey", false)

assert.Nil(t, err, "Expected no error")
assert.Equal(t, "testValue", result)
assert.Equal(t, true, valueSet)
}

func TestGetBackendParameterStringWrongType(t *testing.T) {
testMap := make(map[string]interface{})
testMap["testKey"] = 0

parser := NewParser(testMap)
result, valueSet, err := parser.GetBackendParameterString("testKey", false)

assert.Error(t, err, "Expected error")
assert.Equal(t, errors.New("Expected testKey to be string, was int"), err)
assert.Equal(t, "", result)
assert.Equal(t, false, valueSet)
}

func TestGetBackendParameterStringNotSetOptional(t *testing.T) {
testMap := make(map[string]interface{})

parser := NewParser(testMap)
result, valueSet, err := parser.GetBackendParameterString("testKey", true)

assert.Nil(t, err, "Expected no error")
assert.Equal(t, "", result)
assert.Equal(t, false, valueSet)
}

func TestGetBackendParameterStringNotSetNotOptional(t *testing.T) {
testMap := make(map[string]interface{})

parser := NewParser(testMap)
result, valueSet, err := parser.GetBackendParameterString("testKey", false)

assert.Error(t, err, "Expected error")
assert.Equal(t, errors.New("testKey must be defined, was not found var-file"), err)
assert.Equal(t, "", result)
assert.Equal(t, false, valueSet)
}

//
// GetBackendParameterInt
//

func TestGetBackendParameterInt(t *testing.T) {
testMap := make(map[string]interface{})
testMap["testKey"] = 10

parser := NewParser(testMap)
result, valueSet, err := parser.GetBackendParameterInt("testKey", false)

assert.Nil(t, err, "Expected no error")
assert.Equal(t, 10, result)
assert.Equal(t, true, valueSet)
}

func TestGetBackendParameterIntWrongType(t *testing.T) {
testMap := make(map[string]interface{})
testMap["testKey"] = "10"

parser := NewParser(testMap)
result, valueSet, err := parser.GetBackendParameterInt("testKey", false)

assert.Error(t, err, "Expected error")
assert.Equal(t, errors.New("Expected testKey to be int, was string"), err)
assert.Equal(t, 0, result)
assert.Equal(t, false, valueSet)
}

func TestGetBackendParameterIntNotSetOptional(t *testing.T) {
testMap := make(map[string]interface{})

parser := NewParser(testMap)
result, valueSet, err := parser.GetBackendParameterInt("testKey", true)

assert.Nil(t, err, "Expected no error")
assert.Equal(t, 0, result)
assert.Equal(t, false, valueSet)
}

func TestGetBackendParameterIntNotSetNotOptional(t *testing.T) {
testMap := make(map[string]interface{})

parser := NewParser(testMap)
result, valueSet, err := parser.GetBackendParameterInt("testKey", false)

assert.Error(t, err, "Expected error")
assert.Equal(t, errors.New("testKey must be defined, was not found var-file"), err)
assert.Equal(t, 0, result)
assert.Equal(t, false, valueSet)
}

//
// GetBackendParameterBool
//

func TestGetBackendParameterBool(t *testing.T) {
testMap := make(map[string]interface{})
testMap["testKey"] = false

parser := NewParser(testMap)
result, valueSet, err := parser.GetBackendParameterBool("testKey", false)

assert.Nil(t, err, "Expected no error")
assert.Equal(t, false, result)
assert.Equal(t, true, valueSet)
}

func TestGetBackendParameterBoolWrongType(t *testing.T) {
testMap := make(map[string]interface{})
testMap["testKey"] = "false"

parser := NewParser(testMap)
result, valueSet, err := parser.GetBackendParameterBool("testKey", false)

assert.Error(t, err, "Expected error")
assert.Equal(t, errors.New("Expected testKey to be bool, was string"), err)
assert.Equal(t, false, result)
assert.Equal(t, false, valueSet)
}

func TestGetBackendParameterBoolNotSetOptional(t *testing.T) {
testMap := make(map[string]interface{})

parser := NewParser(testMap)
result, valueSet, err := parser.GetBackendParameterBool("testKey", true)

assert.Nil(t, err, "Expected no error")
assert.Equal(t, false, result)
assert.Equal(t, false, valueSet)
}

func TestGetBackendParameterBoolNotSetNotOptional(t *testing.T) {
testMap := make(map[string]interface{})

parser := NewParser(testMap)
result, valueSet, err := parser.GetBackendParameterBool("testKey", false)

assert.Error(t, err, "Expected error")
assert.Equal(t, errors.New("testKey must be defined, was not found var-file"), err)
assert.Equal(t, false, result)
assert.Equal(t, false, valueSet)
}

//
// getSingleBackendParameterInterface
//

func TestGetSingleBackendParameterInterfaceInvalidFileContent(t *testing.T) {
parser := NewParser(nil)
result, valueSet, err := parser.getSingleBackendParameterInterface("testKey", false, "string")

assert.Error(t, err, "Expected error")
assert.Equal(t, errors.New("Unknown var-file format"), err)
assert.Equal(t, nil, result)
assert.Equal(t, false, valueSet)
}
6 changes: 3 additions & 3 deletions helper/parser/parser.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package parser

type ParserAPI interface {
GetBackendParameterString(key string, optional bool) (error, string)
GetBackendParameterInt(key string, optional bool) (error, int)
GetBackendParameterBool(key string, optional bool) (error, bool)
GetBackendParameterString(key string, optional bool) (string, bool, error)
GetBackendParameterInt(key string, optional bool) (int, bool, error)
GetBackendParameterBool(key string, optional bool) (bool, bool, error)
}

type Parser struct {
Expand Down
Loading

0 comments on commit cd96d7a

Please sign in to comment.