|
| 1 | +"""Lists Email Delivery Service """ |
| 2 | +# :license: MIT, see LICENSE for more details. |
| 3 | + |
| 4 | +import click |
| 5 | + |
| 6 | +from SoftLayer.CLI import environment |
| 7 | +from SoftLayer.CLI import formatting |
| 8 | +from SoftLayer.managers.account import AccountManager |
| 9 | +from SoftLayer.managers.email import EmailManager |
| 10 | +from SoftLayer import utils |
| 11 | + |
| 12 | + |
| 13 | +@click.command() |
| 14 | +@environment.pass_env |
| 15 | +def cli(env): |
| 16 | + """Lists Email Delivery Service """ |
| 17 | + manager = AccountManager(env.client) |
| 18 | + email_manager = EmailManager(env.client) |
| 19 | + result = manager.get_network_message_delivery_accounts() |
| 20 | + |
| 21 | + table = formatting.KeyValueTable(['name', 'value']) |
| 22 | + table.align['name'] = 'r' |
| 23 | + table.align['value'] = 'l' |
| 24 | + table_information = formatting.KeyValueTable(['id', 'username', 'hostname', 'description', 'vendor']) |
| 25 | + table_information.align['id'] = 'r' |
| 26 | + table_information.align['username'] = 'l' |
| 27 | + |
| 28 | + for email in result: |
| 29 | + table_information.add_row([email.get('id'), email.get('username'), email.get('emailAddress'), |
| 30 | + utils.lookup(email, 'type', 'description'), |
| 31 | + utils.lookup(email, 'vendor', 'keyName')]) |
| 32 | + |
| 33 | + overview_table = _build_overview_table(email_manager.get_account_overview(email.get('id'))) |
| 34 | + statistics = email_manager.get_statistics(email.get('id')) |
| 35 | + |
| 36 | + table.add_row(['email_information', table_information]) |
| 37 | + table.add_row(['email_overview', overview_table]) |
| 38 | + for statistic in statistics: |
| 39 | + table.add_row(['statistics', build_statistics_table(statistic)]) |
| 40 | + |
| 41 | + env.fout(table) |
| 42 | + |
| 43 | + |
| 44 | +def _build_overview_table(email_overview): |
| 45 | + table = formatting.Table( |
| 46 | + ['credit_allowed', 'credits_remain', 'credits_overage', 'credits_used', |
| 47 | + 'package', 'reputation', 'requests']) |
| 48 | + table.align['name'] = 'r' |
| 49 | + table.align['value'] = 'l' |
| 50 | + |
| 51 | + table.add_row([email_overview.get('creditsAllowed'), email_overview.get('creditsRemain'), |
| 52 | + email_overview.get('creditsOverage'), email_overview.get('creditsUsed'), |
| 53 | + email_overview.get('package'), email_overview.get('reputation'), |
| 54 | + email_overview.get('requests')]) |
| 55 | + |
| 56 | + return table |
| 57 | + |
| 58 | + |
| 59 | +def build_statistics_table(statistics): |
| 60 | + """statistics records of Email Delivery account""" |
| 61 | + table = formatting.Table(['delivered', 'requests', 'bounces', 'opens', 'clicks', 'spam_reports']) |
| 62 | + table.align['name'] = 'r' |
| 63 | + table.align['value'] = 'l' |
| 64 | + |
| 65 | + table.add_row([statistics.get('delivered'), statistics.get('requests'), |
| 66 | + statistics.get('bounces'), statistics.get('opens'), |
| 67 | + statistics.get('clicks'), statistics.get('spamReports')]) |
| 68 | + |
| 69 | + return table |
0 commit comments