hetzner_dns

package module
v0.0.0-...-0df4622 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

README

README

Use Hetzner DNS API from Golang.

Install

go get github.com/panta/go-hetzner-dns

Usage

Authentication

Hetzner DNS API uses an API token. You can either explicitly set the token in the ApiKey field of the Client object, or you can set the environment variable HETZNER_API_KEY.

API

Almost all Hetzner DNS APIs are supported. See the example in cmd/example or the tests in hetzner_dns_test.go for a more complete list.

import "github.com/panta/go-hetzner-dns"

client := hetzner_dns.Client{}

// ...

zonesResponse, err := client.GetZones(context.Background(), "", "", 1, 100)
if err != nil {
    log.Fatal(err)
}

// ...

recordsResponse, err := client.GetRecords(context.Background(), "zone-id", 0, 0)
if err != nil {
    log.Fatal(err)
}
Overriding the http.Client

It's possible to use a custom http.Client object by setting the HttpClient field in the Client object. If not set, the library will create one for you.

Example program

To build the example program on a unix-like:

$ make

then:

$ export HETZNER_API_KEY="....."
$ ./go-hetzner-dns list
$ ./go-hetzner-dns add-record -zone=ZONEID RECORD_NAME TYPE RECORD_VALUE
$ ./go-hetzner-dns update-record -zone=ZONEID RECORD_NAME TYPE RECORD_VALUE

Author

By Marco Pantaleoni.

LICENSE

Copyright 2021 Marco Pantaleoni.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Index

Constants

View Source
const (
	BASE_URL        = "https://dns.hetzner.com/api/v1"
	DEFAULT_TIMEOUT = time.Second * 30
)

Variables

View Source
var (
	ErrAPIKeyNotSet = errors.New("hetzner_dns: API key has not been set")
	ErrMissingID    = errors.New("hetzner_dns: missing record ID")
)
View Source
var (
	HETZNER_TIME_FORMATS = []string{
		"2006-01-02 15:04:05.000 -0700 MST",
		"2006-01-02 15:04:05.00 -0700 MST",
		"2006-01-02 15:04:05.0 -0700 MST",
		"2006-01-02 15:04:05 -0700 MST",
		time.RFC3339,
		time.RFC3339Nano,
	}
)

Functions

This section is empty.

Types

type BulkRecordRequest

type BulkRecordRequest struct {
	Records []RecordRequest `json:"records"`
}

type BulkRecordResponse

type BulkRecordResponse struct {
	Records        []Record        `json:"records"`
	ValidRecords   []RecordRequest `json:"valid_records,omitempty"`
	InvalidRecords []RecordRequest `json:"invalid_records,omitempty"`
	FailedRecords  []RecordRequest `json:"failed_records,omitempty"`
}

type Client

type Client struct {
	BaseURL string
	ApiKey  string
	Debug   bool

	HttpClient *http.Client
}

Client is the API service client structure.

func (*Client) BulkCreateRecords

func (client *Client) BulkCreateRecords(ctx context.Context, bulkRecordsRequest *BulkRecordRequest) (*BulkRecordResponse, error)

func (*Client) BulkUpdateRecords

func (client *Client) BulkUpdateRecords(ctx context.Context, bulkRecordsRequest *BulkRecordRequest) (*BulkRecordResponse, error)

func (*Client) CreateOrUpdateRecord

func (client *Client) CreateOrUpdateRecord(ctx context.Context, record RecordRequest) (*RecordResponse, error)

func (*Client) CreateRecord

func (client *Client) CreateRecord(ctx context.Context, record RecordRequest) (*RecordResponse, error)

func (*Client) DeleteRecord

func (client *Client) DeleteRecord(ctx context.Context, recordId string) error

func (*Client) GetRecord

func (client *Client) GetRecord(ctx context.Context, recordId string) (*RecordResponse, error)

func (*Client) GetRecords

func (client *Client) GetRecords(ctx context.Context, zone_id string, page int, perPage int) (*RecordsResponse, error)

func (*Client) GetZones

func (client *Client) GetZones(ctx context.Context, name string, searchName string, page int, perPage int) (*ZonesResponse, error)

func (*Client) Perform

func (client *Client) Perform(ctx context.Context, method string, endpoint string, queryParams, bodyParams, v interface{}) error

func (*Client) UpdateRecord

func (client *Client) UpdateRecord(ctx context.Context, record RecordRequest) (*RecordResponse, error)

type HetznerTime

type HetznerTime time.Time

HetznerTime is a specialization of time.Time handling Hetzner JSON format for time.

func (*HetznerTime) MarshalJSON

func (hzTime *HetznerTime) MarshalJSON() ([]byte, error)

func (*HetznerTime) String

func (hzTime *HetznerTime) String() string

func (*HetznerTime) UnmarshalJSON

func (hzTime *HetznerTime) UnmarshalJSON(b []byte) error

type Meta

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

type Pagination

type Pagination struct {
	Page         int `json:"page"`
	PerPage      int `json:"per_page"`
	LastPage     int `json:"last_page"`
	TotalEntries int `json:"total_entries"`
}

type Record

type Record struct {
	Type     string      `json:"type"`
	ID       string      `json:"id"`
	Created  HetznerTime `json:"created"`
	Modified HetznerTime `json:"modified"`
	ZoneID   string      `json:"zone_id"`
	Name     string      `json:"name"`
	Value    string      `json:"value"`
	TTL      int         `json:"ttl"`
}

type RecordRequest

type RecordRequest struct {
	ID     string `json:"id,omitempty"`
	ZoneID string `json:"zone_id"`
	Type   string `json:"type"`
	Name   string `json:"name"`
	Value  string `json:"value"`
	TTL    int    `json:"ttl"`
}

type RecordResponse

type RecordResponse struct {
	Record Record `json:"record"`
}

type RecordsResponse

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

type Zone

type Zone struct {
	ID              string      `json:"id"`
	Created         HetznerTime `json:"created"`
	Modified        HetznerTime `json:"modified"`
	LegacyDNSHost   string      `json:"legacy_dns_host"`
	LegacyNs        []string    `json:"legacy_ns"`
	Name            string      `json:"name"`
	Ns              []string    `json:"ns"`
	Owner           string      `json:"owner"`
	Paused          bool        `json:"paused"`
	Permission      string      `json:"permission"`
	Project         string      `json:"project"`
	Registrar       string      `json:"registrar"`
	Status          string      `json:"status"`
	TTL             int         `json:"ttl"`
	Verified        HetznerTime `json:"verified"`
	RecordsCount    int         `json:"records_count"`
	IsSecondaryDNS  bool        `json:"is_secondary_dns"`
	TxtVerification struct {
		Name  string `json:"name"`
		Token string `json:"token"`
	} `json:"txt_verification"`
}

type ZoneRequest

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

type ZonesResponse

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

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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