Skip to content

Commit

Permalink
add multi region support
Browse files Browse the repository at this point in the history
  • Loading branch information
janschumann committed Apr 5, 2020
1 parent e671808 commit a45096d
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions AutoscalingLifecycle/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,23 @@ class ClientFactory(object):
"""


def __init__(self, session: boto3.Session, logger: Logger):
def __init__(self, session: boto3.Session, default_region: str, logger: Logger):
"""
:param session: A boto3 boto3.Session instamce
:type session: boto3.Session
:param default_region: The default region to create clients in
:type default_region: str
:param logger: A LifecycleLogger instance
:type logger: LifecycleLogger
"""
self.session = session
self.logger = logger
self.default_region = default_region
self.clients = {}


def get(self, name: str, region_name: str = 'eu-central-1'):
def get(self, name: str, region_name: str = ''):
"""
Get a boto client. Clients will be cached locally.
E.g. get_client('ssm') will return boto3.client('ssm')
Expand All @@ -45,6 +48,9 @@ def get(self, name: str, region_name: str = 'eu-central-1'):
:return: Service client instance
"""

if region_name == '':
region_name = self.default_region

self.logger.debug('Retrieving client %s in region %s', name, region_name)
key = name + '_' + region_name
client = self.clients.get(key, None)
Expand Down Expand Up @@ -302,14 +308,16 @@ def add_client_spec(self, name, cls, *args):
})


def get(self, name):
def get(self, name, region = ''):
spec = self.__client_specs.get(name, None)
if spec is None:
raise RuntimeError("no specs for %s found" % name)

client = self.__clients.get('name', None)
cname = '{}-{}'.format(name, region)
client = self.__clients.get(cname, None)
if client is None:
client = spec.get('class')(self.client_factory.get(name), self.waiters, self.logging, *spec.get('args'))
client = spec.get('class')(self.client_factory.get(name, region), self.waiters, self.logging, *spec.get('args'))
self.__clients.update({cname: client})

return client

Expand Down Expand Up @@ -844,3 +852,15 @@ def send_command(self, instance_ids, comment, commands, timeout_in_seconds = 60)
self.logger.debug('Command "%s" on instance %s is running: %s', comment, instance_ids, command_id)

return command_id


class SecretsmanagerClient(BaseClient):

def get_current_secret_string(self, secret_id):
self.logger.debug('Secretsmanager: Fetch secret: %s', secret_id)
secret = self.client.get_secret_value(SecretId=secret_id)
secret_string = secret.get("SecretString", "")
if secret_string == "":
self.logger.warning('Secretsmanager: Could not fetch secret: %s', secret_id)

return secret_string

0 comments on commit a45096d

Please sign in to comment.