Skip to content

Commit

Permalink
feat(client): add method to list cert manager requests
Browse files Browse the repository at this point in the history
  • Loading branch information
nettoclaudio committed Oct 28, 2021
1 parent 5197ecb commit a337495
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
31 changes: 31 additions & 0 deletions pkg/rpaas/client/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"net/http"
"net/url"
"strings"

"github.com/tsuru/rpaas-operator/pkg/rpaas/client/types"
)

func (args UpdateCertificateArgs) Validate() error {
Expand Down Expand Up @@ -125,6 +127,35 @@ func (c *client) DeleteCertificate(ctx context.Context, args DeleteCertificateAr
return nil
}

func (c *client) ListCertManagerRequests(ctx context.Context, instance string) ([]types.CertManager, error) {
if instance == "" {
return nil, ErrMissingInstance
}

req, err := c.newRequest("GET", fmt.Sprintf("/resources/%s/cert-manager", instance), nil, instance)
if err != nil {
return nil, err
}

response, err := c.do(ctx, req)
if err != nil {
return nil, err
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
return nil, newErrUnexpectedStatusCodeFromResponse(response)
}

var cmRequests []types.CertManager
err = json.NewDecoder(response.Body).Decode(&cmRequests)
if err != nil {
return nil, err
}

return cmRequests, nil
}

func (args *UpdateCertManagerArgs) Validate() error {
if args.Instance == "" {
return ErrMissingInstance
Expand Down
64 changes: 64 additions & 0 deletions pkg/rpaas/client/certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,70 @@ func TestClientThroughTsuru_DeleteCertificate(t *testing.T) {
}
}

func TestClientThroughTsuru_ListCertManagerRequests(t *testing.T) {
tests := map[string]struct {
instance string
expectedError string
expected []types.CertManager
handler http.HandlerFunc
}{
"when instance is empty": {
expectedError: "rpaasv2: instance cannot be empty",
},

"when server returns several requests": {
instance: "my-instance",
handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, "GET")
assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/cert-manager"), r.URL.RequestURI())
assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization"))

fmt.Fprintf(w, `[{"issuer": "my-issuer", "dnsNames": ["www.example.com", "web.example.com"], "ipAddresses": ["169.196.254.100"]}, {"issuer": "my-issuer-1", "dnsNames": ["*.test"]}]`)
}),
expected: []types.CertManager{
{
Issuer: "my-issuer",
DNSNames: []string{"www.example.com", "web.example.com"},
IPAddresses: []string{"169.196.254.100"},
},
{
Issuer: "my-issuer-1",
DNSNames: []string{"*.test"},
},
},
},

"when server returns an error": {
instance: "my-instance",
handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, "GET")
assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/cert-manager"), r.URL.RequestURI())
assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization"))

w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, `{"Msg": "some error"}`)
}),
expectedError: `rpaasv2: unexpected status code: 500 Internal Server Error, detail: {"Msg": "some error"}`,
},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
client, server := newClientThroughTsuru(t, tt.handler)
defer server.Close()

cmRequests, err := client.ListCertManagerRequests(context.TODO(), tt.instance)
if tt.expectedError == "" {
require.NoError(t, err)
return
}

assert.EqualError(t, err, tt.expectedError)
assert.Equal(t, cmRequests, tt.expected)
})
}
}

func TestClientThroughTsuru_UpdateCertManager(t *testing.T) {
tests := map[string]struct {
args UpdateCertManagerArgs
Expand Down
1 change: 1 addition & 0 deletions pkg/rpaas/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type Client interface {

SetService(service string) (Client, error)

ListCertManagerRequests(ctx context.Context, instance string) ([]types.CertManager, error)
UpdateCertManager(ctx context.Context, args UpdateCertManagerArgs) error
DeleteCertManager(ctx context.Context, instance, issuer string) error
}
9 changes: 9 additions & 0 deletions pkg/rpaas/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type FakeClient struct {
FakeListAccessControlList func(instance string) ([]types.AllowedUpstream, error)
FakeRemoveAccessControlList func(instance, host string, port int) error
FakeSetService func(service string) error
FakeListCertManagerRequests func(instance string) ([]types.CertManager, error)
FakeUpdateCertManager func(args client.UpdateCertManagerArgs) error
FakeDeleteCertManager func(instance, issuer string) error
FakeLog func(args client.LogArgs) error
Expand Down Expand Up @@ -195,6 +196,14 @@ func (f *FakeClient) SetService(service string) (client.Client, error) {
return f, nil
}

func (f *FakeClient) ListCertManagerRequests(ctx context.Context, instance string) ([]types.CertManager, error) {
if f.FakeListCertManagerRequests != nil {
return f.FakeListCertManagerRequests(instance)
}

return nil, nil
}

func (f *FakeClient) UpdateCertManager(ctx context.Context, args client.UpdateCertManagerArgs) error {
if f.FakeUpdateCertManager != nil {
return f.FakeUpdateCertManager(args)
Expand Down

0 comments on commit a337495

Please sign in to comment.