forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzone_hold.go
111 lines (92 loc) · 3.27 KB
/
zone_hold.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"
"fmt"
"net/http"
"time"
"github.com/goccy/go-json"
)
// Retrieve whether the zone is subject to a zone hold, and metadata about the
// hold.
type ZoneHold struct {
Hold *bool `json:"hold,omitempty"`
IncludeSubdomains *bool `json:"include_subdomains,omitempty"`
HoldAfter *time.Time `json:"hold_after,omitempty"`
}
// ZoneHoldResponse represents a response from the Zone Hold endpoint.
type ZoneHoldResponse struct {
Result ZoneHold `json:"result"`
Response
ResultInfo `json:"result_info"`
}
// CreateZoneHoldParams represents params for the Create Zone Hold
// endpoint.
type CreateZoneHoldParams struct {
IncludeSubdomains *bool `url:"include_subdomains,omitempty"`
}
// DeleteZoneHoldParams represents params for the Delete Zone Hold
// endpoint.
type DeleteZoneHoldParams struct {
HoldAfter *time.Time `url:"hold_after,omitempty"`
}
type GetZoneHoldParams struct{}
// CreateZoneHold enforces a zone hold on the zone, blocking the creation and
// activation of zone.
//
// API reference: https://developers.cloudflare.com/api/operations/zones-0-hold-post
func (api *API) CreateZoneHold(ctx context.Context, rc *ResourceContainer, params CreateZoneHoldParams) (ZoneHold, error) {
if rc.Level != ZoneRouteLevel {
return ZoneHold{}, ErrRequiredZoneLevelResourceContainer
}
uri := buildURI(fmt.Sprintf("/zones/%s/hold", rc.Identifier), params)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return ZoneHold{}, err
}
response := &ZoneHoldResponse{}
err = json.Unmarshal(res, &response)
if err != nil {
return ZoneHold{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return response.Result, nil
}
// DeleteZoneHold removes enforcement of a zone hold on the zone, permanently or
// temporarily, allowing the creation and activation of zones with this hostname.
//
// API reference:https://developers.cloudflare.com/api/operations/zones-0-hold-delete
func (api *API) DeleteZoneHold(ctx context.Context, rc *ResourceContainer, params DeleteZoneHoldParams) (ZoneHold, error) {
if rc.Level != ZoneRouteLevel {
return ZoneHold{}, ErrRequiredZoneLevelResourceContainer
}
uri := buildURI(fmt.Sprintf("/zones/%s/hold", rc.Identifier), params)
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return ZoneHold{}, err
}
response := &ZoneHoldResponse{}
err = json.Unmarshal(res, &response)
if err != nil {
return ZoneHold{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return response.Result, nil
}
// GetZoneHold retrieves whether the zone is subject to a zone hold, and the
// metadata about the hold.
//
// API reference: https://developers.cloudflare.com/api/operations/zones-0-hold-get
func (api *API) GetZoneHold(ctx context.Context, rc *ResourceContainer, params GetZoneHoldParams) (ZoneHold, error) {
if rc.Level != ZoneRouteLevel {
return ZoneHold{}, ErrRequiredZoneLevelResourceContainer
}
uri := fmt.Sprintf("/zones/%s/hold", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return ZoneHold{}, err
}
response := &ZoneHoldResponse{}
err = json.Unmarshal(res, &response)
if err != nil {
return ZoneHold{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return response.Result, nil
}