Skip to content

Commit

Permalink
dao, airflow, api: rest: Implement workflow spec version
Browse files Browse the repository at this point in the history
  • Loading branch information
ish-hcc committed Nov 12, 2024
1 parent f0b8093 commit a374505
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 65 deletions.
5 changes: 3 additions & 2 deletions dao/workflowTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ func WorkflowTemplateGet(id string) (*model.GetWorkflowTemplate, error) {
}

return &model.GetWorkflowTemplate{
Name: workflowTemplate.Name,
Data: workflowTemplate.Data,
SpecVersion: workflowTemplate.SpecVersion,
Name: workflowTemplate.Name,
Data: workflowTemplate.Data,
}, nil
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"spec_version": "1.0",
"name": "migrate_infra_workflow",
"data": {
"description": "Migrate Server",
Expand Down
112 changes: 74 additions & 38 deletions pkg/api/rest/controller/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"reflect"
"strconv"
"strings"
"time"

"github.com/cloud-barista/cm-cicada/dao"
Expand Down Expand Up @@ -42,54 +43,78 @@ func toTimeHookFunc() mapstructure.DecodeHookFunc {
}
}

func createDataReqToData(createDataReq model.CreateDataReq) (model.Data, error) {
var taskGroups []model.TaskGroup
var allTasks []model.Task
func createDataReqToData(specVersion string, createDataReq model.CreateDataReq) (model.Data, error) {
specVersionSpilit := strings.Split(specVersion, ".")
if len(specVersionSpilit) != 2 {
return model.Data{}, errors.New("invalid workflow spec version: " + specVersion)
}

for _, tgReq := range createDataReq.TaskGroups {
var tasks []model.Task
for _, tReq := range tgReq.Tasks {
tasks = append(tasks, model.Task{
ID: uuid.New().String(),
Name: tReq.Name,
TaskComponent: tReq.TaskComponent,
RequestBody: tReq.RequestBody,
PathParams: tReq.PathParams,
Dependencies: tReq.Dependencies,
})
}
specVersionMajor, err := strconv.Atoi(specVersionSpilit[0])
if err != nil {
return model.Data{}, errors.New("invalid workflow spec version: " + specVersion)
}

allTasks = append(allTasks, tasks...)
taskGroups = append(taskGroups, model.TaskGroup{
ID: uuid.New().String(),
Name: tgReq.Name,
Description: tgReq.Description,
Tasks: tasks,
})
specVersionMinor, err := strconv.Atoi(specVersionSpilit[1])
if err != nil {
return model.Data{}, errors.New("invalid workflow spec version: " + specVersion)
}

for i, tgReq := range createDataReq.TaskGroups {
for j, tg := range taskGroups {
if tgReq.Name == tg.Name {
if i == j {
continue
var taskGroups []model.TaskGroup
var allTasks []model.Task

if specVersionMajor > 0 && specVersionMajor <= 1 {
if specVersionMinor == 0 {
// v1.0
for _, tgReq := range createDataReq.TaskGroups {
var tasks []model.Task
for _, tReq := range tgReq.Tasks {
tasks = append(tasks, model.Task{
ID: uuid.New().String(),
Name: tReq.Name,
TaskComponent: tReq.TaskComponent,
RequestBody: tReq.RequestBody,
PathParams: tReq.PathParams,
Dependencies: tReq.Dependencies,
})
}

return model.Data{}, errors.New("Duplicated task group name: " + tg.Name)
allTasks = append(allTasks, tasks...)
taskGroups = append(taskGroups, model.TaskGroup{
ID: uuid.New().String(),
Name: tgReq.Name,
Description: tgReq.Description,
Tasks: tasks,
})
}
}
}

for i, tCheck := range allTasks {
for j, t := range allTasks {
if tCheck.Name == t.Name {
if i == j {
continue
for i, tgReq := range createDataReq.TaskGroups {
for j, tg := range taskGroups {
if tgReq.Name == tg.Name {
if i == j {
continue
}

return model.Data{}, errors.New("Duplicated task group name: " + tg.Name)
}
}
}

for i, tCheck := range allTasks {
for j, t := range allTasks {
if tCheck.Name == t.Name {
if i == j {
continue
}

return model.Data{}, errors.New("Duplicated task name: " + t.Name)
return model.Data{}, errors.New("Duplicated task name: " + t.Name)
}
}
}
} else {
return model.Data{}, errors.New("Unsupported workflow spec version: " + specVersion)
}
} else {
return model.Data{}, errors.New("Unsupported workflow spec version: " + specVersion)
}

return model.Data{
Expand Down Expand Up @@ -138,13 +163,19 @@ func CreateWorkflow(c echo.Context) error {
return common.ReturnErrorMsg(c, "Please provide the name.")
}

workflowData, err := createDataReqToData(createWorkflowReq.Data)
var specVersion = model.WorkflowSpecVersion_LATEST
if createWorkflowReq.SpecVersion != "" {
specVersion = createWorkflowReq.SpecVersion
}

workflowData, err := createDataReqToData(specVersion, createWorkflowReq.Data)
if err != nil {
return common.ReturnErrorMsg(c, err.Error())
}

var workflow model.Workflow
workflow.ID = uuid.New().String()
workflow.SpecVersion = specVersion
workflow.Name = createWorkflowReq.Name
workflow.Data = workflowData

Expand Down Expand Up @@ -418,7 +449,12 @@ func UpdateWorkflow(c echo.Context) error {
oldWorkflow.Name = updateWorkflowReq.Name
}

workflowData, err := createDataReqToData(updateWorkflowReq.Data)
var specVersion = model.WorkflowSpecVersion_LATEST
if updateWorkflowReq.SpecVersion != "" {
specVersion = updateWorkflowReq.SpecVersion
}

workflowData, err := createDataReqToData(specVersion, updateWorkflowReq.Data)
if err != nil {
return common.ReturnErrorMsg(c, err.Error())
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/api/rest/controller/workflowTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ func GetWorkflowTemplateByName(c echo.Context) error {
return common.ReturnErrorMsg(c, "workflow template not found with the provided name")
}
return c.JSONPretty(http.StatusOK, model.GetWorkflowTemplate{
Name: workflowTemplate.Name,
Data: workflowTemplate.Data,
SpecVersion: workflowTemplate.SpecVersion,
Name: workflowTemplate.Name,
Data: workflowTemplate.Data,
}, "")
}

Expand Down
25 changes: 22 additions & 3 deletions pkg/api/rest/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1805,6 +1805,9 @@ const docTemplate = `{
},
"name": {
"type": "string"
},
"spec_version": {
"type": "string"
}
}
},
Expand Down Expand Up @@ -1858,14 +1861,18 @@ const docTemplate = `{
"type": "object",
"required": [
"data",
"name"
"name",
"spec_version"
],
"properties": {
"data": {
"$ref": "#/definitions/github_com_cloud-barista_cm-cicada_pkg_api_rest_model.CreateDataReq"
},
"name": {
"type": "string"
},
"spec_version": {
"type": "string"
}
}
},
Expand Down Expand Up @@ -2173,7 +2180,8 @@ const docTemplate = `{
"required": [
"data",
"id",
"name"
"name",
"spec_version"
],
"properties": {
"created_at": {
Expand All @@ -2188,6 +2196,9 @@ const docTemplate = `{
"name": {
"type": "string"
},
"spec_version": {
"type": "string"
},
"updated_at": {
"type": "string"
}
Expand Down Expand Up @@ -2249,7 +2260,8 @@ const docTemplate = `{
"required": [
"data",
"id",
"name"
"name",
"spec_version"
],
"properties": {
"data": {
Expand All @@ -2260,6 +2272,9 @@ const docTemplate = `{
},
"name": {
"type": "string"
},
"spec_version": {
"type": "string"
}
}
},
Expand All @@ -2268,6 +2283,7 @@ const docTemplate = `{
"required": [
"action",
"id",
"spec_version",
"workflowId"
],
"properties": {
Expand All @@ -2283,6 +2299,9 @@ const docTemplate = `{
"id": {
"type": "string"
},
"spec_version": {
"type": "string"
},
"workflowId": {
"type": "string"
}
Expand Down
25 changes: 22 additions & 3 deletions pkg/api/rest/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1798,6 +1798,9 @@
},
"name": {
"type": "string"
},
"spec_version": {
"type": "string"
}
}
},
Expand Down Expand Up @@ -1851,14 +1854,18 @@
"type": "object",
"required": [
"data",
"name"
"name",
"spec_version"
],
"properties": {
"data": {
"$ref": "#/definitions/github_com_cloud-barista_cm-cicada_pkg_api_rest_model.CreateDataReq"
},
"name": {
"type": "string"
},
"spec_version": {
"type": "string"
}
}
},
Expand Down Expand Up @@ -2166,7 +2173,8 @@
"required": [
"data",
"id",
"name"
"name",
"spec_version"
],
"properties": {
"created_at": {
Expand All @@ -2181,6 +2189,9 @@
"name": {
"type": "string"
},
"spec_version": {
"type": "string"
},
"updated_at": {
"type": "string"
}
Expand Down Expand Up @@ -2242,7 +2253,8 @@
"required": [
"data",
"id",
"name"
"name",
"spec_version"
],
"properties": {
"data": {
Expand All @@ -2253,6 +2265,9 @@
},
"name": {
"type": "string"
},
"spec_version": {
"type": "string"
}
}
},
Expand All @@ -2261,6 +2276,7 @@
"required": [
"action",
"id",
"spec_version",
"workflowId"
],
"properties": {
Expand All @@ -2276,6 +2292,9 @@
"id": {
"type": "string"
},
"spec_version": {
"type": "string"
},
"workflowId": {
"type": "string"
}
Expand Down
Loading

0 comments on commit a374505

Please sign in to comment.