|
| 1 | +""" |
| 2 | +Pydantic models for the Domain resource. |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | +import logging |
| 7 | +from typing import Optional |
| 8 | +from pydantic import BaseModel, ConfigDict, Field |
| 9 | +from pydantic.alias_generators import to_camel |
| 10 | + |
| 11 | +from ...utils import update_model_fields |
| 12 | +from ...core import _APIOperationExecutor, APIOperation, AsyncCallable |
| 13 | + |
| 14 | +log = logging.getLogger(__name__) |
| 15 | + |
| 16 | +camel_config = ConfigDict( |
| 17 | + alias_generator=to_camel, |
| 18 | + populate_by_name=True, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +class DomainBase(BaseModel): |
| 23 | + model_config = camel_config |
| 24 | + |
| 25 | + name: str |
| 26 | + team_id: int |
| 27 | + data_center_id: int |
| 28 | + workspaces: dict[str, list[int]] |
| 29 | + certificate_request_status: CertificateRequestStatus |
| 30 | + dns_entries: DNSEntries |
| 31 | + domain_verification_status: DomainVerificationStatus |
| 32 | + custom_config_revision: Optional[int] = None |
| 33 | + custom_config: Optional[CustomDomainConfig] = None |
| 34 | + |
| 35 | + |
| 36 | +class CertificateRequestStatus(BaseModel): |
| 37 | + model_config = camel_config |
| 38 | + issued: bool |
| 39 | + reason: Optional[str] = None |
| 40 | + |
| 41 | + |
| 42 | +class DNSEntries(BaseModel): |
| 43 | + model_config = camel_config |
| 44 | + a: str |
| 45 | + cname: str |
| 46 | + txt: str |
| 47 | + |
| 48 | + |
| 49 | +class DomainVerificationStatus(BaseModel): |
| 50 | + model_config = camel_config |
| 51 | + verified: bool |
| 52 | + reason: Optional[str] = None |
| 53 | + |
| 54 | + |
| 55 | +class CustomDomainConfig(BaseModel): |
| 56 | + max_body_size_mb: int |
| 57 | + max_connection_timeout: int |
| 58 | + use_regex: bool |
| 59 | + restricted: bool |
| 60 | + |
| 61 | + |
| 62 | +class Domain(DomainBase, _APIOperationExecutor): |
| 63 | + update_domain_op: AsyncCallable[None] = Field( |
| 64 | + default=APIOperation( |
| 65 | + method="PATCH", |
| 66 | + endpoint_template="/domains/team/{team_id}/domain/{name}", |
| 67 | + response_model=DomainBase, |
| 68 | + ), |
| 69 | + exclude=True, |
| 70 | + ) |
| 71 | + |
| 72 | + update_workspace_connections_op: AsyncCallable[None] = Field( |
| 73 | + default=APIOperation( |
| 74 | + method="PUT", |
| 75 | + endpoint_template="/domains/team/{team_id}/domain/{name}/workspace-connections", |
| 76 | + response_model=DomainBase, |
| 77 | + ), |
| 78 | + exclude=True, |
| 79 | + ) |
| 80 | + |
| 81 | + verify_domain_op: AsyncCallable[None] = Field( |
| 82 | + default=APIOperation( |
| 83 | + method="POST", |
| 84 | + endpoint_template="/domains/team/{team_id}/domain/{name}/verify", |
| 85 | + response_model=DomainVerificationStatus, |
| 86 | + ), |
| 87 | + exclude=True, |
| 88 | + ) |
| 89 | + |
| 90 | + delete_domain_op: AsyncCallable[None] = Field( |
| 91 | + default=APIOperation( |
| 92 | + method="DELETE", |
| 93 | + endpoint_template="/domains/team/{team_id}/domain/{name}", |
| 94 | + response_model=DomainBase, |
| 95 | + ), |
| 96 | + exclude=True, |
| 97 | + ) |
| 98 | + |
| 99 | + async def update_domain(self, data: CustomDomainConfig) -> DomainBase: |
| 100 | + await self.update_domain_op(data=data) |
| 101 | + update_model_fields(target=self.custom_config, source=data) |
| 102 | + |
| 103 | + async def update_workspace_connections( |
| 104 | + self, data: dict[str, list[int]] |
| 105 | + ) -> DomainBase: |
| 106 | + response = await self.update_workspace_connections_op(data=data) |
| 107 | + update_model_fields(target=self, source=response) |
| 108 | + |
| 109 | + async def verify_status(self) -> DomainVerificationStatus: |
| 110 | + response = await self.verify_domain_op() |
| 111 | + update_model_fields(target=self.domain_verification_status, source=response) |
| 112 | + return response |
| 113 | + |
| 114 | + async def delete(self) -> None: |
| 115 | + await self.delete_domain_op() |
0 commit comments