Browse Source

make adapter

tags/release-0.2
Vladimir Smagin 5 months ago
parent
commit
39cb17e7e1
6 changed files with 83 additions and 42 deletions
  1. +1
    -1
      example/records/record.go
  2. +43
    -6
      example/zones/zone.go
  3. +3
    -3
      hcloud-dns.go
  4. +14
    -14
      records.go
  5. +6
    -2
      types.go
  6. +16
    -16
      zones.go

+ 1
- 1
example/records/record.go View File

@@ -15,7 +15,7 @@ func main() {
}

log.Println("Create new instance")
hdns := hclouddns.New(string(token))
hdns := *hclouddns.New(string(token), &hclouddns.HCloudDNS{})

log.Println("Get zone vhSHpH5mjcB2UywP9XtZGh")
allRecords, err := hdns.GetRecords(hclouddns.HCloudGetRecordsParams{ZoneID: "vhSHpH5mjcB2UywP9XtZGh"})


+ 43
- 6
example/zones/zone.go View File

@@ -7,6 +7,41 @@ import (
hclouddns "git.blindage.org/21h/hcloud-dns"
)

type mockHCloudClientAdapter interface {
GetZone(ID string) (hclouddns.HCloudAnswerGetZone, error)
GetZones(params hclouddns.HCloudGetZonesParams) (hclouddns.HCloudAnswerGetZones, error)
UpdateZone(zone hclouddns.HCloudZone) (hclouddns.HCloudAnswerGetZone, error)
DeleteZone(ID string) (hclouddns.HCloudAnswerDeleteZone, error)
CreateZone(zone hclouddns.HCloudZone) (hclouddns.HCloudAnswerGetZone, error)
ImportZoneString(zoneID string, zonePlainText string) (hclouddns.HCloudAnswerGetZone, error)
ExportZoneToString(zoneID string) (hclouddns.HCloudAnswerGetZonePlainText, error)
ValidateZoneString(zonePlainText string) (hclouddns.HCloudAnswerZoneValidate, error)
GetRecords(params hclouddns.HCloudGetRecordsParams) (hclouddns.HCloudAnswerGetRecords, error)
UpdateRecord(record hclouddns.HCloudRecord) (hclouddns.HCloudAnswerGetRecord, error)
DeleteRecord(ID string) (hclouddns.HCloudAnswerDeleteRecord, error)
CreateRecord(record hclouddns.HCloudRecord) (hclouddns.HCloudAnswerGetRecord, error)
CreateRecordBulk(record []hclouddns.HCloudRecord) (hclouddns.HCloudAnswerCreateRecords, error)
UpdateRecordBulk(record []hclouddns.HCloudRecord) (hclouddns.HCloudAnswerUpdateRecords, error)
}

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

func (m *mockHCloudClient) GetZones(params hclouddns.HCloudGetZonesParams) (hclouddns.HCloudAnswerGetZones, error) {
return hclouddns.HCloudAnswerGetZones{
Zones: []hclouddns.HCloudZone{
{
ID: "HetznerZoneID",
Name: "blindage.org",
TTL: 666,
RecordsCount: 1,
},
},
}, nil
}

func main() {
// Get your own token on Hetzner DNS and save in plain text file
token, err := ioutil.ReadFile("token.txt")
@@ -14,33 +49,35 @@ func main() {
log.Fatalln(err)
}

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

log.Println("Get zones")
zone, err := hdns.GetZones(hclouddns.HCloudGetZonesParams{})
zone, err := hdns.Client.GetZones(hclouddns.HCloudGetZonesParams{})
if err != nil {
log.Fatalln(err)
}
log.Println(zone.Zones)
log.Println(zone.Error)

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

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

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


+ 3
- 3
hcloud-dns.go View File

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

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

+ 14
- 14
records.go View File

@@ -12,7 +12,7 @@ import (
// GetRecord retrieve one single record by ID.
// Accepts single ID of record.
// Returns HCloudAnswerGetRecord with HCloudRecord and error.
func (d *HCloudDNS) GetRecord(ID string) (HCloudAnswerGetRecord, error) {
func (d *HCloudClient) GetRecord(ID string) (HCloudAnswerGetRecord, error) {

client := &http.Client{}
req, err := http.NewRequest("GET", fmt.Sprintf("https://dns.hetzner.com/api/v1/records/%v", ID), nil)
@@ -20,7 +20,7 @@ func (d *HCloudDNS) GetRecord(ID string) (HCloudAnswerGetRecord, error) {
return HCloudAnswerGetRecord{}, err
}

req.Header.Add("Auth-API-Token", d.token)
req.Header.Add("Auth-API-Token", d.Token)

resp, err := client.Do(req)
if err != nil {
@@ -60,7 +60,7 @@ func (d *HCloudDNS) GetRecord(ID string) (HCloudAnswerGetRecord, error) {
// GetRecords retrieve all records of user.
// Accepts HCloudGetRecordsParams struct
// Returns HCloudAnswerGetRecords with array of HCloudRecord, Meta and error.
func (d *HCloudDNS) GetRecords(params HCloudGetRecordsParams) (HCloudAnswerGetRecords, error) {
func (d *HCloudClient) GetRecords(params HCloudGetRecordsParams) (HCloudAnswerGetRecords, error) {

v := url.Values{}
if params.Page != "" {
@@ -79,7 +79,7 @@ func (d *HCloudDNS) GetRecords(params HCloudGetRecordsParams) (HCloudAnswerGetRe
return HCloudAnswerGetRecords{}, err
}

req.Header.Add("Auth-API-Token", d.token)
req.Header.Add("Auth-API-Token", d.Token)

parseFormErr := req.ParseForm()
if parseFormErr != nil {
@@ -124,7 +124,7 @@ func (d *HCloudDNS) GetRecords(params HCloudGetRecordsParams) (HCloudAnswerGetRe
// UpdateRecord makes update of single record by ID.
// Accepts HCloudRecord with fullfilled fields.
// Returns HCloudAnswerGetRecord with HCloudRecord and error.
func (d *HCloudDNS) UpdateRecord(record HCloudRecord) (HCloudAnswerGetRecord, error) {
func (d *HCloudClient) UpdateRecord(record HCloudRecord) (HCloudAnswerGetRecord, error) {

jsonRecordString, err := json.Marshal(record)
if err != nil {
@@ -139,7 +139,7 @@ func (d *HCloudDNS) UpdateRecord(record HCloudRecord) (HCloudAnswerGetRecord, er
}

req.Header.Add("Content-Type", "application/json")
req.Header.Add("Auth-API-Token", d.token)
req.Header.Add("Auth-API-Token", d.Token)

resp, err := client.Do(req)
if err != nil {
@@ -179,7 +179,7 @@ func (d *HCloudDNS) UpdateRecord(record HCloudRecord) (HCloudAnswerGetRecord, er
// DeleteRecord remove record by ID.
// Accepts single ID string.
// Returns HCloudAnswerDeleteRecord and error.
func (d *HCloudDNS) DeleteRecord(ID string) (HCloudAnswerDeleteRecord, error) {
func (d *HCloudClient) DeleteRecord(ID string) (HCloudAnswerDeleteRecord, error) {

client := &http.Client{}
req, err := http.NewRequest("DELETE", fmt.Sprintf("https://dns.hetzner.com/api/v1/records/%v", ID), nil)
@@ -187,7 +187,7 @@ func (d *HCloudDNS) DeleteRecord(ID string) (HCloudAnswerDeleteRecord, error) {
return HCloudAnswerDeleteRecord{}, err
}

req.Header.Add("Auth-API-Token", d.token)
req.Header.Add("Auth-API-Token", d.Token)

resp, err := client.Do(req)
if err != nil {
@@ -222,7 +222,7 @@ func (d *HCloudDNS) DeleteRecord(ID string) (HCloudAnswerDeleteRecord, error) {
// CreateRecord creates new single record.
// Accepts HCloudRecord with record to create, of cource no ID.
// Returns HCloudAnswerGetRecord with HCloudRecord and error.
func (d *HCloudDNS) CreateRecord(record HCloudRecord) (HCloudAnswerGetRecord, error) {
func (d *HCloudClient) CreateRecord(record HCloudRecord) (HCloudAnswerGetRecord, error) {

jsonRecordString, err := json.Marshal(record)
if err != nil {
@@ -237,7 +237,7 @@ func (d *HCloudDNS) CreateRecord(record HCloudRecord) (HCloudAnswerGetRecord, er
}

req.Header.Add("Content-Type", "application/json")
req.Header.Add("Auth-API-Token", d.token)
req.Header.Add("Auth-API-Token", d.Token)

resp, err := client.Do(req)
if err != nil {
@@ -277,7 +277,7 @@ func (d *HCloudDNS) CreateRecord(record HCloudRecord) (HCloudAnswerGetRecord, er
// CreateRecordBulk creates many records at once.
// Accepts array of HCloudRecord, converts it to json and makes POST to Hetzner.
// Returns HCloudAnswerCreateRecords with arrays of HCloudRecord with whole list, valid and invalid, error.
func (d *HCloudDNS) CreateRecordBulk(record []HCloudRecord) (HCloudAnswerCreateRecords, error) {
func (d *HCloudClient) CreateRecordBulk(record []HCloudRecord) (HCloudAnswerCreateRecords, error) {

jsonRecordString, err := json.Marshal(record)
if err != nil {
@@ -292,7 +292,7 @@ func (d *HCloudDNS) CreateRecordBulk(record []HCloudRecord) (HCloudAnswerCreateR
}

req.Header.Add("Content-Type", "application/json")
req.Header.Add("Auth-API-Token", d.token)
req.Header.Add("Auth-API-Token", d.Token)

resp, err := client.Do(req)
if err != nil {
@@ -332,7 +332,7 @@ func (d *HCloudDNS) CreateRecordBulk(record []HCloudRecord) (HCloudAnswerCreateR
// UpdateRecordBulk updates many records at once.
// Accepts array of HCloudRecord, converting to json and makes PUT to Hetzner.
// Returns HCloudAnswerUpdateRecords with arrays of HCloudRecord with updated and failed, error.
func (d *HCloudDNS) UpdateRecordBulk(record []HCloudRecord) (HCloudAnswerUpdateRecords, error) {
func (d *HCloudClient) UpdateRecordBulk(record []HCloudRecord) (HCloudAnswerUpdateRecords, error) {

jsonRecordString, err := json.Marshal(record)
if err != nil {
@@ -347,7 +347,7 @@ func (d *HCloudDNS) UpdateRecordBulk(record []HCloudRecord) (HCloudAnswerUpdateR
}

req.Header.Add("Content-Type", "application/json")
req.Header.Add("Auth-API-Token", d.token)
req.Header.Add("Auth-API-Token", d.Token)

resp, err := client.Do(req)
if err != nil {


+ 6
- 2
types.go View File

@@ -2,7 +2,6 @@ package hclouddns

// Base types
type HCloudDNS struct {
token string `yaml:"token"`
Client HCloudClient
}

@@ -26,7 +25,12 @@ const (
CAA RecordType = "CAA"
)

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

type HCloudClientAdapter interface {
GetZone(ID string) (HCloudAnswerGetZone, error)
GetZones(params HCloudGetZonesParams) (HCloudAnswerGetZones, error)
UpdateZone(zone HCloudZone) (HCloudAnswerGetZone, error)


+ 16
- 16
zones.go View File

@@ -13,14 +13,14 @@ import (
// GetZone retrieve one single zone by ID.
// Accepts zone ID string.
// Returns HCloudAnswerGetZone with HCloudZone and error
func (d *HCloudDNS) GetZone(ID string) (HCloudAnswerGetZone, error) {
func (d *HCloudClient) GetZone(ID string) (HCloudAnswerGetZone, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", fmt.Sprintf("https://dns.hetzner.com/api/v1/zones/%v", ID), nil)
if err != nil {
return HCloudAnswerGetZone{}, err
}

req.Header.Add("Auth-API-Token", d.token)
req.Header.Add("Auth-API-Token", d.Token)

resp, err := client.Do(req)
if err != nil {
@@ -60,7 +60,7 @@ func (d *HCloudDNS) GetZone(ID string) (HCloudAnswerGetZone, error) {
// GetZones retrieve all zones of user.
// Accepts exact name as string, search name with partial name.
// Returns HCloudAnswerGetZones with array of HCloudZone, Meta and error.
func (d *HCloudDNS) GetZones(params HCloudGetZonesParams) (HCloudAnswerGetZones, error) {
func (d *HCloudClient) GetZones(params HCloudGetZonesParams) (HCloudAnswerGetZones, error) {

v := url.Values{}
if params.Name != "" {
@@ -82,7 +82,7 @@ func (d *HCloudDNS) GetZones(params HCloudGetZonesParams) (HCloudAnswerGetZones,
return HCloudAnswerGetZones{}, err
}

req.Header.Add("Auth-API-Token", d.token)
req.Header.Add("Auth-API-Token", d.Token)

parseFormErr := req.ParseForm()
if parseFormErr != nil {
@@ -127,7 +127,7 @@ func (d *HCloudDNS) GetZones(params HCloudGetZonesParams) (HCloudAnswerGetZones,
// UpdateZone makes update of single zone by ID.
// Accepts HCloudZone with fullfilled fields.
// Returns HCloudAnswerGetZone with HCloudZone and error.
func (d *HCloudDNS) UpdateZone(zone HCloudZone) (HCloudAnswerGetZone, error) {
func (d *HCloudClient) UpdateZone(zone HCloudZone) (HCloudAnswerGetZone, error) {

jsonZoneString, err := json.Marshal(zone)
if err != nil {
@@ -142,7 +142,7 @@ func (d *HCloudDNS) UpdateZone(zone HCloudZone) (HCloudAnswerGetZone, error) {
}

req.Header.Add("Content-Type", "application/json")
req.Header.Add("Auth-API-Token", d.token)
req.Header.Add("Auth-API-Token", d.Token)

resp, err := client.Do(req)
if err != nil {
@@ -182,7 +182,7 @@ func (d *HCloudDNS) UpdateZone(zone HCloudZone) (HCloudAnswerGetZone, error) {
// DeleteZone remove zone by ID.
// Accepts single ID string.
// Returns HCloudAnswerDeleteZone with error.
func (d *HCloudDNS) DeleteZone(ID string) (HCloudAnswerDeleteZone, error) {
func (d *HCloudClient) DeleteZone(ID string) (HCloudAnswerDeleteZone, error) {

client := &http.Client{}
req, err := http.NewRequest("DELETE", fmt.Sprintf("https://dns.hetzner.com/api/v1/zones/%v", ID), nil)
@@ -190,7 +190,7 @@ func (d *HCloudDNS) DeleteZone(ID string) (HCloudAnswerDeleteZone, error) {
return HCloudAnswerDeleteZone{}, err
}

req.Header.Add("Auth-API-Token", d.token)
req.Header.Add("Auth-API-Token", d.Token)

resp, err := client.Do(req)
if err != nil {
@@ -225,7 +225,7 @@ func (d *HCloudDNS) DeleteZone(ID string) (HCloudAnswerDeleteZone, error) {
// CreateZone creates new single zone.
// Accepts HCloudZone with record to create, of cource no ID.
// Returns HCloudAnswerGetZone with HCloudZone and error.
func (d *HCloudDNS) CreateZone(zone HCloudZone) (HCloudAnswerGetZone, error) {
func (d *HCloudClient) CreateZone(zone HCloudZone) (HCloudAnswerGetZone, error) {

jsonZoneString, err := json.Marshal(zone)
if err != nil {
@@ -240,7 +240,7 @@ func (d *HCloudDNS) CreateZone(zone HCloudZone) (HCloudAnswerGetZone, error) {
}

req.Header.Add("Content-Type", "application/json")
req.Header.Add("Auth-API-Token", d.token)
req.Header.Add("Auth-API-Token", d.Token)

resp, err := client.Do(req)
if err != nil {
@@ -280,7 +280,7 @@ func (d *HCloudDNS) CreateZone(zone HCloudZone) (HCloudAnswerGetZone, error) {
// ImportZoneString imports single zone from imported text.
// Accepts ID and zonePlainText strings.
// Returns HCloudAnswerGetZone with HCloudZone and error.
func (d *HCloudDNS) ImportZoneString(zoneID string, zonePlainText string) (HCloudAnswerGetZone, error) {
func (d *HCloudClient) ImportZoneString(zoneID string, zonePlainText string) (HCloudAnswerGetZone, error) {

body := strings.NewReader(zonePlainText)

@@ -291,7 +291,7 @@ func (d *HCloudDNS) ImportZoneString(zoneID string, zonePlainText string) (HClou
}

req.Header.Add("Content-Type", "text/plain")
req.Header.Add("Auth-API-Token", d.token)
req.Header.Add("Auth-API-Token", d.Token)

resp, err := client.Do(req)
if err != nil {
@@ -331,7 +331,7 @@ func (d *HCloudDNS) ImportZoneString(zoneID string, zonePlainText string) (HClou
// ExportZoneToString exports single zone from imported text.
// Accepts ID and zonePlainText strings.
// Returns HCloudAnswerGetZonePlainText with HCloudZone and error.
func (d *HCloudDNS) ExportZoneToString(zoneID string) (HCloudAnswerGetZonePlainText, error) {
func (d *HCloudClient) ExportZoneToString(zoneID string) (HCloudAnswerGetZonePlainText, error) {

client := &http.Client{}
req, err := http.NewRequest("GET", fmt.Sprintf("https://dns.hetzner.com/api/v1/zones/%v/export", zoneID), nil)
@@ -340,7 +340,7 @@ func (d *HCloudDNS) ExportZoneToString(zoneID string) (HCloudAnswerGetZonePlainT
}

req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
req.Header.Add("Auth-API-Token", d.token)
req.Header.Add("Auth-API-Token", d.Token)

resp, err := client.Do(req)
if err != nil {
@@ -361,7 +361,7 @@ func (d *HCloudDNS) ExportZoneToString(zoneID string) (HCloudAnswerGetZonePlainT
// ValidateZoneString validate single zone from imported text.
// Accepts ID and zonePlainText strings.
// Returns HCloudAnswerZoneValidate with HCloudZone and error.
func (d *HCloudDNS) ValidateZoneString(zonePlainText string) (HCloudAnswerZoneValidate, error) {
func (d *HCloudClient) ValidateZoneString(zonePlainText string) (HCloudAnswerZoneValidate, error) {

body := strings.NewReader(zonePlainText)

@@ -372,7 +372,7 @@ func (d *HCloudDNS) ValidateZoneString(zonePlainText string) (HCloudAnswerZoneVa
}

req.Header.Add("Content-Type", "text/plain")
req.Header.Add("Auth-API-Token", d.token)
req.Header.Add("Auth-API-Token", d.Token)

resp, err := client.Do(req)
if err != nil {


Loading…
Cancel
Save