-
Notifications
You must be signed in to change notification settings - Fork 4
/
tenant_requests.go
227 lines (176 loc) · 6.5 KB
/
tenant_requests.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package se2
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/pkg/errors"
)
const (
pathTenant = "/environment/v1/tenant"
pathTenantByName = pathTenant + "/%s"
)
// TenantResponse captures the JSON data returned from the endpoints.
type TenantResponse struct {
AuthorizedParty string `json:"authorized_party"`
ID string `json:"id"`
Environment string `json:"environment"`
Name string `json:"name"`
Description string `json:"description"`
}
// GetTenantByName returns the tenant by name.
func (c *Client) GetTenantByName(ctx context.Context, name string) (TenantResponse, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.host+fmt.Sprintf(pathTenantByName, name), nil)
if err != nil {
return TenantResponse{}, errors.Wrap(err, "client.GetTenantByName: http.NewRequest")
}
res, err := c.do(req)
if err != nil {
return TenantResponse{}, errors.Wrap(err, "client.GetTenantByName: c.do")
}
defer func() {
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusOK {
return TenantResponse{}, fmt.Errorf(httpResponseCodeErrorFormat, "client.GetTenantByName", http.StatusOK, res.StatusCode)
}
var t TenantResponse
dec := json.NewDecoder(res.Body)
dec.DisallowUnknownFields()
err = dec.Decode(&t)
if err != nil {
return TenantResponse{}, errors.Wrap(err, "client.GetTenantByName: dec.Decode")
}
return t, nil
}
// createTenantRequest is used to format the incoming description into a JSON body. Users of this client library should
// not need to use this struct directly.
type createTenantRequest struct {
Description string `json:"description"`
}
// CreateTenant creates a new tenant with the given name and description. Description is optional, it can be an
// empty string.
func (c *Client) CreateTenant(ctx context.Context, name, description string) (TenantResponse, error) {
if name == emptyString {
return TenantResponse{}, errors.New("client.CreateTenant: tenant name cannot be empty")
}
var requestBody io.Reader
if description != "" {
m, err := json.Marshal(createTenantRequest{Description: description})
if err != nil {
return TenantResponse{}, errors.Wrap(err, "client.CreateTenant: json marshal create tenant request with description")
}
requestBody = bytes.NewReader(m)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.host+fmt.Sprintf(pathTenantByName, name), requestBody)
if err != nil {
return TenantResponse{}, errors.Wrap(err, "client.CreateTenant: http.NewRequest for POST create tenant")
}
res, err := c.do(req)
if err != nil {
return TenantResponse{}, errors.Wrap(err, "client.CreateTenant: c.do")
}
defer func() {
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusCreated {
return TenantResponse{}, fmt.Errorf(httpResponseCodeErrorFormat, "client.CreateTenant", http.StatusCreated, res.StatusCode)
}
var t TenantResponse
dec := json.NewDecoder(res.Body)
dec.DisallowUnknownFields()
err = dec.Decode(&t)
if err != nil {
return TenantResponse{}, errors.Wrap(err, "client.CreateTenant: dec.Decode")
}
return t, nil
}
// ListTenantResponse is the unmarshaled response from the endpoint.
type ListTenantResponse struct {
Tenants []TenantResponse
}
// ListTenants will list the tenants that the configured API key can access.
func (c *Client) ListTenants(ctx context.Context) (ListTenantResponse, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.host+pathTenant, nil)
if err != nil {
return ListTenantResponse{}, errors.Wrap(err, "client.ListTenants: http.NewRequest")
}
res, err := c.do(req)
if err != nil {
return ListTenantResponse{}, errors.Wrap(err, "client.ListTenants: c.do")
}
defer func() {
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusOK {
return ListTenantResponse{}, fmt.Errorf(httpResponseCodeErrorFormat, "client.ListTenants", http.StatusOK, res.StatusCode)
}
var t ListTenantResponse
dec := json.NewDecoder(res.Body)
dec.DisallowUnknownFields()
err = dec.Decode(&t)
if err != nil {
return ListTenantResponse{}, errors.Wrap(err, "client.ListTenants: dec.Decode")
}
return t, nil
}
// updateTenantRequest is a struct to help constrain the data and marshal the json based on it that ends up in a request
// body. It's internal only, users of the client do not need to know about the existence of this struct.
type updateTenantRequest struct {
Description string `json:"description"`
}
// UpdateTenantByName updates the description of the tenant identified by its name. A tenant's name cannot be changed.
func (c *Client) UpdateTenantByName(ctx context.Context, name, description string) (TenantResponse, error) {
if name == emptyString {
return TenantResponse{}, errors.New("client.UpdateTenantByName: tenant name cannot be empty")
}
m, err := json.Marshal(updateTenantRequest{Description: description})
if err != nil {
return TenantResponse{}, errors.Wrap(err, "client.UpdateTenantByName: json marshal update tenant request")
}
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, c.host+fmt.Sprintf(pathTenantByName, name), bytes.NewReader(m))
if err != nil {
return TenantResponse{}, errors.Wrap(err, "client.UpdateTenantByName: http.NewRequest for POST create tenant")
}
res, err := c.do(req)
if err != nil {
return TenantResponse{}, errors.Wrap(err, "client.UpdateTenantByName: c.do")
}
defer func() {
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusOK {
return TenantResponse{}, fmt.Errorf(httpResponseCodeErrorFormat, "client.UpdateTenantByName", http.StatusOK, res.StatusCode)
}
var t TenantResponse
dec := json.NewDecoder(res.Body)
dec.DisallowUnknownFields()
err = dec.Decode(&t)
if err != nil {
return TenantResponse{}, errors.Wrap(err, "client.UpdateTenantByName: dec.Decode")
}
return t, nil
}
// DeleteTenantByName deletes the tenant identified by its name.
func (c *Client) DeleteTenantByName(ctx context.Context, name string) error {
if name == emptyString {
return errors.New("client.DeleteTenantByName: tenant name cannot be empty")
}
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, fmt.Sprintf(c.host+pathTenantByName, name), nil)
if err != nil {
return errors.Wrap(err, "client.DeleteTenantByName: http.NewRequest")
}
res, err := c.do(req)
if err != nil {
return errors.Wrap(err, "client.DeleteTenantByName: c.do")
}
defer func() {
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusOK {
return fmt.Errorf(httpResponseCodeErrorFormat, "client.DeleteTenantByName", http.StatusOK, res.StatusCode)
}
return nil
}