/
template.go
272 lines (230 loc) · 6.27 KB
/
template.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
261
262
263
264
265
266
267
268
269
270
271
272
package sdk
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"mime/multipart"
"os"
"path/filepath"
)
// TemplateParam can be a String/Date/Script/URL...
type TemplateParam struct {
ID int64 `json:"id" yaml:"-"`
Name string `json:"name"`
Type VariableType `json:"type"`
Value string `json:"value"`
Description string `json:"description" yaml:"desc,omitempty"`
}
// Template definition to help users bootstrap their pipelines
type Template struct {
ID int64 `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Params []TemplateParam `json:"params"`
Hook bool `json:"hook"`
}
// GetBuildTemplate Get the build template corresponding to the given name
func GetBuildTemplate(name string) (*Template, error) {
tpls, err := GetBuildTemplates()
if err != nil {
return nil, err
}
for _, t := range tpls {
if t.Name == name {
return &t, nil
}
}
return nil, fmt.Errorf("%s: not found", err)
}
// GetBuildTemplates retrieves all existing build template from API
func GetBuildTemplates() ([]Template, error) {
uri := "/template/build"
data, code, err := Request("GET", uri, nil)
if err != nil {
return nil, err
}
if code >= 300 {
return nil, fmt.Errorf("HTTP %d", code)
}
var tmpls []Template
err = json.Unmarshal(data, &tmpls)
if err != nil {
return nil, err
}
return tmpls, nil
}
// GetDeploymentTemplates retrieves all existing deployment template from API
func GetDeploymentTemplates() ([]Template, error) {
uri := "/template/deploy"
data, code, err := Request("GET", uri, nil)
if err != nil {
return nil, err
}
if code >= 300 {
return nil, fmt.Errorf("HTTP %d", code)
}
var tmpls []Template
err = json.Unmarshal(data, &tmpls)
if err != nil {
return nil, err
}
return tmpls, nil
}
// ApplyApplicationTemplate creates given application and apply build template
func ApplyApplicationTemplate(projectKey string, name string, build Template) (*Application, error) {
uri := fmt.Sprintf("/project/%s/template", projectKey)
opts := ApplyTemplatesOptions{
ApplicationName: name,
TemplateName: build.Name,
TemplateParams: build.Params,
}
btes, _ := json.Marshal(opts)
data, code, err := Request("POST", uri, btes)
if err != nil {
return nil, err
}
if code >= 300 {
return nil, DecodeError(data)
}
app, err := GetApplication(projectKey, name)
if err != nil {
return nil, err
}
return app, nil
}
//TemplateExtension represents a template store as a binary extension
type TemplateExtension struct {
ID int64 `json:"id" db:"id"`
Name string `json:"name" db:"name"`
Type string `json:"type" db:"type"`
Author string `json:"author" db:"author"`
Description string `json:"description" db:"description"`
Identifier string `json:"identifier" db:"identifier"`
Size int64 `json:"-" db:"size"`
Perm uint32 `json:"-" db:"perm"`
MD5Sum string `json:"md5sum" db:"md5sum"`
ObjectPath string `json:"-" db:"object_path"`
Filename string `json:"-" db:"-"`
Path string `json:"-" db:"-"`
Params []TemplateParam `json:"params" db:"-"`
Actions []string `json:"actions" db:"-"`
}
//ApplyTemplatesOptions represents arguments to create an application and all its components from templates
type ApplyTemplatesOptions struct {
ApplicationName string `json:"name"`
TemplateName string `json:"template"`
TemplateParams []TemplateParam `json:"template_params"`
}
//GetName returns the name of the template extension
func (a *TemplateExtension) GetName() string {
return a.Name
}
//GetPath returns the storage path of the template extension
func (a *TemplateExtension) GetPath() string {
return fmt.Sprintf("templates")
}
//UploadTemplate uploads binary file to perform a new action
func UploadTemplate(filePath string, update bool, name string) ([]byte, error) {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return nil, err
}
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("UploadFile", filepath.Base(filePath))
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
err = writer.Close()
if err != nil {
return nil, err
}
path := "/template/add"
method := "POST"
if update {
method = "PUT"
path = "/template/"
if verbose {
log.Println("Getting templates list")
}
btes, _, errrequest := Request("GET", "/template", nil)
if errrequest != nil {
return nil, errrequest
}
tmpls := []TemplateExtension{}
if errjson := json.Unmarshal(btes, &tmpls); errjson != nil {
return nil, errjson
}
if verbose {
log.Println("Getting templates list : OK")
}
var found bool
for _, t := range tmpls {
if t.Name == name {
path += fmt.Sprintf("%d", t.ID)
found = true
break
}
}
if !found {
return nil, fmt.Errorf("Template %s not found", name)
}
if verbose {
log.Printf("Found template at %s\n", path)
}
}
btes, code, err := UploadMultiPart(method, path, body, SetHeader("uploadfile", filePath), SetHeader("Content-Type", writer.FormDataContentType()))
if err != nil {
return nil, err
}
if code >= 300 {
return nil, fmt.Errorf("HTTP Error %d\n", code)
}
return btes, nil
}
//DeleteTemplate delete Template
func DeleteTemplate(name string) error {
tmpls, err := ListTemplates()
if err != nil {
return err
}
var id int64
var found bool
for _, t := range tmpls {
if t.Name == name {
id = t.ID
found = true
break
}
}
if !found {
return fmt.Errorf("Unable to found template %s", name)
}
path := fmt.Sprintf("/template/%d", id)
if _, _, err := Request("DELETE", path, nil); err != nil {
return err
}
return nil
}
//ListTemplates returns all templates
func ListTemplates() ([]TemplateExtension, error) {
tmpls := []TemplateExtension{}
body, code, err := Request("GET", "/template", nil)
if err != nil {
return nil, err
}
if code >= 300 {
return nil, fmt.Errorf("HTTP Error %d\n", code)
}
if err := json.Unmarshal(body, &tmpls); err != nil {
return nil, err
}
return tmpls, nil
}