From 16a4709be517d8f73fd5d1c284a5175df09d5351 Mon Sep 17 00:00:00 2001 From: guptado <141348137+guptado@users.noreply.github.com> Date: Wed, 28 Feb 2024 23:33:03 +0530 Subject: [PATCH] [NETPROD-3583] Added name param in ListOption to get resource by name (#670) * Added name param in ListOption to get resource by name * Created different method for getting certs by name * modified func to return slice instead of single value --------- Co-authored-by: Andrew Starr-Bochicchio --- certificates.go | 35 +++++++++++++++++++++++++++ certificates_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/certificates.go b/certificates.go index faf26a3e..7612acf0 100644 --- a/certificates.go +++ b/certificates.go @@ -2,6 +2,7 @@ package godo import ( "context" + "fmt" "net/http" "path" ) @@ -13,6 +14,7 @@ const certificatesBasePath = "/v2/certificates" type CertificatesService interface { Get(context.Context, string) (*Certificate, *Response, error) List(context.Context, *ListOptions) ([]Certificate, *Response, error) + ListByName(context.Context, string, *ListOptions) ([]Certificate, *Response, error) Create(context.Context, *CertificateRequest) (*Certificate, *Response, error) Delete(context.Context, string) (*Response, error) } @@ -101,6 +103,39 @@ func (c *CertificatesServiceOp) List(ctx context.Context, opt *ListOptions) ([]C return root.Certificates, resp, nil } +func (c *CertificatesServiceOp) ListByName(ctx context.Context, name string, opt *ListOptions) ([]Certificate, *Response, error) { + + if len(name) < 1 { + return nil, nil, NewArgError("name", "cannot be an empty string") + } + + path := fmt.Sprintf("%s?name=%s", certificatesBasePath, name) + urlStr, err := addOptions(path, opt) + if err != nil { + return nil, nil, err + } + + req, err := c.client.NewRequest(ctx, http.MethodGet, urlStr, nil) + if err != nil { + return nil, nil, err + } + + root := new(certificatesRoot) + resp, err := c.client.Do(ctx, req, root) + if err != nil { + return nil, resp, err + } + + if l := root.Links; l != nil { + resp.Links = l + } + if m := root.Meta; m != nil { + resp.Meta = m + } + + return root.Certificates, resp, err +} + // Create a new certificate with provided configuration. func (c *CertificatesServiceOp) Create(ctx context.Context, cr *CertificateRequest) (*Certificate, *Response, error) { req, err := c.client.NewRequest(ctx, http.MethodPost, certificatesBasePath, cr) diff --git a/certificates_test.go b/certificates_test.go index c94b1272..a5f6d6ed 100644 --- a/certificates_test.go +++ b/certificates_test.go @@ -137,6 +137,63 @@ func TestCertificates_List(t *testing.T) { assert.Equal(t, expectedMeta, resp.Meta) } +func TestCertificates_ListByName(t *testing.T) { + setup() + defer teardown() + + outputResp := `{ + "certificates": [ + { + "id": "892071a0-bb95-49bc-8021-3afd67a210bf", + "name": "web-cert-01", + "dns_names": [ + "somedomain.com", + "api.somedomain.com" + ], + "not_after": "2017-02-22T00:23:00Z", + "sha1_fingerprint": "dfcc9f57d86bf58e321c2c6c31c7a971be244ac7", + "created_at": "2017-02-08T16:02:37Z", + "state": "verified", + "type": "custom" + } + ], + "links": {}, + "meta": { + "total": 1 + } + }` + + certName := "web-cert-01" + + mux.HandleFunc("/v2/certificates", func(w http.ResponseWriter, r *http.Request) { + require.Equal(t, certName, r.URL.Query().Get("name")) + testMethod(t, r, http.MethodGet) + fmt.Fprint(w, outputResp) + }) + + certificates, resp, err := client.Certificates.ListByName(ctx, certName, nil) + + require.NoError(t, err) + + expectedCertificates := []Certificate{{ + ID: "892071a0-bb95-49bc-8021-3afd67a210bf", + Name: "web-cert-01", + DNSNames: []string{"somedomain.com", "api.somedomain.com"}, + NotAfter: "2017-02-22T00:23:00Z", + SHA1Fingerprint: "dfcc9f57d86bf58e321c2c6c31c7a971be244ac7", + Created: "2017-02-08T16:02:37Z", + State: "verified", + Type: "custom", + }} + + assert.Equal(t, expectedCertificates, certificates) + + expectedMeta := &Meta{ + Total: 1, + } + assert.Equal(t, expectedMeta, resp.Meta) +} + func TestCertificates_Create(t *testing.T) { tests := []struct { desc string