Loading edge_cloud_management_api/controllers/app_controllers.py +10 −7 Original line number Original line Diff line number Diff line import uuid from flask import jsonify from flask import jsonify from pydantic import ValidationError 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.managers.log_manager import logger from edge_cloud_management_api.models.application_models import AppManifest from edge_cloud_management_api.models.application_models import AppManifest Loading @@ -12,12 +14,13 @@ def submit_app(body: dict): try: try: # Validate the input data using Pydantic # Validate the input data using Pydantic validated_data = AppManifest(**body) validated_data = AppManifest(**body) validated_data_dict = validated_data.model_dump(mode="json") validated_data_dict["_id"] = str(uuid.uuid4()) # Insert into MongoDB # Insert into MongoDB # app_id = mongo.db.applications.insert_one(validated_data.dict()).inserted_id with MongoManager() as db: app_id = 3 document_id = db.insert_document("apps", validated_data_dict) return ( return ( jsonify({"message": "Application submitted successfully!", "appId": str(app_id)}), jsonify({"appId": str(document_id)}), 201, 201, ) ) Loading edge_cloud_management_api/models/application_models.py +5 −5 Original line number Original line Diff line number Diff line from pydantic import UUID4, BaseModel, HttpUrl, Field from pydantic import BaseModel, HttpUrl, Field # , UUID4 from typing import List, Optional from typing import Any, List, Optional from ipaddress import IPv4Address, IPv6Address from enum import Enum from enum import Enum # from ipaddress import IPv4Address, IPv6Address # from edge_cloud_management_api.models.edge_cloud_models import EdgeCloudZone # from edge_cloud_management_api.models.edge_cloud_models import EdgeCloudZone Loading Loading @@ -105,14 +105,14 @@ class AppManifest(BaseModel): memory: int memory: int storage: int storage: int appId: Optional[UUID4] # appId: Optional[UUID4] name: str = Field(..., pattern="^[A-Za-z][A-Za-z0-9_]{1,63}$") name: str = Field(..., pattern="^[A-Za-z][A-Za-z0-9_]{1,63}$") appProvider: str = Field(..., pattern="^[A-Za-z][A-Za-z0-9_]{7,63}$") appProvider: str = Field(..., pattern="^[A-Za-z][A-Za-z0-9_]{7,63}$") version: str # application version version: str # application version packageType: PackageType packageType: PackageType operatingSystem: Optional[OperatingSystem] operatingSystem: Optional[OperatingSystem] appRepo: AppRepo appRepo: AppRepo requiredResources: RequiredResources requiredResources: Any # Optional[RequiredResources] componentSpec: List[ComponentSpec] componentSpec: List[ComponentSpec] Loading tests/fixtures/submit_app.json +0 −1 Original line number Original line Diff line number Diff line { { "appId": "3fa85f64-5717-4562-b3fc-2c963f66afa6", "name": "H38L1jMMndtyRR3WFCSI_he_m4NwQ0rggZYrOX9r09shL597SF4bGHoqUI5t", "name": "H38L1jMMndtyRR3WFCSI_he_m4NwQ0rggZYrOX9r09shL597SF4bGHoqUI5t", "appProvider": "rQxQr3mSdo3dgYZTh1Hq55CWIqsEPHqB80P9_ja3HH8Bp9Hj9Ygc", "appProvider": "rQxQr3mSdo3dgYZTh1Hq55CWIqsEPHqB80P9_ja3HH8Bp9Hj9Ygc", "version": "string", "version": "string", Loading tests/unit/controllers/test_app_controllers.py +25 −7 Original line number Original line Diff line number Diff line Loading @@ -39,11 +39,28 @@ def mock_get_all_cloud_zones(): yield mock_function yield mock_function example_submit_input = { "name": "etO2zndyzpL1P", "appProvider": "TBPhbx4MO6n", "version": "string", "packageType": "QCOW2", "operatingSystem": {"architecture": "x86_64", "family": "RHEL", "version": "OS_VERSION_UBUNTU_2204_LTS", "license": "OS_LICENSE_TYPE_FREE"}, "appRepo": { "type": "PRIVATEREPO", "imagePath": "https://charts.bitnami.com/bitnami/helm/example-chart:0.1.0", "userName": "string", "credentials": "string", "authType": "DOCKER", "checksum": "string", }, "requiredResources": {}, "componentSpec": [{"componentName": "string", "networkInterfaces": [{"interfaceId": "zKJ9YWHujxe73gorEzEImKfr6", "protocol": "TCP", "port": 65535, "visibilityType": "VISIBILITY_EXTERNAL"}]}], } @pytest.mark.parametrize( @pytest.mark.parametrize( "x_correlator, body, expected_response_status, expected_response_body", "x_correlator, body, expected_response_status, expected_response_body", [ [(None, example_submit_input, 201, {"appId": "3fa85f64-5717-4562-b3fc-2c963f66afa6"})], (None, {}, 201, {"appId": "3fa85f64-5717-4562-b3fc-2c963f66afa6"}), ], ) ) def test_submit_app( def test_submit_app( x_correlator, x_correlator, Loading @@ -57,11 +74,12 @@ def test_submit_app( Test the get_edge_cloud_zones controller. Test the get_edge_cloud_zones controller. """ """ with test_app.test_request_context(): with test_app.test_request_context(): response, response_status = submit_app(x_correlator, body) response, response_status = submit_app(body) assert response_status == expected_response_status assert response_status == expected_response_status if expected_response_status == 400: if expected_response_status == 400: assert response.json["code"] == "VALIDATION_ERROR" assert response.json["code"] == "VALIDATION_ERROR" # elif expected_response_status == 201: elif expected_response_status == 201: assert "appId" in response.json # assert len(response.json) == expected_count # assert len(response.json) == expected_count # mock_get_all_cloud_zones.assert_called_once() # mock_get_all_cloud_zones.assert_called_once() else: else: Loading Loading
edge_cloud_management_api/controllers/app_controllers.py +10 −7 Original line number Original line Diff line number Diff line import uuid from flask import jsonify from flask import jsonify from pydantic import ValidationError 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.managers.log_manager import logger from edge_cloud_management_api.models.application_models import AppManifest from edge_cloud_management_api.models.application_models import AppManifest Loading @@ -12,12 +14,13 @@ def submit_app(body: dict): try: try: # Validate the input data using Pydantic # Validate the input data using Pydantic validated_data = AppManifest(**body) validated_data = AppManifest(**body) validated_data_dict = validated_data.model_dump(mode="json") validated_data_dict["_id"] = str(uuid.uuid4()) # Insert into MongoDB # Insert into MongoDB # app_id = mongo.db.applications.insert_one(validated_data.dict()).inserted_id with MongoManager() as db: app_id = 3 document_id = db.insert_document("apps", validated_data_dict) return ( return ( jsonify({"message": "Application submitted successfully!", "appId": str(app_id)}), jsonify({"appId": str(document_id)}), 201, 201, ) ) Loading
edge_cloud_management_api/models/application_models.py +5 −5 Original line number Original line Diff line number Diff line from pydantic import UUID4, BaseModel, HttpUrl, Field from pydantic import BaseModel, HttpUrl, Field # , UUID4 from typing import List, Optional from typing import Any, List, Optional from ipaddress import IPv4Address, IPv6Address from enum import Enum from enum import Enum # from ipaddress import IPv4Address, IPv6Address # from edge_cloud_management_api.models.edge_cloud_models import EdgeCloudZone # from edge_cloud_management_api.models.edge_cloud_models import EdgeCloudZone Loading Loading @@ -105,14 +105,14 @@ class AppManifest(BaseModel): memory: int memory: int storage: int storage: int appId: Optional[UUID4] # appId: Optional[UUID4] name: str = Field(..., pattern="^[A-Za-z][A-Za-z0-9_]{1,63}$") name: str = Field(..., pattern="^[A-Za-z][A-Za-z0-9_]{1,63}$") appProvider: str = Field(..., pattern="^[A-Za-z][A-Za-z0-9_]{7,63}$") appProvider: str = Field(..., pattern="^[A-Za-z][A-Za-z0-9_]{7,63}$") version: str # application version version: str # application version packageType: PackageType packageType: PackageType operatingSystem: Optional[OperatingSystem] operatingSystem: Optional[OperatingSystem] appRepo: AppRepo appRepo: AppRepo requiredResources: RequiredResources requiredResources: Any # Optional[RequiredResources] componentSpec: List[ComponentSpec] componentSpec: List[ComponentSpec] Loading
tests/fixtures/submit_app.json +0 −1 Original line number Original line Diff line number Diff line { { "appId": "3fa85f64-5717-4562-b3fc-2c963f66afa6", "name": "H38L1jMMndtyRR3WFCSI_he_m4NwQ0rggZYrOX9r09shL597SF4bGHoqUI5t", "name": "H38L1jMMndtyRR3WFCSI_he_m4NwQ0rggZYrOX9r09shL597SF4bGHoqUI5t", "appProvider": "rQxQr3mSdo3dgYZTh1Hq55CWIqsEPHqB80P9_ja3HH8Bp9Hj9Ygc", "appProvider": "rQxQr3mSdo3dgYZTh1Hq55CWIqsEPHqB80P9_ja3HH8Bp9Hj9Ygc", "version": "string", "version": "string", Loading
tests/unit/controllers/test_app_controllers.py +25 −7 Original line number Original line Diff line number Diff line Loading @@ -39,11 +39,28 @@ def mock_get_all_cloud_zones(): yield mock_function yield mock_function example_submit_input = { "name": "etO2zndyzpL1P", "appProvider": "TBPhbx4MO6n", "version": "string", "packageType": "QCOW2", "operatingSystem": {"architecture": "x86_64", "family": "RHEL", "version": "OS_VERSION_UBUNTU_2204_LTS", "license": "OS_LICENSE_TYPE_FREE"}, "appRepo": { "type": "PRIVATEREPO", "imagePath": "https://charts.bitnami.com/bitnami/helm/example-chart:0.1.0", "userName": "string", "credentials": "string", "authType": "DOCKER", "checksum": "string", }, "requiredResources": {}, "componentSpec": [{"componentName": "string", "networkInterfaces": [{"interfaceId": "zKJ9YWHujxe73gorEzEImKfr6", "protocol": "TCP", "port": 65535, "visibilityType": "VISIBILITY_EXTERNAL"}]}], } @pytest.mark.parametrize( @pytest.mark.parametrize( "x_correlator, body, expected_response_status, expected_response_body", "x_correlator, body, expected_response_status, expected_response_body", [ [(None, example_submit_input, 201, {"appId": "3fa85f64-5717-4562-b3fc-2c963f66afa6"})], (None, {}, 201, {"appId": "3fa85f64-5717-4562-b3fc-2c963f66afa6"}), ], ) ) def test_submit_app( def test_submit_app( x_correlator, x_correlator, Loading @@ -57,11 +74,12 @@ def test_submit_app( Test the get_edge_cloud_zones controller. Test the get_edge_cloud_zones controller. """ """ with test_app.test_request_context(): with test_app.test_request_context(): response, response_status = submit_app(x_correlator, body) response, response_status = submit_app(body) assert response_status == expected_response_status assert response_status == expected_response_status if expected_response_status == 400: if expected_response_status == 400: assert response.json["code"] == "VALIDATION_ERROR" assert response.json["code"] == "VALIDATION_ERROR" # elif expected_response_status == 201: elif expected_response_status == 201: assert "appId" in response.json # assert len(response.json) == expected_count # assert len(response.json) == expected_count # mock_get_all_cloud_zones.assert_called_once() # mock_get_all_cloud_zones.assert_called_once() else: else: Loading