Skip to content

Commit

Permalink
use record ID from response of create record (#2)
Browse files Browse the repository at this point in the history
IONOS changed the API so that create response now returns the created
records. That makes the call to read the previously created records
obsolete.
  • Loading branch information
jandelgado authored Feb 15, 2023
1 parent ffec74b commit b9bdfa3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.envrc
_gitignore/
*~
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# IONOS DNS API for `libdns`

This package implements the libdns interfaces for the [IONOS DNS
API (beta)](https://developer.hosting.ionos.de/docs/dns)
API](https://developer.hosting.ionos.de/docs/dns)

## Authenticating

Expand Down
21 changes: 12 additions & 9 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,23 +197,26 @@ func createRecord(ctx context.Context, token string, zoneName string, r libdns.R

uri := fmt.Sprintf("%s/zones/%s/records", APIEndpoint, zoneResp.ID)
req, err := http.NewRequestWithContext(ctx, "POST", uri, bytes.NewBuffer(reqBuffer))
_, err = doRequest(token, req) // no data is returned on success, just 201

if err != nil {
return libdns.Record{}, err
}

// re-read the record so we get it's ID. IONOS API does not return the
// ID currently in the response.
createdRec, err := findRecordInZone(ctx, token, zoneName, libdns.AbsoluteName(r.Name, zoneName), r.Type)
// as result of the POST, a zoneDescriptor array is returned
data, err := doRequest(token, req)
if err != nil {
// Thats bad, the record was created, but we can not read it ?!
// in this case we just return an empty ID
// log.Printf("ERROR could not find record: %+v", err)
return libdns.Record{}, err
}
zones := make([]zoneDescriptor, 0)
if err = json.Unmarshal(data, &zones); err != nil {
return libdns.Record{}, err
}

if len(zones) != 1 {
return libdns.Record{}, fmt.Errorf("unexpected response from create record (size mismatch)")
}

return libdns.Record{
ID: createdRec.ID,
ID: zones[0].ID,
Type: r.Type,
// always return partially qualified name, relative to zone for libdns
Name: libdns.RelativeName(unFQDN(r.Name), zoneName),
Expand Down

0 comments on commit b9bdfa3

Please sign in to comment.