-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgitops.go
248 lines (208 loc) · 6.1 KB
/
gitops.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
package codefresh
import (
"errors"
"fmt"
"time"
)
type (
GitopsAPI interface {
CreateEnvironment(name string, project string, application string, integration string, namespace string) error
SendEnvironment(environment Environment) (map[string]interface{}, error)
DeleteEnvironment(name string) error
GetEnvironments() ([]CFEnvironment, error)
SendEvent(name string, props map[string]string) error
SendApplicationResources(resources *ApplicationResources) error
}
gitops struct {
codefresh *codefresh
}
CodefreshEvent struct {
Event string `json:"event"`
Props map[string]string `json:"props"`
}
MongoCFEnvWrapper struct {
Docs []CFEnvironment `json:"docs"`
}
CFEnvironment struct {
Metadata struct {
Name string `json:"name"`
} `json:"metadata"`
Spec struct {
Type string `json:"type"`
Application string `json:"application"`
Context string `json:"context"`
} `json:"spec"`
}
EnvironmentMetadata struct {
Name string `json:"name"`
}
EnvironmentSpec struct {
Type string `json:"type"`
Context string `json:"context"`
Project string `json:"project"`
Application string `json:"application"`
Namespace string `json:"namespace,omitempty"`
}
EnvironmentPayload struct {
Version string `json:"version"`
Metadata EnvironmentMetadata `json:"metadata"`
Spec EnvironmentSpec `json:"spec"`
}
SyncPolicy struct {
AutoSync bool `json:"autoSync"`
}
Commit struct {
Time *time.Time `json:"time,omitempty"`
Message *string `json:"message"`
Avatar *string `json:"avatar"`
}
EnvironmentActivityRS struct {
From ReplicaState `json:"from"`
To ReplicaState `json:"to"`
}
ReplicaState struct {
Current int64 `json:"current"`
Desired int64 `json:"desired"`
}
Annotation struct {
Key string `json:"key"`
Value string `json:"value"`
}
GitopsUser struct {
Name string `json:"name"`
Avatar string `json:"avatar"`
}
Gitops struct {
Comitters []GitopsUser `json:"comitters"`
Prs []Annotation `json:"prs"`
Issues []Annotation `json:"issues"`
}
Environment struct {
Gitops Gitops `json:"gitops"`
FinishedAt string `json:"finishedAt"`
HealthStatus string `json:"healthStatus"`
SyncStatus string `json:"status"`
HistoryId int64 `json:"historyId"`
SyncRevision string `json:"revision"`
Name string `json:"name"`
Activities []EnvironmentActivity `json:"activities"`
Resources interface{} `json:"resources"`
RepoUrl string `json:"repoUrl"`
Commit Commit `json:"commit"`
SyncPolicy SyncPolicy `json:"syncPolicy"`
Date string `json:"date"`
ParentApp string `json:"parentApp"`
Namespace string `json:"namespace"`
Server string `json:"server"`
Context *string `json:"context"`
}
EnvironmentActivity struct {
Name string `json:"name"`
TargetImages []string `json:"targetImages"`
Status string `json:"status"`
LiveImages []string `json:"liveImages"`
ReplicaSet EnvironmentActivityRS `json:"replicaSet"`
}
ApplicationResources struct {
Name string `json:"name, omitempty"`
HistoryId int64 `json:"historyId"`
Revision string `json:"revision, omitempty"`
Resources interface{} `json:"resources"`
Context *string `json:"context"`
}
)
func newGitopsAPI(codefresh *codefresh) GitopsAPI {
return &gitops{codefresh}
}
func (a *gitops) CreateEnvironment(name string, project string, application string, integration string, namespace string) error {
resp, err := a.codefresh.requestAPI(&requestOptions{
method: "POST",
path: "/api/gitops/application",
body: &EnvironmentPayload{
Version: "1.0",
Metadata: EnvironmentMetadata{
Name: name,
},
Spec: EnvironmentSpec{
Type: "argo",
Context: integration,
Project: project,
Application: application,
Namespace: namespace,
},
},
})
if resp != nil && resp.StatusCode >= 400 {
return errors.New(fmt.Sprintf("Failed to create environment, reason %v", resp.Status))
}
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
func (a *gitops) SendEnvironment(environment Environment) (map[string]interface{}, error) {
var result map[string]interface{}
resp, err := a.codefresh.requestAPI(&requestOptions{method: "POST", path: "/api/environments-v2/argo/events", body: environment})
if err != nil {
return nil, err
}
err = a.codefresh.decodeResponseInto(resp, &result)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return result, nil
}
func (a *gitops) DeleteEnvironment(name string) error {
resp, err := a.codefresh.requestAPI(&requestOptions{
method: "DELETE",
path: fmt.Sprintf("/api/environments-v2/%s", name),
})
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
func (a *gitops) GetEnvironments() ([]CFEnvironment, error) {
var result MongoCFEnvWrapper
resp, err := a.codefresh.requestAPI(&requestOptions{
method: "GET",
path: "/api/gitops/application?plain=true&isEnvironment=false",
})
if err != nil {
return nil, err
}
defer resp.Body.Close()
err = a.codefresh.decodeResponseInto(resp, &result)
if err != nil {
return nil, err
}
return result.Docs, nil
}
func (a *gitops) SendEvent(name string, props map[string]string) error {
event := CodefreshEvent{Event: name, Props: props}
resp, err := a.codefresh.requestAPI(&requestOptions{
method: "POST",
path: "/api/gitops/system/events",
body: event,
})
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
func (a *gitops) SendApplicationResources(resources *ApplicationResources) error {
resp, err := a.codefresh.requestAPI(&requestOptions{
method: "POST",
path: fmt.Sprintf("/api/gitops/resources"),
body: &resources,
})
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}