fix: only query relevant records when looking up record ids on Ionos API
All checks were successful
CI / test (pull_request) Successful in 38s
All checks were successful
CI / test (pull_request) Successful in 38s
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user