From 00aec477a4f9d48081d98a112c7c382b5cbf2ede Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Tue, 12 Sep 2023 14:46:49 +0000 Subject: [PATCH] dns: DefaultDNSProvider() using CLOUDFLARE_DNS_API_TOKEN Signed-off-by: Alejandro Mery --- go.mod | 1 + go.sum | 3 +++ pkg/dns/provider.go | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 pkg/dns/provider.go diff --git a/go.mod b/go.mod index 939924e..605a3a2 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( darvaza.org/slog/handlers/discard v0.4.5 github.com/gofrs/uuid/v5 v5.0.0 github.com/hack-pad/hackpadfs v0.2.1 + github.com/libdns/cloudflare v0.1.0 github.com/libdns/libdns v0.2.1 github.com/mgechev/revive v1.3.3 github.com/spf13/cobra v1.7.0 diff --git a/go.sum b/go.sum index b0a0a72..3cc377a 100644 --- a/go.sum +++ b/go.sum @@ -36,6 +36,9 @@ github.com/hack-pad/hackpadfs v0.2.1 h1:FelFhIhv26gyjujoA/yeFO+6YGlqzmc9la/6iKMI github.com/hack-pad/hackpadfs v0.2.1/go.mod h1:khQBuCEwGXWakkmq8ZiFUvUZz84ZkJ2KNwKvChs4OrU= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/libdns/cloudflare v0.1.0 h1:93WkJaGaiXCe353LHEP36kAWCUw0YjFqwhkBkU2/iic= +github.com/libdns/cloudflare v0.1.0/go.mod h1:a44IP6J1YH6nvcNl1PverfJviADgXUnsozR3a7vBKN8= +github.com/libdns/libdns v0.2.0/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40= github.com/libdns/libdns v0.2.1 h1:Wu59T7wSHRgtA0cfxC+n1c/e+O3upJGWytknkmFEDis= github.com/libdns/libdns v0.2.1/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40= github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= diff --git a/pkg/dns/provider.go b/pkg/dns/provider.go new file mode 100644 index 0000000..6a53a24 --- /dev/null +++ b/pkg/dns/provider.go @@ -0,0 +1,36 @@ +package dns + +import ( + "fmt" + "os" + + "github.com/libdns/cloudflare" + "github.com/libdns/libdns" +) + +const ( + // CloudflareAPIToken is the environment variable + // containing the API Token + CloudflareAPIToken = "CLOUDFLARE_DNS_API_TOKEN" +) + +// Provider manages DNS entries +type Provider interface { + libdns.RecordGetter + libdns.RecordDeleter +} + +// DefaultDNSProvider returns a cloudflare DNS provider +// using an API Token from env [CloudflareAPIToken] +func DefaultDNSProvider() (*cloudflare.Provider, error) { + s := os.Getenv(CloudflareAPIToken) + if s == "" { + return nil, fmt.Errorf("%q: %s", CloudflareAPIToken, "not found") + } + + p := &cloudflare.Provider{ + APIToken: s, + } + + return p, nil +}