hetzner: improve zone ID detection (#1815)

This commit is contained in:
Ludovic Fernandez 2023-01-23 17:38:18 +01:00 committed by GitHub
parent 5ff9695c09
commit 9b0b4cbbd6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 2 deletions

View file

@ -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
}
@ -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)
}

View file

@ -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:"-"`
}