forked from cloudflare/cloudflare-go
/
fallback_domain.go
132 lines (107 loc) · 4.53 KB
/
fallback_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
package cloudflare
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// FallbackDomainResponse represents the response from the get fallback
// domain endpoints.
type FallbackDomainResponse struct {
Response
Result []FallbackDomain `json:"result"`
}
// FallbackDomain represents the individual domain struct.
type FallbackDomain struct {
Suffix string `json:"suffix,omitempty"`
Description string `json:"description,omitempty"`
DNSServer []string `json:"dns_server,omitempty"`
}
// ListFallbackDomains returns all fallback domains within an account.
//
// API reference: https://api.cloudflare.com/#devices-get-local-domain-fallback-list
func (api *API) ListFallbackDomains(ctx context.Context, accountID string) ([]FallbackDomain, error) {
uri := fmt.Sprintf("/%s/%s/devices/policy/fallback_domains", AccountRouteRoot, accountID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []FallbackDomain{}, err
}
var fallbackDomainResponse FallbackDomainResponse
err = json.Unmarshal(res, &fallbackDomainResponse)
if err != nil {
return []FallbackDomain{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return fallbackDomainResponse.Result, nil
}
// ListFallbackDomainsDeviceSettingsPolicy returns all fallback domains within an account for a specific device settings policy.
//
// API reference: https://api.cloudflare.com/#devices-get-local-domain-fallback-list
func (api *API) ListFallbackDomainsDeviceSettingsPolicy(ctx context.Context, accountID, policyID string) ([]FallbackDomain, error) {
uri := fmt.Sprintf("/%s/%s/devices/policy/%s/fallback_domains", AccountRouteRoot, accountID, policyID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []FallbackDomain{}, err
}
var fallbackDomainResponse FallbackDomainResponse
err = json.Unmarshal(res, &fallbackDomainResponse)
if err != nil {
return []FallbackDomain{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return fallbackDomainResponse.Result, nil
}
// UpdateFallbackDomain updates the existing fallback domain policy.
//
// API reference: https://api.cloudflare.com/#devices-set-local-domain-fallback-list
func (api *API) UpdateFallbackDomain(ctx context.Context, accountID string, domains []FallbackDomain) ([]FallbackDomain, error) {
uri := fmt.Sprintf("/%s/%s/devices/policy/fallback_domains", AccountRouteRoot, accountID)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, domains)
if err != nil {
return []FallbackDomain{}, err
}
var fallbackDomainResponse FallbackDomainResponse
err = json.Unmarshal(res, &fallbackDomainResponse)
if err != nil {
return []FallbackDomain{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return fallbackDomainResponse.Result, nil
}
// UpdateFallbackDomainDeviceSettingsPolicy updates the existing fallback domain policy for a specific device settings policy.
//
// API reference: https://api.cloudflare.com/#devices-set-local-domain-fallback-list
func (api *API) UpdateFallbackDomainDeviceSettingsPolicy(ctx context.Context, accountID, policyID string, domains []FallbackDomain) ([]FallbackDomain, error) {
uri := fmt.Sprintf("/%s/%s/devices/policy/%s/fallback_domains", AccountRouteRoot, accountID, policyID)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, domains)
if err != nil {
return []FallbackDomain{}, err
}
var fallbackDomainResponse FallbackDomainResponse
err = json.Unmarshal(res, &fallbackDomainResponse)
if err != nil {
return []FallbackDomain{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return fallbackDomainResponse.Result, nil
}
// RestoreFallbackDomainDefaultsDeviceSettingsPolicy resets the domain fallback values to the default
// list for a specific device settings policy.
//
// API reference: TBA.
func (api *API) RestoreFallbackDomainDefaults(ctx context.Context, accountID string) error {
uri := fmt.Sprintf("/%s/%s/devices/policy/fallback_domains?reset_defaults=true", AccountRouteRoot, accountID)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, []string{})
if err != nil {
return err
}
return nil
}
// RestoreFallbackDomainDefaults resets the domain fallback values to the default
// list.
//
// API reference: TBA.
func (api *API) RestoreFallbackDomainDefaultsDeviceSettingsPolicy(ctx context.Context, accountID, policyID string) error {
uri := fmt.Sprintf("/%s/%s/devices/policy/%s/fallback_domains?reset_defaults=true", AccountRouteRoot, accountID, policyID)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, []string{})
if err != nil {
return err
}
return nil
}