Commit 3d47b7b7 authored by Sergio Gimenez's avatar Sergio Gimenez
Browse files

add zones endpoints and token fallback

parent d4edce5c
Loading
Loading
Loading
Loading
+23 −3
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ from typing import List
from edge_cloud_management_api.configs.env_config import config
from edge_cloud_management_api.managers.log_manager import logger
from edge_cloud_management_api.services.edge_cloud_services import PiEdgeAPIClientFactory
from edge_cloud_management_api.services.storage_service import insert_zones
from edge_cloud_management_api.services.storage_service import insert_zones, get_zones
from edge_cloud_management_api.services.federation_services import FederationManagerClientFactory


@@ -56,7 +56,9 @@ def get_local_zones() -> list[dict]:
        if isinstance(result, dict) and "error" in result:
            logger.error(f"SRM error: {result['error']}")
            return []
        if isinstance(result, list):
            return result
        return []

    except Exception as e:
        logger.exception("Unexpected error while retrieving local zones from SRM: %s", e)
@@ -67,6 +69,16 @@ def get_federated_zones() -> List[EdgeCloudZone]:
    """get partner/federated Operator Platform available zones from Federation Manager"""
    return []

def get_cached_zones() -> list[dict]:
    """Retrieve cached zones from storage, falling back to SRM if empty."""
    try:
        cached = get_zones()
        if cached:
            return cached
    except Exception as e:
        logger.warning("Failed to read cached zones: %s", e)
    return get_local_zones()

def get_all_cloud_zones() -> List[EdgeCloudZone]:
    """Get all available zones from local and federated Operator Platforms"""

@@ -108,7 +120,7 @@ def get_edge_cloud_zones(x_correlator: str | None = None, region=None, status=No
        def query_status_matches(zone: EdgeCloudZone) -> bool:
            return query_params.status is None or zone.edgeCloudZoneStatus == query_params.status

        response = [EdgeCloudZone(**zone).model_dump() for zone in get_all_cloud_zones()]
        response = [EdgeCloudZone(**zone).model_dump() for zone in get_cached_zones()]
        return jsonify(response), 200

    except ValidationError as e:
@@ -131,3 +143,11 @@ def edge_cloud_zone_details(zoneId: str) -> dict:
    api_client = pi_edge_factory.create_pi_edge_api_client()
    result = api_client.edge_cloud_zone_details(zone_id=zoneId)
    return result


def get_edge_cloud_zones_alias(x_correlator: str | None = None, region=None, status=None):
    return get_edge_cloud_zones(x_correlator=x_correlator, region=region, status=status)


def edge_cloud_zone_details_alias(zoneId: str) -> dict:
    return edge_cloud_zone_details(zoneId)
+8 −4
Original line number Diff line number Diff line
@@ -146,7 +146,11 @@ def remove_zone_sync(federationContextId, zoneId):
    return jsonify(response)

def __get_token():
    bearer = connexion.request.headers['Authorization']
    token = bearer.split()[1]
    # __token = requests.post(TOKEN_ENDPOINT, headers=token_headers, data=data).json().get('access_token')
    return token
    bearer = connexion.request.headers.get('Authorization')
    if bearer:
        parts = bearer.split()
        if len(parts) == 2 and parts[0].lower() == "bearer":
            return parts[1]
    if TOKEN_ENDPOINT:
        return requests.post(TOKEN_ENDPOINT, headers=token_headers, data=data).json().get('access_token')
    return None
+8 −1
Original line number Diff line number Diff line
@@ -19,6 +19,14 @@ def get_zone(zone_id: str):
        zone = col.find_one({'_id': zone_id})
        return zone

def get_zones():
        collection = "zones"
        myclient = pymongo.MongoClient(storage_url)
        mydbmongo = myclient[mydb_mongo]
        col = mydbmongo[collection]
        zones = col.find()
        return list(zones)

def delete_partner_zones():
        collection = "zones"
        myclient = pymongo.MongoClient(storage_url)
@@ -55,4 +63,3 @@ def delete_fed(fed_context_id: str):
        mydbmongo = myclient[mydb_mongo]
        col = mydbmongo[collection]
        col.delete_one({'_id': fed_context_id})
+135 −1
Original line number Diff line number Diff line
@@ -625,7 +625,7 @@ paths:
        List of the operators Edge Cloud Zones and their
        status, ordering the results by location and filtering by
        status (active/inactive/unknown)
      operationId: edge_cloud_management_api.controllers.edge_cloud_controller.get_edge_cloud_zones
      operationId: edge_cloud_management_api.controllers.edge_cloud_controller.get_edge_cloud_zones_alias
      parameters:
        - $ref: "#/components/parameters/x-correlator"
        - name: region
@@ -665,6 +665,83 @@ paths:
        "503":
          $ref: "#/components/responses/503"
  /edge-cloud-zones/{zoneId}:
    get:
      tags:
      - Edge Cloud Zones
      summary: Retrieve the details of an Edge Cloud Zone
      description: |
        List of the operators Edge Cloud Zones and their
        status, ordering the results by location and filtering by
        status (active/inactive/unknown)
      operationId: edge_cloud_management_api.controllers.edge_cloud_controller.edge_cloud_zone_details_alias
      parameters:
        - $ref: "#/components/parameters/x-correlator"
        - name: zoneId
          in: path
          description: |
            UID of the specific edge cloud zone
          required: true
          style: simple
          schema:
            type: string
      responses:
        "200":
          description: |
            Successful response, returning the Edge Cloud Zone details
        "404":
          $ref: "#/components/responses/404"
  /zones:
    get:
      # security:
      #   - openId:
      #       - edge-application-management:edge-cloud-zones:read
      tags:
        - Edge Cloud Zones
      summary: Retrieve a list of the operators Edge Cloud Zones and their status
      description: |
        List of the operators Edge Cloud Zones and their
        status, ordering the results by location and filtering by
        status (active/inactive/unknown)
      operationId: edge_cloud_management_api.controllers.edge_cloud_controller.get_edge_cloud_zones
      parameters:
        - $ref: "#/components/parameters/x-correlator"
        - name: region
          description: |
            Human readable name of the geographical Edge Cloud Region of
            the Edge Cloud. Defined by the Edge Cloud Provider.
          in: query
          required: false
          schema:
            $ref: "#/components/schemas/EdgeCloudRegion"
        - name: status
          description: Human readable status of the Edge Cloud Zone
          in: query
          required: false
          schema:
            $ref: "#/components/schemas/EdgeCloudZoneStatus"
      responses:
        "200":
          description: |
            Successful response, returning the
            Available Edge Cloud Zones.
          headers:
            x-correlator:
              $ref: "#/components/headers/x-correlator"
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/EdgeCloudZones"
        "401":
          $ref: "#/components/responses/401"
        "403":
          $ref: "#/components/responses/403"
        "404":
          $ref: "#/components/responses/404"
        "500":
          $ref: "#/components/responses/500"
        "503":
          $ref: "#/components/responses/503"
  /zones/{zoneId}:
    get:
      tags:
      - Edge Cloud Zones
@@ -1151,6 +1228,63 @@ paths:
          description: Bad request
        "404":
          description: Federation not found
  /{federationContextId}/zones/{zoneId}:
    get:
      tags:
      - FederationManagement
      summary: Retrieve reserved resources for a federated zone.
      description: |
        Retrieves details about the computation and network resources that the
        partner OP has reserved for this zone.
      operationId: edge_cloud_management_api.controllers.federation_manager_controller.get_zone_resource_info
      parameters:
      - name: federationContextId
        in: path
        required: true
        style: simple
        explode: false
        schema:
          $ref: '#/components/schemas/FederationContextId'
      - name: zoneId
        in: path
        required: true
        style: simple
        explode: false
        schema:
          type: string
      responses:
        "200":
          description: Zone resource info retrieved
        "404":
          description: Zone not found
    delete:
      tags:
      - FederationManagement
      summary: Remove availability zone reservation for a federation context.
      description: |
        Originating OP informs partner OP that it will no longer access the
        specified zone.
      operationId: edge_cloud_management_api.controllers.federation_manager_controller.remove_zone_sync
      parameters:
      - name: federationContextId
        in: path
        required: true
        style: simple
        explode: false
        schema:
          $ref: '#/components/schemas/FederationContextId'
      - name: zoneId
        in: path
        required: true
        style: simple
        explode: false
        schema:
          type: string
      responses:
        "200":
          description: Zone reservation removed
        "404":
          description: Zone not found
  /fed-context-id:
   get:
     tags: