From 2f435ce726fac5d7f30bf0278826ede586c32898 Mon Sep 17 00:00:00 2001 From: Thomas Hofmann Date: Mon, 5 Dec 2022 20:52:27 +0100 Subject: [PATCH 1/6] add support for subdomains for the ibmcloud dns solver. --- providers/dns/ibmcloud/internal/wrapper.go | 25 ++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/providers/dns/ibmcloud/internal/wrapper.go b/providers/dns/ibmcloud/internal/wrapper.go index 6129d9c157..c6b3c9e97b 100644 --- a/providers/dns/ibmcloud/internal/wrapper.go +++ b/providers/dns/ibmcloud/internal/wrapper.go @@ -2,6 +2,7 @@ package internal import ( "fmt" + "strings" "github.com/softlayer/softlayer-go/datatypes" "github.com/softlayer/softlayer-go/services" @@ -39,7 +40,7 @@ func (w Wrapper) CleanupTXTRecord(fqdn, domain string) error { domainID, err := getDomainID(service, domain) if err != nil { - return fmt.Errorf("failed to get domain ID: %w", err) + return fmt.Errorf("failed to get domain ID for domain: %w", err) } service.Options.Id = domainID @@ -66,7 +67,27 @@ func getDomainID(service services.Dns_Domain, domain string) (*int, error) { return r.Id, nil } - return nil, fmt.Errorf("no data found of domain: %s", domain) + // The domain was not found by name. For subdomains this is not unusal in softlayer. + // So in case a subdomain like sub.toplevel.domain was used try again using the + // parent domain (strip the first part in the domain string -> toplevel.domain). + domainParts := strings.Split(domain, ".") + if len(domainParts) > 2 { + return getDomainID(service, getParentDomain(domainParts)) + } else { + return nil, fmt.Errorf("no data found for domain: %s", domain) + } +} + +func getParentDomain(domainParts []string) string { + numParts := len(domainParts) + var sb strings.Builder + for i := 1; i < numParts; i++ { + sb.WriteString(domainParts[i]) + if i < numParts-1 { + sb.WriteString(".") + } + } + return sb.String() } func findTxtRecords(service services.Dns_Domain, fqdn string) ([]datatypes.Dns_Domain_ResourceRecord, error) { From 9dfbbcfb6bc7dedcfa658760ee69dd88fdf73c49 Mon Sep 17 00:00:00 2001 From: Thomas Hofmann Date: Tue, 6 Dec 2022 14:54:40 +0100 Subject: [PATCH 2/6] Update providers/dns/ibmcloud/internal/wrapper.go Formatting of comment. Co-authored-by: Ludovic Fernandez --- providers/dns/ibmcloud/internal/wrapper.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/providers/dns/ibmcloud/internal/wrapper.go b/providers/dns/ibmcloud/internal/wrapper.go index c6b3c9e97b..7c3eced4ec 100644 --- a/providers/dns/ibmcloud/internal/wrapper.go +++ b/providers/dns/ibmcloud/internal/wrapper.go @@ -67,9 +67,10 @@ func getDomainID(service services.Dns_Domain, domain string) (*int, error) { return r.Id, nil } - // The domain was not found by name. For subdomains this is not unusal in softlayer. - // So in case a subdomain like sub.toplevel.domain was used try again using the - // parent domain (strip the first part in the domain string -> toplevel.domain). + // The domain was not found by name. + // For subdomains this is not unusual in softlayer. + // So in case a subdomain like `sub.toplevel.tld` was used try again using the parent domain + // (strip the first part in the domain string -> `toplevel.tld`). domainParts := strings.Split(domain, ".") if len(domainParts) > 2 { return getDomainID(service, getParentDomain(domainParts)) From b4ae1a3497c854993c40bea679a73681a3fbb58e Mon Sep 17 00:00:00 2001 From: Thomas Hofmann Date: Tue, 6 Dec 2022 14:56:41 +0100 Subject: [PATCH 3/6] Update providers/dns/ibmcloud/internal/wrapper.go I was thinking that this would be the better error message because the value that will be used for %w is actually not the domain ID but a domain name. If you think this should not be changed it is fine with me. Co-authored-by: Ludovic Fernandez --- providers/dns/ibmcloud/internal/wrapper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/dns/ibmcloud/internal/wrapper.go b/providers/dns/ibmcloud/internal/wrapper.go index 7c3eced4ec..1220586121 100644 --- a/providers/dns/ibmcloud/internal/wrapper.go +++ b/providers/dns/ibmcloud/internal/wrapper.go @@ -40,7 +40,7 @@ func (w Wrapper) CleanupTXTRecord(fqdn, domain string) error { domainID, err := getDomainID(service, domain) if err != nil { - return fmt.Errorf("failed to get domain ID for domain: %w", err) + return fmt.Errorf("failed to get domain ID: %w", err) } service.Options.Id = domainID From 09cfef303b208ce06aa7bb1103ff4be5281f28f2 Mon Sep 17 00:00:00 2001 From: Thomas Hofmann Date: Tue, 6 Dec 2022 16:56:52 +0100 Subject: [PATCH 4/6] Update providers/dns/ibmcloud/internal/wrapper.go Use a more efficient way to determine the "parent" domain. Co-authored-by: Ludovic Fernandez --- providers/dns/ibmcloud/internal/wrapper.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/providers/dns/ibmcloud/internal/wrapper.go b/providers/dns/ibmcloud/internal/wrapper.go index 1220586121..5b2e9d0bb5 100644 --- a/providers/dns/ibmcloud/internal/wrapper.go +++ b/providers/dns/ibmcloud/internal/wrapper.go @@ -71,12 +71,12 @@ func getDomainID(service services.Dns_Domain, domain string) (*int, error) { // For subdomains this is not unusual in softlayer. // So in case a subdomain like `sub.toplevel.tld` was used try again using the parent domain // (strip the first part in the domain string -> `toplevel.tld`). - domainParts := strings.Split(domain, ".") - if len(domainParts) > 2 { - return getDomainID(service, getParentDomain(domainParts)) - } else { + _, parent, found := strings.Cut(domain, ".") + if !found || strings.Index(parent, ".") == -1 { return nil, fmt.Errorf("no data found for domain: %s", domain) } + + return getDomainID(service, parent) } func getParentDomain(domainParts []string) string { From ddf993ad69b0d512b6503644f25e2d40d540acb4 Mon Sep 17 00:00:00 2001 From: Thomas Hofmann Date: Tue, 6 Dec 2022 16:57:08 +0100 Subject: [PATCH 5/6] Update providers/dns/ibmcloud/internal/wrapper.go Remove code no longer needed. Co-authored-by: Ludovic Fernandez --- providers/dns/ibmcloud/internal/wrapper.go | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/providers/dns/ibmcloud/internal/wrapper.go b/providers/dns/ibmcloud/internal/wrapper.go index 5b2e9d0bb5..9aa6ef7eea 100644 --- a/providers/dns/ibmcloud/internal/wrapper.go +++ b/providers/dns/ibmcloud/internal/wrapper.go @@ -79,17 +79,6 @@ func getDomainID(service services.Dns_Domain, domain string) (*int, error) { return getDomainID(service, parent) } -func getParentDomain(domainParts []string) string { - numParts := len(domainParts) - var sb strings.Builder - for i := 1; i < numParts; i++ { - sb.WriteString(domainParts[i]) - if i < numParts-1 { - sb.WriteString(".") - } - } - return sb.String() -} func findTxtRecords(service services.Dns_Domain, fqdn string) ([]datatypes.Dns_Domain_ResourceRecord, error) { var results []datatypes.Dns_Domain_ResourceRecord From 2de730eec4096733a8e8346e6b44eb82bf5d542f Mon Sep 17 00:00:00 2001 From: Thomas Hofmann Date: Tue, 6 Dec 2022 17:07:18 +0100 Subject: [PATCH 6/6] fix linter error: "Error: providers/dns/ibmcloud/internal/wrapper.go:75:15: S1003: should use !strings.Contains(parent, ".") instead (gosimple) if !found || strings.Index(parent, ".") == -1 {" --- providers/dns/ibmcloud/internal/wrapper.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/providers/dns/ibmcloud/internal/wrapper.go b/providers/dns/ibmcloud/internal/wrapper.go index 9aa6ef7eea..dd89d1d41c 100644 --- a/providers/dns/ibmcloud/internal/wrapper.go +++ b/providers/dns/ibmcloud/internal/wrapper.go @@ -72,14 +72,13 @@ func getDomainID(service services.Dns_Domain, domain string) (*int, error) { // So in case a subdomain like `sub.toplevel.tld` was used try again using the parent domain // (strip the first part in the domain string -> `toplevel.tld`). _, parent, found := strings.Cut(domain, ".") - if !found || strings.Index(parent, ".") == -1 { + if !found || !strings.Contains(parent, ".") { return nil, fmt.Errorf("no data found for domain: %s", domain) } return getDomainID(service, parent) } - func findTxtRecords(service services.Dns_Domain, fqdn string) ([]datatypes.Dns_Domain_ResourceRecord, error) { var results []datatypes.Dns_Domain_ResourceRecord