Skip to content

Commit

Permalink
feat(catalog): implement entities get-by-name endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
odsod committed Mar 29, 2023
1 parent 4809378 commit 35a83c6
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
51 changes: 51 additions & 0 deletions catalog/client_entities_getbyname.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package catalog

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)

// GetEntityByNameRequest is the request to the [Client.GetEntityByName] method.
type GetEntityByNameRequest struct {
// Kind of the entity to get.
Kind string
// Namespace of the entity to get.
Namespace string
// Name of the entity to get.
Name string
}

// GetEntityByName gets an entity by its kind, namespace and name.
//
// See: https://backstage.io/docs/features/software-catalog/software-catalog-api/#get-entitiesby-namekindnamespacename
func (c *Client) GetEntityByName(ctx context.Context, request *GetEntityByNameRequest) (*Entity, error) {
const pathTemplate = "/api/catalog/entities/by-name/%s/%s/%s"
path := fmt.Sprintf(
pathTemplate,
url.PathEscape(request.Kind),
url.PathEscape(request.Namespace),
url.PathEscape(request.Name),
)
var rawEntity json.RawMessage
if err := c.get(ctx, path, nil, func(response *http.Response) error {
data, err := io.ReadAll(response.Body)
if err != nil {
return err
}
rawEntity = data
return nil
}); err != nil {
return nil, err
}
entity := Entity{
Raw: rawEntity,
}
if err := json.Unmarshal(rawEntity, &entity); err != nil {
return nil, err
}
return &entity, nil
}
45 changes: 42 additions & 3 deletions cmd/backstage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,23 @@ func newCatalogCommand() *cobra.Command {
Use: "catalog",
Short: "Work with the Backstage catalog",
}
cmd.AddCommand(newListEntitiesCommand())
cmd.AddCommand(newEntitiesCommand())
return cmd
}

func newListEntitiesCommand() *cobra.Command {
func newEntitiesCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list-entities",
Use: "entities",
Short: "Read entities from the Backstage catalog",
}
cmd.AddCommand(newEntitiesListCommand())
cmd.AddCommand(newEntitiesGetByNameCommand())
return cmd
}

func newEntitiesListCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List entities in the catalog",
}
filters := cmd.Flags().StringArray("filter", nil, "select only a subset of all entities")
Expand Down Expand Up @@ -142,6 +152,35 @@ func newListEntitiesCommand() *cobra.Command {
return cmd
}

func newEntitiesGetByNameCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "get-by-name",
Short: "Get an entity by its kind, namespace and name",
}
kind := cmd.Flags().String("kind", "", "kind of the entity to get")
_ = cmd.MarkFlagRequired("kind")
namespace := cmd.Flags().String("namespace", "default", "namespace of the entity to get")
name := cmd.Flags().String("name", "", "name of the entity to get")
_ = cmd.MarkFlagRequired("name")
cmd.RunE = func(cmd *cobra.Command, args []string) error {
client, err := newCatalogClient()
if err != nil {
return err
}
entity, err := client.GetEntityByName(cmd.Context(), &catalog.GetEntityByNameRequest{
Kind: *kind,
Namespace: *namespace,
Name: *name,
})
if err != nil {
return err
}
printRawJSON(cmd, entity.Raw)
return nil
}
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 35a83c6

Please sign in to comment.