Skip to content

Commit f154caf

Browse files
committed
fix: 404
1 parent 0198282 commit f154caf

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

providers/dns/f5xc/f5xc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
113113

114114
existingRRSet, err := d.client.GetRRSet(context.Background(), dns01.UnFqdn(authZone), d.config.GroupName, subDomain, "TXT")
115115
if err != nil {
116-
return err
116+
return fmt.Errorf("f5xc: get RR Set: %w", err)
117117
}
118118

119119
// New RRSet.
120-
if existingRRSet.RRSet.TXTRecord == nil {
120+
if existingRRSet == nil || existingRRSet.RRSet.TXTRecord == nil {
121121
rrSet := internal.RRSet{
122122
Description: "lego",
123123
TTL: d.config.TTL,

providers/dns/f5xc/internal/client.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ func (c *Client) GetRRSet(ctx context.Context, dnsZoneName, groupName, recordNam
8686

8787
err = c.do(req, result)
8888
if err != nil {
89+
usce := &APIError{}
90+
if errors.As(err, &usce) && usce.StatusCode == http.StatusNotFound {
91+
return nil, nil
92+
}
93+
8994
return nil, err
9095
}
9196

@@ -148,8 +153,7 @@ func (c *Client) do(req *http.Request, result any) error {
148153
defer func() { _ = resp.Body.Close() }()
149154

150155
if resp.StatusCode/100 != 2 {
151-
raw, _ := io.ReadAll(resp.Body)
152-
return errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw)
156+
return parseError(req, resp)
153157
}
154158

155159
if result == nil {
@@ -192,3 +196,15 @@ func newJSONRequest(ctx context.Context, method string, endpoint *url.URL, paylo
192196

193197
return req, nil
194198
}
199+
200+
func parseError(req *http.Request, resp *http.Response) error {
201+
raw, _ := io.ReadAll(resp.Body)
202+
203+
apiErr := APIError{StatusCode: resp.StatusCode}
204+
err := json.Unmarshal(raw, &apiErr)
205+
if err != nil {
206+
return errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw)
207+
}
208+
209+
return &apiErr
210+
}

providers/dns/f5xc/internal/client_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ func TestClient_Get(t *testing.T) {
122122
assert.Equal(t, expected, result)
123123
}
124124

125+
func TestClient_Get_not_found(t *testing.T) {
126+
client := setupTest(t, "GET /api/config/dns/namespaces/system/dns_zones/example.com/rrsets/groupA/www/TXT", http.StatusNotFound, "error_404.json")
127+
128+
result, err := client.GetRRSet(context.Background(), "example.com", "groupA", "www", "TXT")
129+
require.NoError(t, err)
130+
131+
assert.Nil(t, result)
132+
}
133+
125134
func TestClient_Get_error(t *testing.T) {
126135
client := setupTest(t, "GET /api/config/dns/namespaces/system/dns_zones/example.com/rrsets/groupA/www/TXT", http.StatusBadRequest, "")
127136

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"code": 5,
3+
"details": [],
4+
"message": "the requested resource record was not found: (group,name,type) (acme-records,_acme-challenge,TXT)"
5+
}

providers/dns/f5xc/internal/types.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
package internal
22

3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
type APIError struct {
9+
StatusCode int `json:"-"`
10+
Code int `json:"code"`
11+
Details []string `json:"details"`
12+
Message string `json:"message"`
13+
}
14+
15+
func (a *APIError) Error() string {
16+
var details string
17+
if len(a.Details) > 0 {
18+
details = " " + strings.Join(a.Details, ", ")
19+
}
20+
21+
return fmt.Sprintf("code: %d, message: %s%s", a.Code, a.Message, details)
22+
}
23+
324
type APIRRSet struct {
425
DNSZoneName string `json:"dns_zone_name,omitempty"`
526
GroupName string `json:"group_name,omitempty"`

0 commit comments

Comments
 (0)