forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_keys.go
64 lines (52 loc) · 2.29 KB
/
access_keys.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
package cloudflare
import (
"context"
"fmt"
"net/http"
"time"
"github.com/goccy/go-json"
)
type AccessKeysConfig struct {
KeyRotationIntervalDays int `json:"key_rotation_interval_days"`
LastKeyRotationAt time.Time `json:"last_key_rotation_at"`
DaysUntilNextRotation int `json:"days_until_next_rotation"`
}
type AccessKeysConfigUpdateRequest struct {
KeyRotationIntervalDays int `json:"key_rotation_interval_days"`
}
type accessKeysConfigResponse struct {
Response
Result AccessKeysConfig `json:"result"`
}
// AccessKeysConfig returns the Access Keys Configuration for an account.
//
// API reference: https://api.cloudflare.com/#access-keys-configuration-get-access-keys-configuration
func (api *API) AccessKeysConfig(ctx context.Context, accountID string) (AccessKeysConfig, error) {
uri := fmt.Sprintf("/%s/%s/access/keys", AccountRouteRoot, accountID)
return api.accessKeysRequest(ctx, http.MethodGet, uri, nil)
}
// UpdateAccessKeysConfig updates the Access Keys Configuration for an account.
//
// API reference: https://api.cloudflare.com/#access-keys-configuration-update-access-keys-configuration
func (api *API) UpdateAccessKeysConfig(ctx context.Context, accountID string, request AccessKeysConfigUpdateRequest) (AccessKeysConfig, error) {
uri := fmt.Sprintf("/%s/%s/access/keys", AccountRouteRoot, accountID)
return api.accessKeysRequest(ctx, http.MethodPut, uri, request)
}
// RotateAccessKeys rotates the Access Keys for an account and returns the updated Access Keys Configuration
//
// API reference: https://api.cloudflare.com/#access-keys-configuration-rotate-access-keys
func (api *API) RotateAccessKeys(ctx context.Context, accountID string) (AccessKeysConfig, error) {
uri := fmt.Sprintf("/%s/%s/access/keys/rotate", AccountRouteRoot, accountID)
return api.accessKeysRequest(ctx, http.MethodPost, uri, nil)
}
func (api *API) accessKeysRequest(ctx context.Context, method, uri string, params interface{}) (AccessKeysConfig, error) {
res, err := api.makeRequestContext(ctx, method, uri, params)
if err != nil {
return AccessKeysConfig{}, err
}
var keysConfigResponse accessKeysConfigResponse
if err := json.Unmarshal(res, &keysConfigResponse); err != nil {
return AccessKeysConfig{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return keysConfigResponse.Result, nil
}