forked from cxpsemea/Cx1ClientGo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplications.go
More file actions
241 lines (196 loc) · 6.17 KB
/
Copy pathapplications.go
File metadata and controls
241 lines (196 loc) · 6.17 KB
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
package Cx1ClientGo
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
)
// Applications
func (c Cx1Client) GetApplications(limit uint) ([]Application, error) {
c.logger.Debug("Get Cx1 Applications")
var ApplicationResponse struct {
TotalCount uint64
FilteredCount uint64
Applications []Application
}
body := url.Values{
//"offset": {fmt.Sprintf("%d", 0)},
"limit": {fmt.Sprintf("%d", limit)},
//"sort": {"+created_at"},
}
response, err := c.sendRequest(http.MethodGet, fmt.Sprintf("/applications?%v", body.Encode()), nil, nil)
if err != nil {
return ApplicationResponse.Applications, err
}
err = json.Unmarshal(response, &ApplicationResponse)
c.logger.Tracef("Retrieved %d applications", len(ApplicationResponse.Applications))
return ApplicationResponse.Applications, err
}
func (c Cx1Client) GetApplicationById(id string) (Application, error) {
c.logger.Debugf("Get Cx1 Applications by id: %v", id)
var application Application
response, err := c.sendRequest(http.MethodGet, fmt.Sprintf("/applications/%v", id), nil, nil)
if err != nil {
return application, err
}
err = json.Unmarshal(response, &application)
return application, err
}
func (c Cx1Client) GetApplicationsByName(name string, limit uint64) ([]Application, error) {
c.logger.Debugf("Get Cx1 Applications by name: %v", name)
var ApplicationResponse struct {
TotalCount uint64
FilteredCount uint64
Applications []Application
}
body := url.Values{
//"offset": {fmt.Sprintf("%d", 0)},
"limit": {fmt.Sprintf("%d", limit)},
"name": {name},
}
response, err := c.sendRequest(http.MethodGet, fmt.Sprintf("/applications?%v", body.Encode()), nil, nil)
if err != nil {
return ApplicationResponse.Applications, err
}
err = json.Unmarshal(response, &ApplicationResponse)
c.logger.Tracef("Retrieved %d applications", len(ApplicationResponse.Applications))
return ApplicationResponse.Applications, err
}
func (c Cx1Client) GetApplicationByName(name string) (Application, error) {
apps, err := c.GetApplicationsByName(name, 0)
if err != nil {
return Application{}, err
}
for _, a := range apps {
if a.Name == name {
return a, nil
}
}
return Application{}, fmt.Errorf("no application found named %v", name)
}
func (c Cx1Client) CreateApplication(appname string) (Application, error) {
c.logger.Debugf("Create Application: %v", appname)
data := map[string]interface{}{
"name": appname,
"description": "",
"criticality": 3,
"rules": []ApplicationRule{},
"tags": map[string]string{},
}
var app Application
jsonBody, err := json.Marshal(data)
if err != nil {
return app, err
}
response, err := c.sendRequest(http.MethodPost, "/applications", bytes.NewReader(jsonBody), nil)
if err != nil {
c.logger.Tracef("Error while creating application: %s", err)
return app, err
}
err = json.Unmarshal(response, &app)
return app, err
}
func (c Cx1Client) DeleteApplication(applicationId string) error {
c.depwarn("DeleteApplication", "DeleteApplicationByID")
return c.DeleteApplicationByID(applicationId)
}
func (c Cx1Client) DeleteApplicationByID(applicationId string) error {
c.logger.Debugf("Delete Application: %v", applicationId)
_, err := c.sendRequest(http.MethodDelete, fmt.Sprintf("/applications/%v", applicationId), nil, nil)
if err != nil {
c.logger.Tracef("Error while deleting application: %s", err)
return err
}
return nil
}
// convenience
func (c Cx1Client) GetApplicationCount() (uint64, error) {
c.logger.Debug("Get Cx1 Projects")
var ApplicationResponse struct {
TotalCount uint64
FilteredTotalCount uint64
}
body := url.Values{
//"offset": {fmt.Sprintf("%d", 0)},
"limit": {fmt.Sprintf("%d", 1)},
//"sort": {"+created_at"},
}
response, err := c.sendRequest(http.MethodGet, fmt.Sprintf("/applications?%v", body.Encode()), nil, nil)
if err != nil {
return 0, err
}
err = json.Unmarshal(response, &ApplicationResponse)
return ApplicationResponse.TotalCount, err
}
func (c Cx1Client) GetApplicationCountByName(name string) (uint64, error) {
c.logger.Debug("Get Cx1 Projects")
var ApplicationResponse struct {
TotalCount uint64
FilteredTotalCount uint64
}
body := url.Values{
//"offset": {fmt.Sprintf("%d", 0)},
"limit": {fmt.Sprintf("%d", 1)},
"name": {name},
}
response, err := c.sendRequest(http.MethodGet, fmt.Sprintf("/applications?%v", body.Encode()), nil, nil)
if err != nil {
return 0, err
}
err = json.Unmarshal(response, &ApplicationResponse)
return ApplicationResponse.FilteredTotalCount, err
}
func (a *Application) String() string {
return fmt.Sprintf("[%v] %v", ShortenGUID(a.ApplicationID), a.Name)
}
func (c Cx1Client) GetOrCreateApplication(name string) (Application, error) {
c.depwarn("GetOrCreateApplication", "GetOrCreateApplicationByName")
return c.GetOrCreateApplicationByName(name)
}
func (c Cx1Client) GetOrCreateApplicationByName(name string) (Application, error) {
app, err := c.GetApplicationByName(name)
if err == nil {
return app, nil
}
return c.CreateApplication(name)
}
func (c Cx1Client) UpdateApplication(app *Application) error {
c.logger.Debugf("Update application: %v", app.String())
jsonBody, err := json.Marshal(*app)
if err != nil {
return err
}
_, err = c.sendRequest(http.MethodPut, fmt.Sprintf("/applications/%v", app.ApplicationID), bytes.NewReader(jsonBody), nil)
if err != nil {
c.logger.Tracef("Error while updating application: %s", err)
return err
}
return nil
}
func (a *Application) GetRuleByType(ruletype string) *ApplicationRule {
for id := range a.Rules {
if a.Rules[id].Type == ruletype {
return &(a.Rules[id])
}
}
return nil
}
func (a *Application) AddRule(ruletype, value string) {
rule := a.GetRuleByType(ruletype)
if rule == nil {
var newrule ApplicationRule
newrule.Type = ruletype
newrule.Value = value
a.Rules = append(a.Rules, newrule)
} else {
if rule.Value == value || strings.Contains(fmt.Sprintf(";%v;", rule.Value), fmt.Sprintf(";%v;", value)) {
return // rule value already contains this value
}
rule.Value = fmt.Sprintf("%v;%v", rule.Value, value)
}
}
func (a *Application) AssignProject(project *Project) {
a.AddRule("project.name.in", project.Name)
}