Documentation
¶
Overview ¶
Package cfdns is a non-official GO CloudFlare DNS API client for go.
All requests sent by this library use configurable exponential back-off for retrying failed requests and implements a soft-limiter to avoid exhausting CloudFlare's client request quota.
Index ¶
- Variables
- func ReadAll[T any](ctx context.Context, it *Iterator[T]) ([]*T, error)
- type Client
- func (c *Client) CreateRecord(ctx context.Context, req *CreateRecordRequest) (*CreateRecordResponse, error)
- func (c *Client) DeleteRecord(ctx context.Context, req *DeleteRecordRequest) (*DeleteRecordResponse, error)
- func (c *Client) ListRecords(req *ListRecordsRequest) *Iterator[ListRecordsResponseItem]
- func (c *Client) ListZones(_ *ListZonesRequest) *Iterator[ListZonesResponseItem]
- func (c *Client) UpdateRecord(ctx context.Context, req *UpdateRecordRequest) (*UpdateRecordResponse, error)
- type CloudFlareError
- type CreateRecordRequest
- type CreateRecordResponse
- type Credentials
- type DeleteRecordRequest
- type DeleteRecordResponse
- type HTTPError
- type Iterator
- type ListRecordsRequest
- type ListRecordsResponseItem
- type ListZonesRequest
- type ListZonesResponseItem
- type Option
- type UpdateRecordRequest
- type UpdateRecordResponse
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrEmptyToken = errors.New("Provided token is empty")
ErrEmptyToken is returned when the credentials generator produces an empty authentication token.
Functions ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func NewClient ¶
func NewClient(creds Credentials, options ...Option) *Client
func (*Client) CreateRecord ¶
func (c *Client) CreateRecord( ctx context.Context, req *CreateRecordRequest, ) (*CreateRecordResponse, error)
CreateRecord creates a DNS record on CloudFlare. A TTL of 1 second or less will use CloudFlare's "automatic" TTL.
API Reference: https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-create-dns-record
Example ¶
package main import ( "context" "fmt" "os" "time" "github.com/simplesurance/cfdns" ) func main() { 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 (*Client) DeleteRecord ¶
func (c *Client) DeleteRecord( ctx context.Context, req *DeleteRecordRequest, ) (*DeleteRecordResponse, error)
DeleteRecord deletes a DNS record on CloudFlare.
API Reference: https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-delete-dns-record
func (*Client) ListRecords ¶
func (c *Client) ListRecords( req *ListRecordsRequest, ) *Iterator[ListRecordsResponseItem]
ListRecords lists DNS records on a zone.
API Reference: https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-list-dns-records
func (*Client) ListZones ¶
func (c *Client) ListZones( _ *ListZonesRequest, ) *Iterator[ListZonesResponseItem]
Listzones lists zones on CloudFlare.
API Reference: https://developers.cloudflare.com/api/operations/zones-get
Example ¶
package main import ( "context" "errors" "fmt" "io" "os" "github.com/simplesurance/cfdns" ) func main() { 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 (*Client) UpdateRecord ¶
func (c *Client) UpdateRecord( ctx context.Context, req *UpdateRecordRequest, ) (*UpdateRecordResponse, error)
UpdateRecord updates a DNS record on CloudFlare. A TTL of 1 second or less will use the "automatic" TTL from CloudFlare.
API Reference: https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-update-dns-record
type CloudFlareError ¶
type CloudFlareError struct { HTTPError HTTPError // contains filtered or unexported fields }
Example ¶
package main import ( "context" "errors" "fmt" "os" "time" "github.com/simplesurance/cfdns" ) func main() { 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.
func (CloudFlareError) Error ¶
func (ce CloudFlareError) Error() string
func (*CloudFlareError) IsAnyCFErrorCode ¶
IsAnyCFErrorCode returns true if the CloudFlare error includes any of the provided codes.
func (CloudFlareError) Unwrap ¶
func (ce CloudFlareError) Unwrap() error
type CreateRecordRequest ¶
type CreateRecordResponse ¶
type Credentials ¶
type Credentials interface {
// contains filtered or unexported methods
}
func APIToken ¶
func APIToken(token string) (Credentials, error)
type DeleteRecordRequest ¶
type DeleteRecordResponse ¶
type DeleteRecordResponse struct{}
type HTTPError ¶
Example ¶
package main import ( "context" "errors" "fmt" "os" "time" "github.com/simplesurance/cfdns" ) func main() { 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 (HTTPError) IsPermanent ¶
IsPermanent returns true if should not try again the same request.
type Iterator ¶
type Iterator[T any] struct { // contains filtered or unexported fields }
Iterator implements an iterator algorithm from a function that fetches blocks of data. This allows having fixed-memory usage when reading arbitrary-sized structs without leaking implementation details about how to paginate consecutive blocks of data.
type ListRecordsRequest ¶
type ListRecordsResponseItem ¶
type ListZonesRequest ¶
type ListZonesRequest struct{}
type ListZonesResponseItem ¶
type Option ¶
type Option func(*settings)
func WithHTTPClient ¶
func WithLogSuccessfulResponses ¶
WithLogSuccessfulResponses allows logging full request and response send to CloudFlare in case of successful response. Debug log must also be enabled.
Error responses will always be logged if debug log is enabled.
func WithLogger ¶
func WithRateLimiter ¶
func WithRequestTimeout ¶
WithRequestTimeout configures how long to wait for an HTTP request. The default is 1 minute. Setting a value of 0 will make it use the default behavior of the HTTP client being used, that might be waiting forever.
type UpdateRecordRequest ¶
type UpdateRecordResponse ¶
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
niltarget
Package niltarget is a log driver that does nothing, dropping all messages.
|
Package niltarget is a log driver that does nothing, dropping all messages. |
testtarget
Package testtarget is a log driver that sends log messages to a go test.
|
Package testtarget is a log driver that sends log messages to a go test. |
texttarget
Package texttarget is a log driver that encodes log messages as text and writes log messages to a provided writer.
|
Package texttarget is a log driver that encodes log messages as text and writes log messages to a provided writer. |