-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexternal_policy_client.go
146 lines (128 loc) · 4.1 KB
/
external_policy_client.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
package policy_client
import (
"fmt"
"net/http"
"strings"
"code.cloudfoundry.org/cf-networking-helpers/json_client"
"code.cloudfoundry.org/lager/v3"
)
//go:generate counterfeiter -o ./fakes/external_policy_client.go --fake-name ExternalPolicyClient . ExternalPolicyClient
type ExternalPolicyClient interface {
GetPolicies(token string) ([]Policy, error)
GetPoliciesByID(token string, ids ...string) ([]Policy, error)
GetPoliciesV0(token string) ([]PolicyV0, error)
GetPoliciesV0ByID(token string, ids ...string) ([]PolicyV0, error)
DeletePolicies(token string, policies []Policy) error
DeletePoliciesV0(token string, policies []PolicyV0) error
AddPolicies(token string, policies []Policy) error
AddPoliciesV0(token string, policies []PolicyV0) error
}
type ExternalClient struct {
JsonClient json_client.JsonClient
Chunker Chunker
}
func NewExternal(logger lager.Logger, httpClient json_client.HttpClient, baseURL string) *ExternalClient {
return &ExternalClient{
JsonClient: json_client.New(logger, httpClient, baseURL),
Chunker: &SimpleChunker{ChunkSize: DefaultMaxPolicies},
}
}
func (c *ExternalClient) GetPolicies(token string) ([]Policy, error) {
var policies struct {
Policies []Policy `json:"policies"`
}
err := c.JsonClient.Do("GET", "/networking/v1/external/policies", nil, &policies, token)
if err != nil {
return nil, parseHttpError(err)
}
return policies.Policies, nil
}
func (c *ExternalClient) GetPoliciesByID(token string, ids ...string) ([]Policy, error) {
var policies struct {
Policies []Policy `json:"policies"`
}
route := "/networking/v1/external/policies?id=" + strings.Join(ids, ",")
err := c.JsonClient.Do("GET", route, nil, &policies, token)
if err != nil {
return nil, parseHttpError(err)
}
return policies.Policies, nil
}
func (c *ExternalClient) GetPoliciesV0(token string) ([]PolicyV0, error) {
var policies struct {
Policies []PolicyV0 `json:"policies"`
}
err := c.JsonClient.Do("GET", "/networking/v0/external/policies", nil, &policies, token)
if err != nil {
return nil, parseHttpError(err)
}
return policies.Policies, nil
}
func (c *ExternalClient) GetPoliciesV0ByID(token string, ids ...string) ([]PolicyV0, error) {
var policies struct {
Policies []PolicyV0 `json:"policies"`
}
route := "/networking/v0/external/policies?id=" + strings.Join(ids, ",")
err := c.JsonClient.Do("GET", route, nil, &policies, token)
if err != nil {
return nil, parseHttpError(err)
}
return policies.Policies, nil
}
func (c *ExternalClient) AddPolicies(token string, policies []Policy) error {
reqPolicies := map[string][]Policy{
"policies": policies,
}
err := c.JsonClient.Do("POST", "/networking/v1/external/policies", reqPolicies, nil, token)
if err != nil {
return parseHttpError(err)
}
return nil
}
func (c *ExternalClient) AddPoliciesV0(token string, policies []PolicyV0) error {
chunks := c.Chunker.Chunk(policies)
for _, chunk := range chunks {
reqPolicies := map[string][]PolicyV0{
"policies": chunk,
}
err := c.JsonClient.Do("POST", "/networking/v0/external/policies", reqPolicies, nil, token)
if err != nil {
return parseHttpError(err)
}
}
return nil
}
func (c *ExternalClient) DeletePolicies(token string, policies []Policy) error {
reqPolicies := map[string][]Policy{
"policies": policies,
}
err := c.JsonClient.Do("POST", "/networking/v1/external/policies/delete", reqPolicies, nil, token)
if err != nil {
return parseHttpError(err)
}
return nil
}
func (c *ExternalClient) DeletePoliciesV0(token string, policies []PolicyV0) error {
chunks := c.Chunker.Chunk(policies)
for _, chunk := range chunks {
reqPolicies := map[string][]PolicyV0{
"policies": chunk,
}
err := c.JsonClient.Do("POST", "/networking/v0/external/policies/delete", reqPolicies, nil, token)
if err != nil {
return parseHttpError(err)
}
}
return nil
}
// Check if error is bad status code and parse out the JSON body
func parseHttpError(err error) error {
httpErr, ok := err.(*json_client.HttpResponseCodeError)
if ok {
return fmt.Errorf("%d %s: %s", httpErr.StatusCode,
http.StatusText(httpErr.StatusCode),
httpErr.Message,
)
}
return err
}