-
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.
- Loading branch information
Showing
8 changed files
with
209 additions
and
109 deletions.
There are no files selected for viewing
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,39 @@ | ||
package terra | ||
|
||
import "fmt" | ||
|
||
type ErrWithCmdOutput struct { | ||
Underlying error | ||
Output *output | ||
} | ||
|
||
func (e *ErrWithCmdOutput) Error() string { | ||
return fmt.Sprintf("error while running command: %v; %s", e.Underlying, e.Output.Stderr()) | ||
} | ||
|
||
// MaxRetriesExceeded is an error that occurs when the maximum amount of retries is exceeded. | ||
type MaxRetriesExceeded struct { | ||
Description string | ||
MaxRetries int | ||
} | ||
|
||
func (err MaxRetriesExceeded) Error() string { | ||
return fmt.Sprintf("'%s' unsuccessful after %d retries", err.Description, err.MaxRetries) | ||
} | ||
|
||
// FatalError is a marker interface for errors that should not be retried. | ||
type FatalError struct { | ||
Underlying error | ||
} | ||
|
||
func (err FatalError) Error() string { | ||
return fmt.Sprintf("FatalError{Underlying: %v}", err.Underlying) | ||
} | ||
|
||
// OutputKeyNotFound occurs when terraform output does not contain a value for the key | ||
// specified in the function call | ||
type OutputKeyNotFound string | ||
|
||
func (err OutputKeyNotFound) Error() string { | ||
return fmt.Sprintf("output doesn't contain a value for the key %q", string(err)) | ||
} |
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,92 @@ | ||
package terra | ||
|
||
import ( | ||
"strings" | ||
"sync" | ||
) | ||
|
||
// output contains the output after running a command. | ||
type output struct { | ||
stdout *outputStream | ||
stderr *outputStream | ||
// merged contains stdout and stderr merged into one stream. | ||
merged *merged | ||
} | ||
|
||
func newOutput() *output { | ||
m := new(merged) | ||
return &output{ | ||
merged: m, | ||
stdout: &outputStream{ | ||
merged: m, | ||
}, | ||
stderr: &outputStream{ | ||
merged: m, | ||
}, | ||
} | ||
} | ||
|
||
func (o *output) Stdout() string { | ||
if o == nil { | ||
return "" | ||
} | ||
|
||
return o.stdout.String() | ||
} | ||
|
||
func (o *output) Stderr() string { | ||
if o == nil { | ||
return "" | ||
} | ||
|
||
return o.stderr.String() | ||
} | ||
|
||
func (o *output) Combined() string { | ||
if o == nil { | ||
return "" | ||
} | ||
|
||
return o.merged.String() | ||
} | ||
|
||
type outputStream struct { | ||
Lines []string | ||
*merged | ||
} | ||
|
||
func (st *outputStream) WriteString(s string) (n int, err error) { | ||
st.Lines = append(st.Lines, string(s)) | ||
return st.merged.WriteString(s) | ||
} | ||
|
||
func (st *outputStream) String() string { | ||
if st == nil { | ||
return "" | ||
} | ||
|
||
return strings.Join(st.Lines, "\n") | ||
} | ||
|
||
type merged struct { | ||
// ensure that there are no parallel writes | ||
sync.Mutex | ||
Lines []string | ||
} | ||
|
||
func (m *merged) String() string { | ||
if m == nil { | ||
return "" | ||
} | ||
|
||
return strings.Join(m.Lines, "\n") | ||
} | ||
|
||
func (m *merged) WriteString(s string) (n int, err error) { | ||
m.Lock() | ||
defer m.Unlock() | ||
|
||
m.Lines = append(m.Lines, string(s)) | ||
|
||
return len(s), 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,41 @@ | ||
package terra | ||
|
||
import ( | ||
"strings" | ||
"sync" | ||
) | ||
import "encoding/json" | ||
|
||
// output contains the output after runnig a command. | ||
type output struct { | ||
stdout *outputStream | ||
stderr *outputStream | ||
// merged contains stdout and stderr merged into one stream. | ||
merged *merged | ||
// OutputAll calls terraform output returns all values as a map. | ||
// If there is error fetching the output, fails the test | ||
func OutputAll(options *Options) (map[string]interface{}, error) { | ||
return OutputForKeysE(options, nil) | ||
} | ||
|
||
func newOutput() *output { | ||
m := new(merged) | ||
return &output{ | ||
merged: m, | ||
stdout: &outputStream{ | ||
merged: m, | ||
}, | ||
stderr: &outputStream{ | ||
merged: m, | ||
}, | ||
// OutputForKeysE calls terraform output for the given key list and returns values as a map. | ||
// The returned values are of type interface{} and need to be type casted as necessary. Refer to output_test.go | ||
func OutputForKeysE(options *Options, keys []string) (map[string]interface{}, error) { | ||
out, err := RunTerraformCommandAndGetStdoutE(options, FormatArgs(options, "output", "-no-color", "-json")...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
func (o *output) Stdout() string { | ||
if o == nil { | ||
return "" | ||
} | ||
|
||
return o.stdout.String() | ||
} | ||
|
||
func (o *output) Stderr() string { | ||
if o == nil { | ||
return "" | ||
} | ||
|
||
return o.stderr.String() | ||
} | ||
|
||
func (o *output) Combined() string { | ||
if o == nil { | ||
return "" | ||
outputMap := map[string]map[string]interface{}{} | ||
if err := json.Unmarshal([]byte(out), &outputMap); err != nil { | ||
return nil, err | ||
} | ||
|
||
return o.merged.String() | ||
} | ||
|
||
type outputStream struct { | ||
Lines []string | ||
*merged | ||
} | ||
|
||
func (st *outputStream) WriteString(s string) (n int, err error) { | ||
st.Lines = append(st.Lines, string(s)) | ||
return st.merged.WriteString(s) | ||
} | ||
|
||
func (st *outputStream) String() string { | ||
if st == nil { | ||
return "" | ||
if keys == nil { | ||
outputKeys := make([]string, 0, len(outputMap)) | ||
for k := range outputMap { | ||
outputKeys = append(outputKeys, k) | ||
} | ||
keys = outputKeys | ||
} | ||
|
||
return strings.Join(st.Lines, "\n") | ||
} | ||
|
||
type merged struct { | ||
// ensure that there are no parallel writes | ||
sync.Mutex | ||
Lines []string | ||
} | ||
|
||
func (m *merged) String() string { | ||
if m == nil { | ||
return "" | ||
resultMap := make(map[string]interface{}) | ||
for _, key := range keys { | ||
value, containsValue := outputMap[key]["value"] | ||
if !containsValue { | ||
return nil, OutputKeyNotFound(key) | ||
} | ||
resultMap[key] = value | ||
} | ||
|
||
return strings.Join(m.Lines, "\n") | ||
} | ||
|
||
func (m *merged) WriteString(s string) (n int, err error) { | ||
m.Lock() | ||
defer m.Unlock() | ||
|
||
m.Lines = append(m.Lines, string(s)) | ||
|
||
return len(s), nil | ||
return resultMap, 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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
resource null_resource date { | ||
provisioner local-exec { | ||
command = "date" | ||
} | ||
resource local_file foo { | ||
content = "foo!" | ||
filename = "${path.module}/foo.bar" | ||
} |
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,3 @@ | ||
output foo { | ||
value = local_file.foo.content | ||
} |