Skip to content

add new email feature #1483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SoftLayer/CLI/email/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Network Delivery account"""
40 changes: 40 additions & 0 deletions SoftLayer/CLI/email/detail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Display details for a specified email account."""
# :license: MIT, see LICENSE for more details.

import click
from SoftLayer.CLI.email.list import build_statistics_table
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting
from SoftLayer.managers.email import EmailManager
from SoftLayer import utils


@click.command()
@click.argument('identifier')
@environment.pass_env
def cli(env, identifier):
"""Display details for a specified email."""

email_manager = EmailManager(env.client)
result = email_manager.get_instance(identifier)

table = formatting.KeyValueTable(['name', 'value'])
table.align['name'] = 'r'
table.align['value'] = 'l'

table.add_row(['id', result.get('id')])
table.add_row(['username', result.get('username')])
table.add_row(['email_address', result.get('emailAddress')])
table.add_row(['create_date', result.get('createDate')])
table.add_row(['category_code', utils.lookup(result, 'billingItem', 'categoryCode')])
table.add_row(['description', utils.lookup(result, 'billingItem', 'description')])
table.add_row(['type_description', utils.lookup(result, 'type', 'description')])
table.add_row(['type', utils.lookup(result, 'type', 'keyName')])
table.add_row(['vendor', utils.lookup(result, 'vendor', 'keyName')])

statistics = email_manager.get_statistics(identifier)

for statistic in statistics:
table.add_row(['statistics', build_statistics_table(statistic)])

env.fout(table)
41 changes: 41 additions & 0 deletions SoftLayer/CLI/email/edit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Edit details of an Delivery email account."""
# :license: MIT, see LICENSE for more details.

import click

from SoftLayer.CLI import environment
from SoftLayer.CLI import exceptions
from SoftLayer.managers.email import EmailManager


@click.command()
@click.argument('identifier')
@click.option('--username', help="Sets username for this account")
@click.option('--email', help="Sets the contact email for this account")
@click.option('--password',
help="Password must be between 8 and 20 characters "
"and must contain one letter and one number.")
@environment.pass_env
def cli(env, identifier, username, email, password):
"""Edit details of an email delivery account."""
email_manager = EmailManager(env.client)

data = {}
update = False
if email:
if email_manager.update_email(identifier, email):
update = True
else:
raise exceptions.CLIAbort("Failed to Edit emailAddress account")
if username:
data['username'] = username
if password:
data['password'] = password
if len(data) != 0:
if email_manager.editObject(identifier, **data):
update = True
else:
raise exceptions.CLIAbort("Failed to Edit email account")

if update:
env.fout('Updated Successfully')
69 changes: 69 additions & 0 deletions SoftLayer/CLI/email/list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""Lists Email Delivery Service """
# :license: MIT, see LICENSE for more details.

import click

from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting
from SoftLayer.managers.account import AccountManager
from SoftLayer.managers.email import EmailManager
from SoftLayer import utils


@click.command()
@environment.pass_env
def cli(env):
"""Lists Email Delivery Service """
manager = AccountManager(env.client)
email_manager = EmailManager(env.client)
result = manager.get_network_message_delivery_accounts()

table = formatting.KeyValueTable(['name', 'value'])
table.align['name'] = 'r'
table.align['value'] = 'l'
table_information = formatting.KeyValueTable(['id', 'username', 'hostname', 'description', 'vendor'])
table_information.align['id'] = 'r'
table_information.align['username'] = 'l'

for email in result:
table_information.add_row([email.get('id'), email.get('username'), email.get('emailAddress'),
utils.lookup(email, 'type', 'description'),
utils.lookup(email, 'vendor', 'keyName')])

overview_table = _build_overview_table(email_manager.get_account_overview(email.get('id')))
statistics = email_manager.get_statistics(email.get('id'))

table.add_row(['email_information', table_information])
table.add_row(['email_overview', overview_table])
for statistic in statistics:
table.add_row(['statistics', build_statistics_table(statistic)])

env.fout(table)


def _build_overview_table(email_overview):
table = formatting.Table(
['credit_allowed', 'credits_remain', 'credits_overage', 'credits_used',
'package', 'reputation', 'requests'])
table.align['name'] = 'r'
table.align['value'] = 'l'

table.add_row([email_overview.get('creditsAllowed'), email_overview.get('creditsRemain'),
email_overview.get('creditsOverage'), email_overview.get('creditsUsed'),
email_overview.get('package'), email_overview.get('reputation'),
email_overview.get('requests')])

return table


def build_statistics_table(statistics):
"""statistics records of Email Delivery account"""
table = formatting.Table(['delivered', 'requests', 'bounces', 'opens', 'clicks', 'spam_reports'])
table.align['name'] = 'r'
table.align['value'] = 'l'

table.add_row([statistics.get('delivered'), statistics.get('requests'),
statistics.get('bounces'), statistics.get('opens'),
statistics.get('clicks'), statistics.get('spamReports')])

return table
5 changes: 5 additions & 0 deletions SoftLayer/CLI/routes.py
Original file line number Diff line number Diff line change
@@ -119,6 +119,11 @@
('block:volume-convert', 'SoftLayer.CLI.block.convert:cli'),
('block:volume-set-note', 'SoftLayer.CLI.block.set_note:cli'),

('email', 'SoftLayer.CLI.email'),
('email:list', 'SoftLayer.CLI.email.list:cli'),
('email:detail', 'SoftLayer.CLI.email.detail:cli'),
('email:edit', 'SoftLayer.CLI.email.edit:cli'),

('event-log', 'SoftLayer.CLI.event_log'),
('event-log:get', 'SoftLayer.CLI.event_log.get:cli'),
('event-log:types', 'SoftLayer.CLI.event_log.types:cli'),
24 changes: 24 additions & 0 deletions SoftLayer/fixtures/SoftLayer_Account.py
Original file line number Diff line number Diff line change
@@ -1076,3 +1076,27 @@
"username": "SL01SEV1234567_111"
}
]

getNetworkMessageDeliveryAccounts = [
{
"accountId": 147258,
"createDate": "2020-07-06T10:29:11-06:00",
"id": 1232123,
"typeId": 21,
"username": "[email protected]",
"vendorId": 1,
"type": {
"description": "Delivery of messages through e-mail",
"id": 21,
"keyName": "EMAIL",
"name": "Email"
},
"vendor": {
"id": 1,
"keyName": "SENDGRID",
"name": "SendGrid"
},
"emailAddress": "[email protected]",
"smtpAccess": "1"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
getAccountOverview = {
"creditsAllowed": 25000,
"creditsOverage": 0,
"creditsRemain": 25000,
"creditsUsed": 0,
"package": "Free Package",
"reputation": 100,
"requests": 56
}

getStatistics = [{
"blocks": 0,
"bounces": 0,
"clicks": 0,
"date": "2021-04-28",
"delivered": 0,
"invalidEmail": 0,
"opens": 0,
"repeatBounces": 0,
"repeatSpamReports": 0,
"repeatUnsubscribes": 0,
"requests": 0,
"spamReports": 0,
"uniqueClicks": 0,
"uniqueOpens": 0,
"unsubscribes": 0
}]

getObject = {
"accountId": 123456,
"createDate": "2020-07-06T10:29:11-06:00",
"id": 1232123,
"password": "Test123456789",
"typeId": 21,
"username": "[email protected]",
"vendorId": 1,
"billingItem": {
"categoryCode": "network_message_delivery",
"description": "Free Package",
"id": 695735054,
"notes": "[email protected]",
},
"type": {
"description": "Delivery of messages through e-mail",
"id": 21,
"keyName": "EMAIL",
"name": "Email"
},
"vendor": {
"id": 1,
"keyName": "SENDGRID",
"name": "SendGrid"
},
"emailAddress": "[email protected]",
"smtpAccess": "1"
}

editObject = True
updateEmailAddress = True
10 changes: 10 additions & 0 deletions SoftLayer/managers/account.py
Original file line number Diff line number Diff line change
@@ -293,3 +293,13 @@ def get_routers(self, mask=None, location=None):
}

return self.client['SoftLayer_Account'].getRouters(filter=object_filter, mask=mask)

def get_network_message_delivery_accounts(self):
"""Gets all Network Message delivery accounts.

:returns: Network Message delivery accounts
"""

_mask = """vendor,type"""

return self.client['SoftLayer_Account'].getNetworkMessageDeliveryAccounts(mask=_mask)
85 changes: 85 additions & 0 deletions SoftLayer/managers/email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""
SoftLayer.email
~~~~~~~~~~~~~~~~~~~~~~~
Email manager

:license: MIT, see License for more details.
"""

from SoftLayer import utils


# Invalid names are ignored due to long method names and short argument names
# pylint: disable=invalid-name, no-self-use


class EmailManager(utils.IdentifierMixin, object):
"""Common functions for getting information from the email service

:param SoftLayer.API.BaseClient client: the client instance
"""

def __init__(self, client):
self.client = client

def get_account_overview(self, identifier):
"""Gets all the Network Message Delivery Account Overview

:returns: Network Message Delivery Account overview
"""
return self.client.call('SoftLayer_Network_Message_Delivery_Email_Sendgrid',
'getAccountOverview', id=identifier)

def get_statistics(self, identifier, days=30):
"""gets statistics from email accounts

:days: range number
:returns: statistics Network Message Delivery Account
"""
body = [["requests", "delivered", "opens", "clicks", "bounds"],
True,
True,
True,
days
]

return self.client.call('SoftLayer_Network_Message_Delivery_Email_Sendgrid',
'getStatistics', id=identifier, *body)

def get_instance(self, identifier):
"""Gets the Network_Message_Delivery_Email_Sendgrid instance

:return: Network_Message_Delivery_Email_Sendgrid
"""

_mask = """emailAddress,type,billingItem,vendor"""

return self.client.call('SoftLayer_Network_Message_Delivery_Email_Sendgrid',
'getObject', id=identifier, mask=_mask)

def editObject(self, identifier, username=None, password=None):
"""Edit email delivery account related details.

:param int identifier: The ID of the email account
:param string username: username of the email account.
:param string email: email of the email account.
:param string password: password of the email account to be updated to.
"""
data = {}
if username:
data['username'] = username
if password:
data['password'] = password

return self.client.call('SoftLayer_Network_Message_Delivery_Email_Sendgrid',
'editObject', data, id=identifier)

def update_email(self, identifier, email):
"""Edit email address delivery account .

:param int identifier: The ID of the email account
:param string email: email of the email account.

"""
return self.client.call('SoftLayer_Network_Message_Delivery_Email_Sendgrid',
'updateEmailAddress', email, id=identifier)
5 changes: 5 additions & 0 deletions docs/api/managers/email.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.. _email:

.. automodule:: SoftLayer.managers.email
:members:
:inherited-members:
1 change: 1 addition & 0 deletions docs/cli.rst
Original file line number Diff line number Diff line change
@@ -79,6 +79,7 @@ To discover the available commands, simply type `slcli`.
config CLI configuration.
dedicatedhost Dedicated Host.
dns Domain Name System.
email Email Deliviry Network
event-log Event Logs.
file File Storage.
firewall Firewalls.
17 changes: 17 additions & 0 deletions docs/cli/email.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.. _cli_email:

Email Commands
=================


.. click:: SoftLayer.CLI.email.list:cli
:prog: email list
:show-nested:

.. click:: SoftLayer.CLI.email.detail:cli
:prog: email detail
:show-nested:

.. click:: SoftLayer.CLI.email.edit:cli
:prog: email edit
:show-nested:
Loading