|
| 1 | +from datacrunch import DataCrunchClient |
| 2 | +from typing import List |
| 3 | +from datacrunch.containers.containers import ComputeResource |
| 4 | + |
| 5 | + |
| 6 | +def list_all_compute_resources(client: DataCrunchClient) -> List[ComputeResource]: |
| 7 | + """List all available compute resources. |
| 8 | +
|
| 9 | + Args: |
| 10 | + client (DataCrunchClient): The DataCrunch API client. |
| 11 | +
|
| 12 | + Returns: |
| 13 | + List[ComputeResource]: List of all compute resources. |
| 14 | + """ |
| 15 | + return client.containers.get_compute_resources() |
| 16 | + |
| 17 | + |
| 18 | +def list_available_compute_resources(client: DataCrunchClient) -> List[ComputeResource]: |
| 19 | + """List only the available compute resources. |
| 20 | +
|
| 21 | + Args: |
| 22 | + client (DataCrunchClient): The DataCrunch API client. |
| 23 | +
|
| 24 | + Returns: |
| 25 | + List[ComputeResource]: List of available compute resources. |
| 26 | + """ |
| 27 | + all_resources = client.containers.get_compute_resources() |
| 28 | + return [r for r in all_resources if r.is_available] |
| 29 | + |
| 30 | + |
| 31 | +def list_compute_resources_by_size(client: DataCrunchClient, size: int) -> List[ComputeResource]: |
| 32 | + """List compute resources filtered by size. |
| 33 | +
|
| 34 | + Args: |
| 35 | + client (DataCrunchClient): The DataCrunch API client. |
| 36 | + size (int): The size to filter by. |
| 37 | +
|
| 38 | + Returns: |
| 39 | + List[ComputeResource]: List of compute resources with the specified size. |
| 40 | + """ |
| 41 | + all_resources = client.containers.get_compute_resources() |
| 42 | + return [r for r in all_resources if r.size == size] |
| 43 | + |
| 44 | + |
| 45 | +def main(): |
| 46 | + # Initialize the client with your credentials |
| 47 | + client = DataCrunchClient( |
| 48 | + client_id="your_client_id", |
| 49 | + client_secret="your_client_secret" |
| 50 | + ) |
| 51 | + |
| 52 | + # Example 1: List all compute resources |
| 53 | + print("\nAll compute resources:") |
| 54 | + all_resources = list_all_compute_resources(client) |
| 55 | + for resource in all_resources: |
| 56 | + print( |
| 57 | + f"Name: {resource.name}, Size: {resource.size}, Available: {resource.is_available}") |
| 58 | + |
| 59 | + # Example 2: List available compute resources |
| 60 | + print("\nAvailable compute resources:") |
| 61 | + available_resources = list_available_compute_resources(client) |
| 62 | + for resource in available_resources: |
| 63 | + print(f"Name: {resource.name}, Size: {resource.size}") |
| 64 | + |
| 65 | + # Example 3: List compute resources of size 8 |
| 66 | + print("\nCompute resources with size 8:") |
| 67 | + size_8_resources = list_compute_resources_by_size(client, 8) |
| 68 | + for resource in size_8_resources: |
| 69 | + print(f"Name: {resource.name}, Available: {resource.is_available}") |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + main() |
0 commit comments