Skip to content

Commit 29b6931

Browse files
Merge pull request #1483 from caberos/issue1482
add new email feature + `slcli email list` + `slcli email detail` + `slcli email edit`
2 parents 4694601 + 9de3dfc commit 29b6931

File tree

14 files changed

+423
-0
lines changed

14 files changed

+423
-0
lines changed

SoftLayer/CLI/email/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Network Delivery account"""

SoftLayer/CLI/email/detail.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Display details for a specified email account."""
2+
# :license: MIT, see LICENSE for more details.
3+
4+
import click
5+
from SoftLayer.CLI.email.list import build_statistics_table
6+
from SoftLayer.CLI import environment
7+
from SoftLayer.CLI import formatting
8+
from SoftLayer.managers.email import EmailManager
9+
from SoftLayer import utils
10+
11+
12+
@click.command()
13+
@click.argument('identifier')
14+
@environment.pass_env
15+
def cli(env, identifier):
16+
"""Display details for a specified email."""
17+
18+
email_manager = EmailManager(env.client)
19+
result = email_manager.get_instance(identifier)
20+
21+
table = formatting.KeyValueTable(['name', 'value'])
22+
table.align['name'] = 'r'
23+
table.align['value'] = 'l'
24+
25+
table.add_row(['id', result.get('id')])
26+
table.add_row(['username', result.get('username')])
27+
table.add_row(['email_address', result.get('emailAddress')])
28+
table.add_row(['create_date', result.get('createDate')])
29+
table.add_row(['category_code', utils.lookup(result, 'billingItem', 'categoryCode')])
30+
table.add_row(['description', utils.lookup(result, 'billingItem', 'description')])
31+
table.add_row(['type_description', utils.lookup(result, 'type', 'description')])
32+
table.add_row(['type', utils.lookup(result, 'type', 'keyName')])
33+
table.add_row(['vendor', utils.lookup(result, 'vendor', 'keyName')])
34+
35+
statistics = email_manager.get_statistics(identifier)
36+
37+
for statistic in statistics:
38+
table.add_row(['statistics', build_statistics_table(statistic)])
39+
40+
env.fout(table)

SoftLayer/CLI/email/edit.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""Edit details of an Delivery email account."""
2+
# :license: MIT, see LICENSE for more details.
3+
4+
import click
5+
6+
from SoftLayer.CLI import environment
7+
from SoftLayer.CLI import exceptions
8+
from SoftLayer.managers.email import EmailManager
9+
10+
11+
@click.command()
12+
@click.argument('identifier')
13+
@click.option('--username', help="Sets username for this account")
14+
@click.option('--email', help="Sets the contact email for this account")
15+
@click.option('--password',
16+
help="Password must be between 8 and 20 characters "
17+
"and must contain one letter and one number.")
18+
@environment.pass_env
19+
def cli(env, identifier, username, email, password):
20+
"""Edit details of an email delivery account."""
21+
email_manager = EmailManager(env.client)
22+
23+
data = {}
24+
update = False
25+
if email:
26+
if email_manager.update_email(identifier, email):
27+
update = True
28+
else:
29+
raise exceptions.CLIAbort("Failed to Edit emailAddress account")
30+
if username:
31+
data['username'] = username
32+
if password:
33+
data['password'] = password
34+
if len(data) != 0:
35+
if email_manager.editObject(identifier, **data):
36+
update = True
37+
else:
38+
raise exceptions.CLIAbort("Failed to Edit email account")
39+
40+
if update:
41+
env.fout('Updated Successfully')

SoftLayer/CLI/email/list.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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

SoftLayer/CLI/routes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@
119119
('block:volume-convert', 'SoftLayer.CLI.block.convert:cli'),
120120
('block:volume-set-note', 'SoftLayer.CLI.block.set_note:cli'),
121121

122+
('email', 'SoftLayer.CLI.email'),
123+
('email:list', 'SoftLayer.CLI.email.list:cli'),
124+
('email:detail', 'SoftLayer.CLI.email.detail:cli'),
125+
('email:edit', 'SoftLayer.CLI.email.edit:cli'),
126+
122127
('event-log', 'SoftLayer.CLI.event_log'),
123128
('event-log:get', 'SoftLayer.CLI.event_log.get:cli'),
124129
('event-log:types', 'SoftLayer.CLI.event_log.types:cli'),

SoftLayer/fixtures/SoftLayer_Account.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,3 +1076,27 @@
10761076
"username": "SL01SEV1234567_111"
10771077
}
10781078
]
1079+
1080+
getNetworkMessageDeliveryAccounts = [
1081+
{
1082+
"accountId": 147258,
1083+
"createDate": "2020-07-06T10:29:11-06:00",
1084+
"id": 1232123,
1085+
"typeId": 21,
1086+
"username": "test_CLI@ie.ibm.com",
1087+
"vendorId": 1,
1088+
"type": {
1089+
"description": "Delivery of messages through e-mail",
1090+
"id": 21,
1091+
"keyName": "EMAIL",
1092+
"name": "Email"
1093+
},
1094+
"vendor": {
1095+
"id": 1,
1096+
"keyName": "SENDGRID",
1097+
"name": "SendGrid"
1098+
},
1099+
"emailAddress": "test_CLI@ie.ibm.com",
1100+
"smtpAccess": "1"
1101+
}
1102+
]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
getAccountOverview = {
2+
"creditsAllowed": 25000,
3+
"creditsOverage": 0,
4+
"creditsRemain": 25000,
5+
"creditsUsed": 0,
6+
"package": "Free Package",
7+
"reputation": 100,
8+
"requests": 56
9+
}
10+
11+
getStatistics = [{
12+
"blocks": 0,
13+
"bounces": 0,
14+
"clicks": 0,
15+
"date": "2021-04-28",
16+
"delivered": 0,
17+
"invalidEmail": 0,
18+
"opens": 0,
19+
"repeatBounces": 0,
20+
"repeatSpamReports": 0,
21+
"repeatUnsubscribes": 0,
22+
"requests": 0,
23+
"spamReports": 0,
24+
"uniqueClicks": 0,
25+
"uniqueOpens": 0,
26+
"unsubscribes": 0
27+
}]
28+
29+
getObject = {
30+
"accountId": 123456,
31+
"createDate": "2020-07-06T10:29:11-06:00",
32+
"id": 1232123,
33+
"password": "Test123456789",
34+
"typeId": 21,
35+
"username": "techsupport3@ie.ibm.com",
36+
"vendorId": 1,
37+
"billingItem": {
38+
"categoryCode": "network_message_delivery",
39+
"description": "Free Package",
40+
"id": 695735054,
41+
"notes": "techsupport3@ie.ibm.com",
42+
},
43+
"type": {
44+
"description": "Delivery of messages through e-mail",
45+
"id": 21,
46+
"keyName": "EMAIL",
47+
"name": "Email"
48+
},
49+
"vendor": {
50+
"id": 1,
51+
"keyName": "SENDGRID",
52+
"name": "SendGrid"
53+
},
54+
"emailAddress": "techsupport3@ie.ibm.com",
55+
"smtpAccess": "1"
56+
}
57+
58+
editObject = True
59+
updateEmailAddress = True

SoftLayer/managers/account.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,13 @@ def get_routers(self, mask=None, location=None):
293293
}
294294

295295
return self.client['SoftLayer_Account'].getRouters(filter=object_filter, mask=mask)
296+
297+
def get_network_message_delivery_accounts(self):
298+
"""Gets all Network Message delivery accounts.
299+
300+
:returns: Network Message delivery accounts
301+
"""
302+
303+
_mask = """vendor,type"""
304+
305+
return self.client['SoftLayer_Account'].getNetworkMessageDeliveryAccounts(mask=_mask)

SoftLayer/managers/email.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""
2+
SoftLayer.email
3+
~~~~~~~~~~~~~~~~~~~~~~~
4+
Email manager
5+
6+
:license: MIT, see License for more details.
7+
"""
8+
9+
from SoftLayer import utils
10+
11+
12+
# Invalid names are ignored due to long method names and short argument names
13+
# pylint: disable=invalid-name, no-self-use
14+
15+
16+
class EmailManager(utils.IdentifierMixin, object):
17+
"""Common functions for getting information from the email service
18+
19+
:param SoftLayer.API.BaseClient client: the client instance
20+
"""
21+
22+
def __init__(self, client):
23+
self.client = client
24+
25+
def get_account_overview(self, identifier):
26+
"""Gets all the Network Message Delivery Account Overview
27+
28+
:returns: Network Message Delivery Account overview
29+
"""
30+
return self.client.call('SoftLayer_Network_Message_Delivery_Email_Sendgrid',
31+
'getAccountOverview', id=identifier)
32+
33+
def get_statistics(self, identifier, days=30):
34+
"""gets statistics from email accounts
35+
36+
:days: range number
37+
:returns: statistics Network Message Delivery Account
38+
"""
39+
body = [["requests", "delivered", "opens", "clicks", "bounds"],
40+
True,
41+
True,
42+
True,
43+
days
44+
]
45+
46+
return self.client.call('SoftLayer_Network_Message_Delivery_Email_Sendgrid',
47+
'getStatistics', id=identifier, *body)
48+
49+
def get_instance(self, identifier):
50+
"""Gets the Network_Message_Delivery_Email_Sendgrid instance
51+
52+
:return: Network_Message_Delivery_Email_Sendgrid
53+
"""
54+
55+
_mask = """emailAddress,type,billingItem,vendor"""
56+
57+
return self.client.call('SoftLayer_Network_Message_Delivery_Email_Sendgrid',
58+
'getObject', id=identifier, mask=_mask)
59+
60+
def editObject(self, identifier, username=None, password=None):
61+
"""Edit email delivery account related details.
62+
63+
:param int identifier: The ID of the email account
64+
:param string username: username of the email account.
65+
:param string email: email of the email account.
66+
:param string password: password of the email account to be updated to.
67+
"""
68+
data = {}
69+
if username:
70+
data['username'] = username
71+
if password:
72+
data['password'] = password
73+
74+
return self.client.call('SoftLayer_Network_Message_Delivery_Email_Sendgrid',
75+
'editObject', data, id=identifier)
76+
77+
def update_email(self, identifier, email):
78+
"""Edit email address delivery account .
79+
80+
:param int identifier: The ID of the email account
81+
:param string email: email of the email account.
82+
83+
"""
84+
return self.client.call('SoftLayer_Network_Message_Delivery_Email_Sendgrid',
85+
'updateEmailAddress', email, id=identifier)

docs/api/managers/email.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.. _email:
2+
3+
.. automodule:: SoftLayer.managers.email
4+
:members:
5+
:inherited-members:

0 commit comments

Comments
 (0)