Skip to content

Commit

Permalink
hetzner: improve zone ID detection (#1815)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Jan 23, 2023
1 parent 5ff9695 commit 9b0b4cb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
13 changes: 11 additions & 2 deletions providers/dns/hetzner/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (c *Client) DeleteRecord(recordID string) error {

// GetZoneID gets the zone ID for a domain.
func (c *Client) GetZoneID(domain string) (string, error) {
zones, err := c.getZones()
zones, err := c.getZones(domain)
if err != nil {
return "", err
}
Expand All @@ -144,17 +144,26 @@ func (c *Client) GetZoneID(domain string) (string, error) {
}

// https://dns.hetzner.com/api-docs#operation/GetZones
func (c *Client) getZones() (*Zones, error) {
func (c *Client) getZones(name string) (*Zones, error) {
endpoint, err := c.createEndpoint("api", "v1", "zones")
if err != nil {
return nil, fmt.Errorf("failed to create endpoint: %w", err)
}

query := endpoint.Query()
query.Set("name", name)
endpoint.RawQuery = query.Encode()

resp, err := c.do(http.MethodGet, endpoint, nil)
if err != nil {
return nil, fmt.Errorf("could not get zones: %w", err)
}

// EOF fallback
if resp.StatusCode == http.StatusNotFound {
return &Zones{}, nil
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("could not get zones: %s", resp.Status)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,18 @@ type Zone struct {
// Zones a set of DNS zones.
type Zones struct {
Zones []Zone `json:"zones"`
Meta Meta `json:"meta,omitempty"`
}

// Meta response metadata.
type Meta struct {
Pagination Pagination `json:"pagination,omitempty"`
}

// Pagination information about pagination.
type Pagination struct {
Page int `json:"page,omitempty" url:"page"`
PerPage int `json:"per_page,omitempty" url:"per_page"`
LastPage int `json:"last_page,omitempty" url:"-"`
TotalEntries int `json:"total_entries,omitempty" url:"-"`
}

0 comments on commit 9b0b4cb

Please sign in to comment.