Skip to content

Commit

Permalink
Fix_475: Correct timestamp in 'tink template get <id>' command
Browse files Browse the repository at this point in the history
Signed-off-by: parauliya <[email protected]>
  • Loading branch information
parauliya committed Apr 8, 2021
1 parent 838453e commit 03c8f6a
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 67 deletions.
3 changes: 2 additions & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
ev "github.com/tinkerbell/tink/db/events"
"github.com/tinkerbell/tink/db/migration"
"github.com/tinkerbell/tink/protos/events"
tb "github.com/tinkerbell/tink/protos/template"
pb "github.com/tinkerbell/tink/protos/workflow"
)

Expand All @@ -38,7 +39,7 @@ type hardware interface {

type template interface {
CreateTemplate(ctx context.Context, name string, data string, id uuid.UUID) error
GetTemplate(ctx context.Context, fields map[string]string, deleted bool) (string, string, string, error)
GetTemplate(ctx context.Context, fields map[string]string, deleted bool) (*tb.WorkflowTemplate, error)
DeleteTemplate(ctx context.Context, name string) 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
3 changes: 2 additions & 1 deletion db/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/google/uuid"
"github.com/tinkerbell/tink/db"
tb "github.com/tinkerbell/tink/protos/template"
pb "github.com/tinkerbell/tink/protos/workflow"
)

Expand All @@ -25,5 +26,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, fields map[string]string, deleted bool) (string, string, string, error)
GetTemplateFunc func(ctx context.Context, fields map[string]string, deleted bool) (*tb.WorkflowTemplate, error)
}
3 changes: 2 additions & 1 deletion db/mock/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/golang/protobuf/ptypes/timestamp"
"github.com/google/uuid"
tb "github.com/tinkerbell/tink/protos/template"
)

type Template struct {
Expand Down Expand Up @@ -41,7 +42,7 @@ 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, fields map[string]string, deleted bool) (string, string, string, error) {
func (d DB) GetTemplate(ctx context.Context, fields map[string]string, deleted bool) (*tb.WorkflowTemplate, error) {
return d.GetTemplateFunc(ctx, fields, deleted)
}

Expand Down
33 changes: 23 additions & 10 deletions db/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/google/uuid"
"github.com/pkg/errors"
tb "github.com/tinkerbell/tink/protos/template"
wflow "github.com/tinkerbell/tink/workflow"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -48,43 +49,55 @@ func (d TinkDB) CreateTemplate(ctx context.Context, name string, data string, id
}

// GetTemplate returns template which is not deleted
func (d TinkDB) GetTemplate(ctx context.Context, fields map[string]string, deleted bool) (string, string, string, error) {
func (d TinkDB) GetTemplate(ctx context.Context, fields map[string]string, deleted bool) (*tb.WorkflowTemplate, error) {
getCondition, err := buildGetCondition(fields)
if err != nil {
return "", "", "", errors.Wrap(err, "failed to get template")
return &tb.WorkflowTemplate{}, errors.Wrap(err, "failed to get template")
}

var query string
if !deleted {
query = `
SELECT id, name, data
SELECT id, name, data, created_at, updated_at
FROM template
WHERE
` + getCondition + ` AND
deleted_at IS NULL
`
} else {
query = `
SELECT id, name, data
SELECT id, name, data, created_at, updated_at
FROM template
WHERE
` + getCondition + `
`
}

row := d.instance.QueryRowContext(ctx, query)
id := []byte{}
name := []byte{}
data := []byte{}
err = row.Scan(&id, &name, &data)
var (
id string
name string
data string
createdAt time.Time
updatedAt time.Time
)
err = row.Scan(&id, &name, &data, &createdAt, &updatedAt)
if err == nil {
return string(id), string(name), string(data), nil
crAt, _ := ptypes.TimestampProto(createdAt)
upAt, _ := ptypes.TimestampProto(updatedAt)
return &tb.WorkflowTemplate{
Id: id,
Name: name,
Data: data,
CreatedAt: crAt,
UpdatedAt: upAt,
}, nil
}
if err != sql.ErrNoRows {
err = errors.Wrap(err, "SELECT")
d.logger.Error(err)
}
return "", "", "", err
return &tb.WorkflowTemplate{}, err
}

// DeleteTemplate deletes a workflow template by id
Expand Down
24 changes: 12 additions & 12 deletions db/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ func TestCreateTemplate(t *testing.T) {
}(),
},
Expectation: func(t *testing.T, input []*workflow.Workflow, tinkDB *db.TinkDB) {
wID, wName, wData, err := tinkDB.GetTemplate(ctx, map[string]string{"id": input[0].ID}, false)
wtmpl, err := tinkDB.GetTemplate(ctx, map[string]string{"id": input[0].ID}, false)
if err != nil {
t.Error(err)
}
w := workflow.MustParse([]byte(wData))
w.ID = wID
w.Name = wName
w := workflow.MustParse([]byte(wtmpl.GetData()))
w.ID = wtmpl.GetId()
w.Name = wtmpl.GetName()
if dif := cmp.Diff(input[0], w); dif != "" {
t.Errorf(dif)
}
Expand Down Expand Up @@ -96,12 +96,12 @@ func TestCreateTemplate(t *testing.T) {
}(),
},
Expectation: func(t *testing.T, input []*workflow.Workflow, tinkDB *db.TinkDB) {
_, wName, _, err := tinkDB.GetTemplate(context.Background(), map[string]string{"id": input[0].ID}, false)
wtmpl, err := tinkDB.GetTemplate(context.Background(), map[string]string{"id": input[0].ID}, false)
if err != nil {
t.Error(err)
}
if wName != "updated-name" {
t.Errorf("expected name to be \"%s\", got \"%s\"", "updated-name", wName)
if wtmpl.GetName() != "updated-name" {
t.Errorf("expected name to be \"%s\", got \"%s\"", "updated-name", wtmpl.GetName())
}
},
},
Expand Down Expand Up @@ -251,13 +251,13 @@ func TestDeleteTemplate(t *testing.T) {
func TestGetTemplate(t *testing.T) {
ctx := context.Background()
expectation := func(t *testing.T, input *workflow.Workflow, tinkDB *db.TinkDB) {
wID, wName, wData, err := tinkDB.GetTemplate(ctx, map[string]string{"id": input.ID}, false)
wtmpl, err := tinkDB.GetTemplate(ctx, map[string]string{"id": input.ID}, false)
if err != nil {
t.Error(err)
}
w := workflow.MustParse([]byte(wData))
w.ID = wID
w.Name = wName
w := workflow.MustParse([]byte(wtmpl.GetData()))
w.ID = wtmpl.GetId()
w.Name = wtmpl.GetName()
if dif := cmp.Diff(input, w); dif != "" {
t.Errorf(dif)
}
Expand Down Expand Up @@ -353,7 +353,7 @@ func TestGetTemplateWithInvalidID(t *testing.T) {
}()

id := uuid.New().String()
_, _, _, err := tinkDB.GetTemplate(ctx, map[string]string{"id": id}, false)
_, err := tinkDB.GetTemplate(ctx, map[string]string{"id": id}, false)
if err == nil {
t.Error("expected error, got nil")
}
Expand Down
4 changes: 2 additions & 2 deletions db/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,12 @@ func TestGetWorkflow(t *testing.T) {
}

func createWorkflow(ctx context.Context, tinkDB *db.TinkDB, in *input) (string, error) {
_, _, tmpData, err := tinkDB.GetTemplate(context.Background(), map[string]string{"id": in.template.ID}, false)
wtmpl, err := tinkDB.GetTemplate(context.Background(), map[string]string{"id": in.template.ID}, false)
if err != nil {
return "", err
}

data, err := workflow.RenderTemplate(in.template.ID, tmpData, []byte(in.devices))
data, err := workflow.RenderTemplate(in.template.ID, wtmpl.GetData(), []byte(in.devices))
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions grpc-server/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s *server) GetTemplate(ctx context.Context, in *template.GetRequest) (*tem
"id": in.GetId(),
"name": in.GetName(),
}
id, n, d, err := s.db.GetTemplate(ctx, fields, false)
wtmpl, err := s.db.GetTemplate(ctx, fields, false)
s.logger.Info("done " + msg)
if err != nil {
metrics.CacheErrors.With(labels).Inc()
Expand All @@ -72,7 +72,7 @@ func (s *server) GetTemplate(ctx context.Context, in *template.GetRequest) (*tem
}
l.Error(err)
}
return &template.WorkflowTemplate{Id: id, Name: n, Data: d}, err
return wtmpl, err
}

// DeleteTemplate implements template.DeleteTemplate
Expand Down
Loading

0 comments on commit 03c8f6a

Please sign in to comment.