From eb71862449b93f496c678d892e0c0ad827278136 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Mon, 22 May 2023 16:35:31 +0100 Subject: [PATCH] Sometimes the first page does not have any results. Try next page if there's a next token. Signed-off-by: Pedro Algarvio --- tools/vm.py | 75 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/tools/vm.py b/tools/vm.py index 944f2fe6cc2..f7b2837ae1b 100644 --- a/tools/vm.py +++ b/tools/vm.py @@ -720,41 +720,50 @@ class VM: client = boto3.client("ec2", region_name=self.region_name) # Let's search for the launch template corresponding to this AMI launch_template_name = None + next_token = "" try: - response = response = client.describe_launch_templates( - Filters=[ - { - "Name": "tag:spb:is-golden-image-template", - "Values": ["true"], - }, - { - "Name": "tag:spb:project", - "Values": ["salt-project"], - }, - { - "Name": "tag:spb:environment", - "Values": [environment], - }, - { - "Name": "tag:spb:image-id", - "Values": [self.config.ami], - }, - ] - ) - log.debug( - "Search for launch template response:\n%s", pprint.pformat(response) - ) - for details in response.get("LaunchTemplates"): - if launch_template_name is not None: - log.warning( - "Multiple launch templates for the same AMI. This is not " - "supposed to happen. Picked the first one listed: %s", - response, - ) - break - launch_template_name = details["LaunchTemplateName"] + while True: + response = response = client.describe_launch_templates( + Filters=[ + { + "Name": "tag:spb:is-golden-image-template", + "Values": ["true"], + }, + { + "Name": "tag:spb:project", + "Values": ["salt-project"], + }, + { + "Name": "tag:spb:environment", + "Values": [environment], + }, + { + "Name": "tag:spb:image-id", + "Values": [self.config.ami], + }, + ], + NextToken=next_token, + ) + log.debug( + "Search for launch template response:\n%s", + pprint.pformat(response), + ) + for details in response.get("LaunchTemplates"): + if launch_template_name is not None: + log.warning( + "Multiple launch templates for the same AMI. This is not " + "supposed to happen. Picked the first one listed: %s", + response, + ) + break + launch_template_name = details["LaunchTemplateName"] - if launch_template_name is None: + if launch_template_name is not None: + break + + next_token = response.get("NextToken") + if next_token: + continue self.ctx.error(f"Could not find a launch template for {self.name!r}") self.ctx.exit(1) except ClientError as exc: