This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
/
applicationdeployment.go
218 lines (182 loc) · 5.56 KB
/
applicationdeployment.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
package client
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"github.com/skatteetaten/ao/pkg/deploymentspec"
)
type ApplicationDeploymentClient interface {
Doer
Deploy(deployPayload *DeployPayload) (*DeployResults, error)
Delete(deletePayload *DeletePayload) (*DeleteResults, error)
Exists(existPayload *ExistsPayload) (*ExistsResults, error)
GetApplyResult(deployId string) (string, error)
}
type (
applicationDeploymentRef struct {
Environment string `json:"environment"`
Application string `json:"application"`
}
ApplicationRef struct {
Namespace string `json:"namespace"`
Name string `json:"name"`
}
DeployResults struct {
Message string
Success bool
Results []DeployResult
}
DeployResult struct {
DeployId string `json:"deployId"`
DeploymentSpec deploymentspec.DeploymentSpec `json:"deploymentSpec"`
Success bool `json:"success"`
Ignored bool `json:"ignored"`
Reason string `json:"reason"`
Warnings []string `json:"warnings"`
}
DeployPayload struct {
ApplicationDeploymentRefs []applicationDeploymentRef `json:"applicationDeploymentRefs"`
Overrides map[string]string `json:"overrides"`
Deploy bool `json:"deploy"`
}
DeleteResults struct {
Message string
Success bool
Results []DeleteResult
}
DeleteResult struct {
ApplicationRef ApplicationRef `json:"applicationRef"`
Success bool `json:"success"`
Reason string `json:"reason"`
}
DeletePayload struct {
ApplicationRefs []ApplicationRef `json:"applicationRefs"`
}
ExistsResults struct {
Message string
Success bool
Results []ExistsResult
}
ExistsResult struct {
ApplicationRef ApplicationRef `json:"applicationRef"`
Exists bool `json:"exists"`
Success bool `json:"success"`
Message string `json:"message"`
}
ExistsPayload struct {
ApplicationDeploymentRefs []applicationDeploymentRef `json:"adr"`
}
)
func NewApplicationDeploymentRef(name string) *applicationDeploymentRef {
slice := strings.Split(name, "/")
return &applicationDeploymentRef{
Environment: slice[0],
Application: slice[1],
}
}
func NewApplicationRef(namespace, name string) *ApplicationRef {
return &ApplicationRef{
Namespace: namespace,
Name: name,
}
}
func NewDeployPayload(applications []string, overrides map[string]string) *DeployPayload {
applicationDeploymentRefs := createApplicationDeploymentRefs(applications)
return &DeployPayload{
ApplicationDeploymentRefs: applicationDeploymentRefs,
Overrides: overrides,
Deploy: true,
}
}
func NewDeletePayload(applicationRefs []ApplicationRef) *DeletePayload {
return &DeletePayload{
ApplicationRefs: applicationRefs,
}
}
func NewExistsPayload(applications []string) *ExistsPayload {
applicationDeploymentRefs := createApplicationDeploymentRefs(applications)
return &ExistsPayload{
ApplicationDeploymentRefs: applicationDeploymentRefs,
}
}
func (api *ApiClient) Deploy(deployPayload *DeployPayload) (*DeployResults, error) {
payload, err := json.Marshal(deployPayload)
if err != nil {
return nil, errors.New("failed to marshal DeployPayload")
}
endpoint := fmt.Sprintf("/apply/%s", api.Affiliation)
response, err := api.Do(http.MethodPut, endpoint, payload)
if err != nil {
return nil, err
}
var deploys DeployResults
err = json.Unmarshal(response.Items, &deploys.Results)
if err != nil {
return nil, err
}
if !response.Success {
for _, deploy := range deploys.Results {
// Hack-ish solution since validation errors and deploy errors have
// different payload. TODO: Fix error response from Boober.
if deploy.DeploymentSpec.Name() == "-" {
return nil, response.Error()
}
}
}
deploys.Message = response.Message
if response.Success {
deploys.Success = true
}
return &deploys, nil
}
func (api *ApiClient) Delete(deletePayload *DeletePayload) (*DeleteResults, error) {
payload, err := json.Marshal(deletePayload)
if err != nil {
return nil, errors.New("Failed to marshal DeletePayload")
}
endpoint := fmt.Sprintf("/applicationdeployment/delete")
response, err := api.Do(http.MethodPost, endpoint, payload)
if err != nil {
return nil, err
}
var deleteResults DeleteResults
err = json.Unmarshal(response.Items, &deleteResults.Results)
if err != nil {
return nil, err
}
deleteResults.Message = response.Message
if response.Success {
deleteResults.Success = true
}
return &deleteResults, nil
}
func (api *ApiClient) Exists(existsPayload *ExistsPayload) (*ExistsResults, error) {
payload, err := json.Marshal(existsPayload)
if err != nil {
return nil, errors.New("Failed to marshal ExistsPayload")
}
endpoint := fmt.Sprintf("/applicationdeployment/%s", api.Affiliation)
response, err := api.Do(http.MethodPost, endpoint, payload)
if err != nil {
return nil, err
}
var existsResults ExistsResults
err = json.Unmarshal(response.Items, &existsResults.Results)
if err != nil {
return nil, err
}
existsResults.Message = response.Message
if response.Success {
existsResults.Success = true
}
return &existsResults, nil
}
func createApplicationDeploymentRefs(apps []string) []applicationDeploymentRef {
var applicationDeploymentRefs []applicationDeploymentRef
for _, app := range apps {
applicationDeploymentRefs = append(applicationDeploymentRefs, *NewApplicationDeploymentRef(app))
}
return applicationDeploymentRefs
}