import re from typing import Any from uuid import UUID from pydantic import UUID4, BaseModel, ConfigDict, field_validator from backfill.connectors.ows_pdp.models.tenant_type import TenantType from backfill.constants import AttachAndDetachOperation, JobType class Job(BaseModel): keys: list[str] job_type: JobType model_config = ConfigDict( str_strip_whitespace=True, ) class Manifest(BaseModel): bucket: str jobs: list[Job] model_config = ConfigDict( str_strip_whitespace=True, ) @field_validator("bucket", mode="after") @classmethod def validate_bucket(cls, v: str) -> Any: """Validate that the bucket name starts with dev, qa, prod or uat.""" if not re.match("^(dev|qa|prod|uat)", v): raise ValueError("bucket must begin with dev|qa|prod|uat") return v class AttachAndDetachRow(BaseModel): """AttachAndDetachRow represents a single row in the attach and detach job csv.""" identity_uuid: UUID4 tenant_uuid: UUID tenant_type: TenantType role: str operation: AttachAndDetachOperation model_config = ConfigDict( str_strip_whitespace=True, ) @field_validator( "identity_uuid", "tenant_uuid", "tenant_type", "operation", mode="before", ) @classmethod def trim(cls, v: Any) -> Any: """Trim a field that is a string before it is validated for a different type.""" if isinstance(v, str): return v.strip() return v