forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addressing_ip_prefix.go
148 lines (125 loc) · 5.09 KB
/
addressing_ip_prefix.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
package cloudflare
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
// IPPrefix contains information about an IP prefix.
type IPPrefix struct {
ID string `json:"id"`
CreatedAt *time.Time `json:"created_at"`
ModifiedAt *time.Time `json:"modified_at"`
CIDR string `json:"cidr"`
AccountID string `json:"account_id"`
Description string `json:"description"`
Approved string `json:"approved"`
OnDemandEnabled bool `json:"on_demand_enabled"`
OnDemandLocked bool `json:"on_demand_locked"`
Advertised bool `json:"advertised"`
AdvertisedModifiedAt *time.Time `json:"advertised_modified_at"`
}
// AdvertisementStatus contains information about the BGP status of an IP prefix.
type AdvertisementStatus struct {
Advertised bool `json:"advertised"`
AdvertisedModifiedAt *time.Time `json:"advertised_modified_at"`
}
// ListIPPrefixResponse contains a slice of IP prefixes.
type ListIPPrefixResponse struct {
Response
Result []IPPrefix `json:"result"`
}
// GetIPPrefixResponse contains a specific IP prefix's API Response.
type GetIPPrefixResponse struct {
Response
Result IPPrefix `json:"result"`
}
// GetAdvertisementStatusResponse contains an API Response for the BGP status of the IP Prefix.
type GetAdvertisementStatusResponse struct {
Response
Result AdvertisementStatus `json:"result"`
}
// IPPrefixUpdateRequest contains information about prefix updates.
type IPPrefixUpdateRequest struct {
Description string `json:"description"`
}
// AdvertisementStatusUpdateRequest contains information about bgp status updates.
type AdvertisementStatusUpdateRequest struct {
Advertised bool `json:"advertised"`
}
// ListPrefixes lists all IP prefixes for a given account
//
// API reference: https://api.cloudflare.com/#ip-address-management-prefixes-list-prefixes
func (api *API) ListPrefixes(ctx context.Context, accountID string) ([]IPPrefix, error) {
uri := fmt.Sprintf("/accounts/%s/addressing/prefixes", accountID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []IPPrefix{}, err
}
result := ListIPPrefixResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return []IPPrefix{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result.Result, nil
}
// GetPrefix returns a specific IP prefix
//
// API reference: https://api.cloudflare.com/#ip-address-management-prefixes-prefix-details
func (api *API) GetPrefix(ctx context.Context, accountID, ID string) (IPPrefix, error) {
uri := fmt.Sprintf("/accounts/%s/addressing/prefixes/%s", accountID, ID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return IPPrefix{}, err
}
result := GetIPPrefixResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return IPPrefix{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result.Result, nil
}
// UpdatePrefixDescription edits the description of the IP prefix
//
// API reference: https://api.cloudflare.com/#ip-address-management-prefixes-update-prefix-description
func (api *API) UpdatePrefixDescription(ctx context.Context, accountID, ID string, description string) (IPPrefix, error) {
uri := fmt.Sprintf("/accounts/%s/addressing/prefixes/%s", accountID, ID)
res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, IPPrefixUpdateRequest{Description: description})
if err != nil {
return IPPrefix{}, err
}
result := GetIPPrefixResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return IPPrefix{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result.Result, nil
}
// GetAdvertisementStatus returns the BGP status of the IP prefix
//
// API reference: https://api.cloudflare.com/#ip-address-management-prefixes-update-prefix-description
func (api *API) GetAdvertisementStatus(ctx context.Context, accountID, ID string) (AdvertisementStatus, error) {
uri := fmt.Sprintf("/accounts/%s/addressing/prefixes/%s/bgp/status", accountID, ID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return AdvertisementStatus{}, err
}
result := GetAdvertisementStatusResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return AdvertisementStatus{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result.Result, nil
}
// UpdateAdvertisementStatus changes the BGP status of an IP prefix
//
// API reference: https://api.cloudflare.com/#ip-address-management-prefixes-update-prefix-description
func (api *API) UpdateAdvertisementStatus(ctx context.Context, accountID, ID string, advertised bool) (AdvertisementStatus, error) {
uri := fmt.Sprintf("/accounts/%s/addressing/prefixes/%s/bgp/status", accountID, ID)
res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, AdvertisementStatusUpdateRequest{Advertised: advertised})
if err != nil {
return AdvertisementStatus{}, err
}
result := GetAdvertisementStatusResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return AdvertisementStatus{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result.Result, nil
}