forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devices_managed_networks.go
164 lines (130 loc) · 5.41 KB
/
devices_managed_networks.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
package cloudflare
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
type Config struct {
TlsSockAddr string `json:"tls_sockaddr,omitempty"`
Sha256 string `json:"sha256,omitempty"`
}
type DeviceManagedNetwork struct {
NetworkID string `json:"network_id,omitempty"`
Type string `json:"type"`
Name string `json:"name"`
Config *Config `json:"config"`
}
type DeviceManagedNetworkResponse struct {
Response
Result DeviceManagedNetwork `json:"result"`
}
type DeviceManagedNetworkListResponse struct {
Response
Result []DeviceManagedNetwork `json:"result"`
}
type ListDeviceManagedNetworksParams struct{}
type CreateDeviceManagedNetworkParams struct {
NetworkID string `json:"network_id,omitempty"`
Type string `json:"type"`
Name string `json:"name"`
Config *Config `json:"config"`
}
type UpdateDeviceManagedNetworkParams struct {
NetworkID string `json:"network_id,omitempty"`
Type string `json:"type"`
Name string `json:"name"`
Config *Config `json:"config"`
}
// ListDeviceManagedNetwork returns all Device Managed Networks for a given
// account.
//
// API reference : https://api.cloudflare.com/#device-managed-networks-list-device-managed-networks
func (api *API) ListDeviceManagedNetworks(ctx context.Context, rc *ResourceContainer, params ListDeviceManagedNetworksParams) ([]DeviceManagedNetwork, error) {
if rc.Level != AccountRouteLevel {
return []DeviceManagedNetwork{}, ErrRequiredAccountLevelResourceContainer
}
uri := fmt.Sprintf("/%s/%s/devices/networks", rc.Level, rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []DeviceManagedNetwork{}, err
}
var response DeviceManagedNetworkListResponse
err = json.Unmarshal(res, &response)
if err != nil {
return []DeviceManagedNetwork{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return response.Result, nil
}
// CreateDeviceManagedNetwork creates a new Device Managed Network.
//
// API reference: https://api.cloudflare.com/#device-managed-networks-create-device-managed-network
func (api *API) CreateDeviceManagedNetwork(ctx context.Context, rc *ResourceContainer, params CreateDeviceManagedNetworkParams) (DeviceManagedNetwork, error) {
if rc.Level != AccountRouteLevel {
return DeviceManagedNetwork{}, ErrRequiredAccountLevelResourceContainer
}
uri := fmt.Sprintf("/%s/%s/devices/networks", rc.Level, rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params)
if err != nil {
return DeviceManagedNetwork{}, err
}
var deviceManagedNetworksResponse DeviceManagedNetworkResponse
if err := json.Unmarshal(res, &deviceManagedNetworksResponse); err != nil {
return DeviceManagedNetwork{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return deviceManagedNetworksResponse.Result, err
}
// UpdateDeviceManagedNetwork Update a Device Managed Network.
//
// API reference: https://api.cloudflare.com/#device-managed-networks-update-device-managed-network
func (api *API) UpdateDeviceManagedNetwork(ctx context.Context, rc *ResourceContainer, params UpdateDeviceManagedNetworkParams) (DeviceManagedNetwork, error) {
if rc.Level != AccountRouteLevel {
return DeviceManagedNetwork{}, ErrRequiredAccountLevelResourceContainer
}
uri := fmt.Sprintf("/%s/%s/devices/networks/%s", rc.Level, rc.Identifier, params.NetworkID)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params)
if err != nil {
return DeviceManagedNetwork{}, err
}
var deviceManagedNetworksResponse DeviceManagedNetworkResponse
if err := json.Unmarshal(res, &deviceManagedNetworksResponse); err != nil {
return DeviceManagedNetwork{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return deviceManagedNetworksResponse.Result, err
}
// GetDeviceManagedNetwork gets a single Device Managed Network.
//
// API reference: https://api.cloudflare.com/#device-managed-networks-device-managed-network-details
func (api *API) GetDeviceManagedNetwork(ctx context.Context, rc *ResourceContainer, networkID string) (DeviceManagedNetwork, error) {
if rc.Level != AccountRouteLevel {
return DeviceManagedNetwork{}, ErrRequiredAccountLevelResourceContainer
}
uri := fmt.Sprintf("/%s/%s/devices/networks/%s", rc.Level, rc.Identifier, networkID)
deviceManagedNetworksResponse := DeviceManagedNetworkResponse{}
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return DeviceManagedNetwork{}, err
}
if err := json.Unmarshal(res, &deviceManagedNetworksResponse); err != nil {
return DeviceManagedNetwork{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return deviceManagedNetworksResponse.Result, err
}
// DeleteManagedNetworks deletes a Device Managed Network.
//
// API reference: https://api.cloudflare.com/#device-managed-networks-delete-device-managed-network
func (api *API) DeleteManagedNetworks(ctx context.Context, rc *ResourceContainer, networkID string) ([]DeviceManagedNetwork, error) {
if rc.Level != AccountRouteLevel {
return []DeviceManagedNetwork{}, ErrRequiredAccountLevelResourceContainer
}
uri := fmt.Sprintf("/%s/%s/devices/networks/%s", rc.Level, rc.Identifier, networkID)
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return []DeviceManagedNetwork{}, err
}
var response DeviceManagedNetworkListResponse
if err := json.Unmarshal(res, &response); err != nil {
return []DeviceManagedNetwork{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return response.Result, err
}