Skip to content

Commit

Permalink
plugin: working to remove cert-manager request by name
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Sep 16, 2024
1 parent 9a914af commit 137f5da
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 10 deletions.
8 changes: 7 additions & 1 deletion cmd/plugin/rpaasv2/cmd/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/plugin/rpaasv2/cmd/certificates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 28 additions & 1 deletion pkg/rpaas/client/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/rpaas/client/certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pkg/rpaas/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 14 additions & 4 deletions pkg/rpaas/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 137f5da

Please sign in to comment.