zoneManager.go raw
1 package v2
2
3 import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "fmt"
8 "io"
9 "net/http"
10 "time"
11 )
12
13 type (
14 // Zone represents an unmarshalled zone body from API response.
15 Zone struct {
16 ID string `json:"id"`
17 ProjectID string `json:"project_id"`
18 Name string `json:"name"`
19 Comment string `json:"comment"`
20 CreatedAt time.Time `json:"created_at"`
21 UpdatedAt time.Time `json:"updated_at"`
22 Disabled bool `json:"disabled"`
23 delegationInfo
24 }
25
26 delegationInfo struct {
27 DelegationCheckedAt time.Time `json:"delegation_checked_at"`
28 LastDelegatedAt time.Time `json:"last_delegated_at"`
29 LastCheckStatus bool `json:"last_check_status"`
30 }
31
32 zoneCreateForm struct {
33 Name string `json:"name"`
34 }
35
36 zoneUpdateComment struct {
37 Comment string `json:"comment"`
38 }
39
40 zoneUpdateState struct {
41 Disabled bool `json:"disabled"`
42 }
43 zoneProtectionState struct {
44 Protected bool `json:"protected"`
45 }
46 )
47
48 func (z *Zone) CreationForm() (io.Reader, error) {
49 form := zoneCreateForm{Name: z.Name}
50 body, err := json.Marshal(form)
51
52 return bytes.NewReader(body), err
53 }
54
55 // GetZone returns a single zone by its id.
56 func (c *Client) GetZone(ctx context.Context, zoneID string, _ *map[string]string) (*Zone, error) {
57 r, e := c.prepareRequest(
58 ctx, http.MethodGet, fmt.Sprintf(zonePath, zoneID), nil, nil, nil,
59 )
60
61 return processRequest[Zone](c.httpClient, r, e)
62 }
63
64 // ListZones returns a list of zones by options.
65 func (c *Client) ListZones(ctx context.Context, options *map[string]string) (Listable[Zone], error) {
66 r, e := c.prepareRequest(
67 ctx, http.MethodGet, rootPath, nil, options, nil,
68 )
69
70 return processRequest[List[Zone]](c.httpClient, r, e)
71 }
72
73 // CreateZone request to create of a new zone.
74 func (c *Client) CreateZone(ctx context.Context, zone Creatable) (*Zone, error) {
75 body, err := zone.CreationForm()
76 if err != nil {
77 return nil, fmt.Errorf("create zone: %w", err)
78 }
79 r, e := c.prepareRequest(
80 ctx, http.MethodPost, rootPath, body, nil, nil,
81 )
82
83 return processRequest[Zone](c.httpClient, r, e)
84 }
85
86 // DeleteZone request to delete of the zone by id.
87 func (c *Client) DeleteZone(ctx context.Context, zoneID string) error {
88 r, e := c.prepareRequest(
89 ctx, http.MethodDelete, fmt.Sprintf(zonePath, zoneID), nil, nil, nil,
90 )
91 _, err := processRequest[Zone](c.httpClient, r, e)
92
93 return err
94 }
95
96 // UpdateZoneComment request to update the comment for zone by zoneID.
97 func (c *Client) UpdateZoneComment(ctx context.Context, zoneID string, comment string) error {
98 updateComment, err := json.Marshal(zoneUpdateComment{
99 Comment: comment,
100 })
101 if err != nil {
102 return fmt.Errorf("zone marshal: %w", err)
103 }
104 form := bytes.NewReader(updateComment)
105 r, e := c.prepareRequest(
106 ctx, http.MethodPatch, fmt.Sprintf(zonePath, zoneID), form, nil, nil,
107 )
108 _, err = processRequest[Zone](c.httpClient, r, e)
109
110 return err
111 }
112
113 // UpdateZoneState request to enable/disable service for zone by zoneID.
114 func (c *Client) UpdateZoneState(ctx context.Context, zoneID string, disabled bool) error {
115 updateState, err := json.Marshal(zoneUpdateState{
116 Disabled: disabled,
117 })
118 if err != nil {
119 return fmt.Errorf("zone marshal: %w", err)
120 }
121 form := bytes.NewReader(updateState)
122 r, e := c.prepareRequest(
123 ctx, http.MethodPatch, fmt.Sprintf(zonePathUpdateState, zoneID), form, nil, nil,
124 )
125 _, err = processRequest[Zone](c.httpClient, r, e)
126
127 return err
128 }
129
130 // UpdateProtectionState request to enable/disable zone protection from delete operation.
131 func (c *Client) UpdateProtectionState(ctx context.Context, zoneID string, protected bool) error {
132 updateState, err := json.Marshal(zoneProtectionState{
133 Protected: protected,
134 })
135 if err != nil {
136 return fmt.Errorf("zone marshal: %w", err)
137 }
138 form := bytes.NewReader(updateState)
139 r, e := c.prepareRequest(
140 ctx, http.MethodPatch, fmt.Sprintf(zonePathUpdateProtection, zoneID), form, nil, nil,
141 )
142 _, err = processRequest[Zone](c.httpClient, r, e)
143
144 return err
145 }
146