-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from janritter/refactor/remove-dot-terraform-f…
…older Refactor/remove dot terraform folder
- Loading branch information
Showing
9 changed files
with
258 additions
and
99 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
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 |
---|---|---|
@@ -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 | ||
} |
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,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) | ||
} |
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.