-
Notifications
You must be signed in to change notification settings - Fork 5
/
labels.go
218 lines (180 loc) · 6.49 KB
/
labels.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
package illumioapi
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strconv"
"strings"
)
// A Label represents an Illumio Label.
type Label struct {
CreatedAt string `json:"created_at,omitempty"`
CreatedBy *CreatedBy `json:"created_by,omitempty"`
Deleted bool `json:"deleted,omitempty"`
ExternalDataReference string `json:"external_data_reference,omitempty"`
ExternalDataSet string `json:"external_data_set,omitempty"`
Href string `json:"href,omitempty"`
Key string `json:"key,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
UpdatedBy *UpdatedBy `json:"updated_by,omitempty"`
Value string `json:"value,omitempty"`
}
// CreatedBy represents the CreatedBy property of an object
type CreatedBy struct {
Href string `json:"href"`
}
// UpdatedBy represents the UpdatedBy property of an object
type UpdatedBy struct {
Href string `json:"href"`
}
// GetAllLabels returns a slice of all Labels in the Illumio PCE.
// The first API call to the PCE does not use the async option.
// If the array length is >=500, it re-runs with async.
func (p *PCE) GetAllLabels() ([]Label, APIResponse, error) {
// Build the API URL
apiURL, err := url.Parse("https://" + pceSanitization(p.FQDN) + ":" + strconv.Itoa(p.Port) + "/api/v2/orgs/" + strconv.Itoa(p.Org) + "/labels")
if err != nil {
return nil, APIResponse{}, fmt.Errorf("get all labels - %s", err)
}
// Call the API
api, err := apicall("GET", apiURL.String(), *p, nil, false)
if err != nil {
return nil, api, fmt.Errorf("get all workloads - %s", err)
}
// Unmarshal response to struct
var labels []Label
json.Unmarshal([]byte(api.RespBody), &labels)
// If length is 500, re-run with async
if len(labels) >= 500 {
api, err = apicall("GET", apiURL.String(), *p, nil, true)
if err != nil {
return nil, api, fmt.Errorf("get all workloads - %s", err)
}
// Unmarshal response to struct
var asyncLabels []Label
json.Unmarshal([]byte(api.RespBody), &asyncLabels)
return asyncLabels, api, nil
}
// Return if less than 500
return labels, api, nil
}
// GetLabelMaps returns a map of labels with the HREF as the key
func (p *PCE) GetLabelMaps() (APIResponse, error) {
labels, apiResp, err := p.GetAllLabels()
if err != nil {
return apiResp, fmt.Errorf("get href label map - %s", err)
}
p.LabelMapH = make(map[string]Label)
p.LabelMapKV = make(map[string]Label)
for _, l := range labels {
p.LabelMapH[l.Href] = l
p.LabelMapKV[l.Key+l.Value] = l
}
return apiResp, nil
}
// GetLabelbyKeyValue finds a label based on the key and value.
// It will only return one Label that is an exact match.
func (p *PCE) GetLabelbyKeyValue(key, value string) (Label, APIResponse, error) {
var l Label
var labels []Label
var api APIResponse
// Build the API URL and Query Parameters
apiURL, err := url.Parse("https://" + pceSanitization(p.FQDN) + ":" + strconv.Itoa(p.Port) + "/api/v2/orgs/" + strconv.Itoa(p.Org) + "/labels")
if err != nil {
return l, api, fmt.Errorf("get label - %s", err)
}
q := apiURL.Query()
q.Set("key", key)
q.Set("value", value)
apiURL.RawQuery = q.Encode()
// Call the API
api, err = apicall("GET", apiURL.String(), *p, nil, false)
if err != nil {
return l, api, fmt.Errorf("get label - %s", err)
}
// Unmarshal respones to struct
json.Unmarshal([]byte(api.RespBody), &labels)
//Illumio API returns any label that contains the search team. We need exact
for _, label := range labels {
if label.Value == value {
return label, api, nil
}
}
// If we reach here, a label doesn't exist - return an emtpy label struct and no error
return l, api, nil
}
// GetLabelbyHref returns a label based on the provided HREF.
func (p *PCE) GetLabelbyHref(href string) (Label, APIResponse, error) {
var l Label
var api APIResponse
// Build the API URL and Query Parameters
apiURL, err := url.Parse("https://" + pceSanitization(p.FQDN) + ":" + strconv.Itoa(p.Port) + "/api/v2/" + href)
if err != nil {
return l, api, fmt.Errorf("get label by href - %s", err)
}
// Call the API
api, err = apicall("GET", apiURL.String(), *p, nil, false)
if err != nil {
return l, api, fmt.Errorf("get label by href- %s", err)
}
// Unmarshal respones to struct
json.Unmarshal([]byte(api.RespBody), &l)
// If we reach here, a label doesn't exist - return an emtpy label struct and no error
return l, api, nil
}
// CreateLabel creates a new Label in the Illumio PCE.
func (p *PCE) CreateLabel(label Label) (Label, APIResponse, error) {
var newLabel Label
var api APIResponse
var err error
// Check to make sure the label key is valid
label.Key = strings.ToLower(label.Key)
if label.Key != "app" && label.Key != "env" && label.Key != "role" && label.Key != "loc" {
return newLabel, api, errors.New("label key is not app, env, role, or loc")
}
// Build the API URL
apiURL, err := url.Parse("https://" + pceSanitization(p.FQDN) + ":" + strconv.Itoa(p.Port) + "/api/v2/orgs/" + strconv.Itoa(p.Org) + "/labels")
if err != nil {
return newLabel, api, fmt.Errorf("create label - %s", err)
}
// Create payload
labelJSON, err := json.Marshal(label)
if err != nil {
return newLabel, api, fmt.Errorf("create label - %s", err)
}
api.ReqBody = string(labelJSON)
// Call the API
api, err = apicall("POST", apiURL.String(), *p, labelJSON, false)
if err != nil {
return newLabel, api, fmt.Errorf("create label - %s", err)
}
// Unmarshal new label
json.Unmarshal([]byte(api.RespBody), &newLabel)
return newLabel, api, nil
}
// UpdateLabel updates an existing label in the Illumio PCE.
// The provided label struct must include an Href.
// Properties that cannot be included in the PUT method will be ignored.
func (p *PCE) UpdateLabel(label Label) (APIResponse, error) {
var api APIResponse
var err error
// Build the API URL
apiURL, err := url.Parse("https://" + pceSanitization(p.FQDN) + ":" + strconv.Itoa(p.Port) + "/api/v2" + label.Href)
if err != nil {
return api, fmt.Errorf("update label - %s", err)
}
// Create a new label with just the fields that should be updated
l := Label{Value: label.Value, ExternalDataReference: label.ExternalDataReference, ExternalDataSet: label.ExternalDataSet}
// Call the API
labelJSON, err := json.Marshal(l)
if err != nil {
return api, fmt.Errorf("update label - %s", err)
}
api.ReqBody = string(labelJSON)
api, err = apicall("PUT", apiURL.String(), *p, labelJSON, false)
if err != nil {
return api, fmt.Errorf("update label - %s", err)
}
return api, nil
}