|
| 1 | +"""Support module used throughout the ADF |
| 2 | +""" |
| 3 | +from enum import Enum |
| 4 | +from botocore.config import Config |
| 5 | +from botocore.exceptions import ClientError, BotoCoreError |
| 6 | +from logger import configure_logger |
| 7 | +from .account import Account |
| 8 | + |
| 9 | + |
| 10 | +LOGGER = configure_logger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +class SupportLevel(Enum): |
| 14 | + BASIC = "basic" |
| 15 | + DEVELOPER = "developer" |
| 16 | + BUSINESS = "business" |
| 17 | + ENTERPRISE = "enterprise" |
| 18 | + |
| 19 | + |
| 20 | +class Support: # pylint: disable=R0904 |
| 21 | + """Class used for accessing AWS Support API |
| 22 | + """ |
| 23 | + _config = Config(retries=dict(max_attempts=30)) |
| 24 | + |
| 25 | + def __init__(self, role): |
| 26 | + self.client = role.client("support", region_name='us-east-1', config=Support._config) |
| 27 | + |
| 28 | + def get_support_level(self) -> SupportLevel: |
| 29 | + """ |
| 30 | + Gets the AWS Support Level of the current Account |
| 31 | + based on the Role passed in during the init of the Support class. |
| 32 | +
|
| 33 | + :returns: |
| 34 | + SupportLevels Enum defining the level of AWS support. |
| 35 | +
|
| 36 | + :raises: |
| 37 | + ClientError |
| 38 | + BotoCoreError |
| 39 | +
|
| 40 | + """ |
| 41 | + try: |
| 42 | + severity_levels = self.client.get_severity_levels()['severityLevels'] |
| 43 | + available_support_codes = [level['code'] for level in severity_levels] |
| 44 | + |
| 45 | + # See: https://aws.amazon.com/premiumsupport/plans/ for insights into the interpretation of |
| 46 | + # the available support codes. |
| 47 | + |
| 48 | + if 'critical' in available_support_codes: # Business Critical System Down Severity |
| 49 | + return SupportLevel.ENTERPRISE |
| 50 | + if 'urgent' in available_support_codes: # Production System Down Severity |
| 51 | + return SupportLevel.BUSINESS |
| 52 | + if 'low' in available_support_codes: # System Impaired Severity |
| 53 | + return SupportLevel.DEVELOPER |
| 54 | + |
| 55 | + return SupportLevel.BASIC |
| 56 | + |
| 57 | + except (ClientError, BotoCoreError) as e: |
| 58 | + if e.response["Error"]["Code"] == "SubscriptionRequiredException": |
| 59 | + LOGGER.info('Enterprise Support is not enabled') |
| 60 | + return SupportLevel.BASIC |
| 61 | + raise |
| 62 | + |
| 63 | + def set_support_level_for_account(self, account: Account, account_id: str, current_level: SupportLevel = SupportLevel.BASIC): |
| 64 | + """ |
| 65 | + Sets the support level for the account. If the current_value is the same as the value in the instance |
| 66 | + of the account Class it will not create a new ticket. |
| 67 | +
|
| 68 | + Currently only supports "basic|enterprise" tiers. |
| 69 | +
|
| 70 | + :param account: Instance of Account class |
| 71 | + :param account_id: AWS Account ID of the account that will have support configured for it. |
| 72 | + :param current_level: SupportLevel value that represents the current support tier of the account (Default: Basic) |
| 73 | + :return: Void |
| 74 | + :raises: ValueError if account.support_level is not a valid/supported SupportLevel. |
| 75 | + """ |
| 76 | + desired_level = SupportLevel(account.support_level) |
| 77 | + |
| 78 | + if desired_level is current_level: |
| 79 | + LOGGER.info(f'Account {account.full_name} ({account_id}) already has {desired_level.value} support enabled.') |
| 80 | + |
| 81 | + elif desired_level is SupportLevel.ENTERPRISE: |
| 82 | + LOGGER.info(f'Enabling {desired_level.value} for Account {account.full_name} ({account_id})') |
| 83 | + self._enable_support_for_account(account, account_id, desired_level) |
| 84 | + |
| 85 | + else: |
| 86 | + LOGGER.error(f'Invalid support tier configured: {desired_level.value}. ' |
| 87 | + f'Currently only "{SupportLevel.BASIC.value}" or "{SupportLevel.ENTERPRISE.value}" ' |
| 88 | + 'are accepted.', exc_info=True) |
| 89 | + raise ValueError(f'Invalid Support Tier Value: {desired_level.value}') |
| 90 | + |
| 91 | + def _enable_support_for_account(self, account: Account, account_id, desired_level: SupportLevel): |
| 92 | + """ |
| 93 | + Raises a support ticket in the organization root account, enabling support for the account specified |
| 94 | + by account_id. |
| 95 | +
|
| 96 | + :param account: Instance of Account class |
| 97 | + :param account_id: AWS Account ID, of the account that will have support configured |
| 98 | + :param desired_level: Desired Support Level |
| 99 | + :return: Void |
| 100 | + :raises: ClientError, BotoCoreError. |
| 101 | + """ |
| 102 | + try: |
| 103 | + cc_email = account.email |
| 104 | + subject = f'[ADF] Enable {desired_level.value} Support for account: {account_id}' |
| 105 | + body = ( |
| 106 | + f'Hello, \n' |
| 107 | + f'Can {desired_level.value} support be enabled on Account: {account_id} ({account.email}) \n' |
| 108 | + 'Thank you!\n' |
| 109 | + '(This ticket was raised automatically via ADF)' |
| 110 | + |
| 111 | + ) |
| 112 | + LOGGER.info(f'Creating AWS Support ticket. {desired_level.value} Support for Account ' |
| 113 | + f'{account.full_name}({account_id})') |
| 114 | + |
| 115 | + response = self.client.create_case( |
| 116 | + subject=subject, |
| 117 | + serviceCode='account-management', |
| 118 | + severityCode='low', |
| 119 | + categoryCode='billing', |
| 120 | + communicationBody=body, |
| 121 | + ccEmailAddresses=[ |
| 122 | + cc_email, |
| 123 | + ], |
| 124 | + language='en', |
| 125 | + ) |
| 126 | + |
| 127 | + LOGGER.info(f'AWS Support ticket: {response["caseId"]} ' |
| 128 | + f'has been created. {desired_level.value} Support has ' |
| 129 | + f'been requested on Account {account.full_name} ({account_id}). ' |
| 130 | + f'{account.email} has been CCd') |
| 131 | + |
| 132 | + except (ClientError, BotoCoreError): |
| 133 | + LOGGER.error(f'Failed to enable {desired_level.value} support for account: ' |
| 134 | + f'{account.full_name} ({account.alias}): {account_id}', exc_info=True) |
| 135 | + raise |
0 commit comments