Skip to content

Commit 8ff1fa9

Browse files
authored
Merge pull request #760 from kjhandy/khandy/WDAPI-659
Adding support for fallback domains
2 parents 02c51b7 + 95dfa19 commit 8ff1fa9

File tree

2 files changed

+204
-0
lines changed

2 files changed

+204
-0
lines changed

fallback_domain.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package cloudflare
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"net/http"
8+
9+
"github.com/pkg/errors"
10+
)
11+
12+
// FallbackDomainResponse represents the response from the get fallback
13+
// domain endpoints.
14+
type FallbackDomainResponse struct {
15+
Response
16+
Result []FallbackDomain `json:"result"`
17+
}
18+
19+
// FallbackDomain represents the individual domain struct.
20+
type FallbackDomain struct {
21+
Suffix string `json:"suffix,omitempty"`
22+
Description string `json:"description,omitempty"`
23+
DNSServer string `json:"dns_server,omitempty"`
24+
}
25+
26+
// ListFallbackDomains returns all fallback domains within an account.
27+
//
28+
// API reference: https://api.cloudflare.com/#devices-get-local-domain-fallback-list
29+
func (api *API) ListFallbackDomains(ctx context.Context, accountID string) ([]FallbackDomain, error) {
30+
uri := fmt.Sprintf("/%s/%s/devices/policy/fallback_domains", AccountRouteRoot, accountID)
31+
32+
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
33+
if err != nil {
34+
return []FallbackDomain{}, err
35+
}
36+
37+
var fallbackDomainResponse FallbackDomainResponse
38+
err = json.Unmarshal(res, &fallbackDomainResponse)
39+
if err != nil {
40+
return []FallbackDomain{}, errors.Wrap(err, errUnmarshalError)
41+
}
42+
43+
return fallbackDomainResponse.Result, nil
44+
}
45+
46+
// UpdateFallbackDomain updates the existing fallback domain policy.
47+
//
48+
// API reference: https://api.cloudflare.com/#devices-set-local-domain-fallback-list
49+
func (api *API) UpdateFallbackDomain(ctx context.Context, accountID string, domains []FallbackDomain) ([]FallbackDomain, error) {
50+
uri := fmt.Sprintf("/%s/%s/devices/policy/fallback_domains", AccountRouteRoot, accountID)
51+
52+
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, domains)
53+
if err != nil {
54+
return []FallbackDomain{}, err
55+
}
56+
57+
var fallbackDomainResponse FallbackDomainResponse
58+
err = json.Unmarshal(res, &fallbackDomainResponse)
59+
if err != nil {
60+
return []FallbackDomain{}, errors.Wrap(err, errUnmarshalError)
61+
}
62+
63+
return fallbackDomainResponse.Result, nil
64+
}

fallback_domain_test.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package cloudflare
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestListFallbackDomain(t *testing.T) {
13+
setup()
14+
defer teardown()
15+
16+
handler := func(w http.ResponseWriter, r *http.Request) {
17+
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
18+
w.Header().Set("content-type", "application/json")
19+
fmt.Fprintf(w, `
20+
{
21+
"success": true,
22+
"errors": [],
23+
"messages": [],
24+
"result": [
25+
{
26+
"suffix": "example.com",
27+
"description": "Domain bypass for local development"
28+
}
29+
]
30+
}
31+
`)
32+
}
33+
34+
want := []FallbackDomain{{
35+
Suffix: "example.com",
36+
Description: "Domain bypass for local development",
37+
}}
38+
39+
mux.HandleFunc("/accounts/"+testAccountID+"/devices/policy/fallback_domains", handler)
40+
41+
actual, err := client.ListFallbackDomains(context.Background(), testAccountID)
42+
43+
if assert.NoError(t, err) {
44+
assert.Equal(t, want, actual)
45+
}
46+
}
47+
48+
func TestFallbackDomainDNSServer(t *testing.T) {
49+
setup()
50+
defer teardown()
51+
52+
handler := func(w http.ResponseWriter, r *http.Request) {
53+
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
54+
w.Header().Set("content-type", "application/json")
55+
fmt.Fprintf(w, `
56+
{
57+
"success": true,
58+
"errors": [],
59+
"messages": [],
60+
"result": [
61+
{
62+
"suffix": "example.com",
63+
"description": "Domain bypass for local development",
64+
"dns_server": "['192.168.0.1', '10.1.1.1']"
65+
}
66+
]
67+
}
68+
`)
69+
}
70+
71+
want := []FallbackDomain{{
72+
Suffix: "example.com",
73+
Description: "Domain bypass for local development",
74+
DNSServer: "['192.168.0.1', '10.1.1.1']",
75+
}}
76+
77+
mux.HandleFunc("/accounts/"+testAccountID+"/devices/policy/fallback_domains", handler)
78+
79+
actual, err := client.ListFallbackDomains(context.Background(), testAccountID)
80+
81+
if assert.NoError(t, err) {
82+
assert.Equal(t, want, actual)
83+
}
84+
}
85+
86+
func TestUpdateFallbackDomain(t *testing.T) {
87+
setup()
88+
defer teardown()
89+
90+
handler := func(w http.ResponseWriter, r *http.Request) {
91+
assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PUT', got %s", r.Method)
92+
w.Header().Set("content-type", "application/json")
93+
fmt.Fprintf(w, `
94+
{
95+
"success": true,
96+
"errors": [],
97+
"messages": [],
98+
"result": [
99+
{
100+
"suffix": "example_one.com",
101+
"description": "example one",
102+
"dns_server": "['192.168.0.1', '10.1.1.1']"
103+
},
104+
{
105+
"suffix": "example_two.com",
106+
"description": "example two"
107+
},
108+
{
109+
"suffix": "example_three.com",
110+
"description": "example three"
111+
}
112+
]
113+
}
114+
`)
115+
}
116+
117+
domains := []FallbackDomain{
118+
{
119+
Suffix: "example_one.com",
120+
Description: "example one",
121+
DNSServer: "['192.168.0.1', '10.1.1.1']",
122+
},
123+
{
124+
Suffix: "example_two.com",
125+
Description: "example two",
126+
},
127+
{
128+
Suffix: "example_three.com",
129+
Description: "example three",
130+
},
131+
}
132+
133+
mux.HandleFunc("/accounts/"+testAccountID+"/devices/policy/fallback_domains", handler)
134+
135+
actual, err := client.UpdateFallbackDomain(context.Background(), testAccountID, domains)
136+
137+
if assert.NoError(t, err) {
138+
assert.Equal(t, domains, actual)
139+
}
140+
}

0 commit comments

Comments
 (0)