Skip to content

Commit

Permalink
db, airflow: Check method type when registering task components
Browse files Browse the repository at this point in the history
  • Loading branch information
ish-hcc committed Nov 22, 2024
1 parent 28fbf45 commit 34e0d46
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 66 deletions.
129 changes: 72 additions & 57 deletions db/taskComponent.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type ConfigFile struct {
APIConnectionID string `json:"api_connection_id"`
SwaggerYAMLEndpoint string `json:"swagger_yaml_endpoint"`
Endpoint string `json:"endpoint"`
Method string `json:"method"`
Extra map[string]interface{} `json:"extra"`
}

Expand Down Expand Up @@ -167,7 +168,6 @@ func TaskComponentInit() error {
if configFile.Extra != nil {
taskComponent = &model.TaskComponent{}
taskComponent.Data.Options.Extra = configFile.Extra

} else {
var connectionFound bool
var connection model.Connection
Expand All @@ -189,7 +189,7 @@ func TaskComponentInit() error {
}

endpoint := strings.TrimPrefix(configFile.Endpoint, spec.BasePath)
taskComponent, err = processEndpoint(connection.ID, spec, endpoint)
taskComponent, err = processEndpoint(connection.ID, spec, endpoint, configFile.Method)
if err != nil {
logger.Println(logger.WARN, true, fmt.Sprintf("failed to process endpoint: %v", err))
continue
Expand Down Expand Up @@ -412,72 +412,87 @@ func generateRequestBodyExample(schema SchemaModel) string {
return string(jsonBytes)
}

func processEndpoint(connectionID string, spec *SwaggerSpec, targetEndpoint string) (*model.TaskComponent, error) {
func processEndpoint(connectionID string, spec *SwaggerSpec, targetEndpoint, targetMethod string) (*model.TaskComponent, error) {
targetEndpoint = normalizePath(targetEndpoint)
for path, pathItem := range spec.Paths {
if normalizePath(path) == targetEndpoint {
for method, operation := range pathItem {
taskComponent := &model.TaskComponent{
Data: model.TaskComponentData{
Options: model.TaskComponentOptions{
APIConnectionID: connectionID,
Endpoint: joinPaths(spec.BasePath, path),
Method: strings.ToUpper(method),
},
},
}
methodFoundCount := 0

pathParams := model.ParameterStructure{
Properties: make(map[string]model.PropertyDef),
}
queryParams := model.ParameterStructure{
Properties: make(map[string]model.PropertyDef),
var method string
var operation Operation

for method, operation = range pathItem {
methodFoundCount++

if targetMethod != "" && strings.EqualFold(targetMethod, method) {
break
}
}

for _, param := range operation.Parameters {
switch param.In {
case "path":
if param.Required {
pathParams.Required = append(pathParams.Required, param.Name)
}
pathParams.Properties[param.Name] = model.PropertyDef{
Type: param.Type,
Description: param.Description,
Default: param.Default,
Enum: param.Enum,
}
case "query":
if param.Required {
queryParams.Required = append(queryParams.Required, param.Name)
}
queryParams.Properties[param.Name] = model.PropertyDef{
Type: param.Type,
Description: param.Description,
Default: param.Default,
Enum: param.Enum,
}
case "body":
if param.Schema != nil && param.Schema.Ref != "" {
schema := resolveSchemaRef(param.Schema.Ref, spec.Definitions)
taskComponent.Data.BodyParams = convertSchemaToParams(schema)

requestBodyExample := generateRequestBodyExample(schema)
if requestBodyExample != "" {
taskComponent.Data.Options.RequestBody = requestBodyExample
}
if methodFoundCount > 1 {
return nil, fmt.Errorf("multiple methods found with the same endpoint: %s", targetEndpoint)
}

taskComponent := &model.TaskComponent{
Data: model.TaskComponentData{
Options: model.TaskComponentOptions{
APIConnectionID: connectionID,
Endpoint: joinPaths(spec.BasePath, path),
Method: strings.ToUpper(method),
},
},
}

pathParams := model.ParameterStructure{
Properties: make(map[string]model.PropertyDef),
}
queryParams := model.ParameterStructure{
Properties: make(map[string]model.PropertyDef),
}

for _, param := range operation.Parameters {
switch param.In {
case "path":
if param.Required {
pathParams.Required = append(pathParams.Required, param.Name)
}
pathParams.Properties[param.Name] = model.PropertyDef{
Type: param.Type,
Description: param.Description,
Default: param.Default,
Enum: param.Enum,
}
case "query":
if param.Required {
queryParams.Required = append(queryParams.Required, param.Name)
}
queryParams.Properties[param.Name] = model.PropertyDef{
Type: param.Type,
Description: param.Description,
Default: param.Default,
Enum: param.Enum,
}
case "body":
if param.Schema != nil && param.Schema.Ref != "" {
schema := resolveSchemaRef(param.Schema.Ref, spec.Definitions)
taskComponent.Data.BodyParams = convertSchemaToParams(schema)

requestBodyExample := generateRequestBodyExample(schema)
if requestBodyExample != "" {
taskComponent.Data.Options.RequestBody = requestBodyExample
}
}
}
}

if len(pathParams.Properties) > 0 {
taskComponent.Data.PathParams = pathParams
}
if len(queryParams.Properties) > 0 {
taskComponent.Data.QueryParams = queryParams
}

return taskComponent, nil
if len(pathParams.Properties) > 0 {
taskComponent.Data.PathParams = pathParams
}
if len(queryParams.Properties) > 0 {
taskComponent.Data.QueryParams = queryParams
}

return taskComponent, nil
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"description": "Do infra migration with beetle.",
"api_connection_id": "beetle_api",
"swagger_yaml_endpoint": "/beetle/api/doc.yaml",
"endpoint": "/migration/ns/{nsId}/mci"
"endpoint": "/migration/ns/{nsId}/mci",
"method": "POST"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"description": "Do infra recommendation with beetle.",
"api_connection_id": "beetle_api",
"swagger_yaml_endpoint": "/beetle/api/doc.yaml",
"endpoint": "/recommendation/mci"
"endpoint": "/recommendation/mci",
"method": "POST"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"description": "Do software migration by grasshopper.",
"api_connection_id": "grasshopper_api",
"swagger_yaml_endpoint": "/grasshopper/api/doc.yaml",
"endpoint": "/software/migrate"
"endpoint": "/software/migrate",
"method": "POST"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"description": "Register target information to the source group by honeybee.",
"api_connection_id": "honeybee_api",
"swagger_yaml_endpoint": "/honeybee/api/doc.yaml",
"endpoint": "/source_group/{sgId}/target"
"endpoint": "/source_group/{sgId}/target",
"method": "POST"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"description": "Get refined infra information from honeybee.",
"api_connection_id": "honeybee_api",
"swagger_yaml_endpoint": "/honeybee/api/doc.yaml",
"endpoint": "/source_group/{sgId}/infra/refined"
"endpoint": "/source_group/{sgId}/infra/refined",
"method": "GET"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"description": "Get saved software information by honeybee.",
"api_connection_id": "honeybee_api",
"swagger_yaml_endpoint": "/honeybee/api/doc.yaml",
"endpoint": "/source_group/{sgId}/software"
"endpoint": "/source_group/{sgId}/software",
"method": "GET"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"description": "Import infra information by honeybee.",
"api_connection_id": "honeybee_api",
"swagger_yaml_endpoint": "/honeybee/api/doc.yaml",
"endpoint": "/source_group/{sgId}/import/infra"
"endpoint": "/source_group/{sgId}/import/infra",
"method": "POST"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"description": "Import software information by honeybee.",
"api_connection_id": "honeybee_api",
"swagger_yaml_endpoint": "/honeybee/api/doc.yaml",
"endpoint": "/source_group/{sgId}/import/software"
"endpoint": "/source_group/{sgId}/import/software",
"method": "POST"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"description": "Create MCI Dynamically from common spec and image.",
"api_connection_id": "tumblebug_api",
"swagger_yaml_endpoint": "/tumblebug/api/doc.yaml",
"endpoint": "/ns/{nsId}/mciDynamic"
"endpoint": "/ns/{nsId}/mciDynamic",
"method": "POST"
}

0 comments on commit 34e0d46

Please sign in to comment.