Skip to content

Commit

Permalink
Merge pull request #476 from parauliya/Fix_475
Browse files Browse the repository at this point in the history
Fix_475: Correct timestamps in get by Id commands for template and workflow
  • Loading branch information
thebsdbox authored Apr 21, 2021
2 parents 5d58c4b + c5fbe14 commit 89f7a80
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 70 deletions.
3 changes: 2 additions & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/pkg/errors"
migrate "github.com/rubenv/sql-migrate"
"github.com/tinkerbell/tink/db/migration"
tb "github.com/tinkerbell/tink/protos/template"
pb "github.com/tinkerbell/tink/protos/workflow"
)

Expand All @@ -34,7 +35,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
19 changes: 15 additions & 4 deletions db/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,18 +337,29 @@ func (d TinkDB) GetWorkflowsForWorker(id string) ([]string, error) {
// GetWorkflow returns a workflow
func (d TinkDB) GetWorkflow(ctx context.Context, id string) (Workflow, error) {
query := `
SELECT template, devices
SELECT template, devices, created_at, updated_at
FROM workflow
WHERE
id = $1
AND
deleted_at IS NULL;
`
row := d.instance.QueryRowContext(ctx, query, id)
var tmp, tar string
err := row.Scan(&tmp, &tar)
var (
tmp, tar string
crAt, upAt time.Time
)
err := row.Scan(&tmp, &tar, &crAt, &upAt)
if err == nil {
return Workflow{ID: id, Template: tmp, Hardware: tar}, nil
createdAt, _ := ptypes.TimestampProto(crAt)
updatedAt, _ := ptypes.TimestampProto(upAt)
return Workflow{
ID: id,
Template: tmp,
Hardware: tar,
CreatedAt: createdAt,
UpdatedAt: updatedAt,
}, nil
}
if err != sql.ErrNoRows {
err = errors.Wrap(err, "SELECT")
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 89f7a80

Please sign in to comment.