-
Notifications
You must be signed in to change notification settings - Fork 1
/
ctapi.go
260 lines (213 loc) · 7.17 KB
/
ctapi.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package main
import (
"context"
"fmt"
"net/url"
"os"
log "github.com/sirupsen/logrus"
"github.com/cloudtruth/cloudtruth-go-client"
)
type CTApi struct {
configuration *cloudtruth.Configuration
client *cloudtruth.APIClient
environments map[string]cloudtruth.Environment
projects map[string]cloudtruth.Project
}
type CTProject struct {
ctapi *CTApi
environment_name string
environment_id string
project_name string
project_id string
tag string
parameters map[string]Parameter
templates map[string]Template
}
type Parameter struct {
id string
name string
value string
secret bool
}
type Template struct {
id string
name string
body string
value func() string
}
func NewCTApi(api_key string, api_url string, user_agent string, debug bool) *CTApi {
log.Debug("Creating new CTApi")
ctapi := new(CTApi)
ctapi.configuration = cloudtruth.NewConfiguration()
ctapi.configuration.UserAgent = user_agent
u, err := url.Parse(api_url)
if err != nil {
log.Fatalf("Invalid url: %s, err: %v", api_url, err)
}
ctapi.configuration.Host = u.Host
ctapi.configuration.Scheme = u.Scheme
ctapi.configuration.Debug = debug
ctapi.configuration.AddDefaultHeader("Authorization", fmt.Sprintf("Api-Key %s", api_key))
ctapi.client = cloudtruth.NewAPIClient(ctapi.configuration)
ctapi.environments = make(map[string]cloudtruth.Environment)
ctapi.projects = make(map[string]cloudtruth.Project)
return ctapi
}
func (ctapi *CTApi) Environments() map[string]cloudtruth.Environment {
if len(ctapi.environments) == 0 {
log.Debug("Fetching environments")
resp, r, err := ctapi.client.EnvironmentsApi.EnvironmentsList(context.Background()).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `EnvironmentsApi.EnvironmentsList``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
os.Exit(2)
}
for _, p := range resp.Results {
ctapi.environments[p.Name] = p
}
}
return ctapi.environments
}
func (ctapi *CTApi) EnvironmentId(name string) string {
if len(ctapi.environments) != 0 {
return ctapi.environments[name].Id
}
log.Debugf("Looking up environment id: %s", name)
request := ctapi.client.EnvironmentsApi.EnvironmentsList(context.Background())
request = request.Name(name)
resp, r, err := request.Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `EnvironmentsApi.EnvironmentsList``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
os.Exit(2)
}
if len(resp.Results) != 1 || resp.Results[0].Name != name {
log.Errorf("Could not find environment: %s", name)
os.Exit(2)
}
return resp.Results[0].Id
}
func (ctapi *CTApi) Projects() map[string]cloudtruth.Project {
if len(ctapi.projects) == 0 {
log.Debug("Fetching projects")
resp, r, err := ctapi.client.ProjectsApi.ProjectsList(context.Background()).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `ProjectsApi.ProjectsList``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
os.Exit(2)
}
for _, p := range resp.Results {
ctapi.projects[p.Name] = p
}
}
return ctapi.projects
}
func (ctapi *CTApi) ProjectId(name string) string {
if len(ctapi.projects) != 0 {
return ctapi.projects[name].Id
}
log.Debugf("Looking up project id: %s", name)
request := ctapi.client.ProjectsApi.ProjectsList(context.Background())
request = request.Name(name)
resp, r, err := request.Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `ProjectsApi.ProjectsList``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
os.Exit(2)
}
if len(resp.Results) != 1 || resp.Results[0].Name != name {
log.Errorf("Could not find project: %s", name)
os.Exit(2)
}
return resp.Results[0].Id
}
func NewCTProject(ctapi *CTApi, project_name string, environment_name string, tag string) *CTProject {
ctproject := new(CTProject)
ctproject.ctapi = ctapi
ctproject.project_name = project_name
ctproject.project_id = ctapi.ProjectId(project_name)
ctproject.environment_name = environment_name
ctproject.environment_id = ctapi.EnvironmentId(environment_name)
ctproject.tag = tag
ctproject.parameters = make(map[string]Parameter)
ctproject.templates = make(map[string]Template)
return ctproject
}
func (ctproject *CTProject) Parameters() map[string]Parameter {
if len(ctproject.parameters) == 0 {
log.Infof("Fetching parameters %s=%s/%s=%s",
ctproject.project_name, ctproject.project_id,
ctproject.environment_name, ctproject.environment_id)
request := ctproject.ctapi.client.ProjectsApi.ProjectsParametersList(context.Background(), ctproject.project_id)
request = request.Environment(ctproject.environment_id)
request = request.Evaluate(true)
if ctproject.tag != "" {
request = request.Tag(ctproject.tag)
}
resp, r, err := request.Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `ProjectsApi.ProjectsParametersList``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
os.Exit(2)
}
for _, p := range resp.Results {
v := ""
if len(p.ValuesFlat) != 0 {
v = *p.ValuesFlat[0].Value.Get()
}
ctproject.parameters[p.Name] = Parameter{p.Id, p.Name, v, *p.Secret}
}
}
return ctproject.parameters
}
func (ctproject *CTProject) Template(template_name string) string {
template := ctproject.templates[template_name]
log.Infof("Fetching template %s=%s/%s=%s/%s=%s",
ctproject.project_name, ctproject.project_id,
ctproject.environment_name, ctproject.environment_id,
template.name, template.id)
request := ctproject.ctapi.client.ProjectsApi.ProjectsTemplatesRetrieve(context.Background(), template.id, ctproject.project_id)
request = request.Environment(ctproject.environment_id)
if ctproject.tag != "" {
request = request.Tag(ctproject.tag)
}
resp, r, err := request.Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `ProjectsApi.ProjectsTemplatesRetrieve``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
os.Exit(2)
}
return *resp.Body
}
func (ctproject *CTProject) Templates() map[string]Template {
if len(ctproject.templates) == 0 {
log.Infof("Fetching templates %s=%s/%s=%s",
ctproject.project_name, ctproject.project_id,
ctproject.environment_name, ctproject.environment_id)
request := ctproject.ctapi.client.ProjectsApi.ProjectsTemplatesList(context.Background(), ctproject.project_id)
request = request.Environment(ctproject.environment_id)
request = request.Evaluate(false)
if ctproject.tag != "" {
request = request.Tag(ctproject.tag)
}
resp, r, err := request.Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `ProjectsApi.ProjectsTemplatesList``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
os.Exit(2)
}
for _, t := range resp.Results {
template_name := t.Name
log.Debugf("Adding template reference '%s'", template_name)
anon := func() string { return ctproject.Template(template_name) }
ctproject.templates[t.Name] = Template{t.Id, t.Name, "", anon}
}
}
return ctproject.templates
}
func (template *Template) Get() string {
if template.body == "" {
template.body = template.value()
}
return template.body
}