forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_seats.go
111 lines (92 loc) · 3.71 KB
/
access_seats.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
package cloudflare
import (
"context"
"errors"
"fmt"
"net/http"
"time"
"github.com/goccy/go-json"
)
var errMissingAccessSeatUID = errors.New("missing required access seat UID")
// AccessUpdateAccessUserSeatResult represents a Access User Seat.
type AccessUpdateAccessUserSeatResult struct {
AccessSeat *bool `json:"access_seat"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
GatewaySeat *bool `json:"gateway_seat"`
SeatUID string `json:"seat_uid,omitempty"`
}
// UpdateAccessUserSeatParams represents the update payload for access seats.
type UpdateAccessUserSeatParams struct {
SeatUID string `json:"seat_uid,omitempty"`
AccessSeat *bool `json:"access_seat"`
GatewaySeat *bool `json:"gateway_seat"`
}
// UpdateAccessUsersSeatsParams represents the update payload for multiple access seats.
type UpdateAccessUsersSeatsParams []struct {
SeatUID string `json:"seat_uid,omitempty"`
AccessSeat *bool `json:"access_seat"`
GatewaySeat *bool `json:"gateway_seat"`
}
// AccessUserSeatResponse represents the response from the access user seat endpoints.
type UpdateAccessUserSeatResponse struct {
Response
Result []AccessUpdateAccessUserSeatResult `json:"result"`
ResultInfo `json:"result_info"`
}
// UpdateAccessUserSeat updates a single Access User Seat.
//
// API documentation: https://developers.cloudflare.com/api/operations/zero-trust-seats-update-a-user-seat
func (api *API) UpdateAccessUserSeat(ctx context.Context, rc *ResourceContainer, params UpdateAccessUserSeatParams) ([]AccessUpdateAccessUserSeatResult, error) {
if rc.Level != AccountRouteLevel {
return []AccessUpdateAccessUserSeatResult{}, fmt.Errorf(errInvalidResourceContainerAccess, rc.Level)
}
if params.SeatUID == "" {
return []AccessUpdateAccessUserSeatResult{}, errMissingAccessSeatUID
}
uri := fmt.Sprintf(
"/%s/%s/access/seats",
rc.Level,
rc.Identifier,
)
// this requests expects an array of params, but this method only accepts a single param
res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, []UpdateAccessUserSeatParams{params})
if err != nil {
return []AccessUpdateAccessUserSeatResult{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}
var updateAccessUserSeatResponse UpdateAccessUserSeatResponse
err = json.Unmarshal(res, &updateAccessUserSeatResponse)
if err != nil {
return []AccessUpdateAccessUserSeatResult{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return updateAccessUserSeatResponse.Result, nil
}
// UpdateAccessUsersSeats updates many Access User Seats.
//
// API documentation: https://developers.cloudflare.com/api/operations/zero-trust-seats-update-a-user-seat
func (api *API) UpdateAccessUsersSeats(ctx context.Context, rc *ResourceContainer, params UpdateAccessUsersSeatsParams) ([]AccessUpdateAccessUserSeatResult, error) {
if rc.Level != AccountRouteLevel {
return []AccessUpdateAccessUserSeatResult{}, fmt.Errorf(errInvalidResourceContainerAccess, rc.Level)
}
for _, param := range params {
if param.SeatUID == "" {
return []AccessUpdateAccessUserSeatResult{}, errMissingAccessSeatUID
}
}
uri := fmt.Sprintf(
"/%s/%s/access/seats",
rc.Level,
rc.Identifier,
)
// this requests expects an array of params, but this method only accepts a single param
res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, params)
if err != nil {
return []AccessUpdateAccessUserSeatResult{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}
var updateAccessUserSeatResponse UpdateAccessUserSeatResponse
err = json.Unmarshal(res, &updateAccessUserSeatResponse)
if err != nil {
return []AccessUpdateAccessUserSeatResult{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return updateAccessUserSeatResponse.Result, nil
}