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 725af94 commit dd98d6c
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 16 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
6 changes: 3 additions & 3 deletions pkg/rpaas/client/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (c *client) GetMetadata(ctx context.Context, instance string) (*types.Metad
}

pathName := fmt.Sprintf("/resources/%s/metadata", instance)
req, err := c.newRequest("GET", pathName, nil, instance)
req, err := c.newRequest("GET", pathName, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -55,7 +55,7 @@ func (c *client) SetMetadata(ctx context.Context, instance string, metadata *typ
body := bytes.NewReader(b)

pathName := fmt.Sprintf("/resources/%s/metadata", instance)
req, err := c.newRequest("POST", pathName, body, instance)
req, err := c.newRequest("POST", pathName, body)
if err != nil {
return err
}
Expand Down Expand Up @@ -86,7 +86,7 @@ func (c *client) UnsetMetadata(ctx context.Context, instance string, metadata *t
body := bytes.NewReader(b)

pathName := fmt.Sprintf("/resources/%s/metadata", instance)
req, err := c.newRequest("DELETE", pathName, body, instance)
req, err := c.newRequest("DELETE", pathName, body)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/rpaas/client/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestClientThroughTsuru_GetMetadata(t *testing.T) {
instance: "my-instance",
handler: 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/metadata"), r.URL.RequestURI())
assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/metadata", FakeTsuruService, "my-instance"), r.URL.RequestURI())
assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization"))

metadata := types.Metadata{
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestClientThroughTsuru_SetMetadata(t *testing.T) {
instance: "my-instance",
handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, "POST")
assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/metadata"), r.URL.RequestURI())
assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/metadata", FakeTsuruService, "my-instance"), r.URL.RequestURI())
assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization"))
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
assert.NotNil(t, r.Body)
Expand Down Expand Up @@ -154,7 +154,7 @@ func TestClientThroughTsuru_UnsetMetadata(t *testing.T) {
instance: "my-instance",
handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, "DELETE")
assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/metadata"), r.URL.RequestURI())
assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/metadata", FakeTsuruService, "my-instance"), r.URL.RequestURI())
assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization"))
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
assert.NotNil(t, r.Body)
Expand Down

0 comments on commit dd98d6c

Please sign in to comment.