Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support fetching templates by name #349

Merged
merged 5 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree with this, but should just be squashed in the commit that does it :D

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