Skip to content

Commit 2ec9592

Browse files
committed
[Test] Improve the script to make a sample request to a deployed ParallelCluster API.
Signed-off-by: Giacomo Marciani <[email protected]>
1 parent 7a6c18a commit 2ec9592

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

api/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,15 @@ through unit tests and integration tests that exercise the operations.
9999
In order to test the API specifically, there are integraiton tests which will deploy the API and test the functionality using
100100
the generated client.
101101

102+
### Invoking the API
103+
104+
Install requirements for the example:
105+
```
106+
pip install -r client/requirements.txt
107+
```
108+
109+
Invoke a deployed ParallelCluster API:
110+
```
111+
python client/example.py --region [REGION] --stack-name [PCAPI_STACK_NAME]
112+
```
113+

api/client/example.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,49 @@
1313
# language governing permissions and limitations under the License.
1414

1515
import boto3
16+
import click
1617
from pcluster_client.api import cluster_operations_api
1718
from pcluster_client import Configuration, ApiClient, ApiException
1819

19-
apigateway = boto3.client("apigateway")
2020

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):
2325
"""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)
2928

3029
with ApiClient(configuration) as api_client:
3130
client = cluster_operations_api.ClusterOperationsApi(api_client)
3231
region_filter = region
3332

3433
try:
3534
response = client.list_clusters(region=region_filter)
36-
print("clusters: ", [c["cluster_name"] for c in response["clusters"]])
35+
print("Response: ", response)
3736
except ApiException as ex:
3837
print("Exception when calling ClusterOperationsApi->list_clusters: %s\n" % ex)
3938

4039

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+
4160
if __name__ == "__main__":
4261
request()

api/client/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
boto3>=1.16.14
2+
click~=8.1.7

0 commit comments

Comments
 (0)