forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device_posture_rule.go
159 lines (132 loc) · 4.99 KB
/
device_posture_rule.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
package cloudflare
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/pkg/errors"
)
// DevicePostureRule represents a device posture rule.
type DevicePostureRule struct {
ID string `json:"id,omitempty"`
Type string `json:"type"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Schedule string `json:"schedule,omitempty"`
Match []DevicePostureRuleMatch `json:"match,omitempty"`
Input DevicePostureRuleInput `json:"input,omitempty"`
}
// DevicePostureRuleMatch represents the conditions that the client must match to run the rule.
type DevicePostureRuleMatch struct {
Platform string `json:"platform,omitempty"`
}
// DevicePostureRuleInput represents the value to be checked against.
type DevicePostureRuleInput struct {
ID string `json:"id"`
}
// DevicePostureRuleListResponse represents the response from the list
// device posture rules endpoint.
type DevicePostureRuleListResponse struct {
Result []DevicePostureRule `json:"result"`
Response
ResultInfo `json:"result_info"`
}
// DevicePostureRuleDetailResponse is the API response, containing a single
// device posture rule.
type DevicePostureRuleDetailResponse struct {
Response
Result DevicePostureRule `json:"result"`
}
// DevicePostureRules returns all device posture rules within an account.
//
// API reference: https://api.cloudflare.com/#device-posture-rules-list-device-posture-rules
func (api *API) DevicePostureRules(ctx context.Context, accountID string) ([]DevicePostureRule, ResultInfo, error) {
uri := fmt.Sprintf("/%s/%s/devices/posture", AccountRouteRoot, accountID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []DevicePostureRule{}, ResultInfo{}, err
}
var devicePostureRuleListResponse DevicePostureRuleListResponse
err = json.Unmarshal(res, &devicePostureRuleListResponse)
if err != nil {
return []DevicePostureRule{}, ResultInfo{}, errors.Wrap(err, errUnmarshalError)
}
return devicePostureRuleListResponse.Result, devicePostureRuleListResponse.ResultInfo, nil
}
// DevicePostureRule returns a single device posture rule based on the rule ID.
//
// API reference: https://api.cloudflare.com/#device-posture-rules-device-posture-rules-details
func (api *API) DevicePostureRule(ctx context.Context, accountID, ruleID string) (DevicePostureRule, error) {
uri := fmt.Sprintf(
"/%s/%s/devices/posture/%s",
AccountRouteRoot,
accountID,
ruleID,
)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return DevicePostureRule{}, err
}
var devicePostureRuleDetailResponse DevicePostureRuleDetailResponse
err = json.Unmarshal(res, &devicePostureRuleDetailResponse)
if err != nil {
return DevicePostureRule{}, errors.Wrap(err, errUnmarshalError)
}
return devicePostureRuleDetailResponse.Result, nil
}
// CreateDevicePostureRule creates a new device posture rule.
//
// API reference: https://api.cloudflare.com/#device-posture-rules-create-device-posture-rule
func (api *API) CreateDevicePostureRule(ctx context.Context, accountID string, rule DevicePostureRule) (DevicePostureRule, error) {
uri := fmt.Sprintf("/%s/%s/devices/posture", AccountRouteRoot, accountID)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, rule)
if err != nil {
return DevicePostureRule{}, err
}
var devicePostureRuleDetailResponse DevicePostureRuleDetailResponse
err = json.Unmarshal(res, &devicePostureRuleDetailResponse)
if err != nil {
return DevicePostureRule{}, errors.Wrap(err, errUnmarshalError)
}
return devicePostureRuleDetailResponse.Result, nil
}
// UpdateDevicePostureRule updates an existing device posture rule.
//
// API reference: https://api.cloudflare.com/#device-posture-rules-update-device-posture-rule
func (api *API) UpdateDevicePostureRule(ctx context.Context, accountID string, rule DevicePostureRule) (DevicePostureRule, error) {
if rule.ID == "" {
return DevicePostureRule{}, errors.Errorf("device posture rule ID cannot be empty")
}
uri := fmt.Sprintf(
"/%s/%s/devices/posture/%s",
AccountRouteRoot,
accountID,
rule.ID,
)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, rule)
if err != nil {
return DevicePostureRule{}, err
}
var devicePostureRuleDetailResponse DevicePostureRuleDetailResponse
err = json.Unmarshal(res, &devicePostureRuleDetailResponse)
if err != nil {
return DevicePostureRule{}, errors.Wrap(err, errUnmarshalError)
}
return devicePostureRuleDetailResponse.Result, nil
}
// DeleteDevicePostureRule deletes a device posture rule.
//
// API reference: https://api.cloudflare.com/#device-posture-rules-delete-device-posture-rule
func (api *API) DeleteDevicePostureRule(ctx context.Context, accountID, ruleID string) error {
uri := fmt.Sprintf(
"/%s/%s/devices/posture/%s",
AccountRouteRoot,
accountID,
ruleID,
)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
return nil
}