Move connection handling failure to connect_to_aws

Make use of better error handling mechanism
This commit is contained in:
Will Thames 2015-07-27 11:44:05 +10:00 committed by Abhijit Menon-Sen
parent c08137a3d2
commit b0133d9c8f

View file

@ -165,8 +165,7 @@ class Ec2Inventory(object):
# as pre 2.24 boto will fall over otherwise # as pre 2.24 boto will fall over otherwise
if self.boto_profile: if self.boto_profile:
if not hasattr(boto.ec2.EC2Connection, 'profile_name'): if not hasattr(boto.ec2.EC2Connection, 'profile_name'):
sys.stderr.write("boto version must be >= 2.24 to use profile\n") self.fail_with_error("boto version must be >= 2.24 to use profile")
sys.exit(1)
# Cache # Cache
if self.args.refresh_cache: if self.args.refresh_cache:
@ -386,7 +385,6 @@ class Ec2Inventory(object):
continue continue
self.ec2_instance_filters[filter_key].append(filter_value) self.ec2_instance_filters[filter_key].append(filter_value)
def parse_cli_args(self): def parse_cli_args(self):
''' Command line argument processing ''' ''' Command line argument processing '''
@ -425,10 +423,7 @@ class Ec2Inventory(object):
conn = boto.connect_euca(host=self.eucalyptus_host) conn = boto.connect_euca(host=self.eucalyptus_host)
conn.APIVersion = '2010-08-31' conn.APIVersion = '2010-08-31'
else: else:
conn = ec2.connect_to_region(region) conn = self.connect_to_aws(ec2, region)
# connect_to_region will fail "silently" by returning None if the region name is wrong or not supported
if conn is None:
self.fail_with_error("region name: %s likely not supported, or AWS is down. connection to region failed." % region)
return conn return conn
def boto_fix_security_token_in_profile(self, connect_args): def boto_fix_security_token_in_profile(self, connect_args):
@ -438,7 +433,6 @@ class Ec2Inventory(object):
connect_args['security_token'] = boto.config.get(profile, 'aws_security_token') connect_args['security_token'] = boto.config.get(profile, 'aws_security_token')
return connect_args return connect_args
def connect_to_aws(self, module, region): def connect_to_aws(self, module, region):
connect_args = {} connect_args = {}
@ -448,9 +442,11 @@ class Ec2Inventory(object):
self.boto_fix_security_token_in_profile(connect_args) self.boto_fix_security_token_in_profile(connect_args)
conn = module.connect_to_region(region, **connect_args) conn = module.connect_to_region(region, **connect_args)
# connect_to_region will fail "silently" by returning None if the region name is wrong or not supported
if conn is None:
self.fail_with_error("region name: %s likely not supported, or AWS is down. connection to region failed." % region)
return conn return conn
def get_instances_by_region(self, region): def get_instances_by_region(self, region):
''' Makes an AWS EC2 API call to the list of instances in a particular ''' Makes an AWS EC2 API call to the list of instances in a particular
region ''' region '''
@ -462,14 +458,8 @@ class Ec2Inventory(object):
for filter_key, filter_values in self.ec2_instance_filters.items(): for filter_key, filter_values in self.ec2_instance_filters.items():
reservations.extend(conn.get_all_instances(filters = { filter_key : filter_values })) reservations.extend(conn.get_all_instances(filters = { filter_key : filter_values }))
else: else:
conn = ec2.connect_to_region(region, profile_name=self.boto_profile) reservations = conn.get_all_instances()
# connect_to_region will fail "silently" by returning None if the region name is wrong or not supported
if conn is None:
print("region name: %s likely not supported, or AWS is down. connection to region failed." % region)
sys.exit(1)
reservations = conn.get_all_instances()
for reservation in reservations: for reservation in reservations:
for instance in reservation.instances: for instance in reservation.instances:
self.add_instance(instance, region) self.add_instance(instance, region)
@ -482,7 +472,6 @@ class Ec2Inventory(object):
error = "Error connecting to %s backend.\n%s" % (backend, e.message) error = "Error connecting to %s backend.\n%s" % (backend, e.message)
self.fail_with_error(error, 'getting EC2 instances') self.fail_with_error(error, 'getting EC2 instances')
def get_rds_instances_by_region(self, region): def get_rds_instances_by_region(self, region):
''' Makes an AWS API call to the list of RDS instances in a particular ''' Makes an AWS API call to the list of RDS instances in a particular
region ''' region '''