-
Notifications
You must be signed in to change notification settings - Fork 5
/
servicebinding.go
114 lines (93 loc) · 3.43 KB
/
servicebinding.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
package illumioapi
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
)
// A ServiceBinding binds a worklad to a Virtual Service
type ServiceBinding struct {
Href string `json:"href,omitempty"`
VirtualService VirtualService `json:"virtual_service"`
Workload Workload `json:"workload"`
PortOverrides []PortOverrides `json:"port_overrides"`
}
// PortOverrides override a port on a virtual service binding.
type PortOverrides struct {
Port int `json:"port"`
Proto int `json:"proto"`
NewPort int `json:"new_port"`
}
// GetAllServiceBindings returns a slice of all workload bindings for a virtual service.
//
// 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) GetAllServiceBindings(virtualService VirtualService) ([]ServiceBinding, 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) + "/service_bindings")
if err != nil {
return nil, api, fmt.Errorf("get all service bindings - %s", err)
}
// Set the query parameters
q := apiURL.Query()
q.Set("virtual_service", virtualService.Href)
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 service bindings - %s", err)
}
var serviceBindings []ServiceBinding
json.Unmarshal([]byte(api.RespBody), &serviceBindings)
// If length is 500, re-run with async
if len(serviceBindings) >= 500 {
api, err = apicall("GET", apiURL.String(), *p, nil, true)
if err != nil {
return nil, api, fmt.Errorf("get all service bindings - %s", err)
}
// Unmarshal response to struct
var asyncServiceBindings []ServiceBinding
json.Unmarshal([]byte(api.RespBody), &asyncServiceBindings)
return asyncServiceBindings, api, nil
}
// Return if there are less than 500
return serviceBindings, api, nil
}
// CreateServiceBinding binds new workloads to a virtual service
func (p *PCE) CreateServiceBinding(serviceBindings []ServiceBinding, virtualService VirtualService) ([]ServiceBinding, APIResponse, error) {
var newServBindings []ServiceBinding
var api APIResponse
var err error
// Build the API URL
apiURL, err := url.Parse("https://" + pceSanitization(p.FQDN) + ":" + strconv.Itoa(p.Port) + "/api/v2/orgs/" + strconv.Itoa(p.Org) + "/service_bindings")
if err != nil {
return newServBindings, api, fmt.Errorf("create service binding - %s", err)
}
// Sanitize Bindings
sanSBs := []ServiceBinding{}
for _, sb := range serviceBindings {
sb.sanitizeBindings()
sanSBs = append(sanSBs, sb)
}
serviceBindings = sanSBs
// Call the API
sbJSON, err := json.Marshal(serviceBindings)
if err != nil {
return newServBindings, api, fmt.Errorf("create service binding - %s", err)
}
api.ReqBody = string(sbJSON)
api, err = apicall("POST", apiURL.String(), *p, sbJSON, false)
if err != nil {
return newServBindings, api, fmt.Errorf("create servince binding - %s", err)
}
// Unmarshal new Virtual service
json.Unmarshal([]byte(api.RespBody), &newServBindings)
return newServBindings, api, nil
}
// SanitizeBindings preps bindings for update or create
func (sb *ServiceBinding) sanitizeBindings() {
sb.Href = ""
sb.VirtualService = VirtualService{Href: sb.VirtualService.SetActive().Href}
sb.Workload = Workload{Href: sb.Workload.Href}
}