-
Notifications
You must be signed in to change notification settings - Fork 9
/
formation.go
148 lines (127 loc) · 3.44 KB
/
formation.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
package cloud66
import (
"fmt"
"strconv"
"strings"
"time"
)
type Formation struct {
Uid string `json:"uid"`
Name string `json:"name"`
Stencils []Stencil `json:"stencils"`
BaseTemplates []BaseTemplate `json:"base_templates"`
Policies []Policy `json:"policies"`
Transformations []Transformation `json:"transformations"`
Workflows []Workflow `json:"workflows"`
HelmReleases []HelmRelease `json:"helm_releases"`
FormationFilters []FormationFilter `json:"filters"`
CreatedAt time.Time `json:"created_at_iso"`
UpdatedAt time.Time `json:"updated_at_iso"`
Tags []string `json:"tags"`
}
func (f *Formation) FindStencil(stencilName string) *Stencil {
for _, stencil := range f.Stencils {
if stencil.Filename == stencilName {
return &stencil
}
}
return nil
}
func (c *Client) Formations(stackUid string, fullContent bool) ([]Formation, error) {
queryStrings := make(map[string]string)
queryStrings["page"] = "1"
if fullContent {
queryStrings["full_content"] = "1"
}
var p Pagination
var result []Formation
var formationRes []Formation
for {
req, err := c.NewRequest("GET", fmt.Sprintf("/stacks/%s/formations.json", stackUid), nil, queryStrings)
if err != nil {
return nil, err
}
formationRes = nil
err = c.DoReq(req, &formationRes, &p)
if err != nil {
return nil, err
}
result = append(result, formationRes...)
if p.Current < p.Next {
queryStrings["page"] = strconv.Itoa(p.Next)
} else {
break
}
}
return result, nil
}
func (c *Client) CreateFormation(stackUid string, name string, templateRepo string, templateBranch string, tags []string) (*Formation, error) {
type base struct {
Repo string `json:"repo"`
Branch string `json:"branch"`
}
params := struct {
Base base `json:"base_template"`
Name string `json:"name"`
Tags []string `json:"tags"`
}{
Name: name,
Tags: tags,
}
params.Base = base{
Repo: templateRepo,
Branch: templateBranch,
}
req, err := c.NewRequest("POST", "/stacks/"+stackUid+"/formations.json", params, nil)
if err != nil {
return nil, err
}
var formationRes *Formation
err = c.DoReq(req, &formationRes, nil)
if err != nil {
return nil, err
}
return formationRes, nil
}
func (c *Client) CreateFormationMultiBtr(stackUid string, name string, baseTemplates []*BaseTemplate, tags []string) (*Formation, error) {
type base struct {
Repo string `json:"repo"`
Branch string `json:"branch"`
}
params := struct {
Base []base `json:"base_templates"`
Name string `json:"name"`
Tags []string `json:"tags"`
}{
Name: name,
Tags: tags,
}
baseList := make([]base, 0)
for _, value := range baseTemplates {
baseList = append(baseList, base{
Repo: value.GitRepo,
Branch: value.GitBranch,
})
}
params.Base = baseList
req, err := c.NewRequest("POST", "/stacks/"+stackUid+"/formations.json", params, nil)
if err != nil {
return nil, err
}
var formationRes *Formation
err = c.DoReq(req, &formationRes, nil)
if err != nil {
return nil, err
}
return formationRes, nil
}
func (f *Formation) FindIndexByRepoAndBranch(repo string, branch string) int {
repo = strings.TrimSpace(repo)
branch = strings.TrimSpace(branch)
for index, btr := range f.BaseTemplates {
if strings.TrimSpace(btr.GitRepo) == repo && strings.TrimSpace(btr.GitBranch) == branch {
return index
}
}
return -1
}