Browse Source

prepare for tests

tags/release-0.2
Vladimir Smagin 5 months ago
parent
commit
28c94ccdc8
4 changed files with 69 additions and 23 deletions
  1. +61
    -9
      example/zones/zone.go
  2. +2
    -3
      hcloud-dns.go
  3. +4
    -10
      types.go
  4. +2
    -1
      zones.go

+ 61
- 9
example/zones/zone.go View File

@@ -25,8 +25,20 @@ type mockHCloudClientAdapter interface {
}

type mockHCloudClient struct {
Token string `yaml:"token"`
Client mockHCloudClientAdapter
Token string `yaml:"token"`
}

// New instance
func New(t string) mockHCloudClientAdapter {
return &mockHCloudClient{
Token: t,
}
}

// Mock all methods

func (m *mockHCloudClient) GetZone(ID string) (hclouddns.HCloudAnswerGetZone, error) {
return hclouddns.HCloudAnswerGetZone{}, nil
}

func (m *mockHCloudClient) GetZones(params hclouddns.HCloudGetZonesParams) (hclouddns.HCloudAnswerGetZones, error) {
@@ -42,6 +54,46 @@ func (m *mockHCloudClient) GetZones(params hclouddns.HCloudGetZonesParams) (hclo
}, nil
}

// zones
func (m *mockHCloudClient) UpdateZone(zone hclouddns.HCloudZone) (hclouddns.HCloudAnswerGetZone, error) {
return hclouddns.HCloudAnswerGetZone{}, nil
}
func (m *mockHCloudClient) DeleteZone(ID string) (hclouddns.HCloudAnswerDeleteZone, error) {
return hclouddns.HCloudAnswerDeleteZone{}, nil
}
func (m *mockHCloudClient) CreateZone(zone hclouddns.HCloudZone) (hclouddns.HCloudAnswerGetZone, error) {
return hclouddns.HCloudAnswerGetZone{}, nil
}
func (m *mockHCloudClient) ImportZoneString(zoneID string, zonePlainText string) (hclouddns.HCloudAnswerGetZone, error) {
return hclouddns.HCloudAnswerGetZone{}, nil
}
func (m *mockHCloudClient) ExportZoneToString(zoneID string) (hclouddns.HCloudAnswerGetZonePlainText, error) {
return hclouddns.HCloudAnswerGetZonePlainText{}, nil
}
func (m *mockHCloudClient) ValidateZoneString(zonePlainText string) (hclouddns.HCloudAnswerZoneValidate, error) {
return hclouddns.HCloudAnswerZoneValidate{}, nil
}

// records
func (m *mockHCloudClient) GetRecords(params hclouddns.HCloudGetRecordsParams) (hclouddns.HCloudAnswerGetRecords, error) {
return hclouddns.HCloudAnswerGetRecords{}, nil
}
func (m *mockHCloudClient) UpdateRecord(record hclouddns.HCloudRecord) (hclouddns.HCloudAnswerGetRecord, error) {
return hclouddns.HCloudAnswerGetRecord{}, nil
}
func (m *mockHCloudClient) DeleteRecord(ID string) (hclouddns.HCloudAnswerDeleteRecord, error) {
return hclouddns.HCloudAnswerDeleteRecord{}, nil
}
func (m *mockHCloudClient) CreateRecord(record hclouddns.HCloudRecord) (hclouddns.HCloudAnswerGetRecord, error) {
return hclouddns.HCloudAnswerGetRecord{}, nil
}
func (m *mockHCloudClient) CreateRecordBulk(record []hclouddns.HCloudRecord) (hclouddns.HCloudAnswerCreateRecords, error) {
return hclouddns.HCloudAnswerCreateRecords{}, nil
}
func (m *mockHCloudClient) UpdateRecordBulk(record []hclouddns.HCloudRecord) (hclouddns.HCloudAnswerUpdateRecords, error) {
return hclouddns.HCloudAnswerUpdateRecords{}, nil
}

func main() {
// Get your own token on Hetzner DNS and save in plain text file
token, err := ioutil.ReadFile("token.txt")
@@ -50,11 +102,11 @@ func main() {
}

log.Println("Create new instance", string(token))
hdns := hclouddns.New(string(token), &hclouddns.HCloudClient{})
// hdns := hclouddns.New("", &mockHCloudClient{})
// hdns := hclouddns.New(string(token))
hdns := New("mytoken")

log.Println("Get zones")
zone, err := hdns.Client.GetZones(hclouddns.HCloudGetZonesParams{})
zone, err := hdns.GetZones(hclouddns.HCloudGetZonesParams{})
if err != nil {
log.Fatalln(err)
}
@@ -62,22 +114,22 @@ func main() {
log.Println(zone.Error)

zoneID := zone.Zones[0].ID
log.Println("Export zone to plain text")
zonePlainText, err := hdns.Client.ExportZoneToString(zoneID)
log.Println("Export zone to plain text:", zoneID)
zonePlainText, err := hdns.ExportZoneToString(zoneID)
if err != nil {
log.Fatalln(err)
}
log.Println(zonePlainText.ZonePlainText)

log.Println("Validate plain text zone")
zoneValid, err := hdns.Client.ValidateZoneString(zonePlainText.ZonePlainText)
zoneValid, err := hdns.ValidateZoneString(zonePlainText.ZonePlainText)
if err != nil {
log.Fatalln(err)
}
log.Println(zoneValid.ValidRecords)

log.Println("Import zone to plain text")
zoneImport, err := hdns.Client.ImportZoneString(zoneID, zonePlainText.ZonePlainText)
zoneImport, err := hdns.ImportZoneString(zoneID, zonePlainText.ZonePlainText)
if err != nil {
log.Fatalln(err)
}


+ 2
- 3
hcloud-dns.go View File

@@ -10,9 +10,8 @@
package hclouddns

// New instance
func New(t string, c HCloudClientAdapter) *HCloudClient {
func New(t string) HCloudClientAdapter {
return &HCloudClient{
Client: c,
Token: t,
Token: t,
}
}

+ 4
- 10
types.go View File

@@ -1,10 +1,5 @@
package hclouddns

// Base types
type HCloudDNS struct {
Client HCloudClient
}

// RecordType supported by Hetzner
type RecordType string

@@ -25,11 +20,6 @@ const (
CAA RecordType = "CAA"
)

type HCloudClient struct {
Token string `yaml:"token"`
Client HCloudClientAdapter
}

type HCloudClientAdapter interface {
GetZone(ID string) (HCloudAnswerGetZone, error)
GetZones(params HCloudGetZonesParams) (HCloudAnswerGetZones, error)
@@ -47,6 +37,10 @@ type HCloudClientAdapter interface {
UpdateRecordBulk(record []HCloudRecord) (HCloudAnswerUpdateRecords, error)
}

type HCloudClient struct {
Token string `yaml:"token"`
}

// Hetzner errors roundabout. Fuck you Hetzner.
type HCloudError struct {
Code int `json:"code,omitempty"`


+ 2
- 1
zones.go View File

@@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
@@ -61,6 +62,7 @@ func (d *HCloudClient) GetZone(ID string) (HCloudAnswerGetZone, error) {
// Accepts exact name as string, search name with partial name.
// Returns HCloudAnswerGetZones with array of HCloudZone, Meta and error.
func (d *HCloudClient) GetZones(params HCloudGetZonesParams) (HCloudAnswerGetZones, error) {
log.Println("token orig", d)

v := url.Values{}
if params.Name != "" {
@@ -75,7 +77,6 @@ func (d *HCloudClient) GetZones(params HCloudGetZonesParams) (HCloudAnswerGetZon
if params.PerPage != "" {
v.Add("per_page", params.PerPage)
}

client := &http.Client{}
req, err := http.NewRequest("GET", fmt.Sprintf("https://dns.hetzner.com/api/v1/zones?%v", v.Encode()), nil)
if err != nil {


Loading…
Cancel
Save