|
| 1 | +import boto3 |
| 2 | +import os |
| 3 | +import time |
| 4 | + |
| 5 | +class AmazonMQBrokerLogic: |
| 6 | + |
| 7 | + def __init__(self, broker_name): |
| 8 | + self.broker_name = broker_name |
| 9 | + |
| 10 | + def create(self, multi_az, instance_type, user, password, security_groups, subnets): |
| 11 | + print(f"Creating AMQ instance {self.broker_name}") |
| 12 | + deployment_mode = "ACTIVE_STANDBY_MULTI_AZ" if multi_az.lower() == "true" else "SINGLE_INSTANCE" |
| 13 | + |
| 14 | + client = boto3.client('mq') |
| 15 | + response = client.create_broker( |
| 16 | + AutoMinorVersionUpgrade=False, |
| 17 | + BrokerName=self.broker_name, |
| 18 | + DeploymentMode=deployment_mode, |
| 19 | + EngineType='ACTIVEMQ', |
| 20 | + EngineVersion='5.15.0', |
| 21 | + HostInstanceType=instance_type, |
| 22 | + PubliclyAccessible=False, |
| 23 | + SecurityGroups=security_groups, |
| 24 | + SubnetIds=subnets, |
| 25 | + Users=[ |
| 26 | + { |
| 27 | + 'ConsoleAccess': True, |
| 28 | + 'Password': password, |
| 29 | + 'Username': user |
| 30 | + } |
| 31 | + ] |
| 32 | + ) |
| 33 | + print(f"Broker Id: {response['BrokerId']} Broker Arn: {response['BrokerArn']}") |
| 34 | + active = self.endpoint(response['BrokerId'],1) |
| 35 | + response.update({'Active': active}) |
| 36 | + |
| 37 | + standby = self.endpoint(response['BrokerId'],2) if multi_az.lower() == "true" else "NONE" |
| 38 | + response.update({'Standby': standby}) |
| 39 | + |
| 40 | + print(f"Creating Amazon MQ instance\n{response}") |
| 41 | + return response |
| 42 | + |
| 43 | + def wait_broker_status(self, id, lambda_context): |
| 44 | + client = boto3.client('mq') |
| 45 | + |
| 46 | + while True: |
| 47 | + response = client.describe_broker(BrokerId=id) |
| 48 | + state = response['BrokerState'] |
| 49 | + |
| 50 | + print(f"Broker state: {state}") |
| 51 | + if state == 'RUNNING': |
| 52 | + print(f"Matched {state} - OK ") |
| 53 | + return True |
| 54 | + elif state == 'CREATION_FAILED': |
| 55 | + print(f"Matched {state} - ERROR ") |
| 56 | + return False |
| 57 | + elif lambda_context.get_remaining_time_in_millis() < 10000: |
| 58 | + print(f"Less than 10 seconds left of Lambda execution time, exiting with empty hands") |
| 59 | + return None |
| 60 | + else: |
| 61 | + print(f"Waiting for 5 seconds, time remaining" + |
| 62 | + f"in this lambda execution {lambda_context.get_remaining_time_in_millis()}ms") |
| 63 | + time.sleep(5) |
| 64 | + |
| 65 | + def compare_broker_properites(self, id, properties): |
| 66 | + client = boto3.client('mq') |
| 67 | + response = client.describe_broker(BrokerId=id) |
| 68 | + |
| 69 | + deployment_mode = "ACTIVE_STANDBY_MULTI_AZ" if properties['MultiAZ'].lower() == "true" else "SINGLE_INSTANCE" |
| 70 | + |
| 71 | + if (properties['SecurityGroups'] == response['SecurityGroups']) and \ |
| 72 | + (properties['Subnets'] == response['SubnetIds']) and \ |
| 73 | + (properties['InstanceType'] == response['HostInstanceType']) and \ |
| 74 | + (deployment_mode == response['DeploymentMode']) and \ |
| 75 | + (properties['Name'] == response['BrokerName']): |
| 76 | + return True |
| 77 | + else: |
| 78 | + return False |
| 79 | + |
| 80 | + def get_broker_data(self, id, multi_az): |
| 81 | + data = {} |
| 82 | + client = boto3.client('mq') |
| 83 | + response = client.describe_broker(BrokerId=id) |
| 84 | + |
| 85 | + data.update({'BrokerId': response['BrokerId']}) |
| 86 | + data.update({'BrokerArn': response['BrokerArn']}) |
| 87 | + |
| 88 | + active = self.endpoint(response['BrokerId'],1) |
| 89 | + data.update({'Active': active}) |
| 90 | + |
| 91 | + standby = self.endpoint(response['BrokerId'],2) if multi_az.lower() == "true" else "NONE" |
| 92 | + data.update({'Standby': standby}) |
| 93 | + |
| 94 | + return data |
| 95 | + |
| 96 | + def delete(self,id): |
| 97 | + client = boto3.client('mq') |
| 98 | + client.delete_broker( |
| 99 | + BrokerId=id |
| 100 | + ) |
| 101 | + |
| 102 | + def endpoint(self,id,n): |
| 103 | + return f"{id}-{n}.mq.{os.environ['AWS_REGION']}.amazonaws.com" |
0 commit comments