From 137f5da2e5adc15958da280d3bf8b704e3d42160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilson=20J=C3=BAnior?= Date: Mon, 16 Sep 2024 09:48:16 -0300 Subject: [PATCH] plugin: working to remove cert-manager request by name --- cmd/plugin/rpaasv2/cmd/certificates.go | 8 +++++- cmd/plugin/rpaasv2/cmd/certificates_test.go | 2 +- pkg/rpaas/client/certificate.go | 29 ++++++++++++++++++++- pkg/rpaas/client/certificate_test.go | 4 +-- pkg/rpaas/client/client.go | 3 ++- pkg/rpaas/client/fake/client.go | 18 ++++++++++--- 6 files changed, 54 insertions(+), 10 deletions(-) diff --git a/cmd/plugin/rpaasv2/cmd/certificates.go b/cmd/plugin/rpaasv2/cmd/certificates.go index 21ea8aa9..df70adcd 100644 --- a/cmd/plugin/rpaasv2/cmd/certificates.go +++ b/cmd/plugin/rpaasv2/cmd/certificates.go @@ -192,7 +192,13 @@ func runDeleteCertificate(c *cli.Context) error { } if c.Bool("cert-manager") { - if err = client.DeleteCertManager(c.Context, c.String("instance"), c.String("issuer")); err != nil { + if c.String("name") != "" { + err = client.DeleteCertManagerByName(c.Context, c.String("instance"), c.String("name")) + } else { + err = client.DeleteCertManagerByIssuer(c.Context, c.String("instance"), c.String("issuer")) + } + + if err != nil { return err } diff --git a/cmd/plugin/rpaasv2/cmd/certificates_test.go b/cmd/plugin/rpaasv2/cmd/certificates_test.go index 61b9890b..17295c3a 100644 --- a/cmd/plugin/rpaasv2/cmd/certificates_test.go +++ b/cmd/plugin/rpaasv2/cmd/certificates_test.go @@ -222,7 +222,7 @@ func TestDeleteCertificate(t *testing.T) { name: "removing a certificate request for Cert Manager", args: []string{"./rpaasv2", "certificates", "delete", "-i", "my-instance", "--cert-manager", "--issuer", "some-issuer"}, client: &fake.FakeClient{ - FakeDeleteCertManager: func(instance, issuer string) error { + FakeDeleteCertManagerByIssuer: func(instance, issuer string) error { assert.Equal(t, "my-instance", instance) assert.Equal(t, "some-issuer", issuer) return nil diff --git a/pkg/rpaas/client/certificate.go b/pkg/rpaas/client/certificate.go index c8d45dd2..91b509b5 100644 --- a/pkg/rpaas/client/certificate.go +++ b/pkg/rpaas/client/certificate.go @@ -193,7 +193,34 @@ func (c *client) UpdateCertManager(ctx context.Context, args UpdateCertManagerAr return nil } -func (c *client) DeleteCertManager(ctx context.Context, instance, issuer string) error { +func (c *client) DeleteCertManagerByName(ctx context.Context, instance, name string) error { + if instance == "" { + return ErrMissingInstance + } + + data := url.Values{} + if name != "" { + data.Set("name", name) + } + + req, err := c.newRequestWithQueryString("DELETE", fmt.Sprintf("/resources/%s/cert-manager", instance), nil, data) + if err != nil { + return err + } + + response, err := c.do(ctx, req) + if err != nil { + return err + } + + if response.StatusCode != http.StatusOK { + return newErrUnexpectedStatusCodeFromResponse(response) + } + + return nil +} + +func (c *client) DeleteCertManagerByIssuer(ctx context.Context, instance, issuer string) error { if instance == "" { return ErrMissingInstance } diff --git a/pkg/rpaas/client/certificate_test.go b/pkg/rpaas/client/certificate_test.go index 1a4494c1..67f3660e 100644 --- a/pkg/rpaas/client/certificate_test.go +++ b/pkg/rpaas/client/certificate_test.go @@ -303,7 +303,7 @@ func TestClientThroughTsuru_UpdateCertManager(t *testing.T) { } } -func TestClientThroughTsuru_DeleteCertManager(t *testing.T) { +func TestClientThroughTsuru_DeleteCertManagerByIssuer(t *testing.T) { tests := map[string]struct { instance string issuer string @@ -344,7 +344,7 @@ func TestClientThroughTsuru_DeleteCertManager(t *testing.T) { client, server := newClientThroughTsuru(t, tt.handler) defer server.Close() - err := client.DeleteCertManager(context.TODO(), tt.instance, tt.issuer) + err := client.DeleteCertManagerByIssuer(context.TODO(), tt.instance, tt.issuer) if tt.expectedError == "" { require.NoError(t, err) return diff --git a/pkg/rpaas/client/client.go b/pkg/rpaas/client/client.go index 5a63ef1d..85f42c74 100644 --- a/pkg/rpaas/client/client.go +++ b/pkg/rpaas/client/client.go @@ -180,7 +180,8 @@ type Client interface { ListCertManagerRequests(ctx context.Context, instance string) ([]types.CertManager, error) UpdateCertManager(ctx context.Context, args UpdateCertManagerArgs) error - DeleteCertManager(ctx context.Context, instance, issuer string) error + DeleteCertManagerByName(ctx context.Context, instance, name string) error + DeleteCertManagerByIssuer(ctx context.Context, instance, issuer string) error GetMetadata(ctx context.Context, instance string) (*types.Metadata, error) SetMetadata(ctx context.Context, instance string, metadata *types.Metadata) error diff --git a/pkg/rpaas/client/fake/client.go b/pkg/rpaas/client/fake/client.go index 33ed37f8..e99d4760 100644 --- a/pkg/rpaas/client/fake/client.go +++ b/pkg/rpaas/client/fake/client.go @@ -38,7 +38,6 @@ type FakeClient struct { 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 FakeAddExtraFiles func(args client.ExtraFilesArgs) error FakeUpdateExtraFiles func(args client.ExtraFilesArgs) error @@ -48,6 +47,9 @@ type FakeClient struct { FakeGetMetadata func(instance string) (*types.Metadata, error) FakeSetMetadata func(instance string, metadata *types.Metadata) error FakeUnsetMetadata func(instance string, metadata *types.Metadata) error + + FakeDeleteCertManagerByName func(instance, name string) error + FakeDeleteCertManagerByIssuer func(instance, issuer string) error } func (f *FakeClient) Info(ctx context.Context, args client.InfoArgs) (*types.InstanceInfo, error) { @@ -221,9 +223,17 @@ func (f *FakeClient) UpdateCertManager(ctx context.Context, args client.UpdateCe return nil } -func (f *FakeClient) DeleteCertManager(ctx context.Context, instance, issuer string) error { - if f.FakeDeleteCertManager != nil { - return f.FakeDeleteCertManager(instance, issuer) +func (f *FakeClient) DeleteCertManagerByName(ctx context.Context, instance, name string) error { + if f.FakeDeleteCertManagerByName != nil { + return f.FakeDeleteCertManagerByName(instance, name) + } + + return nil +} + +func (f *FakeClient) DeleteCertManagerByIssuer(ctx context.Context, instance, issuer string) error { + if f.FakeDeleteCertManagerByName != nil { + return f.FakeDeleteCertManagerByName(instance, issuer) } return nil