diff contrib/automation/hgautomation/aws.py @ 42277:dd6a9723ae2b

automation: don't create resources when deleting things Otherwise running these commands can result in resources being created. In the case of `purge-ec2-resources`, we will create resources only to delete them immediately afterwards! With this change, `purge-ec2-resources` now no-ops if no resources exist. # no-check-commit because foo_bar function name Differential Revision: https://phab.mercurial-scm.org/D6285
author Gregory Szorc <gregory.szorc@gmail.com>
date Fri, 19 Apr 2019 05:20:33 -0700
parents fcb97cb91ff8
children 8dc22a209420
line wrap: on
line diff
--- a/contrib/automation/hgautomation/aws.py	Fri Apr 19 05:15:43 2019 -0700
+++ b/contrib/automation/hgautomation/aws.py	Fri Apr 19 05:20:33 2019 -0700
@@ -180,7 +180,7 @@
 class AWSConnection:
     """Manages the state of a connection with AWS."""
 
-    def __init__(self, automation, region: str):
+    def __init__(self, automation, region: str, ensure_ec2_state: bool=True):
         self.automation = automation
         self.local_state_path = automation.state_path
 
@@ -191,11 +191,12 @@
         self.ec2resource = self.session.resource('ec2')
         self.iamclient = self.session.client('iam')
         self.iamresource = self.session.resource('iam')
-
-        ensure_key_pairs(automation.state_path, self.ec2resource)
+        self.security_groups = {}
 
-        self.security_groups = ensure_security_groups(self.ec2resource)
-        ensure_iam_state(self.iamresource)
+        if ensure_ec2_state:
+            ensure_key_pairs(automation.state_path, self.ec2resource)
+            self.security_groups = ensure_security_groups(self.ec2resource)
+            ensure_iam_state(self.iamresource)
 
     def key_pair_path_private(self, name):
         """Path to a key pair private key file."""