from dataclasses import dataclass from typing import TypedDict @dataclass class ProfileInfo: """A minimal representation of a Profile.""" profile_type: str profile_id: int roles: list[str] uuid: str @dataclass class Tenant: """A tenant object (Vendor, SubAccount, Collaborator, or LabelParticipant).""" type: str uuid: str class AdminableTenant(TypedDict): """Dict containing a tenant and a user's profiles linked to it.""" tenant: dict profiles: list[ProfileInfo] def get_primary_label(node_labels: frozenset) -> str or None: tenant_labels = {'Vendor', 'Subaccount', 'Collaborator', 'LabelParticipant'} matching_labels = tenant_labels.intersection(node_labels) if matching_labels: return next(iter(matching_labels)) return None def format_adminable_tenants_result(result: list[dict[str, any]]) -> list[AdminableTenant]: """Return a list of dicts containing tenants and associated profiles.""" tenants = [] for each in result: tenant_node = each.get('tenant') tenant_label = get_primary_label(tenant_node.labels) tenant = dict(tenant_node.items()) profiles = each.get('profiles') tenants.append( AdminableTenant( # tenant=Tenant( # type=tenant_label, # uuid=tenant['uuid'] # ), tenant={ 'type': tenant_label, 'uuid': tenant['uuid'] }, profiles=[ProfileInfo( profile_type=p['profileType'], profile_id=p['profileId'], roles=p['roles'], uuid=p['uuid'] ) for p in profiles] ) ) return tenants