-
Notifications
You must be signed in to change notification settings - Fork 29
/
service_manager.go
247 lines (197 loc) · 5.45 KB
/
service_manager.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
package client
import (
"errors"
"fmt"
"log"
"strconv"
"github.com/ciscoecosystem/aci-go-client/container"
"github.com/ciscoecosystem/aci-go-client/models"
)
type ServiceManager struct {
MOURL string
client *Client
}
func NewServiceManager(moURL string, client *Client) *ServiceManager {
sm := &ServiceManager{
MOURL: moURL,
client: client,
}
return sm
}
func (sm *ServiceManager) Get(dn string) (*container.Container, error) {
finalURL := fmt.Sprintf("%s/%s.json", sm.MOURL, dn)
req, err := sm.client.MakeRestRequest("GET", finalURL, nil, true)
if err != nil {
return nil, err
}
obj, _, err := sm.client.Do(req)
if err != nil {
return nil, err
}
if obj == nil {
return nil, errors.New("Empty response body")
}
log.Printf("[DEBUG] Exit from GET %s", finalURL)
return obj, CheckForErrors(obj, "GET", sm.client.skipLoggingPayload)
}
func createJsonPayload(payload map[string]string) (*container.Container, error) {
containerJSON := []byte(fmt.Sprintf(`{
"%s": {
"attributes": {
}
}
}`, payload["classname"]))
return container.ParseJSON(containerJSON)
}
func (sm *ServiceManager) Save(obj models.Model) error {
jsonPayload, _, err := sm.PrepareModel(obj)
if err != nil {
return err
}
req, err := sm.client.MakeRestRequest("POST", fmt.Sprintf("%s.json", sm.MOURL), jsonPayload, true)
if err != nil {
return err
}
cont, _, err := sm.client.Do(req)
if err != nil {
return err
}
return CheckForErrors(cont, "POST", sm.client.skipLoggingPayload)
}
// CheckForErrors parses the response and checks of there is an error attribute in the response
func CheckForErrors(cont *container.Container, method string, skipLoggingPayload bool) error {
number, err := strconv.Atoi(models.G(cont, "totalCount"))
if err != nil {
if !skipLoggingPayload {
log.Printf("[DEBUG] Exit from errors, Unable to parse error count from response %v", cont)
} else {
log.Printf("[DEBUG] Exit from errors %s", err.Error())
}
return err
}
imdata := cont.S("imdata").Index(0)
if number > 0 {
if imdata.Exists("error") {
if models.StripQuotes(imdata.Path("error.attributes.code").String()) == "103" {
if !skipLoggingPayload {
log.Printf("[DEBUG] Exit from error 103 %v", cont)
}
return nil
} else {
if models.StripQuotes(imdata.Path("error.attributes.text").String()) == "" && models.StripQuotes(imdata.Path("error.attributes.code").String()) == "403" {
if !skipLoggingPayload {
log.Printf("[DEBUG] Exit from authentication error 403 %v", cont)
}
return errors.New("Unable to authenticate. Please check your credentials")
}
if !skipLoggingPayload {
log.Printf("[DEBUG] Exit from errors %v", cont)
}
return errors.New(models.StripQuotes(imdata.Path("error.attributes.text").String()))
}
}
}
if imdata.String() == "{}" && method == "GET" {
if !skipLoggingPayload {
log.Printf("[DEBUG] Exit from error (Empty response) %v", cont)
}
return errors.New("Error retriving Object: Object may not exists")
}
if !skipLoggingPayload {
log.Printf("[DEBUG] Exit from errors %v", cont)
}
return nil
}
func (sm *ServiceManager) Delete(obj models.Model) error {
jsonPayload, className, err := sm.PrepareModel(obj)
if err != nil {
return err
}
jsonPayload.Set("deleted", className, "attributes", "status")
req, err := sm.client.MakeRestRequest("POST", fmt.Sprintf("%s.json", sm.MOURL), jsonPayload, true)
if err != nil {
return err
}
_, _, err = sm.client.Do(req)
if err != nil {
return err
}
return nil
}
func (sm *ServiceManager) PostViaURL(url string, obj models.Model) (*container.Container, error) {
jsonPayload, _, err := sm.PrepareModel(obj)
if err != nil {
return nil, err
}
req, err := sm.client.MakeRestRequest("POST", url, jsonPayload, true)
if err != nil {
return nil, err
}
cont, _, err := sm.client.Do(req)
if !sm.client.skipLoggingPayload {
log.Printf("PostViaUrl %+v", obj)
}
if err != nil {
return nil, err
}
if cont == nil {
return nil, errors.New("Empty response body")
}
return cont, CheckForErrors(cont, "POST", sm.client.skipLoggingPayload)
}
func (sm *ServiceManager) GetViaURL(url string) (*container.Container, error) {
req, err := sm.client.MakeRestRequest("GET", url, nil, true)
if err != nil {
return nil, err
}
obj, _, err := sm.client.Do(req)
if !sm.client.skipLoggingPayload {
log.Printf("Getvia url %+v", obj)
}
if err != nil {
return nil, err
}
if obj == nil {
return nil, errors.New("Empty response body")
}
return obj, CheckForErrors(obj, "GET", sm.client.skipLoggingPayload)
}
func (sm *ServiceManager) DeleteByDn(dn, className string) error {
containerJSON := []byte(fmt.Sprintf(`{
"%s": {
"attributes": {
"dn": "%s",
"status": "deleted"
}
}
}`, className, dn))
jsonPayload, err := container.ParseJSON(containerJSON)
if err != nil {
return err
}
req, err := sm.client.MakeRestRequest("POST", fmt.Sprintf("%s.json", sm.MOURL), jsonPayload, true)
if err != nil {
return err
}
_, _, err = sm.client.Do(req)
if err != nil {
return err
}
return nil
}
func (sm *ServiceManager) PrepareModel(obj models.Model) (*container.Container, string, error) {
cont, err := obj.ToMap()
if err != nil {
return nil, "", err
}
jsonPayload, err := createJsonPayload(cont)
if err != nil {
return nil, "", err
}
className := cont["classname"]
delete(cont, "classname")
for key, value := range cont {
jsonPayload.Set(value, className, "attributes", key)
}
return jsonPayload, className, nil
}