forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devices_dex.go
173 lines (138 loc) · 5.57 KB
/
devices_dex.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
package cloudflare
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
type DeviceDexTestData map[string]interface{}
type DeviceDexTest struct {
TestID string `json:"test_id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Interval string `json:"interval"`
Enabled bool `json:"enabled"`
Updated time.Time `json:"updated"`
Created time.Time `json:"created"`
Data *DeviceDexTestData `json:"data"`
}
type DeviceDexTests struct {
DexTests []DeviceDexTest `json:"dex_tests"`
}
type DeviceDexTestResponse struct {
Response
Result DeviceDexTest `json:"result"`
}
type DeviceDexTestListResponse struct {
Response
Result DeviceDexTests `json:"result"`
}
type ListDeviceDexTestParams struct{}
type CreateDeviceDexTestParams struct {
TestID string `json:"test_id,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Interval string `json:"interval"`
Enabled bool `json:"enabled"`
Data *DeviceDexTestData `json:"data"`
}
type UpdateDeviceDexTestParams struct {
TestID string `json:"test_id,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Interval string `json:"interval"`
Enabled bool `json:"enabled"`
Data *DeviceDexTestData `json:"data"`
}
// ListDexTests returns all Device Dex Tests for a given account.
//
// API reference : https://developers.cloudflare.com/api/operations/device-dex-test-details
func (api *API) ListDexTests(ctx context.Context, rc *ResourceContainer, params ListDeviceDexTestParams) (DeviceDexTests, error) {
if rc.Level != AccountRouteLevel {
return DeviceDexTests{}, ErrRequiredAccountLevelResourceContainer
}
uri := fmt.Sprintf("/%s/%s/devices/dex_tests", rc.Level, rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return DeviceDexTests{}, err
}
var response DeviceDexTestListResponse
err = json.Unmarshal(res, &response)
if err != nil {
return DeviceDexTests{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return response.Result, nil
}
// CreateDeviceDexTest created a new Device Dex Test
//
// API reference: https://developers.cloudflare.com/api/operations/device-dex-test-create-device-dex-test
func (api *API) CreateDeviceDexTest(ctx context.Context, rc *ResourceContainer, params CreateDeviceDexTestParams) (DeviceDexTest, error) {
if rc.Level != AccountRouteLevel {
return DeviceDexTest{}, ErrRequiredAccountLevelResourceContainer
}
uri := fmt.Sprintf("/%s/%s/devices/dex_tests", rc.Level, rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params)
if err != nil {
return DeviceDexTest{}, err
}
var deviceDexTestResponse DeviceDexTestResponse
if err := json.Unmarshal(res, &deviceDexTestResponse); err != nil {
return DeviceDexTest{}, fmt.Errorf("%s: %w\n\nres: %s", errUnmarshalError, err, string(res))
}
return deviceDexTestResponse.Result, err
}
// UpdateDeviceDexTest Updates a Device Dex Test.
//
// API reference: https://developers.cloudflare.com/api/operations/device-dex-test-update-device-dex-test
func (api *API) UpdateDeviceDexTest(ctx context.Context, rc *ResourceContainer, params UpdateDeviceDexTestParams) (DeviceDexTest, error) {
if rc.Level != AccountRouteLevel {
return DeviceDexTest{}, ErrRequiredAccountLevelResourceContainer
}
uri := fmt.Sprintf("/%s/%s/devices/dex_tests/%s", rc.Level, rc.Identifier, params.TestID)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params)
if err != nil {
return DeviceDexTest{}, err
}
var deviceDexTestsResponse DeviceDexTestResponse
if err := json.Unmarshal(res, &deviceDexTestsResponse); err != nil {
return DeviceDexTest{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return deviceDexTestsResponse.Result, err
}
// GetDeviceDexTest gets a single Device Dex Test.
//
// API reference: https://developers.cloudflare.com/api/operations/device-dex-test-get-device-dex-test
func (api *API) GetDeviceDexTest(ctx context.Context, rc *ResourceContainer, testID string) (DeviceDexTest, error) {
if rc.Level != AccountRouteLevel {
return DeviceDexTest{}, ErrRequiredAccountLevelResourceContainer
}
uri := fmt.Sprintf("/%s/%s/devices/dex_tests/%s", rc.Level, rc.Identifier, testID)
deviceDexTestResponse := DeviceDexTestResponse{}
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return DeviceDexTest{}, err
}
if err := json.Unmarshal(res, &deviceDexTestResponse); err != nil {
return DeviceDexTest{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return deviceDexTestResponse.Result, err
}
// DeleteDexTest deletes a Device Dex Test.
//
// API reference: https://developers.cloudflare.com/api/operations/device-dex-test-delete-device-dex-test
func (api *API) DeleteDexTest(ctx context.Context, rc *ResourceContainer, testID string) (DeviceDexTests, error) {
if rc.Level != AccountRouteLevel {
return DeviceDexTests{}, ErrRequiredAccountLevelResourceContainer
}
uri := fmt.Sprintf("/%s/%s/devices/dex_tests/%s", rc.Level, rc.Identifier, testID)
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return DeviceDexTests{}, err
}
var response DeviceDexTestListResponse
if err := json.Unmarshal(res, &response); err != nil {
return DeviceDexTests{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return response.Result, err
}