|
13 | 13 | # language governing permissions and limitations under the License.
|
14 | 14 |
|
15 | 15 | import boto3
|
| 16 | +import click |
16 | 17 | from pcluster_client.api import cluster_operations_api
|
17 | 18 | from pcluster_client import Configuration, ApiClient, ApiException
|
18 | 19 |
|
19 |
| -apigateway = boto3.client("apigateway") |
20 | 20 |
|
21 |
| - |
22 |
| -def request(): |
| 21 | +@click.command() |
| 22 | +@click.option("--stack-name", help="ParallelCluster API stack name") |
| 23 | +@click.option("--region", help="AWS region") |
| 24 | +def request(stack_name: str, region: str): |
23 | 25 | """Makes a simple request to the API Gateway"""
|
24 |
| - apis = apigateway.get_rest_apis()["items"] |
25 |
| - api_id = next(api["id"] for api in apis if api["name"] == "ParallelCluster") |
26 |
| - region = boto3.session.Session().region_name |
27 |
| - host = f"{api_id}.execute-api.{region}.amazonaws.com" |
28 |
| - configuration = Configuration(host=f"https://{host}/prod") |
| 26 | + invoke_url = describe_stack_output(region, stack_name, "ParallelClusterApiInvokeUrl") |
| 27 | + configuration = Configuration(host=invoke_url) |
29 | 28 |
|
30 | 29 | with ApiClient(configuration) as api_client:
|
31 | 30 | client = cluster_operations_api.ClusterOperationsApi(api_client)
|
32 | 31 | region_filter = region
|
33 | 32 |
|
34 | 33 | try:
|
35 | 34 | response = client.list_clusters(region=region_filter)
|
36 |
| - print("clusters: ", [c["cluster_name"] for c in response["clusters"]]) |
| 35 | + print("Response: ", response) |
37 | 36 | except ApiException as ex:
|
38 | 37 | print("Exception when calling ClusterOperationsApi->list_clusters: %s\n" % ex)
|
39 | 38 |
|
40 | 39 |
|
| 40 | +def describe_stack_output(region: str, stack_name: str, output_name: str): |
| 41 | + try: |
| 42 | + # Describe stack |
| 43 | + cloudformation = boto3.client("cloudformation", region_name=region) |
| 44 | + response = cloudformation.describe_stacks(StackName=stack_name) |
| 45 | + |
| 46 | + # Get the stack details |
| 47 | + stacks = response.get("Stacks", []) |
| 48 | + if not stacks: |
| 49 | + print(f"No stacks found with the name: {stack_name}") |
| 50 | + return None |
| 51 | + |
| 52 | + # Extract output |
| 53 | + outputs = stacks[0].get("Outputs", []) |
| 54 | + return list(filter(lambda o: o['OutputKey'] == 'ParallelClusterApiInvokeUrl', outputs))[0]['OutputValue'] |
| 55 | + |
| 56 | + except Exception as e: |
| 57 | + print(f"Cannot describe output '{output_name}' for stack '{stack_name}': {e}") |
| 58 | + return None |
| 59 | + |
41 | 60 | if __name__ == "__main__":
|
42 | 61 | request()
|
0 commit comments