forked from Craigdigital/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fallback_domain.go
79 lines (64 loc) · 2.37 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
package cloudflare
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/pkg/errors"
)
// 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{}, errors.Wrap(err, errUnmarshalError)
}
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{}, errors.Wrap(err, errUnmarshalError)
}
return fallbackDomainResponse.Result, nil
}
// RestoreFallbackDomainDefaults resets the domain fallback values to the default
// list.
//
// 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
}