From b9bdfa39b70f13e8e980a0d4c2f065578ebe122b Mon Sep 17 00:00:00 2001 From: Jan Delgado Date: Wed, 15 Feb 2023 20:41:47 +0100 Subject: [PATCH] use record ID from response of create record (#2) IONOS changed the API so that create response now returns the created records. That makes the call to read the previously created records obsolete. --- .gitignore | 1 + README.md | 2 +- client.go | 21 ++++++++++++--------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 07003ad..e9c45e9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +.envrc _gitignore/ *~ diff --git a/README.md b/README.md index 3740892..dd00348 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/client.go b/client.go index 1154b28..34105b2 100644 --- a/client.go +++ b/client.go @@ -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),