|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Example showing how to manage cluster metadata with the CentML SDK. |
| 4 | +
|
| 5 | +Covers listing clusters (including deployment_creation_disabled) and updating |
| 6 | +that flag via PUT /clusters/{cluster_id}/metadata. Running this script lists |
| 7 | +clusters you can access; the update helper shows the call pattern and is not |
| 8 | +invoked automatically. |
| 9 | +
|
| 10 | +This uses the centml CLI authentication, so make sure you are logged in to the |
| 11 | +centml CLI before running it. Updating cluster metadata requires admin |
| 12 | +privileges (PERM_ADMIN_MANAGE_HARDWARE) and only works on org-owned clusters |
| 13 | +(global clusters return 404). |
| 14 | +""" |
| 15 | + |
| 16 | +from centml.sdk import UpdateClusterMetadataRequest |
| 17 | +from centml.sdk.api import get_centml_client |
| 18 | + |
| 19 | + |
| 20 | +def list_clusters(): |
| 21 | + """List accessible clusters and whether new deployments are disabled.""" |
| 22 | + with get_centml_client() as client: |
| 23 | + clusters = client.get_clusters().results |
| 24 | + |
| 25 | + if not clusters: |
| 26 | + print("No clusters found.") |
| 27 | + return |
| 28 | + |
| 29 | + print(f"\nFound {len(clusters)} cluster(s)\n") |
| 30 | + for cluster in sorted(clusters, key=lambda x: x.id): |
| 31 | + region = cluster.region if cluster.region else "N/A" |
| 32 | + print(f"ID: {cluster.id}") |
| 33 | + print(f"Cluster Name: {cluster.cluster_name}") |
| 34 | + print(f"Display Name: {cluster.display_name}") |
| 35 | + print(f"Region: {region}") |
| 36 | + print(f"Deployment Creation Disabled: {cluster.deployment_creation_disabled}") |
| 37 | + print("-" * 40) |
| 38 | + |
| 39 | + |
| 40 | +def update_cluster_metadata(cluster_id: int, deployment_creation_disabled: bool): |
| 41 | + """Toggle whether new deployments can be created on an org-owned cluster.""" |
| 42 | + request = UpdateClusterMetadataRequest(deployment_creation_disabled=deployment_creation_disabled) |
| 43 | + with get_centml_client() as client: |
| 44 | + cluster = client.update_cluster_metadata(cluster_id, request) |
| 45 | + print( |
| 46 | + f"Updated cluster {cluster.id} ({cluster.display_name}): " |
| 47 | + f"deployment_creation_disabled={cluster.deployment_creation_disabled}" |
| 48 | + ) |
| 49 | + return cluster |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == "__main__": |
| 53 | + list_clusters() |
| 54 | + # Example (requires admin privileges on an org-owned cluster): |
| 55 | + # update_cluster_metadata(cluster_id=1, deployment_creation_disabled=True) |
0 commit comments