Skip to content

Commit

Permalink
Add functionality for users to retrieve template by Name as well as UUID
Browse files Browse the repository at this point in the history
Signed-off-by: William Belcher <william.belcher@hotmail.com>
Co-Authored-by: Manuel Mendez <mmendez@equinix.com>
Signed-off-by: Manuel Mendez <mmendez@equinix.com>
  • Loading branch information
Belchy06 and mmlb committed Dec 14, 2021
1 parent fcd4faf commit 3a9bb2a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
42 changes: 32 additions & 10 deletions cmd/tink-cli/cmd/get/get.go
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"

"github.com/google/uuid"
"github.com/jedib0t/go-pretty/table"
"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand All @@ -19,6 +20,8 @@ type Options struct {
RetrieveData func(context.Context, *client.FullClient) ([]interface{}, error)
// RetrieveByID is used when a get command has a list of arguments
RetrieveByID func(context.Context, *client.FullClient, string) (interface{}, error)
// RetrieveByName is used when a get command has a list of arguments
RetrieveByName func(context.Context, *client.FullClient, string) (interface{}, error)
// PopulateTable populates a table with the data retrieved with the RetrieveData function.
PopulateTable func([]interface{}, table.Writer) error

Expand Down Expand Up @@ -95,16 +98,7 @@ func NewGetCommand(opt Options) *cobra.Command {
t.SetOutputMirror(cmd.OutOrStdout())

if len(args) != 0 {
if opt.RetrieveByID == nil {
return errors.New("option RetrieveByID is not implemented for this resource yet. Please have a look at the issue in GitHub or open a new one")
}
for _, requestedID := range args {
s, err := opt.RetrieveByID(cmd.Context(), opt.fullClient, requestedID)
if err != nil {
continue
}
data = append(data, s)
}
data, err = retrieveMulti(opt, cmd, args)
} else {
data, err = opt.RetrieveData(cmd.Context(), opt.fullClient)
}
Expand Down Expand Up @@ -154,3 +148,31 @@ func NewGetCommand(opt Options) *cobra.Command {
opt.clientConnOpt.SetFlags(cmd.PersistentFlags())
return cmd
}

func retrieveMulti(opt Options, cmd *cobra.Command, args []string) ([]interface{}, error) {
var data []interface{}
for _, arg := range args {
var retriever func(context.Context, *client.FullClient, string) (interface{}, error)
if _, err := uuid.Parse(arg); err != nil {
// arg is invalid UUID, search for arg in `name` field of db
if opt.RetrieveByName == nil {
return nil, errors.New("get by Name is not implemented for this resource yet, please have a look at the issue in GitHub or open a new one")
}
retriever = opt.RetrieveByName
} else {
// arg is a valid UUID, search for arg in `id` field of db
if opt.RetrieveByID == nil {
return nil, errors.New("get by ID is not implemented for this resource yet, please have a look at the issue in GitHub or open a new one")
}
retriever = opt.RetrieveByID
}

s, err := retriever(cmd.Context(), opt.fullClient, arg)
if err != nil {
continue
}
data = append(data, s)
}

return data, nil
}
34 changes: 21 additions & 13 deletions cmd/tink-cli/cmd/template/get.go
Expand Up @@ -26,19 +26,18 @@ var GetCmd = &cobra.Command{
if len(args) == 0 {
return fmt.Errorf("%v requires an argument", c.UseLine())
}
for _, arg := range args {
if _, err := uuid.Parse(arg); err != nil {
return fmt.Errorf("invalid uuid: %s", arg)
}
}
return nil
},
Run: func(c *cobra.Command, args []string) {
for _, arg := range args {
req := template.GetRequest{
GetBy: &template.GetRequest_Id{
Id: arg,
},
req := template.GetRequest{}
// Parse arg[0] to see if it is a UUID
if _, err := uuid.Parse(arg); err == nil {
// UUID
req.GetBy = &template.GetRequest_Id{Id: arg}
} else {
// String (Name)
req.GetBy = &template.GetRequest_Name{Name: arg}
}
t, err := client.TemplateClient.GetTemplate(context.Background(), &req)
if err != nil {
Expand All @@ -61,6 +60,14 @@ func (h *getTemplate) RetrieveByID(ctx context.Context, cl *client.FullClient, r
})
}

func (h *getTemplate) RetrieveByName(_ context.Context, cl *client.FullClient, requestName string) (interface{}, error) {
return cl.TemplateClient.GetTemplate(context.Background(), &template.GetRequest{
GetBy: &template.GetRequest_Name{
Name: requestName,
},
})
}

func (h *getTemplate) RetrieveData(ctx context.Context, cl *client.FullClient) ([]interface{}, error) {
list, err := cl.TemplateClient.ListTemplates(ctx, &template.ListRequest{
FilterBy: &template.ListRequest_Name{
Expand Down Expand Up @@ -98,9 +105,10 @@ func (h *getTemplate) PopulateTable(data []interface{}, t table.Writer) error {
func NewGetOptions() get.Options {
h := getTemplate{}
return get.Options{
Headers: []string{"ID", "Name", "Created At", "Updated At"},
RetrieveByID: h.RetrieveByID,
RetrieveData: h.RetrieveData,
PopulateTable: h.PopulateTable,
Headers: []string{"ID", "Name", "Created At", "Updated At"},
RetrieveByID: h.RetrieveByID,
RetrieveByName: h.RetrieveByName,
RetrieveData: h.RetrieveData,
PopulateTable: h.PopulateTable,
}
}

0 comments on commit 3a9bb2a

Please sign in to comment.