forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addressing_address_map.go
285 lines (239 loc) · 10.8 KB
/
addressing_address_map.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package cloudflare
import (
"context"
"fmt"
"net/http"
"time"
"github.com/goccy/go-json"
)
// AddressMap contains information about an address map.
type AddressMap struct {
ID string `json:"id"`
Description *string `json:"description,omitempty"`
DefaultSNI *string `json:"default_sni"`
Enabled *bool `json:"enabled"`
Deletable *bool `json:"can_delete"`
CanModifyIPs *bool `json:"can_modify_ips"`
Memberships []AddressMapMembership `json:"memberships"`
IPs []AddressMapIP `json:"ips"`
CreatedAt time.Time `json:"created_at"`
ModifiedAt time.Time `json:"modified_at"`
}
type AddressMapIP struct {
IP string `json:"ip"`
CreatedAt time.Time `json:"created_at"`
}
type AddressMapMembershipContainer struct {
Identifier string `json:"identifier"`
Kind AddressMapMembershipKind `json:"kind"`
}
type AddressMapMembership struct {
Identifier string `json:"identifier"`
Kind AddressMapMembershipKind `json:"kind"`
Deletable *bool `json:"can_delete"`
CreatedAt time.Time `json:"created_at"`
}
func (ammb *AddressMapMembershipContainer) URLFragment() string {
switch ammb.Kind {
case AddressMapMembershipAccount:
return fmt.Sprintf("accounts/%s", ammb.Identifier)
case AddressMapMembershipZone:
return fmt.Sprintf("zones/%s", ammb.Identifier)
default:
return fmt.Sprintf("%s/%s", ammb.Kind, ammb.Identifier)
}
}
type AddressMapMembershipKind string
const (
AddressMapMembershipZone AddressMapMembershipKind = "zone"
AddressMapMembershipAccount AddressMapMembershipKind = "account"
)
// ListAddressMapResponse contains a slice of address maps.
type ListAddressMapResponse struct {
Response
Result []AddressMap `json:"result"`
}
// GetAddressMapResponse contains a specific address map's API Response.
type GetAddressMapResponse struct {
Response
Result AddressMap `json:"result"`
}
// CreateAddressMapParams contains information about an address map to be created.
type CreateAddressMapParams struct {
Description *string `json:"description"`
Enabled *bool `json:"enabled"`
IPs []string `json:"ips"`
Memberships []AddressMapMembershipContainer `json:"memberships"`
}
// UpdateAddressMapParams contains information about an address map to be updated.
type UpdateAddressMapParams struct {
ID string `json:"-"`
Description *string `json:"description,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
DefaultSNI *string `json:"default_sni,omitempty"`
}
// AddressMapFilter contains filter parameters for finding a list of address maps.
type ListAddressMapsParams struct {
IP *string `url:"ip,omitempty"`
CIDR *string `url:"cidr,omitempty"`
}
// CreateIPAddressToAddressMapParams contains request parameters to add/remove IP address to/from an address map.
type CreateIPAddressToAddressMapParams struct {
// ID represents the target address map for adding the IP address.
ID string
// The IP address.
IP string
}
// CreateMembershipToAddressMapParams contains request parameters to add/remove membership from an address map.
type CreateMembershipToAddressMapParams struct {
// ID represents the target address map for adding the membershp.
ID string
Membership AddressMapMembershipContainer
}
type DeleteMembershipFromAddressMapParams struct {
// ID represents the target address map for removing the IP address.
ID string
Membership AddressMapMembershipContainer
}
type DeleteIPAddressFromAddressMapParams struct {
// ID represents the target address map for adding the membershp.
ID string
IP string
}
// ListAddressMaps lists all address maps owned by the account.
//
// API reference: https://developers.cloudflare.com/api/operations/ip-address-management-address-maps-list-address-maps
func (api *API) ListAddressMaps(ctx context.Context, rc *ResourceContainer, params ListAddressMapsParams) ([]AddressMap, error) {
if rc.Level != AccountRouteLevel {
return []AddressMap{}, ErrRequiredAccountLevelResourceContainer
}
uri := buildURI(fmt.Sprintf("/%s/addressing/address_maps", rc.URLFragment()), params)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []AddressMap{}, err
}
result := ListAddressMapResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return []AddressMap{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result.Result, nil
}
// CreateAddressMap creates a new address map under the account.
//
// API reference: https://developers.cloudflare.com/api/operations/ip-address-management-address-maps-create-address-map
func (api *API) CreateAddressMap(ctx context.Context, rc *ResourceContainer, params CreateAddressMapParams) (AddressMap, error) {
if rc.Level != AccountRouteLevel {
return AddressMap{}, ErrRequiredAccountLevelResourceContainer
}
uri := fmt.Sprintf("/%s/addressing/address_maps", rc.URLFragment())
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params)
if err != nil {
return AddressMap{}, err
}
result := GetAddressMapResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return AddressMap{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result.Result, nil
}
// GetAddressMap returns a specific address map.
//
// API reference: https://developers.cloudflare.com/api/operations/ip-address-management-address-maps-address-map-details
func (api *API) GetAddressMap(ctx context.Context, rc *ResourceContainer, id string) (AddressMap, error) {
if rc.Level != AccountRouteLevel {
return AddressMap{}, ErrRequiredAccountLevelResourceContainer
}
uri := fmt.Sprintf("/%s/addressing/address_maps/%s", rc.URLFragment(), id)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return AddressMap{}, err
}
result := GetAddressMapResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return AddressMap{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result.Result, nil
}
// UpdateAddressMap modifies properties of an address map owned by the account.
//
// API reference: https://developers.cloudflare.com/api/operations/ip-address-management-address-maps-update-address-map
func (api *API) UpdateAddressMap(ctx context.Context, rc *ResourceContainer, params UpdateAddressMapParams) (AddressMap, error) {
if rc.Level != AccountRouteLevel {
return AddressMap{}, ErrRequiredAccountLevelResourceContainer
}
uri := fmt.Sprintf("/%s/addressing/address_maps/%s", rc.URLFragment(), params.ID)
res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, params)
if err != nil {
return AddressMap{}, err
}
result := GetAddressMapResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return AddressMap{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result.Result, nil
}
// DeleteAddressMap deletes a particular address map owned by the account.
//
// API reference: https://developers.cloudflare.com/api/operations/ip-address-management-address-maps-delete-address-map
func (api *API) DeleteAddressMap(ctx context.Context, rc *ResourceContainer, id string) error {
if rc.Level != AccountRouteLevel {
return ErrRequiredAccountLevelResourceContainer
}
uri := fmt.Sprintf("/%s/addressing/address_maps/%s", rc.URLFragment(), id)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
return err
}
// CreateIPAddressToAddressMap adds an IP address from a prefix owned by the account to a particular address map.
//
// API reference: https://developers.cloudflare.com/api/operations/ip-address-management-address-maps-add-an-ip-to-an-address-map
func (api *API) CreateIPAddressToAddressMap(ctx context.Context, rc *ResourceContainer, params CreateIPAddressToAddressMapParams) error {
if rc.Level != AccountRouteLevel {
return ErrRequiredAccountLevelResourceContainer
}
uri := fmt.Sprintf("/%s/addressing/address_maps/%s/ips/%s", rc.URLFragment(), params.ID, params.IP)
_, err := api.makeRequestContext(ctx, http.MethodPut, uri, nil)
return err
}
// DeleteIPAddressFromAddressMap removes an IP address from a particular address map.
//
// API reference: https://developers.cloudflare.com/api/operations/ip-address-management-address-maps-remove-an-ip-from-an-address-map
func (api *API) DeleteIPAddressFromAddressMap(ctx context.Context, rc *ResourceContainer, params DeleteIPAddressFromAddressMapParams) error {
if rc.Level != AccountRouteLevel {
return ErrRequiredAccountLevelResourceContainer
}
uri := fmt.Sprintf("/%s/addressing/address_maps/%s/ips/%s", rc.URLFragment(), params.ID, params.IP)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
return err
}
// CreateMembershipToAddressMap adds a zone/account as a member of a particular address map.
//
// API reference:
// - account: https://developers.cloudflare.com/api/operations/ip-address-management-address-maps-add-an-account-membership-to-an-address-map
// - zone: https://developers.cloudflare.com/api/operations/ip-address-management-address-maps-add-a-zone-membership-to-an-address-map
func (api *API) CreateMembershipToAddressMap(ctx context.Context, rc *ResourceContainer, params CreateMembershipToAddressMapParams) error {
if rc.Level != AccountRouteLevel {
return ErrRequiredAccountLevelResourceContainer
}
if params.Membership.Kind != AddressMapMembershipZone && params.Membership.Kind != AddressMapMembershipAccount {
return fmt.Errorf("requested membershp kind (%q) is not supported", params.Membership.Kind)
}
uri := fmt.Sprintf("/%s/addressing/address_maps/%s/%s", rc.URLFragment(), params.ID, params.Membership.URLFragment())
_, err := api.makeRequestContext(ctx, http.MethodPut, uri, nil)
return err
}
// DeleteMembershipFromAddressMap removes a zone/account as a member of a particular address map.
//
// API reference:
// - account: https://developers.cloudflare.com/api/operations/ip-address-management-address-maps-remove-an-account-membership-from-an-address-map
// - zone: https://developers.cloudflare.com/api/operations/ip-address-management-address-maps-remove-a-zone-membership-from-an-address-map
func (api *API) DeleteMembershipFromAddressMap(ctx context.Context, rc *ResourceContainer, params DeleteMembershipFromAddressMapParams) error {
if rc.Level != AccountRouteLevel {
return ErrRequiredAccountLevelResourceContainer
}
if params.Membership.Kind != AddressMapMembershipZone && params.Membership.Kind != AddressMapMembershipAccount {
return fmt.Errorf("requested membershp kind (%q) is not supported", params.Membership.Kind)
}
uri := fmt.Sprintf("/%s/addressing/address_maps/%s/%s", rc.URLFragment(), params.ID, params.Membership.URLFragment())
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
return err
}