diff --git a/pkg/rpaas/client/certificate.go b/pkg/rpaas/client/certificate.go index 3f132fe07..1ff79485b 100644 --- a/pkg/rpaas/client/certificate.go +++ b/pkg/rpaas/client/certificate.go @@ -14,6 +14,8 @@ import ( "net/http" "net/url" "strings" + + "github.com/tsuru/rpaas-operator/pkg/rpaas/client/types" ) func (args UpdateCertificateArgs) Validate() error { @@ -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 diff --git a/pkg/rpaas/client/certificate_test.go b/pkg/rpaas/client/certificate_test.go index 5b52101d8..ada71dcae 100644 --- a/pkg/rpaas/client/certificate_test.go +++ b/pkg/rpaas/client/certificate_test.go @@ -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 diff --git a/pkg/rpaas/client/client.go b/pkg/rpaas/client/client.go index 4dea939a3..5e2cdc3d7 100644 --- a/pkg/rpaas/client/client.go +++ b/pkg/rpaas/client/client.go @@ -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 } diff --git a/pkg/rpaas/client/fake/client.go b/pkg/rpaas/client/fake/client.go index 794e520b4..bf609cc50 100644 --- a/pkg/rpaas/client/fake/client.go +++ b/pkg/rpaas/client/fake/client.go @@ -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 @@ -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)