luadns

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2025 License: MIT Imports: 10 Imported by: 4 modules, 3 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

LuaDNS Go Client

This is the Go client for LuaDNS REST API.

Build Status GoDoc

Usage:

package main

import (
	"context"
	"flag"
	"fmt"
	"log"
	"os"

	api "github.com/luadns/luadns-go"
)

var email string
var key string

func main() {
	flag.StringVar(&email, "email", "joe@example.com", "your email address")
	flag.StringVar(&key, "key", "", "your API key")
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s [options]\n", os.Args[0])
		flag.PrintDefaults()
	}
	flag.Parse()

	ctx := context.Background()

	c := api.NewClient(email, key)
	user, err := c.Me(ctx)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("email:  ", user.Email)
	fmt.Println("name:   ", user.Name)
	fmt.Println("package:", user.Package)

	zones, err := c.ListZones(ctx, &api.ListParams{Query: "example.org"})
	if err != nil {
		log.Fatalln(err)
	}

	for _, z := range zones {
		fmt.Println("===> zone", z.Name)
		records, err := c.ListRecords(ctx, z, &api.ListParams{})
		if err != nil {
			log.Fatalln(err)
		}

		for _, r := range records {
			fmt.Println("    ", r.Name, r.Type, r.Content, r.TTL)
		}
	}
}

Example

$ go run examples/main.go -email=YOUR_EMAIL -key=YOUR_API_KEY
email:   joe@example.com
name:    Example User
package: Free
===> zone example.org
     example.org. SOA ns1.luadns.net. hostmaster.luadns.net. 1693213328 1200 120 604800 3600 3600
     example.org. NS ns1.luadns.net. 86400
     example.org. NS ns2.luadns.net. 86400
     example.org. NS ns3.luadns.net. 86400
     example.org. NS ns4.luadns.net. 86400
     example.org. A 1.1.1.1 86400
     mail.example.org. CNAME ghs.google.com. 86400
     www.example.org. CNAME example.org. 86400
     example.org. MX 5 aspmx.l.google.com. 86400
     _sip._udp.example.org. SRV 0 0 5060 sip.example.com. 86400
     example.org. TXT v=spf1 a mx include:_spf.google.com ~all 86400

Documentation

Index

Constants

View Source
const (
	TypeA        = "A"
	TypeAAAA     = "AAAA"
	TypeALIAS    = "ALIAS"
	TypeCAA      = "CAAA"
	TypeCNAME    = "CNAME"
	TypeDS       = "DS"
	TypeFORWARD  = "FORWARD"
	TypeMX       = "MX"
	TypeNS       = "NS"
	TypePTR      = "PTR"
	TypeREDIRECT = "REDIRECT"
	TypeSLAVE    = "SLAVE"
	TypeSOA      = "SOA"
	TypeSPF      = "SPF"
	TypeSRV      = "SRV"
	TypeSSHFP    = "SSHFP"
	TypeTLSA     = "TLSA"
	TypeTXT      = "TXT"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BadRequestError

type BadRequestError []InputError

BadRequestError represents a list of validation errors returned by the API server.

func (BadRequestError) Error

func (e BadRequestError) Error() string

type Client

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

Client represents a REST API client for LuaDNS API.

func NewClient

func NewClient(email, apiKey string, opts ...OptFunc) *Client

NewClient initializes the REST API client and configures authentication.

func (*Client) CreateManyRecords added in v0.3.0

func (c *Client) CreateManyRecords(ctx context.Context, zone *Zone, recs []*RR) ([]*Record, error)

CreateManyRecords creates multiple DNS records using supplied RRs.

See: http://www.luadns.com/api.html#create-many-records

func (*Client) CreateRecord

func (c *Client) CreateRecord(ctx context.Context, zone *Zone, attrs *Record) (*Record, error)

CreateRecord creates a zone record using supplied attributes.

See: http://www.luadns.com/api.html#create-a-record

func (*Client) CreateZone

func (c *Client) CreateZone(ctx context.Context, attrs *Zone) (*Zone, error)

CreateZone creates a new zone using supplied attributes.

See: http://www.luadns.com/api.html#create-a-zone

func (*Client) DeleteManyRecords added in v0.3.0

func (c *Client) DeleteManyRecords(ctx context.Context, zone *Zone, recs []*RR) ([]*Record, error)

DeleteManyRecords deletes multiple zone records matching supplied RRs.

Only records with exact match of input name, type, content and TTL are being deleted. For input RRs only name is required, the other fields are optional.

See: http://www.luadns.com/api.html#delete-many-records

Example:

  • &RR{{Name: "example.com."}} - matches all example.com. records
  • &RR{{Name: "example.com.", Type: "TXT"}} - matches all example.com. records of TXT type

func (*Client) DeleteRecord

func (c *Client) DeleteRecord(ctx context.Context, zone *Zone, recordID int64) (*Record, error)

DeleteRecord deletes a zone record identfied by `recordID`.

See: http://www.luadns.com/api.html#delete-a-record

func (*Client) DeleteZone

func (c *Client) DeleteZone(ctx context.Context, zoneID int64) (*Zone, error)

DeleteZone delete specific zone using zone ID.

See: http://www.luadns.com/api.html#delete-a-zone

func (*Client) GetRecord

func (c *Client) GetRecord(ctx context.Context, zone *Zone, recordID int64) (*Record, error)

GetRecord returns a zone record identified by `recordID`.

See: http://www.luadns.com/api.html#get-a-record

func (*Client) GetZone

func (c *Client) GetZone(ctx context.Context, zoneID int64) (*Zone, error)

GetZone get a specific zone identfied by `zoneID`.

See: http://www.luadns.com/api.html#get-a-zone

func (*Client) ListRecords

func (c *Client) ListRecords(ctx context.Context, zone *Zone, options *ListParams, handlers ...HandlerFunc) ([]*Record, error)

ListRecords returns zone records.

See: http://www.luadns.com/api.html#list-records

func (*Client) ListZones

func (c *Client) ListZones(ctx context.Context, options *ListParams, handlers ...HandlerFunc) ([]*Zone, error)

ListZones returns user zones.

See: http://www.luadns.com/api.html#list-zones

func (*Client) Me

func (c *Client) Me(ctx context.Context) (*User, error)

func (*Client) UpdateManyRecords added in v0.3.0

func (c *Client) UpdateManyRecords(ctx context.Context, zone *Zone, recs []*RR) ([]*Record, error)

UpdateManyRecords updates multiple DNS records using supplied RRs.

For any (name, type) pairs in the input it will change zone records with supplied input records, other records remains unchanged.

See: http://www.luadns.com/api.html#update-many-records

Example:

;; Original records
example.com. 3600 IN A   1.1.1.1
example.com. 3600 IN A   2.2.2.2
example.com. 3600 IN TXT "hello world"

;; Input records
example.com. 3600 IN A   1.1.1.1
example.com. 3600 IN A   3.3.3.3

;; Result records
example.com. 3600 IN A   1.1.1.1
example.com. 3600 IN A   3.3.3.3
example.com. 3600 IN TXT "hello world"

func (*Client) UpdateRecord

func (c *Client) UpdateRecord(ctx context.Context, zone *Zone, recordID int64, attrs *Record) (*Record, error)

UpdateRecord updates a zone record identfied by `recordID` using supplied attributes.

See: http://www.luadns.com/api.html#update-a-record

func (*Client) UpdateZone

func (c *Client) UpdateZone(ctx context.Context, zoneID int64, attrs *Zone) (*Zone, error)

UpdateZone zone identified by `zoneID` using supplied attributes.

See: http://www.luadns.com/api.html#update-a-zone

func (*Client) UserAgent added in v0.3.0

func (c *Client) UserAgent(product string)

UserAgent appends product text to User-Agent header used in HTTP requests.

type ErrBadContentType

type ErrBadContentType struct {
	ContentType string
}

ErrBadContentType represents an error for unexpected content type returned by the API server.

func (*ErrBadContentType) Error

func (e *ErrBadContentType) Error() string

type ErrBadStatusCode

type ErrBadStatusCode struct {
	StatusCode int
}

ErrBadStatusCode represents an error for unexpected status code returned by the API server.

func (*ErrBadStatusCode) Error

func (e *ErrBadStatusCode) Error() string

type ErrTooManyRequests

type ErrTooManyRequests struct {
	Limit int64
	Reset int64
}

ErrTooManyRequests represents an error returned when users exeeds requests quota (status code 429).

func (*ErrTooManyRequests) Error

func (e *ErrTooManyRequests) Error() string

type ForbiddenRequestError

type ForbiddenRequestError struct {
	Status  string `json:"status"`
	Message string `json:"message"`
}

ForbiddenRequestError represents an error returned by the server when input data is valid but the operation is not allowed (status code 403).

func (*ForbiddenRequestError) Error

func (e *ForbiddenRequestError) Error() string

type HandlerFunc

type HandlerFunc func(*http.Response)

HandlerFunc represents a response handler.

func GetListMeta

func GetListMeta(meta *ListMeta) HandlerFunc

GetListMeta parses response headers and fills pagination details.

type InputError

type InputError struct {
	Classification string   `json:"classification"` // DeserializationError, RequiredError, ValidationError
	FieldNames     []string `json:"fieldNames"`     // Example: name
	Message        string   `json:"message"`        // Example: invalid name
}

InputError represents a validation error returned by the API server when input data is invalid (status code 400).

func (*InputError) Error

func (e *InputError) Error() string

type JSONClient

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

JSONClient represents a REST client using JSON format.

func NewAuthJSONClient

func NewAuthJSONClient(username, password string) *JSONClient

NewAuthJSONClient initializes JSON client using Basic authentication.

func NewJSONClient

func NewJSONClient(client *http.Client) *JSONClient

NewJSONClient initializes JSON client.

func (*JSONClient) Delete

func (c *JSONClient) Delete(ctx context.Context, url string, handlers ...HandlerFunc) ([]byte, error)

Delete executes a DELETE request and returns JSON response.

func (*JSONClient) Get

func (c *JSONClient) Get(ctx context.Context, url string, handlers ...HandlerFunc) ([]byte, error)

Get executes a GET request and returns JSON response.

func (*JSONClient) Patch added in v0.3.0

func (c *JSONClient) Patch(ctx context.Context, url string, data any, handlers ...HandlerFunc) ([]byte, error)

Patch executes a PATCH request using JSON body and returns JSON response.

func (*JSONClient) Post

func (c *JSONClient) Post(ctx context.Context, url string, attrs any, handlers ...HandlerFunc) ([]byte, error)

Post executes a POST request using JSON body and returns JSON response.

func (*JSONClient) Put

func (c *JSONClient) Put(ctx context.Context, url string, data any, handlers ...HandlerFunc) ([]byte, error)

Put executes a PUT request using JSON body and returns JSON response.

type ListMeta

type ListMeta struct {
	Page       uint64
	Limit      uint64
	TotalCount uint64
	PagesCount uint64
}

ListMeta stores parsed headers used for result pagination.

type ListParams

type ListParams struct {
	Query     string // q=example.com
	SortBy    string // sort_by=name
	SortOrder string // sort_order=asc/desc
	Limit     uint64 // limit=10
	Page      uint64 // page=1
}

ListParams represents list options accepted by a list endpoints.

func (*ListParams) QueryString

func (opts *ListParams) QueryString() string

QueryString convert the list options to a query string.

type OptFunc

type OptFunc func(*Client)

OptFunc represents a configuration function which are used to configure the REST API client.

func SetBaseURL

func SetBaseURL(url string) OptFunc

SetBaseURL sets a custom baseURL for API requests (used in unit tests).

type RR added in v0.3.0

type RR struct {
	Name    string `json:"name"`
	Type    string `json:"type,omitempty"`
	Content string `json:"content,omitempty"`
	TTL     uint32 `json:"ttl,omitempty"`
}

RR represents a DNS resource record.

type Record

type Record struct {
	ID        int64     `json:"id,omitempty"`
	Name      string    `json:"name"`
	Type      string    `json:"type"`
	Content   string    `json:"content"`
	TTL       uint32    `json:"ttl"`
	ZoneID    int64     `json:"zone_id"`
	CreatedAt time.Time `json:"created_at,omitempty"`
	UpdatedAt time.Time `json:"updated_at,omitempty"`
}

type RecordType

type RecordType string

type RestCallFunc

type RestCallFunc func(ctx context.Context) ([]byte, error)

RestCallFunc represents a call to REST API.

type Transport

type Transport struct {
	*http.Transport
	// contains filtered or unexported fields
}

Transport represents a HTTP transport using Basic authentication and accepts `application/json`.

func (Transport) RoundTrip

func (t Transport) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip implements `http.RoundTrip` interface.

type User

type User struct {
	Email       string   `json:"email"`
	Name        string   `json:"name"`
	RepoURI     string   `json:"repo_uri"`
	APIEnabled  bool     `json:"api_enabled"`
	TFA         bool     `json:"tfa"`
	DeployKey   string   `json:"deploy_key"`
	TTL         uint32   `json:"ttl"`
	Package     string   `json:"package"`
	NameServers []string `json:"name_servers"`
}

type Zone

type Zone struct {
	ID         int64     `json:"id,omitempty"`
	Name       string    `json:"name"`
	Tags       []string  `json:"tags,omitempty"`
	TemplateID int64     `json:"template_id,omitempty"`
	Records    []*Record `json:"records,omitempty"`
	CreatedAt  time.Time `json:"created_at,omitempty"`
	UpdatedAt  time.Time `json:"updated_at,omitempty"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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