From 9baba9a388866660653cb604fa370119a79da919 Mon Sep 17 00:00:00 2001 From: gpapathan87 Date: Fri, 27 Jun 2025 14:40:29 +0300 Subject: [PATCH] update app_controller --- .../controllers/app_controllers.py | 43 +++++++++---------- .../controllers/edge_cloud_controller.py | 9 ++-- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/edge_cloud_management_api/controllers/app_controllers.py b/edge_cloud_management_api/controllers/app_controllers.py index 6efc9f5..8ea2e99 100644 --- a/edge_cloud_management_api/controllers/app_controllers.py +++ b/edge_cloud_management_api/controllers/app_controllers.py @@ -1,11 +1,8 @@ -import uuid from flask import jsonify, request from pydantic import ValidationError -from edge_cloud_management_api.managers.db_manager import MongoManager from edge_cloud_management_api.managers.log_manager import logger -from edge_cloud_management_api.models.application_models import AppManifest, AppZones, AppInstance from edge_cloud_management_api.services.pi_edge_services import PiEdgeAPIClientFactory -from edge_cloud_management_api.services.pi_edge_services import PiEdgeAPIClient + class NotFound404Exception(Exception): @@ -32,7 +29,7 @@ def submit_app(body: dict): ) -def get_apps(x_correlator=None): # noqa: E501 +def get_apps(x_correlator=None): """Retrieve metadata information of all applications""" try: pi_edge_factory = PiEdgeAPIClientFactory() @@ -46,7 +43,7 @@ def get_apps(x_correlator=None): # noqa: E501 ) -def get_app(appId, x_correlator=None): # noqa: E501 +def get_app(appId, x_correlator=None): """Retrieve the information of an Application""" try: pi_edge_factory = PiEdgeAPIClientFactory() @@ -67,7 +64,7 @@ def get_app(appId, x_correlator=None): # noqa: E501 ) -def delete_app(appId, x_correlator=None): # noqa: E501 +def delete_app(appId, x_correlator=None): """Delete Application metadata from an Edge Cloud Provider""" try: pi_edge_factory = PiEdgeAPIClientFactory() @@ -90,41 +87,41 @@ def delete_app(appId, x_correlator=None): # noqa: E501 def create_app_instance(): logger.info("Received request to create app instance") - + try: - + body = request.get_json() logger.debug(f"Request body: {body}") - + app_id = body.get("appId") edge_zone_id = body.get("edgeCloudZoneId") k8s_ref = body.get("kubernetesClusterRef") - + if not app_id or not edge_zone_id or not k8s_ref: return jsonify({"error": "Missing required fields: appId, edgeCloudZoneId, or kubernetesCLusterRef"}), 400 - + logger.info(f"Preparing to send deployment request to SRM for appId={app_id}") - - pi_edge_client_factory = PiEdgeAPICLientFactory() + + pi_edge_client_factory = PiEdgeAPIClientFactory() pi_edge_client = pi_edge_client_factory.create_pi_edge_api_client() - + print("\n === Preparing Deployment Request ===") print(f" Endpoint: {pi_edge_client.base_url}/deployedServiceFunction") print(f" Headers: {pi_edge_client._get_headers()}") print(f"Payload: {body}") print("=== End of Deployment Request ===\n") - + try: - response = pi_edge_client_deploy_service_function(data=body) - + response = pi_edge_client.deploy_service_function(data=body) + if isinstance(response, dict) and "error" in response: logger.warning(f"Failed to deploy service function: {response}") return jsonify({ "warning": "Deployment not completed (SRM service unreachable)", "details": response - + }), 202 - + logger.info(f"Deployment response from SRM: {response}") except Exception as inner_error: logger.error(f"Exception while trying to deploy to SRM: {inner_error}") @@ -132,7 +129,7 @@ def create_app_instance(): "warning": "SRM backend unavailable. Deployment request was built correctly.", "details": str(inner_error) }),202 - + return jsonify({"message": f"Application {app_id} instantiation accepted"}), 202 except ValidationError as e: logger.error(f"Validation error: {str(e)}") @@ -173,12 +170,12 @@ def get_app_instance(app_id=None, x_correlator=None, app_instance_id=None, regio def delete_app_instance(appInstanceId: str, x_correlator=None): - + try: pi_edge_client_factory = PiEdgeAPIClientFactory() pi_edge_client = pi_edge_client_factory.create_pi_edge_api_client() response = pi_edge_client.delete_app_instance(appInstanceId) - return jsonify({'result': response.text, 'status': response.status_code}) + return response.json() except Exception as e: return ( diff --git a/edge_cloud_management_api/controllers/edge_cloud_controller.py b/edge_cloud_management_api/controllers/edge_cloud_controller.py index c344476..ff8261e 100644 --- a/edge_cloud_management_api/controllers/edge_cloud_controller.py +++ b/edge_cloud_management_api/controllers/edge_cloud_controller.py @@ -41,10 +41,11 @@ def get_local_zones() -> list[dict]: logger.error(f"SRM error: {result['error']}") return [] + return result except Exception as e: - logger.exception("Unexpected error while retrieving local zones from SRM") + logger.exception("Unexpected error while retrieving local zones from SRM: %s", e) return [] @@ -58,10 +59,10 @@ def get_all_cloud_zones() -> List[EdgeCloudZone]: return get_local_zones() + get_federated_zones() -def get_edge_cloud_zones(x_correlator: str | None = None, region=None, status=None): +def get_edge_cloud_zones(x_correlator: str | None = None, region=None, status=None): # noqa: E501 """Retrieve a list of the operators Edge Cloud Zones and their status - List of the operators Edge Cloud Zones and their status, ordering the results by location and filtering by status (active/inactive/unknown) + List of the operators Edge Cloud Zones and their status, ordering the results by location and filtering by status (active/inactive/unknown) # noqa: E501 :param x_correlator: Correlation id for the different services :type x_correlator: str @@ -87,8 +88,6 @@ def get_edge_cloud_zones(x_correlator: str | None = None, region=None, status=No """If status is None, return True (don't apply status filtering), otherwise check if the zone status matches the query status""" return (query_params.status is None) or (zone["edgeCloudZoneStatus"] == query_params.status) - # filtered_zones = [zone for zone in get_all_cloud_zones() if (query_region_matches(zone) and query_status_matches(zone))] - response = [EdgeCloudZone(**zone).model_dump() for zone in get_all_cloud_zones()] return jsonify(response), 200 -- GitLab