-
Notifications
You must be signed in to change notification settings - Fork 593
/
accounts.go
151 lines (124 loc) · 4.27 KB
/
accounts.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
package cloudflare
import (
"context"
"fmt"
"net/http"
"time"
"github.com/goccy/go-json"
)
// AccountSettings outlines the available options for an account.
type AccountSettings struct {
EnforceTwoFactor bool `json:"enforce_twofactor"`
}
// Account represents the root object that owns resources.
type Account struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
CreatedOn time.Time `json:"created_on,omitempty"`
Settings *AccountSettings `json:"settings,omitempty"`
}
// AccountResponse represents the response from the accounts endpoint for a
// single account ID.
type AccountResponse struct {
Result Account `json:"result"`
Response
ResultInfo `json:"result_info"`
}
// AccountListResponse represents the response from the list accounts endpoint.
type AccountListResponse struct {
Result []Account `json:"result"`
Response
ResultInfo `json:"result_info"`
}
// AccountDetailResponse is the API response, containing a single Account.
type AccountDetailResponse struct {
Success bool `json:"success"`
Errors []string `json:"errors"`
Messages []string `json:"messages"`
Result Account `json:"result"`
}
// AccountsListParams holds the filterable options for Accounts.
type AccountsListParams struct {
Name string `url:"name,omitempty"`
PaginationOptions
}
// Accounts returns all accounts the logged in user has access to.
//
// API reference: https://api.cloudflare.com/#accounts-list-accounts
func (api *API) Accounts(ctx context.Context, params AccountsListParams) ([]Account, ResultInfo, error) {
res, err := api.makeRequestContext(ctx, http.MethodGet, buildURI("/accounts", params), nil)
if err != nil {
return []Account{}, ResultInfo{}, err
}
var accListResponse AccountListResponse
err = json.Unmarshal(res, &accListResponse)
if err != nil {
return []Account{}, ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return accListResponse.Result, accListResponse.ResultInfo, nil
}
// Account returns a single account based on the ID.
//
// API reference: https://api.cloudflare.com/#accounts-account-details
func (api *API) Account(ctx context.Context, accountID string) (Account, ResultInfo, error) {
uri := fmt.Sprintf("/accounts/%s", accountID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return Account{}, ResultInfo{}, err
}
var accResponse AccountResponse
err = json.Unmarshal(res, &accResponse)
if err != nil {
return Account{}, ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return accResponse.Result, accResponse.ResultInfo, nil
}
// UpdateAccount allows management of an account using the account ID.
//
// API reference: https://api.cloudflare.com/#accounts-update-account
func (api *API) UpdateAccount(ctx context.Context, accountID string, account Account) (Account, error) {
uri := fmt.Sprintf("/accounts/%s", accountID)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, account)
if err != nil {
return Account{}, err
}
var a AccountDetailResponse
err = json.Unmarshal(res, &a)
if err != nil {
return Account{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return a.Result, nil
}
// CreateAccount creates a new account. Note: This requires the Tenant
// entitlement.
//
// API reference: https://developers.cloudflare.com/tenant/tutorial/provisioning-resources#creating-an-account
func (api *API) CreateAccount(ctx context.Context, account Account) (Account, error) {
uri := "/accounts"
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, account)
if err != nil {
return Account{}, err
}
var a AccountDetailResponse
err = json.Unmarshal(res, &a)
if err != nil {
return Account{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return a.Result, nil
}
// DeleteAccount removes an account. Note: This requires the Tenant
// entitlement.
//
// API reference: https://developers.cloudflare.com/tenant/tutorial/provisioning-resources#optional-deleting-accounts
func (api *API) DeleteAccount(ctx context.Context, accountID string) error {
if accountID == "" {
return ErrMissingAccountID
}
uri := fmt.Sprintf("/accounts/%s", accountID)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
return nil
}