gohetznerdns

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

README

gohetznerdns

gohetznerdns is a Go client library for accessing the Hetzner DNS API.

You can view Hetzner DNS API docs here: https://dns.hetzner.com/api-docs

Install

go get github.com/opsheaven/gohetznerdns@vX.Y.Z

where X.Y.Z is the version you need.

or

go get github.com/opsheaven/gohetznerdns

for non Go modules usage or latest version.

Usage

import "github.com/opsheaven/gohetznerdns"

Create a new client, then use the exposed services to access different parts of the Hetzner DNS API.

Authentication

API Access token is needed to access Public API. You can create the api token by following the manual.

You can then use your token in the client:

package main

import (
    "github.com/opsheaven/gohetznerdns"
)

func main() {
    token:="aaaabbbccxcdasda"
    client,err := gohetznerdns.NewClient(token)
    if err != nil {
        fmt.Errorf("invalid token %s", token)
    }
}

Examples

List all domains
package main

import (
	"fmt"
	"os"

	"github.com/opsheaven/gohetznerdns"
)

func main() {
	var err error
	var client *gohetznerdns.HetznerDNS
	var zones []*gohetznerdns.Zone

	if client, err = gohetznerdns.NewClient(os.Getenv("HETZNER_DNS_TOKEN")); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	if zones, err = client.ZoneService.GetAllZones(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	fmt.Printf("%-24s  |  %s\n", "ID", "NAME")
	for _, zone := range zones {
		fmt.Printf("%-24s  |  %s\n", zone.Id, zone.Name)
	}
}
Query Domains By Name
package main

import (
	"fmt"
	"os"

	"github.com/opsheaven/gohetznerdns"
)

func main() {
	var err error
	var client *gohetznerdns.HetznerDNS
	var zones []*gohetznerdns.Zone

	if client, err = gohetznerdns.NewClient(os.Getenv("HETZNER_DNS_TOKEN")); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	if zones, err = client.ZoneService.GetAllZonesByName("opsheaven"); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	fmt.Printf("%-24s  |  %s\n", "ID", "NAME")
	for _, zone := range zones {
		fmt.Printf("%-24s  |  %s\n", zone.Id, zone.Name)
	}
}
Query Records of Domain
package main

import (
	"fmt"
	"os"

	"github.com/opsheaven/gohetznerdns"
)

func main() {
	var err error
	var client *gohetznerdns.HetznerDNS
	var zones []*gohetznerdns.Zone
	var records []*gohetznerdns.Record

	if client, err = gohetznerdns.NewClient(os.Getenv("HETZNER_DNS_TOKEN")); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	if zones, err = client.ZoneService.GetAllZonesByName("opsheaven.space"); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	if records, err = client.RecordService.GetAllRecords(zones[0].Id); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	fmt.Printf("%-32s | %-32s | %-5s | %s\n", "ID", "NAME", "TYPE", "VALUE")
	for _, record := range records {
		fmt.Printf("%-32s | %-32s | %-5s | %s\n", record.Id, record.Name, record.Type, record.Value)
	}
}

Versioning

Each version of the client is tagged and the version is updated accordingly.

To see the list of past versions, run git tag or check releases

Documentation

Overview

Package gohetzner implements Hetzner Public DNS Api client for managing zones and records.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error added in v0.2.0

type Error struct {
	Code    int    `json:"code"`
	Message string `json:"string"`
}

func (*Error) Error added in v0.2.0

func (e *Error) Error() error

type HetznerDNS

type HetznerDNS interface {

	// Configures API Base URL
	SetBaseURL(baseUrl string) error

	// Configures API Token
	SetToken(token string) error

	// Returns Zone Service
	GetZoneService() ZoneService

	// Returns Record Service
	GetRecordService() RecordService
}

Hetzner DNS Public API interface entry interface Exposes DNS and Record service to manage DNS Zone and records. See api documentation for more information [https://dns.hetzner.com/api-docs]

func NewClient

func NewClient(token string) (HetznerDNS, error)

Creates new Hetzner DNS Public API Client with the given token see [HetznerDNS.setToken] to update token after creation

type Meta added in v0.2.0

type Meta struct {
	Pagination *Pagination `json:"pagination"`
}

type Pagination added in v0.2.0

type Pagination struct {
	Page         *int `json:"page"`
	PerPage      *int `json:"per_page"`
	PreviousPage *int `json:"previous_page"`
	NextPage     *int `json:"next_page"`
	LastPage     *int `json:"last_page"`
	TotalEntries *int `json:"total_entries"`
}

type Record

type Record struct {
	Type   *string `json:"type"`
	Id     *string `json:"id"`
	ZoneId *string `json:"zone_id"`
	Name   *string `json:"name"`
	Value  *string `json:"value"`
	TTL    *int    `json:"ttl"`
}

type RecordResponse added in v0.2.0

type RecordResponse struct {
	Record *Record `json:"record"`
	Error  *Error  `json:"error"`
}

type RecordService

type RecordService interface {

	// Returns all records associated with user. [https://dns.hetzner.com/api-docs#operation/GetRecords]
	GetAllRecords(zone_id *string) ([]*Record, error)

	//Returns information about a single record. [https://dns.hetzner.com/api-docs#operation/GetRecord]
	GetRecord(record_id *string) (*Record, error)

	//Creates a new record. [https://dns.hetzner.com/api-docs#operation/CreateRecord]
	CreateRecord(request *Record) (*Record, error)

	//Updates a record. [https://dns.hetzner.com/api-docs#operation/UpdateRecord]
	UpdateRecord(request *Record) (*Record, error)

	//Deletes a record. [https://dns.hetzner.com/api-docs#operation/DeleteRecord]
	DeleteRecord(record_id *string) error
}

Client interfaces for the Hetzner DNS Public API Records endpoint See api documentation for more information [https://dns.hetzner.com/api-docs#tag/Records]

type Records

type Records struct {
	Records []*Record `json:"records"`
}

type Zone

type Zone struct {
	Id              *string   `json:"id"`
	Name            *string   `json:"name"`
	TTL             *int      `json:"ttl"`
	NS              []*string `json:"ns"`
	Paused          *bool     `json:"paused"`
	Status          *string   `json:"status"`
	NumberOfRecords *int      `json:"records_count"`
}

type ZoneList added in v0.2.0

type ZoneList struct {
	Zones []*Zone `json:"zones"`
	Meta  *Meta   `json:"meta"`
}

type ZoneRequest added in v0.2.0

type ZoneRequest struct {
	Name *string `json:"name"`
	TTL  *int    `json:"ttl"`
}

type ZoneResponse added in v0.2.0

type ZoneResponse struct {
	Zone  *Zone  `json:"zone"`
	Error *Error `json:"error"`
}

type ZoneService

type ZoneService interface {

	// Returns all zones associated with user. [https://dns.hetzner.com/api-docs#operation/GetAllZones]
	GetAllZones() ([]*Zone, error)

	// Returns all zones associated with user matching by name. [https://dns.hetzner.com/api-docs#operation/GetAllZones]
	GetAllZonesByName(name *string) ([]*Zone, error)

	// Returns an object containing all information about a zone. [https://dns.hetzner.com/api-docs#operation/GetZone]
	GetZoneById(zoneId *string) (*Zone, error)

	// Creates a zone. [https://dns.hetzner.com/api-docs#operation/CreateZone]
	CreateZone(request *ZoneRequest) (*Zone, error)

	// Updates a zone. [https://dns.hetzner.com/api-docs#operation/UpdateZone]
	UpdateZone(zoneId *string, request *ZoneRequest) (*Zone, error)

	// Deletes a zone. [https://dns.hetzner.com/api-docs#operation/DeleteZone]
	DeleteZone(zoneId *string) error

	// Validate a zone file in text/plain format. [https://dns.hetzner.com/api-docs#operation/ValidateZoneFilePlain]
	ValidateZoneFile(zoneFile *string) error

	// Export a zone file. [https://dns.hetzner.com/api-docs#operation/ExportZoneFile]
	ExportZoneFile(zoneId *string) (*string, error)

	// Import a zone file. [https://dns.hetzner.com/api-docs#operation/ImportZoneFilePlain]
	ImportZoneFile(zoneId, zoneFile *string) (*Zone, error)
}

Client interfaces for the Hetzner DNS Public API Zones endpoint See api documentation for more information [https://dns.hetzner.com/api-docs#tag/Zones]

Jump to

Keyboard shortcuts

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