-
Notifications
You must be signed in to change notification settings - Fork 5
/
crud.go
138 lines (109 loc) · 3.46 KB
/
crud.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
package illumioapi
import (
"encoding/json"
"fmt"
"net/url"
"reflect"
"strconv"
)
// GetHref returns the Illumio object with a specific href
func (p *PCE) GetHref(href string, response interface{}) (APIResponse, error) {
var api APIResponse
// Build the API URL
apiURL, err := url.Parse("https://" + p.cleanFQDN() + ":" + strconv.Itoa(p.Port) + "/api/v2" + href)
if err != nil {
return api, err
}
// Call the API
api, err = p.httpReq("GET", apiURL.String(), nil, false, true)
if err != nil {
return api, err
}
err = json.Unmarshal([]byte(api.RespBody), &response)
return api, err
}
// SyncGet returns a collection of Illumio objects
func (p *PCE) GetCollection(endpoint string, async bool, queryParameters map[string]string, response interface{}) (APIResponse, error) {
// Build the API URL
url, err := url.Parse("https://" + p.cleanFQDN() + ":" + strconv.Itoa(p.Port) + "/api/v2/orgs/" + strconv.Itoa(p.Org) + "/" + endpoint)
if err != nil {
return APIResponse{}, err
}
// Set the query parameters
for key, value := range queryParameters {
q := url.Query()
q.Set(key, value)
url.RawQuery = q.Encode()
}
// Call the API
api, err := p.httpReq("GET", url.String(), nil, async, true)
if err != nil {
return api, fmt.Errorf("get %s - %s", endpoint, err)
}
// Unmarshal response to struct and return
json.Unmarshal([]byte(api.RespBody), &response)
return api, nil
}
// Post sends a POST request to the PCE
func (p *PCE) Post(endpoint string, object, createdObject interface{}) (api APIResponse, err error) {
// Build the API URL
apiURL, err := url.Parse("https://" + p.cleanFQDN() + ":" + strconv.Itoa(p.Port) + "/api/v2/orgs/" + strconv.Itoa(p.Org) + "/" + endpoint)
if err != nil {
return api, err
}
// Create payload
jsonBytes, err := json.Marshal(object)
if err != nil {
return api, err
}
// Call the API
api, err = p.httpReq("POST", apiURL.String(), jsonBytes, false, true)
if err != nil {
return api, err
}
// Add the request body to the API object
api.ReqBody = string(jsonBytes)
// Unmarshal new label
json.Unmarshal([]byte(api.RespBody), &createdObject)
return api, nil
}
// Put sends a PUT request to the PCE.
// The object must include an Href field.
func (p *PCE) Put(object interface{}) (api APIResponse, err error) {
// Build the API URL
href := reflect.ValueOf(object).Elem().FieldByName("Href").Interface().(string)
apiURL, err := url.Parse("https://" + p.cleanFQDN() + ":" + strconv.Itoa(p.Port) + "/api/v2" + href)
if err != nil {
return api, fmt.Errorf("update label - %s", err)
}
// Drop the HREF from the object
reflect.ValueOf(object).Elem().FieldByName("Href").SetString("")
// Create the payload
jsonBytes, err := json.Marshal(object)
if err != nil {
return api, err
}
// Call the API
api, err = p.httpReq("PUT", apiURL.String(), jsonBytes, false, true)
if err != nil {
return api, err
}
// Add the request body to the API object
api.ReqBody = string(jsonBytes)
return api, nil
}
// DeleteHref deletes an existing object in the PCE based on its href.
func (p *PCE) DeleteHref(href string) (APIResponse, error) {
var api APIResponse
// Build the API URL
apiURL, err := url.Parse("https://" + p.cleanFQDN() + ":" + strconv.Itoa(p.Port) + "/api/v2" + href)
if err != nil {
return api, fmt.Errorf("delete href - %s", err)
}
// Call the API
api, err = p.httpReq("DELETE", apiURL.String(), nil, false, true)
if err != nil {
return api, fmt.Errorf("delete href - %s", err)
}
return api, nil
}