Skip to content

Commit

Permalink
Fix tinkerbell#363: Creation of template with the same name
Browse files Browse the repository at this point in the history
Signed-off-by: parauliya <[email protected]>
  • Loading branch information
parauliya committed Dec 4, 2020
1 parent 86057d3 commit 5c4f9aa
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
1 change: 1 addition & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,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) (string, string, string, error)
GetTemplateForWorkflow(ctx context.Context, fields map[string]string) (string, string, string, 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
30 changes: 29 additions & 1 deletion db/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (d TinkDB) CreateTemplate(ctx context.Context, name string, data string, id
return nil
}

// GetTemplate returns a workflow template
// GetTemplate returns template which is not deleted
func (d TinkDB) GetTemplate(ctx context.Context, fields map[string]string) (string, string, string, error) {
getCondition, err := buildGetCondition(fields)
if err != nil {
Expand Down Expand Up @@ -74,6 +74,34 @@ func (d TinkDB) GetTemplate(ctx context.Context, fields map[string]string) (stri
return "", "", "", err
}

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

query := `
SELECT id, name, data
FROM template
WHERE
` + getCondition + `
`
row := d.instance.QueryRowContext(ctx, query)
id := []byte{}
name := []byte{}
data := []byte{}
err = row.Scan(&id, &name, &data)
if err == nil {
return string(id), string(name), string(data), nil
}
if err != sql.ErrNoRows {
err = errors.Wrap(err, "SELECT")
logger.Error(err)
}
return "", "", "", err
}

// DeleteTemplate deletes a workflow template
func (d TinkDB) DeleteTemplate(ctx context.Context, name string) error {
tx, err := d.instance.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelSerializable})
Expand Down
32 changes: 18 additions & 14 deletions grpc-server/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,15 @@ func (s *server) CreateWorkflow(ctx context.Context, in *workflow.CreateRequest)
defer timer.ObserveDuration()

logger.Info(msg)
fields := map[string]string{
"id": in.GetTemplate(),
}
_, _, templateData, err := s.db.GetTemplate(ctx, fields)
if err != nil {
return &workflow.CreateResponse{}, errors.Wrapf(err, errFailedToGetTemplate, in.GetTemplate())
}
data, err := renderTemplate(in.GetTemplate(), templateData, []byte(in.Hardware))

data, err := createYaml(ctx, s.db, in.Template, in.Hardware)
if err != nil {
metrics.CacheErrors.With(labels).Inc()
logger.Error(err)
Expand Down Expand Up @@ -102,7 +109,15 @@ func (s *server) GetWorkflow(ctx context.Context, in *workflow.GetRequest) (*wor
}
l.Error(err)
}
yamlData, err := createYaml(ctx, s.db, w.Template, w.Hardware)

fields := map[string]string{
"id": w.Template,
}
_, _, templateData, err := s.db.GetTemplateForWorkflow(ctx, fields)
if err != nil {
return &workflow.Workflow{}, errors.Wrapf(err, errFailedToGetTemplate, w.Template)
}
data, err := renderTemplate(w.Template, templateData, []byte(w.Hardware))
if err != nil {
return &workflow.Workflow{}, err
}
Expand All @@ -111,7 +126,7 @@ func (s *server) GetWorkflow(ctx context.Context, in *workflow.GetRequest) (*wor
Template: w.Template,
Hardware: w.Hardware,
State: state[w.State],
Data: yamlData,
Data: data,
}
l := logger.With("workflowID", w.ID)
l.Info("done " + msg)
Expand Down Expand Up @@ -270,17 +285,6 @@ func (s *server) ShowWorkflowEvents(req *workflow.GetRequest, stream workflow.Wo
return nil
}

func createYaml(ctx context.Context, db db.Database, templateID string, devices string) (string, error) {
fields := map[string]string{
"id": templateID,
}
_, _, templateData, err := db.GetTemplate(ctx, fields)
if err != nil {
return "", errors.Wrapf(err, errFailedToGetTemplate, templateID)
}
return renderTemplate(templateID, templateData, []byte(devices))
}

func renderTemplate(templateID, templateData string, devices []byte) (string, error) {
var hardware map[string]interface{}
err := json.Unmarshal(devices, &hardware)
Expand Down

0 comments on commit 5c4f9aa

Please sign in to comment.