forked from 3scale/3scale-porta-go-client
/
policy-registry.go
128 lines (104 loc) · 2.95 KB
/
policy-registry.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
package client
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
)
const (
apicastPolicyRegistryEndpoint = "/admin/api/registry/policies.json"
apicastPolicyEndpoint = "/admin/api/registry/policies/%d.json"
)
// ListAPIcastPolicies List existing apicast policies in the registry for the client provider account
func (c *ThreeScaleClient) ListAPIcastPolicies() (*APIcastPolicyRegistry, error) {
req, err := c.buildGetReq(apicastPolicyRegistryEndpoint)
if err != nil {
return nil, err
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
obj := &APIcastPolicyRegistry{}
err = handleJsonResp(resp, http.StatusOK, obj)
return obj, err
}
// ReadAPIcastPolicy Reads 3scale apicast policy from registry
func (c *ThreeScaleClient) ReadAPIcastPolicy(id int64) (*APIcastPolicy, error) {
endpoint := fmt.Sprintf(apicastPolicyEndpoint, id)
req, err := c.buildGetJSONReq(endpoint)
if err != nil {
return nil, err
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
obj := &APIcastPolicy{}
err = handleJsonResp(resp, http.StatusOK, obj)
return obj, err
}
// CreateAPIcastPolicy Create 3scale apicast policy in the registry
func (c *ThreeScaleClient) CreateAPIcastPolicy(item *APIcastPolicy) (*APIcastPolicy, error) {
bodyArr, err := json.Marshal(item.Element)
if err != nil {
return nil, err
}
body := bytes.NewReader(bodyArr)
req, err := c.buildPostJSONReq(apicastPolicyRegistryEndpoint, body)
if err != nil {
return nil, err
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respObj := &APIcastPolicy{}
err = handleJsonResp(resp, http.StatusCreated, respObj)
return respObj, err
}
// UpdateAPIcastPolicy Update existing apicast policy in the registry
func (c *ThreeScaleClient) UpdateAPIcastPolicy(item *APIcastPolicy) (*APIcastPolicy, error) {
if item == nil {
return nil, errors.New("UpdateAPIcastPolicy needs not nil pointer")
}
if item.Element.ID == nil {
return nil, errors.New("UpdateAPIcastPolicy needs not nil ID")
}
endpoint := fmt.Sprintf(apicastPolicyEndpoint, *item.Element.ID)
bodyArr, err := json.Marshal(item.Element)
if err != nil {
return nil, err
}
body := bytes.NewReader(bodyArr)
req, err := c.buildUpdateJSONReq(endpoint, body)
if err != nil {
return nil, err
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respObj := &APIcastPolicy{}
err = handleJsonResp(resp, http.StatusOK, respObj)
return respObj, err
}
// DeleteAPIcastPolicy Delete existing apicast policy in the registry
func (c *ThreeScaleClient) DeleteAPIcastPolicy(id int64) error {
endpoint := fmt.Sprintf(apicastPolicyEndpoint, id)
req, err := c.buildDeleteReq(endpoint, nil)
if err != nil {
return err
}
resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return handleJsonResp(resp, http.StatusOK, nil)
}