From 6cb9b249d212ba6868b5bda9ae0b48997f33c7a0 Mon Sep 17 00:00:00 2001 From: Daniil Rutskiy <10889589+dstdfx@users.noreply.github.com> Date: Fri, 22 Feb 2019 19:24:17 +0300 Subject: [PATCH] selectel: getting sub-domain (#803) --- providers/dns/selectel/internal/client.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/providers/dns/selectel/internal/client.go b/providers/dns/selectel/internal/client.go index 129d2b95..8457bc4a 100644 --- a/providers/dns/selectel/internal/client.go +++ b/providers/dns/selectel/internal/client.go @@ -6,6 +6,7 @@ import ( "fmt" "io/ioutil" "net/http" + "strings" ) // Domain represents domain name. @@ -65,7 +66,9 @@ func NewClient(opts ClientOpts) *Client { } } -// GetDomainByName gets Domain object by its name. +// GetDomainByName gets Domain object by its name. If `domainName` level > 2 and there is +// no such domain on the account - it'll recursively search for the first +// which is exists in Selectel Domain API. func (c *Client) GetDomainByName(domainName string) (*Domain, error) { uri := fmt.Sprintf("/%s", domainName) req, err := c.newRequest(http.MethodGet, uri, nil) @@ -74,9 +77,16 @@ func (c *Client) GetDomainByName(domainName string) (*Domain, error) { } domain := &Domain{} - _, err = c.do(req, domain) + resp, err := c.do(req, domain) if err != nil { - return nil, err + switch { + case resp.StatusCode == http.StatusNotFound && strings.Count(domainName, ".") > 1: + // Look up for the next sub domain + subIndex := strings.Index(domainName, ".") + return c.GetDomainByName(domainName[subIndex+1:]) + default: + return nil, err + } } return domain, nil