digitalocean

package module
v0.0.0-...-dfa7af5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2025 License: MIT Imports: 7 Imported by: 29 modules, 21 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

DigitalOcean for libdns

godoc reference

This package implements the libdns interfaces for the DigitalOcean API (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)

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())
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

type DNS

type DNS struct {
	Record libdns.RR
	ID     string
}

DNS custom struct that implements the libdns.Record interface and keeps the ID field used internally

func (DNS) RR

func (d DNS) RR() libdns.RR

type Provider

type Provider struct {
	Client
	// APIToken is the DigitalOcean API token - see https://www.digitalocean.com/docs/apis-clis/api/create-personal-access-token/
	APIToken string `json:"auth_token"`
}

Provider implements the libdns interfaces for DigitalOcean

func (*Provider) AppendRecords

func (p *Provider) AppendRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error)

AppendRecords adds records to the zone. It returns the records that were added.

func (*Provider) DeleteRecords

func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error)

DeleteRecords deletes the records from the zone.

func (*Provider) GetRecords

func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error)

GetRecords lists all the records in the zone.

func (*Provider) SetRecords

func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error)

SetRecords sets the records in the zone, either by updating existing records or creating new ones. It returns the updated records.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL