-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.go
144 lines (113 loc) · 2.99 KB
/
examples_test.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
package cfdns_test
import (
"context"
"errors"
"fmt"
"io"
"os"
"time"
"github.com/simplesurance/cfdns"
)
func ExampleClient_ListZones() {
ctx := context.Background()
apitoken := os.Getenv("TEST_CF_APITOKEN")
creds, err := cfdns.APIToken(apitoken)
if err != nil {
panic(err)
}
client := cfdns.NewClient(creds)
iter := client.ListZones(&cfdns.ListZonesRequest{})
for {
zone, err := iter.Next(ctx)
if err != nil {
if errors.Is(err, io.EOF) {
break
}
panic(err)
}
fmt.Printf("Found zone %s\n", zone.Name)
}
// Output: Found zone simplesurance.top
}
func ExampleClient_CreateRecord() {
ctx := context.Background()
apitoken := os.Getenv("TEST_CF_APITOKEN")
testZoneID := os.Getenv("TEST_CF_ZONE_ID")
creds, err := cfdns.APIToken(apitoken)
if err != nil {
panic(err)
}
client := cfdns.NewClient(creds)
resp, err := client.CreateRecord(ctx, &cfdns.CreateRecordRequest{
ZoneID: testZoneID,
Name: "example-record",
Type: "CNAME",
Content: "github.com",
Proxied: false,
Comment: "Created by cfdns example",
TTL: 30 * time.Minute,
})
if err != nil {
panic(err)
}
fmt.Printf("Created DNS record %s", resp.Name)
// cleanup
_, _ = client.DeleteRecord(ctx, &cfdns.DeleteRecordRequest{
ZoneID: testZoneID,
RecordID: resp.ID,
})
// Output: Created DNS record example-record.simplesurance.top
}
func ExampleHTTPError() {
ctx := context.Background()
apitoken := os.Getenv("TEST_CF_APITOKEN")
testZoneID := os.Getenv("TEST_CF_ZONE_ID")
creds, err := cfdns.APIToken(apitoken)
if err != nil {
panic(err)
}
client := cfdns.NewClient(creds)
_, err = client.CreateRecord(ctx, &cfdns.CreateRecordRequest{
ZoneID: testZoneID,
Name: "invalid name",
Type: "A",
Content: "github.com",
Comment: "Created by cfdns example",
TTL: 30 * time.Minute,
})
httpErr := cfdns.HTTPError{}
if !errors.As(err, &httpErr) {
panic("not an HTTP error")
}
fmt.Printf("Got HTTP error %v", httpErr.Code) // can also access response headers and raw response body
// Output: Got HTTP error 400
}
func ExampleCloudFlareError() {
ctx := context.Background()
apitoken := os.Getenv("TEST_CF_APITOKEN")
testZoneID := os.Getenv("TEST_CF_ZONE_ID")
creds, err := cfdns.APIToken(apitoken)
if err != nil {
panic(err)
}
client := cfdns.NewClient(creds)
_, err = client.CreateRecord(ctx, &cfdns.CreateRecordRequest{
ZoneID: testZoneID,
Name: "invalid name",
Type: "A",
Content: "github.com",
Comment: "Created by cfdns example",
TTL: 30 * time.Minute,
})
cfErr := cfdns.CloudFlareError{}
if !errors.As(err, &cfErr) {
panic("not a CloudFlareError")
}
fmt.Printf("Got HTTP error %v\n", cfErr.HTTPError.Code) // can also access response headers and raw response body
for _, cfe := range cfErr.Errors {
fmt.Printf("- CF error %d: %s\n", cfe.Code, cfe.Message) // can also access response headers and raw response body
}
// Output:
// Got HTTP error 400
// - CF error 9005: Content for A record must be a valid IPv4 address.
}