-
Notifications
You must be signed in to change notification settings - Fork 539
/
tiered_cache.go
316 lines (261 loc) · 9.24 KB
/
tiered_cache.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package cloudflare
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
)
type TieredCacheType int
const (
TieredCacheOff TieredCacheType = 0
TieredCacheGeneric TieredCacheType = 1
TieredCacheSmart TieredCacheType = 2
)
func (e TieredCacheType) String() string {
switch e {
case TieredCacheGeneric:
return "generic"
case TieredCacheSmart:
return "smart"
case TieredCacheOff:
return "off"
default:
return fmt.Sprintf("%d", int(e))
}
}
type TieredCache struct {
Type TieredCacheType
LastModified time.Time
}
// GetTieredCache allows you to retrieve the current Tiered Cache Settings for a Zone.
// This function does not support custom topologies, only Generic and Smart Tiered Caching.
//
// API Reference: https://api.cloudflare.com/#smart-tiered-cache-get-smart-tiered-cache-setting
// API Reference: https://api.cloudflare.com/#tiered-cache-get-tiered-cache-setting
func (api *API) GetTieredCache(ctx context.Context, rc *ResourceContainer) (TieredCache, error) {
var lastModified time.Time
generic, err := getGenericTieredCache(api, ctx, rc)
if err != nil {
return TieredCache{}, err
}
lastModified = generic.LastModified
smart, err := getSmartTieredCache(api, ctx, rc)
if err != nil {
return TieredCache{}, err
}
if smart.LastModified.After(lastModified) {
lastModified = smart.LastModified
}
if generic.Type == TieredCacheOff {
return TieredCache{Type: TieredCacheOff, LastModified: lastModified}, nil
}
if smart.Type == TieredCacheOff {
return TieredCache{Type: TieredCacheGeneric, LastModified: lastModified}, nil
}
return TieredCache{Type: TieredCacheSmart, LastModified: lastModified}, nil
}
// SetTieredCache allows you to set a zone's tiered cache topology between the available types.
// Using the value of TieredCacheOff will disable Tiered Cache entirely.
//
// API Reference: https://api.cloudflare.com/#smart-tiered-cache-patch-smart-tiered-cache-setting
// API Reference: https://api.cloudflare.com/#tiered-cache-patch-tiered-cache-setting
func (api *API) SetTieredCache(ctx context.Context, rc *ResourceContainer, value TieredCacheType) (TieredCache, error) {
if value == TieredCacheOff {
return api.DeleteTieredCache(ctx, rc)
}
var lastModified time.Time
if value == TieredCacheGeneric {
result, err := deleteSmartTieredCache(api, ctx, rc)
if err != nil {
return TieredCache{}, err
}
lastModified = result.LastModified
result, err = enableGenericTieredCache(api, ctx, rc)
if err != nil {
return TieredCache{}, err
}
if result.LastModified.After(lastModified) {
lastModified = result.LastModified
}
return TieredCache{Type: TieredCacheGeneric, LastModified: lastModified}, nil
}
result, err := enableGenericTieredCache(api, ctx, rc)
if err != nil {
return TieredCache{}, err
}
lastModified = result.LastModified
result, err = enableSmartTieredCache(api, ctx, rc)
if err != nil {
return TieredCache{}, err
}
if result.LastModified.After(lastModified) {
lastModified = result.LastModified
}
return TieredCache{Type: TieredCacheSmart, LastModified: lastModified}, nil
}
// DeleteTieredCache allows you to delete the tiered cache settings for a zone.
// This is equivalent to using SetTieredCache with the value of TieredCacheOff.
//
// API Reference: https://api.cloudflare.com/#smart-tiered-cache-delete-smart-tiered-cache-setting
// API Reference: https://api.cloudflare.com/#tiered-cache-patch-tiered-cache-setting
func (api *API) DeleteTieredCache(ctx context.Context, rc *ResourceContainer) (TieredCache, error) {
var lastModified time.Time
result, err := deleteSmartTieredCache(api, ctx, rc)
if err != nil {
return TieredCache{}, err
}
lastModified = result.LastModified
result, err = disableGenericTieredCache(api, ctx, rc)
if err != nil {
return TieredCache{}, err
}
if result.LastModified.After(lastModified) {
lastModified = result.LastModified
}
return TieredCache{Type: TieredCacheOff, LastModified: lastModified}, nil
}
type tieredCacheResult struct {
ID string `json:"id"`
Value string `json:"value,omitempty"`
LastModified time.Time `json:"modified_on"`
}
type tieredCacheResponse struct {
Result tieredCacheResult `json:"result"`
Response
}
type tieredCacheSetting struct {
Value string `json:"value"`
}
func getGenericTieredCache(api *API, ctx context.Context, rc *ResourceContainer) (TieredCache, error) {
uri := fmt.Sprintf("/zones/%s/argo/tiered_caching", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return TieredCache{Type: TieredCacheOff}, err
}
var response tieredCacheResponse
err = json.Unmarshal(res, &response)
if err != nil {
return TieredCache{Type: TieredCacheOff}, err
}
if !response.Success {
return TieredCache{Type: TieredCacheOff}, errors.New("request to retrieve generic tiered cache failed")
}
if response.Result.Value == "off" {
return TieredCache{Type: TieredCacheOff, LastModified: response.Result.LastModified}, nil
}
return TieredCache{Type: TieredCacheGeneric, LastModified: response.Result.LastModified}, nil
}
func getSmartTieredCache(api *API, ctx context.Context, rc *ResourceContainer) (TieredCache, error) {
uri := fmt.Sprintf("/zones/%s/cache/tiered_cache_smart_topology_enable", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
var notFoundError *NotFoundError
if errors.As(err, ¬FoundError) {
return TieredCache{Type: TieredCacheOff}, nil
}
return TieredCache{Type: TieredCacheOff}, err
}
var response tieredCacheResponse
err = json.Unmarshal(res, &response)
if err != nil {
return TieredCache{Type: TieredCacheOff}, err
}
if !response.Success {
return TieredCache{Type: TieredCacheOff}, errors.New("request to retrieve smart tiered cache failed")
}
if response.Result.Value == "off" {
return TieredCache{Type: TieredCacheOff, LastModified: response.Result.LastModified}, nil
}
return TieredCache{Type: TieredCacheSmart, LastModified: response.Result.LastModified}, nil
}
func enableGenericTieredCache(api *API, ctx context.Context, rc *ResourceContainer) (TieredCache, error) {
uri := fmt.Sprintf("/zones/%s/argo/tiered_caching", rc.Identifier)
setting := tieredCacheSetting{
Value: "on",
}
body, err := json.Marshal(setting)
if err != nil {
return TieredCache{Type: TieredCacheOff}, err
}
res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, body)
if err != nil {
return TieredCache{Type: TieredCacheOff}, err
}
var response tieredCacheResponse
err = json.Unmarshal(res, &response)
if err != nil {
return TieredCache{Type: TieredCacheOff}, err
}
if !response.Success {
return TieredCache{Type: TieredCacheOff}, errors.New("request to enable generic tiered cache failed")
}
return TieredCache{Type: TieredCacheGeneric, LastModified: response.Result.LastModified}, nil
}
func enableSmartTieredCache(api *API, ctx context.Context, rc *ResourceContainer) (TieredCache, error) {
uri := fmt.Sprintf("/zones/%s/cache/tiered_cache_smart_topology_enable", rc.Identifier)
setting := tieredCacheSetting{
Value: "on",
}
body, err := json.Marshal(setting)
if err != nil {
return TieredCache{Type: TieredCacheOff}, err
}
res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, body)
if err != nil {
return TieredCache{Type: TieredCacheOff}, err
}
var response tieredCacheResponse
err = json.Unmarshal(res, &response)
if err != nil {
return TieredCache{Type: TieredCacheOff}, err
}
if !response.Success {
return TieredCache{Type: TieredCacheOff}, errors.New("request to enable smart tiered cache failed")
}
return TieredCache{Type: TieredCacheSmart, LastModified: response.Result.LastModified}, nil
}
func disableGenericTieredCache(api *API, ctx context.Context, rc *ResourceContainer) (TieredCache, error) {
uri := fmt.Sprintf("/zones/%s/argo/tiered_caching", rc.Identifier)
setting := tieredCacheSetting{
Value: "off",
}
body, err := json.Marshal(setting)
if err != nil {
return TieredCache{Type: TieredCacheOff}, err
}
res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, body)
if err != nil {
return TieredCache{Type: TieredCacheOff}, err
}
var response tieredCacheResponse
err = json.Unmarshal(res, &response)
if err != nil {
return TieredCache{Type: TieredCacheOff}, err
}
if !response.Success {
return TieredCache{Type: TieredCacheOff}, errors.New("request to disable generic tiered cache failed")
}
return TieredCache{Type: TieredCacheOff, LastModified: response.Result.LastModified}, nil
}
func deleteSmartTieredCache(api *API, ctx context.Context, rc *ResourceContainer) (TieredCache, error) {
uri := fmt.Sprintf("/zones/%s/cache/tiered_cache_smart_topology_enable", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
var notFoundError *NotFoundError
if errors.As(err, ¬FoundError) {
return TieredCache{Type: TieredCacheOff}, nil
}
return TieredCache{Type: TieredCacheOff}, err
}
var response tieredCacheResponse
err = json.Unmarshal(res, &response)
if err != nil {
return TieredCache{Type: TieredCacheOff}, err
}
if !response.Success {
return TieredCache{Type: TieredCacheOff}, errors.New("request to disable smart tiered cache failed")
}
return TieredCache{Type: TieredCacheOff, LastModified: response.Result.LastModified}, nil
}