https://github.com/libdns/digitalocean
https://github.com/libdns/digitalocean
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/libdns/digitalocean
- Owner: libdns
- License: mit
- Created: 2020-07-21T01:56:03.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-07-28T22:36:59.000Z (almost 3 years ago)
- Last Synced: 2024-06-18T18:32:48.393Z (almost 2 years ago)
- Language: Go
- Size: 11.7 KB
- Stars: 10
- Watchers: 4
- Forks: 14
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# DigitalOcean for `libdns`
[](https://pkg.go.dev/github.com/libdns/digitalocean)
This package implements the libdns interfaces for the [DigitalOcean API](https://developers.digitalocean.com/documentation/v2/#domains) (using the Go implementation from: https://github.com/digitalocean/godo)
## Authenticating
To authenticate you need to supply a DigitalOcean API token.
## Example
Here's a minimal example of how to get all your DNS records using this `libdns` provider (see `_example/main.go`)
```go
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/libdns/digitalocean"
"github.com/libdns/libdns"
)
func main() {
token := os.Getenv("DO_AUTH_TOKEN")
if token == "" {
fmt.Printf("DO_AUTH_TOKEN not set\n")
return
}
zone := os.Getenv("ZONE")
if zone == "" {
fmt.Printf("ZONE not set\n")
return
}
// NOTE: when `DELETE_ENTRIES` is set to `1`, the script will delete the created entries
deleteEntries := os.Getenv("DELETE_ENTRIES") == "1"
provider := digitalocean.Provider{APIToken: token}
records, err := provider.GetRecords(context.TODO(), zone)
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
}
txtTestName := "libdns-test-txt"
txtTestId := ""
aTestName := "libdns-test-a"
aTestId := ""
for _, record := range records {
fmt.Printf("%s (.%s): %s, %s\n", record.RR().Name, zone, record.RR().Data, record.RR().Type)
if record.RR().Name == txtTestName {
txtTestId = record.(digitalocean.DNS).ID
} else if record.RR().Name == aTestName {
aTestId = record.(digitalocean.DNS).ID
}
}
if txtTestId != "" && aTestId != "" {
if deleteEntries {
fmt.Printf("Delete entry for %s (id:%s)\n", txtTestName, txtTestId)
_, err = provider.DeleteRecords(context.TODO(), zone, []libdns.Record{digitalocean.DNS{ID: txtTestId}})
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
}
fmt.Printf("Delete entry for %s (id:%s)\n", aTestName, aTestId)
_, err = provider.DeleteRecords(context.TODO(), zone, []libdns.Record{digitalocean.DNS{ID: aTestId}})
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
}
} else {
fmt.Printf("Replacing entry for %s\n", txtTestName)
_, err = provider.SetRecords(context.TODO(), zone, []libdns.Record{digitalocean.DNS{
Record: libdns.RR{
Type: "TXT",
Name: txtTestName,
Data: fmt.Sprintf("Replacement test entry created by libdns %s", time.Now()),
TTL: time.Duration(30) * time.Second,
},
ID: txtTestId,
}})
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
}
fmt.Printf("Replacing entry for %s\n", aTestName)
_, err = provider.SetRecords(context.TODO(), zone, []libdns.Record{digitalocean.DNS{
Record: libdns.RR{
Type: "A",
Name: aTestName,
Data: "127.0.0.1",
TTL: time.Duration(30) * time.Second,
},
ID: aTestId,
}})
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
}
}
} else {
fmt.Printf("Creating new entry for %s\n", txtTestName)
_, err = provider.AppendRecords(context.TODO(), zone, []libdns.Record{libdns.RR{
Type: "TXT",
Name: txtTestName,
Data: fmt.Sprintf("This is a test entry created by libdns %s", time.Now()),
TTL: time.Duration(30) * time.Second,
}})
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
}
fmt.Printf("Creating new entry for %s\n", aTestName)
_, err = provider.AppendRecords(context.TODO(), zone, []libdns.Record{libdns.RR{
Type: "A",
Name: aTestName,
Data: "127.0.0.1",
TTL: time.Duration(30) * time.Second,
}})
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
}
}
}
```