Skip to content

Commit

Permalink
adding testing and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alpacamybags118 committed Jan 11, 2021
1 parent 5825363 commit 258eb4e
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 49 deletions.
10 changes: 9 additions & 1 deletion modules/terraform/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,16 @@ func (err VarFileNotFound) Error() string {
return fmt.Sprintf("Could not resolve var file %s", err.Path)
}

// InputFileKeyNotFound occurs when tfvar file does not contain a value for the key
// specified in the function call
type InputFileKeyNotFound string

func (err InputFileKeyNotFound) Error() string {
return fmt.Sprintf("tfvar file doesn't contain a value for the key %q", string(err))
}

type HclDecodeError struct {
FilePath string
FilePath string
ErrorText string
}

Expand Down
53 changes: 35 additions & 18 deletions modules/terraform/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,19 @@ func WithDefaultRetryableErrors(t *testing.T, originalOptions *Options) *Options
}

// GetVariableAsStringFromVarFile Gets the string represention of a variable from a provided input file found in VarFile
// For list or map
// For list or map, use GetVariableAsListFromVarFile or GetVariableAsMapFromVarFile, respectively.
func GetVariableAsStringFromVarFile(t *testing.T, option *Options, fileName string, key string) string {
result, err := GetVariableAsStringFromVarFileE(option, fileName, key)
result, err := GetVariableAsStringFromVarFileE(t, option, fileName, key)
require.NoError(t, err)

return result
}

func GetVariableAsStringFromVarFileE(option *Options, fileName string, key string) (string, error) {
variables, err := GetAllVariablesFromVarFileE(option, fileName)
// GetVariableAsStringFromVarFileE Gets the string represention of a variable from a provided input file found in VarFile
// Will return an error if GetAllVariablesFromVarFileE returns an error or the key provided does not exist in the file.
// For list or map, use GetVariableAsListFromVarFile or GetVariableAsMapFromVarFile, respectively.
func GetVariableAsStringFromVarFileE(t *testing.T, option *Options, fileName string, key string) (string, error) {
variables, err := GetAllVariablesFromVarFileE(t, option, fileName)

if err != nil {
return "", err
Expand All @@ -126,27 +129,36 @@ func GetVariableAsStringFromVarFileE(option *Options, fileName string, key strin
variable, exists := variables[key]

if !exists {
return "", OutputKeyNotFound(key)
return "", InputFileKeyNotFound(key)
}

return fmt.Sprintf("%v", variable), nil
}

// GetVariableAsMapFromVarFile Gets the map represention of a variable from a provided input file found in VarFile
func GetVariableAsMapFromVarFile(t *testing.T, option *Options, fileName string, key string) map[string]string {
result, err := GetVariableAsMapFromVarFileE(option, fileName, key)
result, err := GetVariableAsMapFromVarFileE(t, option, fileName, key)
require.NoError(t, err)

return result
}

func GetVariableAsMapFromVarFileE(option *Options, fileName string, key string) (map[string]string, error) {
// GetVariableAsMapFromVarFileE Gets the map represention of a variable from a provided input file found in VarFile
// Returns an error if GetAllVariablesFromVarFileE returns an error, the key provided does not exist, or the value associated with the key is not a map
func GetVariableAsMapFromVarFileE(t *testing.T, option *Options, fileName string, key string) (map[string]string, error) {
resultMap := make(map[string]string)
variables, err := GetAllVariablesFromVarFileE(option, fileName)
variables, err := GetAllVariablesFromVarFileE(t, option, fileName)

if err != nil {
return nil, err
}

_, exists := variables[key]

if !exists {
return nil, InputFileKeyNotFound(key)
}

if reflect.TypeOf(variables[key]).String() != "[]map[string]interface {}" {
return nil, UnexpectedOutputType{Key: key, ExpectedType: "map", ActualType: reflect.TypeOf(variables[key]).String()}
}
Expand All @@ -160,21 +172,28 @@ func GetVariableAsMapFromVarFileE(option *Options, fileName string, key string)
return resultMap, nil
}

// GetVariableAsListFromVarFile Gets the string list represention of a variable from a provided input file found in VarFile
func GetVariableAsListFromVarFile(t *testing.T, option *Options, fileName string, key string) []string {
result, err := GetVariableAsListFromVarFileE(option, fileName, key)
result, err := GetVariableAsListFromVarFileE(t, option, fileName, key)
require.NoError(t, err)

return result
}

func GetVariableAsListFromVarFileE(option *Options, fileName string, key string) ([]string, error) {
// GetVariableAsListFromVarFileE Gets the string list represention of a variable from a provided input file found in VarFile
// Will return error if GetAllVariablesFromVarFileE returns an error, the key provided does not exist, or the value associated with the key is not a list
func GetVariableAsListFromVarFileE(t *testing.T, option *Options, fileName string, key string) ([]string, error) {
resultArray := []string{}
variables, err := GetAllVariablesFromVarFileE(option, fileName)
variables, err := GetAllVariablesFromVarFileE(t, option, fileName)

if err != nil {
return nil, err
}

if _, exists := variables[key]; !exists {
return nil, InputFileKeyNotFound(key)
}

if reflect.TypeOf(variables[key]).String() != "[]interface {}" {
return nil, UnexpectedOutputType{Key: key, ExpectedType: "list", ActualType: reflect.TypeOf(variables[key]).String()}
}
Expand All @@ -186,18 +205,17 @@ func GetVariableAsListFromVarFileE(option *Options, fileName string, key string)
return resultArray, nil
}

// GetVariablesFromVarFiles Parses all data from a provided input file found in VarFile and returns them as a key-value map
// Note that the values in the map are interface{} type so you will need to convert them to the expected data type
// GetAllVariablesFromVarFile Parses all data from a provided input file found in VarFile and returns them as a key-value map
func GetAllVariablesFromVarFile(t *testing.T, option *Options, fileName string) map[string]interface{} {
variableMaps, err := GetAllVariablesFromVarFileE(option, fileName)
variableMaps, err := GetAllVariablesFromVarFileE(t, option, fileName)
require.NoError(t, err)

return variableMaps
}

// GetVariablesFromVarFilesE Parses all data from a provided input file found ind in VarFile and returns them as a key-value map
// Note that the values in the map are interface{} type so you will need to convert them to the expected data type
func GetAllVariablesFromVarFileE(option *Options, fileName string) (map[string]interface{}, error) {
// GetAllVariablesFromVarFileE Parses all data from a provided input file found ind in VarFile and returns them as a key-value map
// Retursn an error if the specified file does not exist, the specified file is not readable, or the specified file cannot be decoded from HCL
func GetAllVariablesFromVarFileE(t *testing.T, option *Options, fileName string) (map[string]interface{}, error) {
variableMap := map[string]interface{}{}
fileIndex := -1
if len(option.VarFiles) == 0 {
Expand Down Expand Up @@ -226,7 +244,6 @@ func GetAllVariablesFromVarFileE(option *Options, fileName string) (map[string]i
if err != nil {
return nil, HclDecodeError{FilePath: option.VarFiles[fileIndex], ErrorText: err.Error()}
}


return variableMap, nil
}
Loading

0 comments on commit 258eb4e

Please sign in to comment.