Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NETPROD-3583] Added name param in ListOption to get resource by name #670

Merged
merged 4 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 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,43 @@ 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
}

if len(root.Certificates) == 0 {
return nil, resp, err
}

return &root.Certificates[0], resp, err
guptado marked this conversation as resolved.
Show resolved Hide resolved
}

// 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)
})

certificate, resp, err := client.Certificates.ListByName(ctx, certName, nil)

require.NoError(t, err)

expectedCertificate := &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, expectedCertificate, certificate)

expectedMeta := &Meta{
Total: 1,
}
assert.Equal(t, expectedMeta, resp.Meta)
}

func TestCertificates_Create(t *testing.T) {
tests := []struct {
desc string
Expand Down
Loading