Skip to content

Commit

Permalink
[NETPROD-3583] Added name param in ListOption to get resource by name (
Browse files Browse the repository at this point in the history
…#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 <[email protected]>
  • Loading branch information
guptado and andrewsomething authored Feb 28, 2024
1 parent d5fe4d2 commit 16a4709
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
35 changes: 35 additions & 0 deletions certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package godo

import (
"context"
"fmt"
"net/http"
"path"
)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
57 changes: 57 additions & 0 deletions certificates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 16a4709

Please sign in to comment.