Skip to content

Commit

Permalink
add filtering to ListTemplates
Browse files Browse the repository at this point in the history
Signed-off-by: Kelly Deng <kelly@packet.com>
  • Loading branch information
kqdeng committed Nov 4, 2020
1 parent 322775a commit c186f90
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 119 deletions.
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
2 changes: 1 addition & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type template interface {
CreateTemplate(ctx context.Context, name string, data string, id uuid.UUID) error
GetTemplate(ctx context.Context, id 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
6 changes: 4 additions & 2 deletions db/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,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
49 changes: 9 additions & 40 deletions go.sum

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions grpc-server/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package grpcserver

import (
"context"
"strings"

"github.com/golang/protobuf/ptypes/timestamp"
"github.com/google/uuid"
Expand Down Expand Up @@ -99,13 +100,18 @@ func (s *server) DeleteTemplate(ctx context.Context, in *template.GetRequest) (*
}

// ListTemplates implements template.ListTemplates
func (s *server) ListTemplates(_ *template.Empty, stream template.TemplateService_ListTemplatesServer) error {
func (s *server) ListTemplates(in *template.ListRequest, stream template.TemplateService_ListTemplatesServer) error {
logger.Info("listtemplates")
labels := prometheus.Labels{"method": "ListTemplates", "op": "list"}
metrics.CacheTotals.With(labels).Inc()
metrics.CacheInFlight.With(labels).Inc()
defer metrics.CacheInFlight.With(labels).Dec()

filter := "%" // default filter will match everything
if in.GetName() != "" {
filter = strings.ReplaceAll(in.GetName(), "*", "%") // replace '*' with psql '%' wildcard
}

s.dbLock.RLock()
ready := s.dbReady
s.dbLock.RUnlock()
Expand All @@ -116,7 +122,7 @@ func (s *server) ListTemplates(_ *template.Empty, stream template.TemplateServic

timer := prometheus.NewTimer(metrics.CacheDuration.With(labels))
defer timer.ObserveDuration()
err := s.db.ListTemplates(func(id, n string, crTime, upTime *timestamp.Timestamp) error {
err := s.db.ListTemplates(filter, func(id, n string, crTime, upTime *timestamp.Timestamp) error {
return stream.Send(&template.WorkflowTemplate{Id: id, Name: n, CreatedAt: crTime, UpdatedAt: upTime})
})

Expand Down
13 changes: 11 additions & 2 deletions http-server/http_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,19 @@ func RegisterTemplateHandlerFromEndpoint(ctx context.Context, mux *runtime.Serve
writeResponse(w, http.StatusOK, fmt.Sprintf(`{"status": "ok", "msg": "template deleted successfully", "id": "%v"}`, gr.Id))
})

// template list handler | GET /v1/templates
// template list handler | GET /v1/templates?name=
templateListPattern := runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1}, []string{"v1", "templates"}, "", runtime.AssumeColonVerbOpt(true)))
mux.Handle("GET", templateListPattern, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
list, err := client.ListTemplates(context.Background(), &template.Empty{})
nameFilter := "*" // default filter will match everything
if query := req.URL.Query()["name"]; len(query) > 0 {
nameFilter = query[0]
}

list, err := client.ListTemplates(context.Background(), &template.ListRequest{
FilterBy: &template.ListRequest_Name{
Name: nameFilter,
},
})
if err != nil {
logger.Error(err)
writeResponse(w, http.StatusInternalServerError, err.Error())
Expand Down
18 changes: 18 additions & 0 deletions protos/hardware/hardware.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c186f90

Please sign in to comment.