forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hyperdrive.go
214 lines (173 loc) · 6.69 KB
/
hyperdrive.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package cloudflare
import (
"context"
"errors"
"fmt"
"net/http"
"github.com/goccy/go-json"
)
var (
ErrMissingHyperdriveConfigID = errors.New("required hyperdrive config id is missing")
ErrMissingHyperdriveConfigName = errors.New("required hyperdrive config name is missing")
ErrMissingHyperdriveConfigOriginDatabase = errors.New("required hyperdrive config origin database is missing")
ErrMissingHyperdriveConfigOriginPassword = errors.New("required hyperdrive config origin password is missing")
ErrMissingHyperdriveConfigOriginHost = errors.New("required hyperdrive config origin host is missing")
ErrMissingHyperdriveConfigOriginScheme = errors.New("required hyperdrive config origin scheme is missing")
ErrMissingHyperdriveConfigOriginUser = errors.New("required hyperdrive config origin user is missing")
)
type HyperdriveConfig struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Origin HyperdriveConfigOrigin `json:"origin,omitempty"`
Caching HyperdriveConfigCaching `json:"caching,omitempty"`
}
type HyperdriveConfigOrigin struct {
Database string `json:"database,omitempty"`
Password string `json:"password"`
Host string `json:"host,omitempty"`
Port int `json:"port,omitempty"`
Scheme string `json:"scheme,omitempty"`
User string `json:"user,omitempty"`
}
type HyperdriveConfigCaching struct {
Disabled *bool `json:"disabled,omitempty"`
MaxAge int `json:"max_age,omitempty"`
StaleWhileRevalidate int `json:"stale_while_revalidate,omitempty"`
}
type HyperdriveConfigListResponse struct {
Response
Result []HyperdriveConfig `json:"result"`
}
type CreateHyperdriveConfigParams struct {
Name string `json:"name"`
Origin HyperdriveConfigOrigin `json:"origin"`
Caching HyperdriveConfigCaching `json:"caching,omitempty"`
}
type HyperdriveConfigResponse struct {
Response
Result HyperdriveConfig `json:"result"`
}
type UpdateHyperdriveConfigParams struct {
HyperdriveID string `json:"-"`
Name string `json:"name"`
Origin HyperdriveConfigOrigin `json:"origin"`
Caching HyperdriveConfigCaching `json:"caching,omitempty"`
}
type ListHyperdriveConfigParams struct{}
// ListHyperdriveConfigs returns the Hyperdrive configs owned by an account.
//
// API reference: https://developers.cloudflare.com/api/operations/list-hyperdrive
func (api *API) ListHyperdriveConfigs(ctx context.Context, rc *ResourceContainer, params ListHyperdriveConfigParams) ([]HyperdriveConfig, error) {
if rc.Identifier == "" {
return []HyperdriveConfig{}, ErrMissingAccountID
}
hResponse := HyperdriveConfigListResponse{}
uri := fmt.Sprintf("/accounts/%s/hyperdrive/configs", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []HyperdriveConfig{}, err
}
err = json.Unmarshal(res, &hResponse)
if err != nil {
return []HyperdriveConfig{}, fmt.Errorf("failed to unmarshal filters JSON data: %w", err)
}
return hResponse.Result, nil
}
// CreateHyperdriveConfig creates a new Hyperdrive config.
//
// API reference: https://developers.cloudflare.com/api/operations/create-hyperdrive
func (api *API) CreateHyperdriveConfig(ctx context.Context, rc *ResourceContainer, params CreateHyperdriveConfigParams) (HyperdriveConfig, error) {
if rc.Identifier == "" {
return HyperdriveConfig{}, ErrMissingAccountID
}
if params.Name == "" {
return HyperdriveConfig{}, ErrMissingHyperdriveConfigName
}
uri := fmt.Sprintf("/accounts/%s/hyperdrive/configs", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params)
if err != nil {
return HyperdriveConfig{}, err
}
var r HyperdriveConfigResponse
err = json.Unmarshal(res, &r)
if err != nil {
return HyperdriveConfig{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// DeleteHyperdriveConfig deletes a Hyperdrive config.
//
// API reference: https://developers.cloudflare.com/api/operations/delete-hyperdrive
func (api *API) DeleteHyperdriveConfig(ctx context.Context, rc *ResourceContainer, hyperdriveID string) error {
if rc.Identifier == "" {
return ErrMissingAccountID
}
if hyperdriveID == "" {
return ErrMissingHyperdriveConfigID
}
uri := fmt.Sprintf("/accounts/%s/hyperdrive/configs/%s", rc.Identifier, hyperdriveID)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return fmt.Errorf("%s: %w", errMakeRequestError, err)
}
return nil
}
// GetHyperdriveConfig returns a single Hyperdrive config based on the ID.
//
// API reference: https://developers.cloudflare.com/api/operations/get-hyperdrive
func (api *API) GetHyperdriveConfig(ctx context.Context, rc *ResourceContainer, hyperdriveID string) (HyperdriveConfig, error) {
if rc.Identifier == "" {
return HyperdriveConfig{}, ErrMissingAccountID
}
if hyperdriveID == "" {
return HyperdriveConfig{}, ErrMissingHyperdriveConfigID
}
uri := fmt.Sprintf("/accounts/%s/hyperdrive/configs/%s", rc.Identifier, hyperdriveID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return HyperdriveConfig{}, err
}
var r HyperdriveConfigResponse
err = json.Unmarshal(res, &r)
if err != nil {
return HyperdriveConfig{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// UpdateHyperdriveConfig updates a Hyperdrive config.
//
// API reference: https://developers.cloudflare.com/api/operations/update-hyperdrive
func (api *API) UpdateHyperdriveConfig(ctx context.Context, rc *ResourceContainer, params UpdateHyperdriveConfigParams) (HyperdriveConfig, error) {
if rc.Identifier == "" {
return HyperdriveConfig{}, ErrMissingAccountID
}
if params.HyperdriveID == "" {
return HyperdriveConfig{}, ErrMissingHyperdriveConfigID
}
if params.Origin.Database == "" {
return HyperdriveConfig{}, ErrMissingHyperdriveConfigOriginDatabase
}
if params.Origin.Password == "" {
return HyperdriveConfig{}, ErrMissingHyperdriveConfigOriginPassword
}
if params.Origin.Host == "" {
return HyperdriveConfig{}, ErrMissingHyperdriveConfigOriginHost
}
if params.Origin.Scheme == "" {
return HyperdriveConfig{}, ErrMissingHyperdriveConfigOriginScheme
}
if params.Origin.User == "" {
return HyperdriveConfig{}, ErrMissingHyperdriveConfigOriginUser
}
uri := fmt.Sprintf("/accounts/%s/hyperdrive/configs/%s", rc.Identifier, params.HyperdriveID)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params)
if err != nil {
return HyperdriveConfig{}, err
}
var r HyperdriveConfigResponse
err = json.Unmarshal(res, &r)
if err != nil {
return HyperdriveConfig{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}