"""Pydantic models related to checking resource authorization.""" from typing import Annotated, List, Optional from annotated_types import Len from ddtrace.trace import tracer from pydantic import BaseModel from pdp.connectors.ows_account import OwsAccountClient from pdp.connectors.redis_client import RedisConnector from pdp.fastapi.schemas.identity import CheckResourceAction from pdp.fastapi.schemas.tenant import IdToUuidExchangeTenant from pdp.proxies.id_to_uuid_exchange_tenant_proxy import IdToUuidExchangeTenantProxy class CheckResourcesRequest(BaseModel): """Request object for checking resources actions.""" resources: Annotated[List[CheckResourceAction], Len(min_length=1)] include_resource_attributes_in_response: bool = False @tracer.wrap() async def update_with_id_to_uuid_exchange( self, redis_connector: RedisConnector, ows_account_client: OwsAccountClient, ) -> None: """Perform IdToUuidExchange on this request's resources's attributes.""" # Get all the IdToUuidExchangeTenants from resources tenants: List[Optional[IdToUuidExchangeTenant]] = [] for check_resource_action in self.resources: tenant = check_resource_action.get_id_to_uuid_exchange_tenant() tenants.append(tenant) proxy = IdToUuidExchangeTenantProxy( tenants=[tenant for tenant in tenants if tenant], redis_connector=redis_connector, ows_account_client=ows_account_client, ) await proxy.gather_tenant_exchange() # Iterate over the resources to update attributes with tenant for tenant, check_resource_action in zip(tenants, self.resources): if tenant: id_exchange_tenant_hierarchy = proxy.get_tenant_exchange( tenant.tenant_type, tenant.tenant_id, ) # Update if there is anything available if id_exchange_tenant_hierarchy: check_resource_action.resource.attributes["tenant"] = ( id_exchange_tenant_hierarchy.to_resource_attributes_dict() )