-
Notifications
You must be signed in to change notification settings - Fork 567
/
workers_domain.go
150 lines (124 loc) · 4.43 KB
/
workers_domain.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
package cloudflare
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)
var (
ErrMissingHostname = errors.New("required hostname missing")
ErrMissingService = errors.New("required service missing")
ErrMissingEnvironment = errors.New("required environment missing")
)
type AttachWorkersDomainParams struct {
ID string `json:"id,omitempty"`
ZoneID string `json:"zone_id,omitempty"`
ZoneName string `json:"zone_name,omitempty"`
Hostname string `json:"hostname,omitempty"`
Service string `json:"service,omitempty"`
Environment string `json:"environment,omitempty"`
}
type WorkersDomain struct {
ID string `json:"id,omitempty"`
ZoneID string `json:"zone_id,omitempty"`
ZoneName string `json:"zone_name,omitempty"`
Hostname string `json:"hostname,omitempty"`
Service string `json:"service,omitempty"`
Environment string `json:"environment,omitempty"`
}
type WorkersDomainResponse struct {
Response
Result WorkersDomain `json:"result"`
}
type ListWorkersDomainParams struct {
ZoneID string `url:"zone_id,omitempty"`
ZoneName string `url:"zone_name,omitempty"`
Hostname string `url:"hostname,omitempty"`
Service string `url:"service,omitempty"`
Environment string `url:"environment,omitempty"`
}
type WorkersDomainListResponse struct {
Response
Result []WorkersDomain `json:"result"`
}
// ListWorkersDomains lists all Worker Domains.
//
// API reference: https://api.cloudflare.com/#worker-domain-list-domains
func (api *API) ListWorkersDomains(ctx context.Context, rc *ResourceContainer, params ListWorkersDomainParams) ([]WorkersDomain, error) {
if rc.Identifier == "" {
return []WorkersDomain{}, ErrMissingAccountID
}
uri := buildURI(fmt.Sprintf("/accounts/%s/workers/domains", rc.Identifier), params)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []WorkersDomain{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}
var r WorkersDomainListResponse
if err := json.Unmarshal(res, &r); err != nil {
return []WorkersDomain{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// AttachWorkersDomain attaches a worker to a zone and hostname.
//
// API reference: https://api.cloudflare.com/#worker-domain-attach-to-domain
func (api *API) AttachWorkersDomain(ctx context.Context, rc *ResourceContainer, domain AttachWorkersDomainParams) (WorkersDomain, error) {
if rc.Identifier == "" {
return WorkersDomain{}, ErrMissingAccountID
}
if domain.ZoneID == "" {
return WorkersDomain{}, ErrMissingZoneID
}
if domain.Hostname == "" {
return WorkersDomain{}, ErrMissingHostname
}
if domain.Service == "" {
return WorkersDomain{}, ErrMissingService
}
if domain.Environment == "" {
return WorkersDomain{}, ErrMissingEnvironment
}
uri := fmt.Sprintf("/accounts/%s/workers/domains", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, domain)
if err != nil {
return WorkersDomain{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}
var r WorkersDomainResponse
if err := json.Unmarshal(res, &r); err != nil {
return WorkersDomain{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// GetWorkersDomain gets a single Worker Domain.
//
// API reference: https://api.cloudflare.com/#worker-domain-get-a-domain
func (api *API) GetWorkersDomain(ctx context.Context, rc *ResourceContainer, domainID string) (WorkersDomain, error) {
if rc.Identifier == "" {
return WorkersDomain{}, ErrMissingAccountID
}
uri := fmt.Sprintf("/accounts/%s/workers/domains/%s", rc.Identifier, domainID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return WorkersDomain{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}
var r WorkersDomainResponse
if err := json.Unmarshal(res, &r); err != nil {
return WorkersDomain{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// DetachWorkersDomain detaches a worker from a zone and hostname.
//
// API reference: https://api.cloudflare.com/#worker-domain-detach-from-domain
func (api *API) DetachWorkersDomain(ctx context.Context, rc *ResourceContainer, domainID string) error {
if rc.Identifier == "" {
return ErrMissingAccountID
}
uri := fmt.Sprintf("/accounts/%s/workers/domains/%s", rc.Identifier, domainID)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return fmt.Errorf("%s: %w", errMakeRequestError, err)
}
return nil
}