-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathargo.go
188 lines (148 loc) · 3.97 KB
/
argo.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
package codefresh
import "fmt"
type (
ArgoAPI interface {
CreateIntegration(integration IntegrationPayloadData) error
UpdateIntegration(name string, integration IntegrationPayloadData) error
GetIntegrations() ([]*IntegrationPayload, error)
GetIntegrationByName(name string) (*IntegrationPayload, error)
DeleteIntegrationByName(name string) error
HeartBeat(error string, version string, integration string) error
SendResources(kind string, items interface{}, amount int, integration string) error
}
argo struct {
codefresh *codefresh
}
IntegrationItem struct {
Amount int `json:"amount"`
}
IntegrationPayloadData struct {
Name string `json:"name"`
Url string `json:"url"`
Clusters IntegrationItem `json:"clusters"`
Applications IntegrationItem `json:"applications"`
Repositories IntegrationItem `json:"repositories"`
Username *string `json:"username"`
Password *string `json:"password"`
Token *string `json:"token"`
ClusterName *string `json:"clusterName"`
ServerVersion *string `json:"serverVersion"`
Provider *string `json:"provider"`
}
IntegrationPayload struct {
Type string `json:"type"`
Data IntegrationPayloadData `json:"data"`
}
Heartbeat struct {
Error string `json:"error"`
AgentVersion string `json:"agentVersion"`
}
AgentState struct {
Kind string `json:"type"`
Items interface{} `json:"items"`
}
)
func newArgoAPI(codefresh *codefresh) ArgoAPI {
return &argo{codefresh}
}
func (a *argo) CreateIntegration(integration IntegrationPayloadData) error {
_, err := a.codefresh.requestAPI(&requestOptions{
path: "/api/argo",
method: "POST",
body: &IntegrationPayload{
Type: "argo-cd",
Data: integration,
},
})
return err
}
func (a *argo) UpdateIntegration(name string, integration IntegrationPayloadData) error {
_, err := a.codefresh.requestAPI(&requestOptions{
method: "PUT",
path: fmt.Sprintf("/api/argo/%s", name),
body: &IntegrationPayload{
Type: "argo-cd",
Data: integration,
},
})
if err != nil {
return err
}
return nil
}
func (a *argo) GetIntegrations() ([]*IntegrationPayload, error) {
var result []*IntegrationPayload
resp, err := a.codefresh.requestAPI(&requestOptions{
method: "GET",
path: "/api/argo",
})
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 *argo) GetIntegrationByName(name string) (*IntegrationPayload, error) {
var result IntegrationPayload
resp, err := a.codefresh.requestAPI(&requestOptions{
method: "GET",
path: fmt.Sprintf("/api/argo/%s", name),
})
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 *argo) DeleteIntegrationByName(name string) error {
_, err := a.codefresh.requestAPI(&requestOptions{
method: "DELETE",
path: fmt.Sprintf("/api/argo/%s", name),
})
if err != nil {
return err
}
return nil
}
func (a *argo) HeartBeat(error string, version string, integration string) error {
var body = Heartbeat{}
if error != "" {
body.Error = error
}
if version != "" {
body.AgentVersion = version
}
resp, err := a.codefresh.requestAPI(&requestOptions{
method: "POST",
path: fmt.Sprintf("/api/argo-agent/%s/heartbeat", integration),
body: body,
})
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
func (a *argo) SendResources(kind string, items interface{}, amount int, integration string) error {
if items == nil {
return nil
}
resp, err := a.codefresh.requestAPI(&requestOptions{
method: "POST",
path: fmt.Sprintf("/api/argo-agent/%s", integration),
body: &AgentState{Kind: kind, Items: items},
})
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}