This repository was archived by the owner on Mar 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoperations.go
254 lines (214 loc) · 6.64 KB
/
operations.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
package podops
import (
"bytes"
"fmt"
"log"
"net/http"
"github.com/txsvc/platform/v2/pkg/api"
"github.com/podops/podops/internal/errordef"
"github.com/podops/podops/internal/messagedef"
"github.com/podops/podops/internal/transport"
)
const (
// NamespacePrefix namespace for the client and CLI
NamespacePrefix = "/a/v1"
// productionRoute route to call ProductionEndpoint
productionRoute = NamespacePrefix + "/production"
// listProductionsRoute route to call ListProductionsEndpoint
listProductionsRoute = NamespacePrefix + "/productions"
// resourceRoute route to call ResourceEndpoint
findResourceRoute = NamespacePrefix + "/resource/%s" // "/get/:id"
getResourceRoute = NamespacePrefix + "/resource/%s/%s/%s" // "/get/:prod/:kind/:id"
updateResourceRoute = NamespacePrefix + "/resource/%s/%s/%s?f=%v" // "/update/:prod/:kind/:id"
listResourcesRoute = NamespacePrefix + "/resource/%s/%s"
deleteResourceRoute = NamespacePrefix + "/resource/%s/%s/%s"
// buildRoute route to call BuildEndpoint
buildRoute = NamespacePrefix + "/build"
// uploadRoute route to the CDN UploadEndpoint
uploadRoute = "/_w/upload"
)
func assertNotEmpty(claims ...string) bool {
if len(claims) == 0 {
return false
}
for _, s := range claims {
if s == "" {
return false
}
}
return true
}
// CreateProduction invokes the CreateProductionEndpoint
func (cl *Client) CreateProduction(name, title, summary string) (*Production, error) {
if !cl.IsValid() {
return nil, errordef.ErrInvalidClientConfiguration
}
if name == "" {
return nil, errordef.ErrInvalidParameters
}
req := Production{
Name: name,
Title: title,
Summary: summary,
}
resp := Production{}
_, err := transport.Post(cl.opts.APIEndpoint, productionRoute, cl.opts.Token, &req, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
// Productions retrieves a list of productions
func (cl *Client) Productions() (*ProductionList, error) {
if !cl.IsValid() {
return nil, errordef.ErrInvalidClientConfiguration
}
var resp ProductionList
_, err := transport.Get(cl.opts.APIEndpoint, listProductionsRoute, cl.opts.Token, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
// CreateResource invokes the ResourceEndpoint
func (cl *Client) CreateResource(production, kind, guid string, force bool, rsrc interface{}) (int, error) {
if !cl.IsValid() {
return http.StatusBadRequest, errordef.ErrInvalidClientConfiguration
}
if !assertNotEmpty(production, kind, guid) {
return http.StatusBadRequest, errordef.ErrInvalidParameters
}
resp := api.StatusObject{}
status, err := transport.Post(cl.opts.APIEndpoint, fmt.Sprintf(updateResourceRoute, production, kind, guid, force), cl.opts.Token, rsrc, &resp)
if err != nil {
return status, err
}
return status, nil
}
// GetResource returns a resource file
func (cl *Client) GetResource(production, kind, guid string, rsrc interface{}) error {
if !cl.IsValid() {
return errordef.ErrInvalidClientConfiguration
}
if !assertNotEmpty(production, kind, guid) {
return errordef.ErrInvalidParameters
}
status, err := transport.Get(cl.opts.APIEndpoint, fmt.Sprintf(getResourceRoute, production, kind, guid), cl.opts.Token, rsrc)
if status == http.StatusBadRequest {
return fmt.Errorf(messagedef.MsgResourceNotFound, fmt.Sprintf("%s/%s-%s", production, kind, guid))
}
if err != nil {
return err
}
return nil
}
// FindResource returns a resource file
func (cl *Client) FindResource(guid string, rsrc interface{}) error {
if !cl.IsValid() {
return errordef.ErrInvalidClientConfiguration
}
if guid == "" {
return errordef.ErrInvalidParameters
}
status, err := transport.Get(cl.opts.APIEndpoint, fmt.Sprintf(findResourceRoute, guid), cl.opts.Token, rsrc)
if status == http.StatusBadRequest {
return fmt.Errorf(messagedef.MsgResourceNotFound, guid)
}
if err != nil {
return err
}
return nil
}
// Resources retrieves a list of resources
func (cl *Client) Resources(production, kind string) (*ResourceList, error) {
if !cl.IsValid() {
return nil, errordef.ErrInvalidClientConfiguration
}
if production == "" {
return nil, errordef.ErrInvalidParameters
}
if kind == "" {
kind = "ALL"
}
var resp ResourceList
_, err := transport.Get(cl.opts.APIEndpoint, fmt.Sprintf(listResourcesRoute, production, kind), cl.opts.Token, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
// UpdateResource invokes the ResourceEndpoint
func (cl *Client) UpdateResource(production, kind, guid string, force bool, rsrc interface{}) (int, error) {
if !cl.IsValid() {
return http.StatusBadRequest, errordef.ErrInvalidClientConfiguration
}
if !assertNotEmpty(production, kind, guid) {
return http.StatusBadRequest, errordef.ErrInvalidParameters
}
resp := api.StatusObject{}
status, err := transport.Put(cl.opts.APIEndpoint, fmt.Sprintf(updateResourceRoute, production, kind, guid, force), cl.opts.Token, rsrc, &resp)
if err != nil {
return status, err
}
return status, nil
}
// DeleteResource deletes a resources
func (cl *Client) DeleteResource(production, kind, guid string) (int, error) {
if !cl.IsValid() {
return http.StatusBadRequest, errordef.ErrInvalidClientConfiguration
}
if !assertNotEmpty(production, kind, guid) {
return http.StatusBadRequest, errordef.ErrInvalidParameters
}
status, err := transport.Delete(cl.opts.APIEndpoint, fmt.Sprintf(deleteResourceRoute, production, kind, guid), cl.opts.Token, nil)
if err != nil {
return status, err
}
return status, nil
}
// Build invokes the BuildEndpoint
func (cl *Client) Build(production string) (*BuildRequest, error) {
if !cl.IsValid() {
return nil, errordef.ErrInvalidClientConfiguration
}
if production == "" {
return nil, errordef.ErrInvalidParameters
}
req := BuildRequest{
GUID: production,
}
resp := BuildRequest{}
_, err := transport.Post(cl.opts.APIEndpoint, buildRoute, cl.opts.Token, &req, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
// Upload invokes the UploadEndpoint
func (cl *Client) Upload(production, path string, force bool) error {
if !cl.IsValid() {
return errordef.ErrInvalidClientConfiguration
}
if !assertNotEmpty(production, path) {
return errordef.ErrInvalidParameters
}
req, err := transport.Upload(cl.opts.CDNEndpoint, uploadRoute, cl.opts.Token, production, "asset", path)
if err != nil {
log.Fatal(err)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
body := &bytes.Buffer{}
_, err = body.ReadFrom(resp.Body)
if err != nil {
log.Fatal(err)
}
resp.Body.Close()
if resp.StatusCode > http.StatusNoContent {
return fmt.Errorf(messagedef.MsgResourceUploadError, fmt.Sprintf("%s:%s", path, resp.Status))
}
return nil
}