From 67231c3d78c7b040d92cf009ed0f5ffe5d6281c2 Mon Sep 17 00:00:00 2001 From: Timo Behrendt Date: Fri, 27 Dec 2024 16:49:20 +0100 Subject: [PATCH] fix: only query relevant records when looking up record ids on Ionos API --- pkg/dnsProvider/ionos/api/ionosAPI.go | 37 ++++++++++++++-------- pkg/dnsProvider/ionos/api/ionosAPI_test.go | 9 +++--- pkg/dnsProvider/ionos/ionos_test.go | 2 +- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/pkg/dnsProvider/ionos/api/ionosAPI.go b/pkg/dnsProvider/ionos/api/ionosAPI.go index 3f3d543..2289c67 100644 --- a/pkg/dnsProvider/ionos/api/ionosAPI.go +++ b/pkg/dnsProvider/ionos/api/ionosAPI.go @@ -7,6 +7,7 @@ import ( "io" "net" "net/http" + "net/url" "realdnydns/model/common" ) @@ -18,7 +19,7 @@ type IonosAPI interface { SetARecord(tld string, subdomain string, ip net.IP, ttl int, prio int, disabled bool) (*common.ARecord, error) GetZoneId(tld string) (string, error) GetRecordId(zoneId string, tld string, subdomain string, recordType string) (string, error) - HttpCall(method string, url string, body io.Reader) (*http.Response, error) + HttpCall(method string, url string, body io.Reader, queryParams map[string]string) (*http.Response, error) } type IonosAPIImpl struct { @@ -76,8 +77,16 @@ func New(APIKey string, BaseURL string) IonosAPI { } } -func (i *IonosAPIImpl) HttpCall(method string, url string, body io.Reader) (*http.Response, error) { - req, err := http.NewRequest(method, i.BaseURL+url, body) +func (i *IonosAPIImpl) HttpCall(method string, path string, body io.Reader, queryParams map[string]string) (*http.Response, error) { + requestUrl, _ := url.Parse(i.BaseURL + path) + + query := requestUrl.Query() + for key, value := range queryParams { + query.Add(key, value) + } + requestUrl.RawQuery = query.Encode() + + req, err := http.NewRequest(method, requestUrl.String(), body) if err != nil { return nil, err } @@ -90,7 +99,7 @@ func (i *IonosAPIImpl) HttpCall(method string, url string, body io.Reader) (*htt } func (i *IonosAPIImpl) GetZoneId(tld string) (string, error) { - res, err := i.HttpCall("GET", "/v1/zones", nil) + res, err := i.HttpCall("GET", "/v1/zones", nil, nil) if err != nil { return "", err } @@ -111,7 +120,14 @@ func (i *IonosAPIImpl) GetZoneId(tld string) (string, error) { } func (i *IonosAPIImpl) GetRecordId(zoneId string, tld string, subdomain string, recordType string) (string, error) { - res, err := i.HttpCall("GET", "/v1/zones/"+zoneId, nil) + var domain string + if subdomain == "@" || subdomain == "" { + domain = tld + } else { + domain = subdomain + "." + tld + } + + res, err := i.HttpCall("GET", "/v1/zones/"+zoneId, nil, map[string]string{"recordName": domain, "recordType": recordType}) if err != nil { return "", err } @@ -122,13 +138,6 @@ func (i *IonosAPIImpl) GetRecordId(zoneId string, tld string, subdomain string, zone := ZoneResponse{} json.Unmarshal(responseBody, &zone) - var domain string - if subdomain == "@" || subdomain == "" { - domain = tld - } else { - domain = subdomain + "." + tld - } - for _, record := range zone.Records { if record.Type == recordType && record.Name == domain { return record.Id, nil @@ -159,7 +168,7 @@ func (i *IonosAPIImpl) SetARecord(tld string, subdomain string, ip net.IP, ttl i return nil, err } - res, err := i.HttpCall("PUT", "/v1/zones/"+zoneId+"/records/"+recordId, bytes.NewReader(changeRecordRequest)) + res, err := i.HttpCall("PUT", "/v1/zones/"+zoneId+"/records/"+recordId, bytes.NewReader(changeRecordRequest), nil) if err != nil { return nil, err } @@ -194,7 +203,7 @@ func (ionos *IonosAPIImpl) GetARecord(tld string, subdomain string) (*common.ARe return nil, err } - res, err := ionos.HttpCall("GET", "/v1/zones/"+zoneId+"/records/"+recordId, nil) + res, err := ionos.HttpCall("GET", "/v1/zones/"+zoneId+"/records/"+recordId, nil, nil) if err != nil { return nil, err } diff --git a/pkg/dnsProvider/ionos/api/ionosAPI_test.go b/pkg/dnsProvider/ionos/api/ionosAPI_test.go index f53fe0b..bb7806a 100644 --- a/pkg/dnsProvider/ionos/api/ionosAPI_test.go +++ b/pkg/dnsProvider/ionos/api/ionosAPI_test.go @@ -76,7 +76,8 @@ func utilMockServerImpl() func(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { if r.RequestURI == "/v1/zones" { response = zonesResponseJson - } else if r.RequestURI == "/v1/zones/1234567890" { + } else if r.RequestURI == "/v1/zones/1234567890?recordName=example.com&recordType=A" || + r.RequestURI == "/v1/zones/1234567890?recordName=sub.example.com&recordType=A" { response = zoneResponseJson } else if r.RequestURI == "/v1/zones/1234567890/records/abcdefghij" { response = recordResponseSubJson @@ -106,7 +107,7 @@ func TestHttpCall(t *testing.T) { func testHttpCallGet(api IonosAPI) func(t *testing.T) { return func(t *testing.T) { - res, err := api.HttpCall("GET", "/v1/zones", nil) + res, err := api.HttpCall("GET", "/v1/zones", nil, nil) if err != nil { t.Fatalf("HttpCall() returned unexpected error: %v", err) @@ -120,7 +121,7 @@ func testHttpCallGet(api IonosAPI) func(t *testing.T) { func testHttpCallPut(api IonosAPI) func(t *testing.T) { return func(t *testing.T) { - res, err := api.HttpCall("PUT", "/v1/zones/1234567890/records/abcdefghij", nil) + res, err := api.HttpCall("PUT", "/v1/zones/1234567890/records/abcdefghij", nil, nil) if err != nil { t.Fatalf("HttpCall() returned unexpected error: %v", err) @@ -141,7 +142,7 @@ func testHttpCallNonExistingEndpoint() func(t *testing.T) { api := New("dummyKey", mockServer.URL) - res, err := api.HttpCall("GET", "/v1/nonExistingEndpoint", nil) + res, err := api.HttpCall("GET", "/v1/nonExistingEndpoint", nil, nil) if err != nil { t.Fatalf("HttpCall() returned unexpected error: %v", err) diff --git a/pkg/dnsProvider/ionos/ionos_test.go b/pkg/dnsProvider/ionos/ionos_test.go index d9b5448..ef51297 100644 --- a/pkg/dnsProvider/ionos/ionos_test.go +++ b/pkg/dnsProvider/ionos/ionos_test.go @@ -82,7 +82,7 @@ func (m *MockIonosAPI) GetZoneId(tld string) (string, error) { return m.GetZoneIdFunc(tld) } -func (m *MockIonosAPI) HttpCall(method string, url string, body io.Reader) (*http.Response, error) { +func (m *MockIonosAPI) HttpCall(method string, url string, body io.Reader, queryParams map[string]string) (*http.Response, error) { return m.HttpCallFunc(method, url, body) } -- 2.49.1