Skip to content

Commit

Permalink
[Test] Improve the script to make a sample request to a deployed Para…
Browse files Browse the repository at this point in the history
…llelCluster API.

Signed-off-by: Giacomo Marciani <[email protected]>
  • Loading branch information
gmarciani committed Dec 2, 2024
1 parent 7a6c18a commit 2ec9592
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
12 changes: 12 additions & 0 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,15 @@ through unit tests and integration tests that exercise the operations.
In order to test the API specifically, there are integraiton tests which will deploy the API and test the functionality using
the generated client.

### Invoking the API

Install requirements for the example:
```
pip install -r client/requirements.txt
```

Invoke a deployed ParallelCluster API:
```
python client/example.py --region [REGION] --stack-name [PCAPI_STACK_NAME]
```

37 changes: 28 additions & 9 deletions api/client/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,49 @@
# language governing permissions and limitations under the License.

import boto3
import click
from pcluster_client.api import cluster_operations_api
from pcluster_client import Configuration, ApiClient, ApiException

apigateway = boto3.client("apigateway")


def request():
@click.command()
@click.option("--stack-name", help="ParallelCluster API stack name")
@click.option("--region", help="AWS region")
def request(stack_name: str, region: str):
"""Makes a simple request to the API Gateway"""
apis = apigateway.get_rest_apis()["items"]
api_id = next(api["id"] for api in apis if api["name"] == "ParallelCluster")
region = boto3.session.Session().region_name
host = f"{api_id}.execute-api.{region}.amazonaws.com"
configuration = Configuration(host=f"https://{host}/prod")
invoke_url = describe_stack_output(region, stack_name, "ParallelClusterApiInvokeUrl")
configuration = Configuration(host=invoke_url)

with ApiClient(configuration) as api_client:
client = cluster_operations_api.ClusterOperationsApi(api_client)
region_filter = region

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


def describe_stack_output(region: str, stack_name: str, output_name: str):
try:
# Describe stack
cloudformation = boto3.client("cloudformation", region_name=region)
response = cloudformation.describe_stacks(StackName=stack_name)

# Get the stack details
stacks = response.get("Stacks", [])
if not stacks:
print(f"No stacks found with the name: {stack_name}")
return None

# Extract output
outputs = stacks[0].get("Outputs", [])
return list(filter(lambda o: o['OutputKey'] == 'ParallelClusterApiInvokeUrl', outputs))[0]['OutputValue']

except Exception as e:
print(f"Cannot describe output '{output_name}' for stack '{stack_name}': {e}")
return None

if __name__ == "__main__":
request()
2 changes: 2 additions & 0 deletions api/client/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
boto3>=1.16.14
click~=8.1.7

0 comments on commit 2ec9592

Please sign in to comment.