Skip to content

Commit

Permalink
feat(catalog): implement delete entity
Browse files Browse the repository at this point in the history
  • Loading branch information
odsod committed Mar 30, 2023
1 parent 0c16069 commit 08e0517
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
26 changes: 26 additions & 0 deletions catalog/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,29 @@ func (c *Client) execute(
}
return nil
}

func (c *Client) delete(
ctx context.Context,
path string,
) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("%s %s: %w", http.MethodDelete, path, err)
}
}()
httpRequest, err := http.NewRequestWithContext(ctx, http.MethodDelete, c.config.baseURL+path, nil)
if err != nil {
return err
}
httpResponse, err := c.httpClient.Do(httpRequest)
if err != nil {
return err
}
defer func() {
_ = httpResponse.Body.Close()
}()
if httpResponse.StatusCode != http.StatusNoContent {
return newStatusError(httpResponse)
}
return nil
}
21 changes: 21 additions & 0 deletions catalog/client_entities_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package catalog

import (
"context"
"fmt"
"net/url"
)

// DeleteEntityByUIDRequest is the request to the [Client.DeleteEntityByUID] method.
type DeleteEntityByUIDRequest struct {
// UID of the entity to get.
UID string
}

// DeleteEntityByUID gets an entity by its kind, namespace and name.
//
// See: https://backstage.io/docs/features/software-catalog/software-catalog-api/#get-entitiesby-uiduid
func (c *Client) DeleteEntityByUID(ctx context.Context, request *DeleteEntityByUIDRequest) error {
const pathTemplate = "/api/catalog/entities/by-uid/%s"
return c.delete(ctx, fmt.Sprintf(pathTemplate, url.PathEscape(request.UID)))
}
19 changes: 19 additions & 0 deletions cmd/backstage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func newEntitiesCommand() *cobra.Command {
cmd.AddCommand(newEntitiesListCommand())
cmd.AddCommand(newEntitiesGetByUIDCommand())
cmd.AddCommand(newEntitiesGetByNameCommand())
cmd.AddCommand(newEntitiesDeleteByUIDCommand())
return cmd
}

Expand Down Expand Up @@ -198,6 +199,24 @@ func newEntitiesGetByUIDCommand() *cobra.Command {
return cmd
}

func newEntitiesDeleteByUIDCommand() *cobra.Command {
cmd := newCommand()
cmd.Use = "delete"
cmd.Short = "Delete an entity by its UID"
uid := cmd.Flags().String("uid", "", "UID of the entity to get")
_ = cmd.MarkFlagRequired("uid")
cmd.RunE = func(cmd *cobra.Command, args []string) error {
client, err := newCatalogClient()
if err != nil {
return err
}
return client.DeleteEntityByUID(cmd.Context(), &catalog.DeleteEntityByUIDRequest{
UID: *uid,
})
}
return cmd
}

func printRawJSON(cmd *cobra.Command, raw json.RawMessage) {
var indented bytes.Buffer
indented.Grow(len(raw) * 2)
Expand Down

0 comments on commit 08e0517

Please sign in to comment.