|
| 1 | +"""Create and delete an isolated Temporal Cloud namespace for CI.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import time |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +from temporalio.api.cloud.cloudservice.v1 import ( |
| 10 | + CreateNamespaceRequest, |
| 11 | + DeleteNamespaceRequest, |
| 12 | + GetAsyncOperationRequest, |
| 13 | + GetNamespaceRequest, |
| 14 | +) |
| 15 | +from temporalio.api.cloud.namespace.v1 import MtlsAuthSpec, NamespaceSpec |
| 16 | +from temporalio.api.cloud.operation.v1 import AsyncOperation |
| 17 | +from temporalio.client import CloudOperationsClient |
| 18 | + |
| 19 | + |
| 20 | +async def wait_for_operation( |
| 21 | + client: CloudOperationsClient, operation: AsyncOperation |
| 22 | +) -> None: |
| 23 | + deadline = time.monotonic() + 10 * 60 |
| 24 | + while True: |
| 25 | + operation = ( |
| 26 | + await client.cloud_service.get_async_operation( |
| 27 | + GetAsyncOperationRequest(async_operation_id=operation.id) |
| 28 | + ) |
| 29 | + ).async_operation |
| 30 | + if operation.state == AsyncOperation.STATE_FULFILLED: |
| 31 | + return |
| 32 | + if operation.state in { |
| 33 | + AsyncOperation.STATE_FAILED, |
| 34 | + AsyncOperation.STATE_CANCELLED, |
| 35 | + AsyncOperation.STATE_REJECTED, |
| 36 | + }: |
| 37 | + raise RuntimeError( |
| 38 | + "Cloud operation " |
| 39 | + f"{operation.id} {AsyncOperation.State.Name(operation.state).lower()}: " |
| 40 | + f"{operation.failure_reason}" |
| 41 | + ) |
| 42 | + if time.monotonic() >= deadline: |
| 43 | + raise TimeoutError(f"Timed out waiting for Cloud operation {operation.id}") |
| 44 | + delay = max( |
| 45 | + operation.check_duration.seconds |
| 46 | + + operation.check_duration.nanos / 1_000_000_000, |
| 47 | + 1, |
| 48 | + ) |
| 49 | + await asyncio.sleep(min(delay, deadline - time.monotonic())) |
| 50 | + |
| 51 | + |
| 52 | +async def create() -> None: |
| 53 | + client = await cloud_client() |
| 54 | + namespace_name = "sdk-python-ci-{}-{}".format( |
| 55 | + os.environ["GITHUB_RUN_ID"], os.environ["GITHUB_RUN_ATTEMPT"] |
| 56 | + ) |
| 57 | + result = await client.cloud_service.create_namespace( |
| 58 | + CreateNamespaceRequest( |
| 59 | + spec=NamespaceSpec( |
| 60 | + name=namespace_name, |
| 61 | + regions=["aws-ca-central-1"], |
| 62 | + retention_days=1, |
| 63 | + mtls_auth=MtlsAuthSpec( |
| 64 | + accepted_client_ca=Path( |
| 65 | + os.environ["TEMPORAL_CLOUD_CLIENT_CA_PATH"] |
| 66 | + ).read_bytes(), |
| 67 | + enabled=True, |
| 68 | + ), |
| 69 | + ) |
| 70 | + ) |
| 71 | + ) |
| 72 | + # Make cleanup possible even if provisioning fails after Cloud accepts the request. |
| 73 | + with open(os.environ["GITHUB_OUTPUT"], "a") as output: |
| 74 | + output.write(f"namespace={result.namespace}\n") |
| 75 | + await wait_for_operation(client, result.async_operation) |
| 76 | + |
| 77 | + |
| 78 | +async def delete(namespace: str) -> None: |
| 79 | + client = await cloud_client() |
| 80 | + existing = await client.cloud_service.get_namespace( |
| 81 | + GetNamespaceRequest(namespace=namespace) |
| 82 | + ) |
| 83 | + result = await client.cloud_service.delete_namespace( |
| 84 | + DeleteNamespaceRequest( |
| 85 | + namespace=namespace, |
| 86 | + resource_version=existing.namespace.resource_version, |
| 87 | + ) |
| 88 | + ) |
| 89 | + await wait_for_operation(client, result.async_operation) |
| 90 | + |
| 91 | + |
| 92 | +async def cloud_client() -> CloudOperationsClient: |
| 93 | + return await CloudOperationsClient.connect( |
| 94 | + api_key=os.environ["TEMPORAL_CLIENT_CLOUD_API_KEY"], |
| 95 | + version=os.environ["TEMPORAL_CLIENT_CLOUD_API_VERSION"], |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +async def main() -> None: |
| 100 | + match sys.argv[1:]: |
| 101 | + case ["create"]: |
| 102 | + await create() |
| 103 | + case ["delete", namespace]: |
| 104 | + await delete(namespace) |
| 105 | + case _: |
| 106 | + raise ValueError("Usage: cloud_namespace.py create|delete <namespace>") |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + asyncio.run(main()) |
0 commit comments