forked from jen20/riviera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_run_command.go
136 lines (110 loc) · 3.03 KB
/
step_run_command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package test
import (
"fmt"
"log"
"reflect"
"github.com/jen20/riviera/azure"
)
type StepRunCommand struct {
RunCommand azure.APICall
CleanupCommand azure.APICall
StateCommandURIKey string
StateCommand azure.APICall
StateBagKey string
}
func (s *StepRunCommand) Run(state AzureStateBag) StepAction {
if s.RunCommand == nil && s.StateCommand == nil {
return Continue
}
azureClient := state.Client()
if s.RunCommand != nil {
log.Printf("[INFO] Running %T command...", s.RunCommand)
r := azureClient.NewRequest()
r.Command = s.RunCommand
response, err := r.Execute()
if err != nil {
state.AppendError(err)
return Halt
}
if response.IsSuccessful() {
state.Put(s.StateBagKey, response.Parsed)
} else {
state.AppendError(response.Error)
return Halt
}
}
if s.StateCommand != nil {
log.Printf("[INFO] Refreshing state with %T command...", s.StateCommand)
var r *azure.Request
if s.StateCommandURIKey == "" {
r = azureClient.NewRequest()
} else {
uri, err := uriFromStateBagKey(state, s.StateBagKey, s.StateCommandURIKey)
if err != nil {
state.AppendError(err)
return Halt
}
if uri == "" {
state.AppendError(fmt.Errorf("URI from state bag was empty - check the (case-sensitive) paths in the test"))
return Halt
}
r = azureClient.NewRequestForURI(uri)
}
r.Command = s.StateCommand
response, err := r.Execute()
if err != nil {
state.AppendError(err)
return Halt
}
if response.IsSuccessful() {
state.Remove(s.StateBagKey)
state.Put(s.StateBagKey, response.Parsed)
return Continue
}
state.Remove(s.StateBagKey)
state.AppendError(response.Error)
return Halt
}
return Continue
}
func (s *StepRunCommand) Cleanup(state AzureStateBag) {
if s.CleanupCommand == nil {
return
}
azureClient := state.Client()
log.Printf("[INFO] Cleaning up with %T command...", s.CleanupCommand)
request := azureClient.NewRequest()
request.Command = s.CleanupCommand
response, err := request.Execute()
if err != nil {
state.AppendError(err)
return
}
if !response.IsSuccessful() {
log.Printf("[INFO] Error running clean up %T command", s.CleanupCommand)
state.AppendError(response.Error)
}
}
func uriFromStateBagKey(state AzureStateBag, stateBagKey, propertyPath string) (string, error) {
item, ok := state.GetOk(stateBagKey)
if !ok {
return "", fmt.Errorf("State bag key %q not found in state", stateBagKey)
}
itemValue := reflect.ValueOf(item)
switch itemValue.Kind() {
case reflect.Struct:
uriField := itemValue.FieldByName(propertyPath)
//TODO(jen20): zero handling
return uriField.String(), nil
case reflect.Ptr:
actualValue := itemValue.Elem()
if actualValue.Kind() != reflect.Struct {
return "", fmt.Errorf("State bag key %q is not a struct or pointer to struct", stateBagKey)
}
uriField := actualValue.FieldByName(propertyPath)
//TODO(jen20): zero handling
return uriField.String(), nil
default:
return "", fmt.Errorf("State bag key %q is not a struct or pointer to struct", stateBagKey)
}
}