-
Notifications
You must be signed in to change notification settings - Fork 5
/
provisioning.go
157 lines (135 loc) · 4.62 KB
/
provisioning.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
package illumioapi
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"strings"
)
// ChangeSubset Hash of pending hrefs, organized by model
type ChangeSubset struct {
FirewallSettings []*FirewallSettings `json:"firewall_settings,omitempty"`
IPLists []*IPList `json:"ip_lists,omitempty"`
LabelGroups []*LabelGroup `json:"label_groups,omitempty"`
RuleSets []*RuleSet `json:"rule_sets,omitempty"`
SecureConnectGateways []*SecureConnectGateways `json:"secure_connect_gateways,omitempty"`
Services []*Service `json:"services,omitempty"`
VirtualServers []*VirtualServer `json:"virtual_servers,omitempty"`
VirtualServices []*VirtualService `json:"virtual_services,omitempty"`
}
// FirewallSettings are a provisionable object
type FirewallSettings struct {
Href string `json:"href"`
}
// Provision is sent to the PCE to provision policy objects
type Provision struct {
ChangeSubset *ChangeSubset `json:"change_subset,omitempty"`
UpdateDescription string `json:"update_description,omitempty"`
}
// SecureConnectGateways represent SecureConnectGateways in provisioning
type SecureConnectGateways struct {
Href string `json:"href"`
}
// VirtualServers reresent virtual servers in provisioning
type VirtualServers struct {
Href string `json:"href"`
}
// ProvisionCS provisions a ChangeSubset
func (p *PCE) ProvisionCS(cs ChangeSubset, comment string) (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) + "/sec_policy")
if err != nil {
return APIResponse{}, fmt.Errorf("provision href - %s", err)
}
// Build the Provision
provision := Provision{ChangeSubset: &cs, UpdateDescription: comment}
provisionJSON, err := json.Marshal(provision)
if err != nil {
return APIResponse{}, err
}
api := APIResponse{ReqBody: string(provisionJSON)}
api, err = apicall("POST", apiURL.String(), *p, provisionJSON, false)
if err != nil {
return api, err
}
return api, nil
}
// ProvisionHref provisions a slice of HREFs
func (p *PCE) ProvisionHref(hrefs []string, comment string) (APIResponse, error) {
// Build our variables
var ipl []*IPList
var services []*Service
var ruleSets []*RuleSet
var labelGroups []*LabelGroup
var virtualServices []*VirtualService
var virtualServers []*VirtualServer
var fs []*FirewallSettings
var secureConnectGateways []*SecureConnectGateways
// Process our list of HREFs
for _, h := range hrefs {
if strings.Contains(h, "/ip_lists/") {
ipl = append(ipl, &IPList{Href: h})
}
// Services
if strings.Contains(h, "/services/") {
services = append(services, &Service{Href: h})
}
// Rule Sets
if strings.Contains(h, "/rule_sets/") {
ruleSets = append(ruleSets, &RuleSet{Href: h})
}
// Label Groups
if strings.Contains(h, "/label_groups/") {
labelGroups = append(labelGroups, &LabelGroup{Href: h})
}
// Virtual Services
if strings.Contains(h, "/virtual_services/") {
virtualServices = append(virtualServices, &VirtualService{Href: h})
}
// Virtual Servers
if strings.Contains(h, "/virtual_servers/") {
virtualServers = append(virtualServers, &VirtualServer{Href: h})
}
// Firewall Settings
if strings.Contains(h, "/firewall_settings/") {
fs = append(fs, &FirewallSettings{Href: h})
}
// SecureConnect Gateway
if strings.Contains(h, "/secure_connect_gateways/") {
secureConnectGateways = append(secureConnectGateways, &SecureConnectGateways{Href: h})
}
}
// Build the Provision
api, err := p.ProvisionCS(ChangeSubset{
FirewallSettings: fs,
IPLists: ipl,
LabelGroups: labelGroups,
RuleSets: ruleSets,
SecureConnectGateways: secureConnectGateways,
Services: services,
VirtualServers: virtualServers,
VirtualServices: virtualServices,
}, comment)
if err != nil {
return api, err
}
return api, nil
}
// GetAllPending gets all the items pending provisioning
func (p *PCE) GetAllPending() (ChangeSubset, 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) + "/sec_policy/pending")
if err != nil {
return ChangeSubset{}, APIResponse{}, err
}
// Call the API
api, err := apicall("GET", apiURL.String(), *p, nil, false)
if err != nil {
return ChangeSubset{}, api, err
}
// Unmarshal response to struct
var cs ChangeSubset
json.Unmarshal([]byte(api.RespBody), &cs)
// Return
return cs, api, nil
}