From 34e0d46a528d2e30c1edcbf5fda0e24505b1de73 Mon Sep 17 00:00:00 2001 From: ish Date: Fri, 22 Nov 2024 19:32:04 +0900 Subject: [PATCH] db, airflow: Check method type when registering task components --- db/taskComponent.go | 129 ++++++++++-------- .../beetle_task_infra_migration.json | 3 +- .../beetle_task_recommend_infra.json | 3 +- .../grasshopper_task_software_migration.json | 3 +- ..._register_target_info_to_source_group.json | 3 +- .../honeybee_task_get_infra_refined.json | 3 +- .../honeybee_task_get_software.json | 3 +- .../honeybee_task_import_infra.json | 3 +- .../honeybee_task_import_software.json | 3 +- .../task_component/tumblebug_mci_dynamic.json | 3 +- 10 files changed, 90 insertions(+), 66 deletions(-) diff --git a/db/taskComponent.go b/db/taskComponent.go index 0a6d230..9b9c8ad 100644 --- a/db/taskComponent.go +++ b/db/taskComponent.go @@ -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"` } @@ -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 @@ -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 @@ -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 } } diff --git a/lib/airflow/example/task_component/beetle_task_infra_migration.json b/lib/airflow/example/task_component/beetle_task_infra_migration.json index 80ea686..adb8616 100644 --- a/lib/airflow/example/task_component/beetle_task_infra_migration.json +++ b/lib/airflow/example/task_component/beetle_task_infra_migration.json @@ -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" } diff --git a/lib/airflow/example/task_component/beetle_task_recommend_infra.json b/lib/airflow/example/task_component/beetle_task_recommend_infra.json index 480c140..74fd7dd 100644 --- a/lib/airflow/example/task_component/beetle_task_recommend_infra.json +++ b/lib/airflow/example/task_component/beetle_task_recommend_infra.json @@ -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" } diff --git a/lib/airflow/example/task_component/grasshopper_task_software_migration.json b/lib/airflow/example/task_component/grasshopper_task_software_migration.json index 48cc02a..1346de2 100644 --- a/lib/airflow/example/task_component/grasshopper_task_software_migration.json +++ b/lib/airflow/example/task_component/grasshopper_task_software_migration.json @@ -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" } diff --git a/lib/airflow/example/task_component/honeybee_register_target_info_to_source_group.json b/lib/airflow/example/task_component/honeybee_register_target_info_to_source_group.json index f603626..b21b718 100644 --- a/lib/airflow/example/task_component/honeybee_register_target_info_to_source_group.json +++ b/lib/airflow/example/task_component/honeybee_register_target_info_to_source_group.json @@ -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" } diff --git a/lib/airflow/example/task_component/honeybee_task_get_infra_refined.json b/lib/airflow/example/task_component/honeybee_task_get_infra_refined.json index 3581bbe..77412e2 100644 --- a/lib/airflow/example/task_component/honeybee_task_get_infra_refined.json +++ b/lib/airflow/example/task_component/honeybee_task_get_infra_refined.json @@ -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" } diff --git a/lib/airflow/example/task_component/honeybee_task_get_software.json b/lib/airflow/example/task_component/honeybee_task_get_software.json index 584bfd4..0521397 100644 --- a/lib/airflow/example/task_component/honeybee_task_get_software.json +++ b/lib/airflow/example/task_component/honeybee_task_get_software.json @@ -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" } diff --git a/lib/airflow/example/task_component/honeybee_task_import_infra.json b/lib/airflow/example/task_component/honeybee_task_import_infra.json index 01d325c..e82a27e 100644 --- a/lib/airflow/example/task_component/honeybee_task_import_infra.json +++ b/lib/airflow/example/task_component/honeybee_task_import_infra.json @@ -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" } diff --git a/lib/airflow/example/task_component/honeybee_task_import_software.json b/lib/airflow/example/task_component/honeybee_task_import_software.json index e797af3..f8e958a 100644 --- a/lib/airflow/example/task_component/honeybee_task_import_software.json +++ b/lib/airflow/example/task_component/honeybee_task_import_software.json @@ -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" } diff --git a/lib/airflow/example/task_component/tumblebug_mci_dynamic.json b/lib/airflow/example/task_component/tumblebug_mci_dynamic.json index 55bb763..881b19d 100644 --- a/lib/airflow/example/task_component/tumblebug_mci_dynamic.json +++ b/lib/airflow/example/task_component/tumblebug_mci_dynamic.json @@ -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" }