-
Notifications
You must be signed in to change notification settings - Fork 5
/
labelgroups.go
199 lines (169 loc) · 6.19 KB
/
labelgroups.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
package illumioapi
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strconv"
"strings"
)
// LabelGroup represents a Label Group in the Illumio PCE
type LabelGroup struct {
Description string `json:"description,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"`
Labels []*Label `json:"labels,omitempty"`
Name string `json:"name,omitempty"`
SubGroups []*SubGroups `json:"sub_groups,omitempty"`
Usage *Usage `json:"usage,omitempty"`
}
// SubGroups represent SubGroups for Label Groups
type SubGroups struct {
Href string `json:"href"`
Name string `json:"name,omitempty"`
}
// Usage covers how a LabelGroup is used in the PCE
type Usage struct {
LabelGroup bool `json:"label_group"`
Rule bool `json:"rule"`
Ruleset bool `json:"ruleset"`
StaticPolicyScopes bool `json:"static_policy_scopes,omitempty"`
}
// GetAllLabelGroups returns a slice of all Label Groups of a
// specific provision status in the Illumio PCE.
//
// The pvoision status must be "draft" or "active".
// The first call does not use the async option.
// If the response array length is >=500, it is re-run enabling async.
func (p *PCE) GetAllLabelGroups(provisionStatus string) ([]LabelGroup, APIResponse, error) {
provisionStatus = strings.ToLower(provisionStatus)
if provisionStatus != "active" && provisionStatus != "draft" {
return nil, APIResponse{}, errors.New("get all label groups - provisionStatus must be active or draft")
}
// Build the API URL
apiURL, err := url.Parse("https://" + pceSanitization(p.FQDN) + ":" + strconv.Itoa(p.Port) + "/api/v2/orgs/" + strconv.Itoa(p.Org) + "/sec_policy/" + provisionStatus + "/label_groups")
if err != nil {
return nil, APIResponse{}, fmt.Errorf("get all label groups - %s", err)
}
// Call the API
api, err := apicall("GET", apiURL.String(), *p, nil, false)
if err != nil {
return nil, api, fmt.Errorf("get all label groups - %s", err)
}
var labelGroups []LabelGroup
json.Unmarshal([]byte(api.RespBody), &labelGroups)
// If length is 500, re-run with async
if len(labelGroups) >= 500 {
api, err = apicall("GET", apiURL.String(), *p, nil, true)
if err != nil {
return nil, api, fmt.Errorf("get all label groups - %s", err)
}
// Unmarshal response to struct
var asyncLabelGroups []LabelGroup
json.Unmarshal([]byte(api.RespBody), &asyncLabelGroups)
return asyncLabelGroups, api, nil
}
// Return if less than 500
return labelGroups, api, nil
}
// CreateLabelGroup creates a new Label Group in the Illumio PCE.
//
// The function will remove properties not in the POST schema
func (p *PCE) CreateLabelGroup(labelGroup LabelGroup) (LabelGroup, APIResponse, error) {
var newLabelGroup LabelGroup
var api APIResponse
var err error
// Sanitize the Label Group
labelGroup.Href = ""
labelGroup.Usage = nil
// Build the API URL
apiURL, err := url.Parse("https://" + pceSanitization(p.FQDN) + ":" + strconv.Itoa(p.Port) + "/api/v2/orgs/" + strconv.Itoa(p.Org) + "/sec_policy/draft/label_groups")
if err != nil {
return newLabelGroup, api, fmt.Errorf("create label group - %s", err)
}
// Call the API
labelGroupJSON, err := json.Marshal(labelGroup)
if err != nil {
return newLabelGroup, api, fmt.Errorf("create label group - %s", err)
}
api.ReqBody = string(labelGroupJSON)
api, err = apicall("POST", apiURL.String(), *p, labelGroupJSON, false)
if err != nil {
return newLabelGroup, api, fmt.Errorf("create label group - %s", err)
}
// Unmarshal response to struct
json.Unmarshal([]byte(api.RespBody), &newLabelGroup)
return newLabelGroup, api, nil
}
// UpdateLabelGroup updates an existing Label Group in the Illumio PCE.
//
// The provided Label Group struct must include an Href.
// The function will remove properties not included in the PUT schema.
func (p *PCE) UpdateLabelGroup(labelGroup LabelGroup) (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" + labelGroup.Href)
if err != nil {
return api, fmt.Errorf("update label group - %s", err)
}
// Remove fields that should be empty for the PUT schema
labelGroup.Href = ""
labelGroup.Usage = nil
labelGroup.Key = ""
// Marshal JSON
labelGroupJSON, err := json.Marshal(labelGroup)
if err != nil {
return api, fmt.Errorf("update label group - %s", err)
}
api.ReqBody = string(labelGroupJSON)
// Call the API
api, err = apicall("PUT", apiURL.String(), *p, labelGroupJSON, false)
if err != nil {
return api, fmt.Errorf("update label group - %s", err)
}
return api, nil
}
// ExpandLabelGroup returns a string of label hrefs in a label group
// Every subgroup (and nested subgroup) is expanded
func (p *PCE) ExpandLabelGroup(href string) (labelHrefs []string) {
// Get the labels from the original label group
a, _ := p.expandLabelGroup(href)
labelHrefs = append(labelHrefs, a...)
// Iterate through the subgroups of the original label group
for _, sg := range p.LabelGroups[href].SubGroups {
// Get the labels in that subgroup and the additional subgroups
l, moreSGs := p.expandLabelGroup(sg.Href)
// Append the labels
labelHrefs = append(labelHrefs, l...)
// While there are more subgroups, continue expanding them
for len(moreSGs) > 0 {
for _, newSG := range moreSGs {
l, moreSGs = p.expandLabelGroup(newSG)
// Append the labels
labelHrefs = append(labelHrefs, l...)
}
}
}
// De-dupe and return
labelGroupMap := make(map[string]bool)
for _, l := range labelHrefs {
labelGroupMap[l] = true
}
labelHrefs = []string{}
for l := range labelGroupMap {
labelHrefs = append(labelHrefs, l)
}
return labelHrefs
}
func (p *PCE) expandLabelGroup(href string) (labelHrefs []string, moreSGs []string) {
for _, l := range p.LabelGroups[href].Labels {
labelHrefs = append(labelHrefs, l.Href)
}
for _, sg := range p.LabelGroups[href].SubGroups {
moreSGs = append(moreSGs, sg.Href)
}
return labelHrefs, moreSGs
}