Browse Source

Add zone validation

tags/release-0.1
Vladimir Smagin 3 months ago
parent
commit
bfee9cbb16
3 changed files with 65 additions and 1 deletions
  1. +7
    -0
      example/zones/zone.go
  2. +6
    -0
      types_complex.go
  3. +52
    -1
      zones.go

+ 7
- 0
example/zones/zone.go View File

@@ -32,6 +32,13 @@ func main() {
}
log.Println(zonePlainText.ZonePlainText)

log.Println("Validate plain text zone")
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.ImportZoneString("vhSHpH5mjcB2UywP9XtZGh", zonePlainText.ZonePlainText)
if err != nil {


+ 6
- 0
types_complex.go View File

@@ -42,6 +42,12 @@ type HCloudAnswerGetZonePlainText struct {
Error HCloudError
}

type HCloudAnswerZoneValidate struct {
ParsedRecords int `json:"parsed_records,omitempty"`
ValidRecords []HCloudZone `json:"valid_records,omitempty"`
Error HCloudError
}

type HCloudAnswerGetZones struct {
Zones []HCloudZone `json:"zones,omitempty"`
Meta HCloudMeta `json:"meta,omitempty"`


+ 52
- 1
zones.go View File

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

// ExportZoneToString exports single zone from imported text.
// Accepts ID and zonePlainText strings.
// Returns HCloudAnswerGetZone with HCloudZone and error.
// Returns HCloudAnswerGetZonePlainText with HCloudZone and error.
func (d *HCloudDNS) ExportZoneToString(zoneID string) (HCloudAnswerGetZonePlainText, error) {

client := &http.Client{}
@@ -357,3 +357,54 @@ func (d *HCloudDNS) ExportZoneToString(zoneID string) (HCloudAnswerGetZonePlainT

return answer, nil
}

// 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) {

body := strings.NewReader(zonePlainText)

client := &http.Client{}
req, err := http.NewRequest("POST", fmt.Sprintf("https://dns.hetzner.com/api/v1/zones/file/validate"), body)
if err != nil {
return HCloudAnswerZoneValidate{}, err
}

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

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

respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return HCloudAnswerZoneValidate{}, err
}

answer := HCloudAnswerZoneValidate{}

err = json.Unmarshal([]byte(respBody), &answer)
if err != nil {
return HCloudAnswerZoneValidate{}, err
}

// parse error
errorResult := HCloudAnswerError{}
err = json.Unmarshal([]byte(respBody), &errorResult)
if err != nil {
//ok, non-standard error, try another form
errorResultString := HCloudAnswerErrorString{}
err = json.Unmarshal([]byte(respBody), &errorResultString)
if err != nil {
return HCloudAnswerZoneValidate{}, err
}
errorResult.Error.Message = errorResultString.Error
errorResult.Error.Code = resp.StatusCode
}
answer.Error = errorResult.Error

return answer, nil
}

Loading…
Cancel
Save