-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtypes.go
238 lines (201 loc) · 7.09 KB
/
types.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package cmd
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"os"
"strings"
"gopkg.in/yaml.v2"
)
var apiKey = os.Getenv("OCTOPUS_API_KEY")
var uri = os.Getenv("OCTOPUS_URI")
var client = http.Client{}
var validVariableTypes = []string{"AzureAccount", "AWSAccount", "Certificate", "String"}
var validScriptSyntaxTypes = []string{"PowerShell", "Bash", "CSharp", "FSharp"}
var validTenancyTypes = []string{"Tenanted", "Untenanted", "TenantedOrUntenanted"}
var validScopeTypes = []string{"TenantTag", "Environment", "Machine", "Channel", "Action", "Role"}
type project struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
ProjectGroup string `yaml:"group"`
Lifecycle string `yaml:"lifecycle"`
Tenanted string `yaml:"tenanted"`
}
type variable struct {
Name string `yaml:"name"`
Value string `yaml:"value,omitempty"`
ScopedValues []map[string]string `yaml:"scopedValues,omitempty"`
Type string `yaml:"type,omitempty"`
Description string `yaml:"description,omitempty"`
}
type step struct {
Name string `yaml:"name"`
Type string `yaml:"type"`
File string `yaml:"file"`
}
type process struct {
Steps []step `yaml:"steps"`
}
type octopipe struct {
Variables []variable `yaml:"variables"`
Project project `yaml:"project"`
Process process `yaml:"process"`
}
type octopusResource interface {
}
type octopusLifecycle struct {
ID string `json:"Id"`
Name string `json:"Name"`
}
type octopusLifecycles []octopusLifecycle
type octopusProjectGroup struct {
ID string `json:"Id"`
Name string `json:"Name"`
}
type octopusProjectGroups []octopusProjectGroup
type octopusProject struct {
ID string `json:"Id"`
Name string `json:"Name"`
Description string `json:"Description"`
VariableSetID string `json:"VariableSetId"`
LifecycleID string `json:"LifecycleId"`
ProjectGroupID string `json:"ProjectGroupId"`
DeploymentProcessID string `json:"DeploymentProcessId"`
TenantedDeploymentMode string `json:"TenantedDeploymentMode"`
Links map[string]string `json:"Links"`
}
type octopusVariable struct {
Name string `json:"Name"`
Value string `json:"Value"`
Description string `json:"Description"`
IsSensitive bool `json:"IsSensitive"`
Scope map[string][]string `json:"Scope,omitempty"`
Type string `json:"Type"`
}
type octopusVariableSetScopeValue struct {
ID string `json:"Id"`
Name string `json:"Name"`
}
type octopusVariableSetScopeValues struct {
Environments []octopusVariableSetScopeValue `json:"Environments"`
Machines []octopusVariableSetScopeValue `json:"Machines"`
Actions []octopusVariableSetScopeValue `json:"Actions"`
Roles []octopusVariableSetScopeValue `json:"Roles"`
Channels []octopusVariableSetScopeValue `json:"Channels"`
TenantTags []octopusVariableSetScopeValue `json:"TenantTags"`
}
type octopusVariableSet struct {
ID string `json:"Id"`
Variables []octopusVariable `json:"Variables"`
Version int `json:"Version"`
Links map[string]string `json:"Links"`
OwnerID string `json:"OwnerId"`
ScopeValues octopusVariableSetScopeValues `json:"ScopeValues"`
}
type octopusDeploymentAction struct {
Name string `json:"Name"`
ActionType string `json:"ActionType"`
WorkerPoolID string `json:"WorkerPoolId"`
Properties map[string]string `json:"Properties"`
}
type octopusDeploymentStep struct {
Name string `json:"Name"`
Actions []octopusDeploymentAction `json:"Actions"`
}
type octopusDeploymentProcess struct {
ID string `json:"Id"`
ProjectID string `json:"ProjectId"`
Version int `json:"Version"`
Steps []octopusDeploymentStep `json:"Steps"`
}
func getOctopusData(o octopusResource, uri string) {
responsebody, status := doOctopusRequest(nil, uri, "GET")
json.Unmarshal(responsebody, o)
if status != 200 {
logAndExitf("Failed to get Octopus resource:\n%s", string(responsebody))
}
}
func putOctopusData(o octopusResource, uri string) {
body, err := json.Marshal(o)
if err != nil {
logAndExitf("Failed to serialize Octopus resource before put:\n%s", err.Error())
}
responsebody, status := doOctopusRequest(body, uri, "PUT")
if status != 200 {
logAndExitf("Failed to put Octopus resource:\n%s", string(responsebody))
}
json.Unmarshal(responsebody, o)
}
func postOctopusData(o octopusResource, uri string) {
body, err := json.Marshal(o)
if err != nil {
logAndExitf("Failed to serialize Octopus resource before post:\n%s", err.Error())
}
responsebody, status := doOctopusRequest(body, uri, "POST")
if status != 201 {
logAndExitf("Failed to post Octopus resource:\n%s", string(responsebody))
}
json.Unmarshal(responsebody, o)
}
func (op *octopipe) importOctopipeFile() {
ofile, err := ioutil.ReadFile("octopipe.yaml")
if err != nil {
logAndExitf("Error opening octopipe.yaml:\n%s", err.Error())
}
err = yaml.Unmarshal(ofile, op)
if err != nil {
logAndExitf("Error importing octopipe.yaml:\n%s", err.Error())
}
}
func (v *octopusVariableSetScopeValues) makeScopeDataSet() (dataset map[string][]octopusVariableSetScopeValue) {
scmap := make(map[string][]octopusVariableSetScopeValue)
scmap["Environment"] = v.Environments
scmap["Machine"] = v.Machines
scmap["Role"] = v.Roles
scmap["TenantTag"] = v.TenantTags
scmap["Action"] = v.Actions
scmap["Channel"] = v.Channels
return scmap
}
func (v *octopusVariableSetScopeValues) getScope(dataset map[string][]octopusVariableSetScopeValue, names string, IDs []string, scopeType string) (scopeIDs []string, scopeNames []string, err error) {
if names != "" {
tnames := strings.Split(names, ",")
scopes := make([]string, 0)
for _, name := range tnames {
found := false
for _, sc := range dataset[scopeType] {
if scopeType == "TenantTag" {
if sc.ID == name {
scopes = append(scopes, sc.ID)
found = true
}
} else {
if sc.Name == name {
scopes = append(scopes, sc.ID)
found = true
}
}
}
if !found {
return nil, nil, errors.New("Scope value with name '" + name + "' not found")
}
}
return scopes, nil, nil
} else if IDs != nil {
names := make([]string, 0)
for _, ID := range IDs {
for _, sc := range dataset[scopeType] {
if sc.ID == ID {
if scopeType == "TenantTag" {
names = append(names, sc.ID)
} else {
names = append(names, sc.Name)
}
}
}
}
return nil, names, nil
}
return nil, nil, errors.New("No names or Ids to process")
}