fix: only query relevant records when looking up record ids on Ionos API #19
@@ -7,6 +7,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"realdnydns/model/common"
|
"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)
|
SetARecord(tld string, subdomain string, ip net.IP, ttl int, prio int, disabled bool) (*common.ARecord, error)
|
||||||
GetZoneId(tld string) (string, error)
|
GetZoneId(tld string) (string, error)
|
||||||
GetRecordId(zoneId string, tld string, subdomain string, recordType 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 {
|
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) {
|
func (i *IonosAPIImpl) HttpCall(method string, path string, body io.Reader, queryParams map[string]string) (*http.Response, error) {
|
||||||
req, err := http.NewRequest(method, i.BaseURL+url, body)
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
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) {
|
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 {
|
if err != nil {
|
||||||
return "", err
|
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) {
|
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 {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -122,13 +138,6 @@ func (i *IonosAPIImpl) GetRecordId(zoneId string, tld string, subdomain string,
|
|||||||
zone := ZoneResponse{}
|
zone := ZoneResponse{}
|
||||||
json.Unmarshal(responseBody, &zone)
|
json.Unmarshal(responseBody, &zone)
|
||||||
|
|
||||||
var domain string
|
|
||||||
if subdomain == "@" || subdomain == "" {
|
|
||||||
domain = tld
|
|
||||||
} else {
|
|
||||||
domain = subdomain + "." + tld
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, record := range zone.Records {
|
for _, record := range zone.Records {
|
||||||
if record.Type == recordType && record.Name == domain {
|
if record.Type == recordType && record.Name == domain {
|
||||||
return record.Id, nil
|
return record.Id, nil
|
||||||
@@ -159,7 +168,7 @@ func (i *IonosAPIImpl) SetARecord(tld string, subdomain string, ip net.IP, ttl i
|
|||||||
return nil, err
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -194,7 +203,7 @@ func (ionos *IonosAPIImpl) GetARecord(tld string, subdomain string) (*common.ARe
|
|||||||
return nil, err
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,7 +76,8 @@ func utilMockServerImpl() func(w http.ResponseWriter, r *http.Request) {
|
|||||||
if r.Method == "GET" {
|
if r.Method == "GET" {
|
||||||
if r.RequestURI == "/v1/zones" {
|
if r.RequestURI == "/v1/zones" {
|
||||||
response = zonesResponseJson
|
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
|
response = zoneResponseJson
|
||||||
} else if r.RequestURI == "/v1/zones/1234567890/records/abcdefghij" {
|
} else if r.RequestURI == "/v1/zones/1234567890/records/abcdefghij" {
|
||||||
response = recordResponseSubJson
|
response = recordResponseSubJson
|
||||||
@@ -106,7 +107,7 @@ func TestHttpCall(t *testing.T) {
|
|||||||
|
|
||||||
func testHttpCallGet(api IonosAPI) func(t *testing.T) {
|
func testHttpCallGet(api IonosAPI) func(t *testing.T) {
|
||||||
return 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 {
|
if err != nil {
|
||||||
t.Fatalf("HttpCall() returned unexpected error: %v", err)
|
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) {
|
func testHttpCallPut(api IonosAPI) func(t *testing.T) {
|
||||||
return 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 {
|
if err != nil {
|
||||||
t.Fatalf("HttpCall() returned unexpected error: %v", err)
|
t.Fatalf("HttpCall() returned unexpected error: %v", err)
|
||||||
@@ -141,7 +142,7 @@ func testHttpCallNonExistingEndpoint() func(t *testing.T) {
|
|||||||
|
|
||||||
api := New("dummyKey", mockServer.URL)
|
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 {
|
if err != nil {
|
||||||
t.Fatalf("HttpCall() returned unexpected error: %v", err)
|
t.Fatalf("HttpCall() returned unexpected error: %v", err)
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ func (m *MockIonosAPI) GetZoneId(tld string) (string, error) {
|
|||||||
return m.GetZoneIdFunc(tld)
|
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)
|
return m.HttpCallFunc(method, url, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user