Skip to content

Commit

Permalink
Support fetching templates by name (#349)
Browse files Browse the repository at this point in the history
## Description

<!--- Please describe what this PR is going to change -->
Adds the ability to query templates by name.   
The `ListTemplates` method has been changed to accept a `FilterRequest` instead of just `Empty`.   
The `FilterRequest` has a field `filter` that accepts a string used to match the names of templates. A wildcard `*` can be used to match a sequence of zero or more characters.  
Passing an empty `FilterRequest` will return all templates.

## Why is this needed

<!--- Link to issue you have raised -->
A user might have the following templates: `hello-world`, `ubuntu-01`, `ubuntu-02`, `ubuntu-03`, `goodbye-world` and wants to list only the linux templates. Before, they would only be able to either list all of the templates, or only one (and only if they already know the id).  
With this PR, they can just call `ListTemplates` with the `FilterRequest`'s filter field to `ubuntu-*` and it will return only `ubuntu-01`, `ubuntu-02`, and `ubuntu-03`.

Fixes: #352 / ENG-9455

## How Has This Been Tested?
<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran to -->
<!--- see how your change affects other areas of the code, etc. -->

* manually
* TBD

## How are existing users impacted? What migration steps/scripts do we need?

<!--- Fixes a bug, unblocks installation, removes a component of the stack etc -->
<!--- Requires a DB migration script, etc. -->
Users using an older version of the tink-cli may experience errors. Best solution is to update the tink-server, tink-cli, and tink-worker to use the images built from this PR

## Checklist:

I have:

- [ ] updated the documentation and/or roadmap (if required)
- [ ] added unit or e2e tests
- [ ] provided instructions on how to upgrade
  • Loading branch information
mergify[bot] authored Nov 4, 2020
2 parents 322775a + 775cdf7 commit a94738e
Show file tree
Hide file tree
Showing 19 changed files with 619 additions and 163 deletions.
6 changes: 5 additions & 1 deletion cmd/tink-cli/cmd/template/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ var deleteCmd = &cobra.Command{
},
Run: func(c *cobra.Command, args []string) {
for _, arg := range args {
req := template.GetRequest{Id: arg}
req := template.GetRequest{
GetBy: &template.GetRequest_Id{
Id: arg,
},
}
if _, err := client.TemplateClient.DeleteTemplate(context.Background(), &req); err != nil {
log.Fatal(err)
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/tink-cli/cmd/template/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ var getCmd = &cobra.Command{
},
Run: func(c *cobra.Command, args []string) {
for _, arg := range args {
req := template.GetRequest{Id: arg}
req := template.GetRequest{
GetBy: &template.GetRequest_Id{
Id: arg,
},
}
t, err := client.TemplateClient.GetTemplate(context.Background(), &req)
if err != nil {
log.Fatal(err)
Expand Down
6 changes: 5 additions & 1 deletion cmd/tink-cli/cmd/template/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ var listCmd = &cobra.Command{
}

func listTemplates(cmd *cobra.Command, t table.Writer) {
list, err := client.TemplateClient.ListTemplates(context.Background(), &template.Empty{})
list, err := client.TemplateClient.ListTemplates(context.Background(), &template.ListRequest{
FilterBy: &template.ListRequest_Name{
Name: "*",
},
})
if err != nil {
log.Fatal(err)
}
Expand Down
16 changes: 14 additions & 2 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db
import (
"context"
"database/sql"
"fmt"
"time"

"github.com/golang/protobuf/ptypes/timestamp"
Expand Down Expand Up @@ -35,9 +36,9 @@ type hardware interface {

type template interface {
CreateTemplate(ctx context.Context, name string, data string, id uuid.UUID) error
GetTemplate(ctx context.Context, id string) (string, string, error)
GetTemplate(ctx context.Context, fields map[string]string) (string, string, error)
DeleteTemplate(ctx context.Context, name string) error
ListTemplates(fn func(id, n string, in, del *timestamp.Timestamp) error) error
ListTemplates(in string, fn func(id, n string, in, del *timestamp.Timestamp) error) error
UpdateTemplate(ctx context.Context, name string, data string, id uuid.UUID) error
}

Expand Down Expand Up @@ -114,3 +115,14 @@ func get(ctx context.Context, db *sql.DB, query string, args ...interface{}) (st

return "", err
}

// buildGetCondition builds a where condition string in the format "column_name = 'field_value' AND"
// takes in a map[string]string with keys being the column name and the values being the field values
func buildGetCondition(fields map[string]string) (string, error) {
for column, field := range fields {
if field != "" {
return fmt.Sprintf("%s = '%s' AND", column, field), nil
}
}
return "", errors.New("one GetBy field must be set to build a get condition")
}
2 changes: 1 addition & 1 deletion db/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ type DB struct {
InsertIntoWorkflowEventTableFunc func(ctx context.Context, wfEvent *pb.WorkflowActionStatus, time time.Time) error
// template
TemplateDB map[string]interface{}
GetTemplateFunc func(ctx context.Context, id string) (string, string, error)
GetTemplateFunc func(ctx context.Context, fields map[string]string) (string, string, error)
}
6 changes: 3 additions & 3 deletions db/mock/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func (d DB) CreateTemplate(ctx context.Context, name string, data string, id uui
}

// GetTemplate returns a workflow template
func (d DB) GetTemplate(ctx context.Context, id string) (string, string, error) {
return d.GetTemplateFunc(ctx, id)
func (d DB) GetTemplate(ctx context.Context, fields map[string]string) (string, string, error) {
return d.GetTemplateFunc(ctx, fields)
}

// DeleteTemplate deletes a workflow template
Expand All @@ -46,7 +46,7 @@ func (d DB) DeleteTemplate(ctx context.Context, name string) error {
}

// ListTemplates returns all saved templates
func (d DB) ListTemplates(fn func(id, n string, in, del *timestamp.Timestamp) error) error {
func (d DB) ListTemplates(in string, fn func(id, n string, in, del *timestamp.Timestamp) error) error {
return nil
}

Expand Down
20 changes: 13 additions & 7 deletions db/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,23 @@ func (d TinkDB) CreateTemplate(ctx context.Context, name string, data string, id
}

// GetTemplate returns a workflow template
func (d TinkDB) GetTemplate(ctx context.Context, id string) (string, string, error) {
func (d TinkDB) GetTemplate(ctx context.Context, fields map[string]string) (string, string, error) {
getCondition, err := buildGetCondition(fields)
if err != nil {
return "", "", errors.Wrap(err, "failed to build get condition")
}

query := `
SELECT name, data
FROM template
WHERE
id = $1
AND
` + getCondition + `
deleted_at IS NULL
`
row := d.instance.QueryRowContext(ctx, query, id)
row := d.instance.QueryRowContext(ctx, query)
name := []byte{}
data := []byte{}
err := row.Scan(&name, &data)
err = row.Scan(&name, &data)
if err == nil {
return string(name), string(data), nil
}
Expand Down Expand Up @@ -95,13 +99,15 @@ func (d TinkDB) DeleteTemplate(ctx context.Context, name string) error {
}

// ListTemplates returns all saved templates
func (d TinkDB) ListTemplates(fn func(id, n string, in, del *timestamp.Timestamp) error) error {
func (d TinkDB) ListTemplates(filter string, fn func(id, n string, in, del *timestamp.Timestamp) error) error {
rows, err := d.instance.Query(`
SELECT id, name, created_at, updated_at
FROM template
WHERE
name ILIKE $1
AND
deleted_at IS NULL;
`)
`, filter)

if err != nil {
return err
Expand Down
17 changes: 5 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/google/go-cmp v0.5.2 // indirect
github.com/google/uuid v1.1.2
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/grpc-ecosystem/grpc-gateway v1.14.6
github.com/grpc-ecosystem/grpc-gateway v1.15.2
github.com/jedib0t/go-pretty v4.3.0+incompatible
github.com/lib/pq v1.2.1-0.20191011153232-f91d3411e481
github.com/mattn/go-runewidth v0.0.5 // indirect
Expand All @@ -33,19 +33,12 @@ require (
github.com/stormcat24/protodep v0.0.0-20200505140716-b02c9ba62816
github.com/stretchr/testify v1.6.1
go.mongodb.org/mongo-driver v1.1.2 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.16.0 // indirect
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a // indirect
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/net v0.0.0-20200904194848-62affa334b73 // indirect
golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6 // indirect
golang.org/x/text v0.3.3 // indirect
golang.org/x/tools v0.0.0-20200921190806-0f52b63a40e8 // indirect
google.golang.org/genproto v0.0.0-20200921165018-b9da36f5f452
google.golang.org/grpc v1.29.1
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect
golang.org/x/sys v0.0.0-20200331124033-c3d80250170d // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
google.golang.org/grpc v1.32.0
google.golang.org/protobuf v1.25.0
gopkg.in/yaml.v2 v2.3.0
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
gotest.tools v2.2.0+incompatible // indirect
honnef.co/go/tools v0.0.1-2020.1.4 // indirect
)
Expand Down
Loading

0 comments on commit a94738e

Please sign in to comment.