-
Notifications
You must be signed in to change notification settings - Fork 4
/
containerworkloadprofiles.go
103 lines (88 loc) · 4.12 KB
/
containerworkloadprofiles.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
package illumioapi
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
)
type ContainerWorkloadProfileAssignLabel struct {
Href string `json:"href,omitempty"`
}
type ContainerWorkloadProfileLabel struct {
Key string `json:"key,omitempty"`
Assignment ContainerWorkloadProfileLabelAssignment `json:"assignment,omitempty"`
}
type ContainerWorkloadProfileLabelAssignment struct {
Href string `json:"href,omitempty"`
Value string `json:"value,omitempty"`
}
// ContainerWorkloadProfile represents a container workload profile in the Illumio PCE
type ContainerWorkloadProfile struct {
Href string `json:"href,omitempty"`
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
Description string `json:"description,omitempty"`
EnforcementMode string `json:"enforcement_mode,omitempty"`
VisibilityLevel string `json:"visibility_level,omitempty"`
Managed *bool `json:"managed,omitempty"`
Linked *bool `json:"linked,omitempty"`
AssignLabels []ContainerWorkloadProfileAssignLabel `json:"assign_labels,omitempty"`
Labels []ContainerWorkloadProfileLabel `json:"labels,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
CreatedBy *CreatedBy `json:"created_by,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
UpdatedBy *UpdatedBy `json:"updated_by,omitempty"`
}
// GetAllContainerWorkloadProfiles returns a slice of ContainerWorkloadProfiles 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.
// QueryParameters can be passed as a map of [key]=value
func (p *PCE) GetAllContainerWorkloadProfiles(queryParameters map[string]string, containerClusterID string) ([]ContainerWorkloadProfile, APIResponse, error) {
var api APIResponse
// Build the API URL
apiURL, err := url.Parse("https://" + pceSanitization(p.FQDN) + ":" + strconv.Itoa(p.Port) + "/api/v2/orgs/" + strconv.Itoa(p.Org) + "/container_clusters/" + containerClusterID + "/container_workload_profiles")
if err != nil {
return nil, api, fmt.Errorf("get all container workload profiles - %s", err)
}
// Set the query parameters
for key, value := range queryParameters {
q := apiURL.Query()
q.Set(key, value)
apiURL.RawQuery = q.Encode()
}
// Call the API
api, err = apicall("GET", apiURL.String(), *p, nil, false)
if err != nil {
return nil, api, fmt.Errorf("get all container workload profiles - %s", err)
}
var containerWorkloadProfiles []ContainerWorkloadProfile
json.Unmarshal([]byte(api.RespBody), &containerWorkloadProfiles)
// Set up the VEN map
p.ContainerWorkloadProfiles = make(map[string]ContainerWorkloadProfile)
// If length is 500, re-run with async
if len(containerWorkloadProfiles) >= 500 {
// Call async
api, err = apicall("GET", apiURL.String(), *p, nil, true)
if err != nil {
return nil, api, fmt.Errorf("get all container clusters - %s", err)
}
// Unmarshal response to asyncWklds and return
var asyncContainerWorkloadProfiles []ContainerWorkloadProfile
json.Unmarshal([]byte(api.RespBody), &asyncContainerWorkloadProfiles)
// Load the PCE with the returned workloads
for _, c := range asyncContainerWorkloadProfiles {
p.ContainerWorkloadProfiles[c.Href] = c
p.ContainerWorkloadProfiles[c.Name] = c
}
p.ContainerWorkloadProfilesSlice = asyncContainerWorkloadProfiles
return asyncContainerWorkloadProfiles, api, nil
}
// Load the PCE with the returned workloads
for _, c := range containerWorkloadProfiles {
p.ContainerWorkloadProfiles[c.Href] = c
p.ContainerWorkloadProfiles[c.Name] = c
}
p.ContainerWorkloadProfilesSlice = containerWorkloadProfiles
// Return if less than 500
return containerWorkloadProfiles, api, nil
}