Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Commit

Permalink
fix: make mailjet_rest package optional
Browse files Browse the repository at this point in the history
  • Loading branch information
jf-marquis-Adeo authored and ttdt committed Oct 11, 2019
1 parent dda7b82 commit b7c3171
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 14 deletions.
6 changes: 3 additions & 3 deletions google/cloud/forseti/common/util/email/email_factory.py
Expand Up @@ -16,16 +16,16 @@

from builtins import object
from google.cloud.forseti.common.util import logger
from google.cloud.forseti.common.util.email import sendgrid_connector
from google.cloud.forseti.common.util.email import mailjet_connector
from google.cloud.forseti.common.util.email import sendgrid_connector
from google.cloud.forseti.common.util.errors import InvalidInputError


LOGGER = logger.get_logger(__name__)

EMAIL_CONNECTOR_FACTORY = {
'sendgrid': sendgrid_connector.SendgridConnector,
'mailjet': mailjet_connector.MailjetConnector
'mailjet': mailjet_connector.MailjetConnector,
'sendgrid': sendgrid_connector.SendgridConnector
}


Expand Down
15 changes: 11 additions & 4 deletions google/cloud/forseti/common/util/email/mailjet_connector.py
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Email utility module."""
"""Mailjet email connector module."""

# The pre-commit linter will complain about useless disable of no-member, but
# this is needed because quiet the Sendgrid no-member error on Travis.
Expand All @@ -25,9 +25,6 @@
from future import standard_library
from requests import Response
from retrying import retry

from mailjet_rest import Client

from google.cloud.forseti.common.util import errors as util_errors
from google.cloud.forseti.common.util import logger
from google.cloud.forseti.common.util import retryable_exceptions
Expand All @@ -37,6 +34,16 @@

LOGGER = logger.get_logger(__name__)

try:
from mailjet_rest import Client
MAILJET_ENABLED = True
except ImportError:
LOGGER.warning('Cannot enable Mailjet connector because the '
'`mailjet_rest` library was not found. Run '
'`sudo pip3 install mailjet_rest` to install '
'Mailjet.')
MAILJET_ENABLED = False


class Attachment:
"""Mailjet attachment."""
Expand Down
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Email utility module."""
"""Sendgrid email connector module."""

# The pre-commit linter will complain about useless disable of no-member, but
# this is needed because quiet the Sendgrid no-member error on Travis.
Expand Down
4 changes: 3 additions & 1 deletion setup.py
Expand Up @@ -40,7 +40,6 @@
'google-auth-httplib2==0.0.3',
'Jinja2==2.10.1',
'jmespath==0.9.3',
'mailjet-rest==1.3.3',
'netaddr==0.7.19',
'pyyaml==4.2b4',
'python-graph-core==1.8.2',
Expand Down Expand Up @@ -68,6 +67,9 @@
OPTIONAL_PACKAGES = {
'profiler': [
'google-cloud-profiler==1.0.8'
],
'mailjet': [
'mailjet-rest==1.3.3'
]
}

Expand Down
8 changes: 6 additions & 2 deletions tests/common/util/mailjet_connector_test.py
Expand Up @@ -12,22 +12,26 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests for the Email utility."""
"""Tests for the Mailjet email connector module."""
import tempfile
import unittest
from base64 import b64decode
from unittest.mock import patch, call
from urllib.error import HTTPError

from google.cloud.forseti.common.util.email import mailjet_connector
from google.cloud.forseti.common.util.email.mailjet_connector import MailjetConnector, Attachment
from google.cloud.forseti.common.util.errors import EmailSendError
from tests.unittest_utils import ForsetiTestCase


class MailjetConnectorTest(ForsetiTestCase):
"""Tests for the Email utility."""
"""Tests for the Mailjet email connector module."""

def setUp(self):
if not mailjet_connector.MAILJET_ENABLED:
self.skipTest('Package `mailjet` not installed.')

self.connector = MailjetConnector(
sender="this field is useless",
recipient="this field is also useless",
Expand Down
4 changes: 2 additions & 2 deletions tests/common/util/sendgrid_connector_test.py
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests for the Email utility."""
"""Tests for the Sendgrid email connector module."""

import unittest.mock as mock
import unittest
Expand All @@ -25,7 +25,7 @@


class SendgridConnectorTest(ForsetiTestCase):
"""Tests for the Email utility."""
"""Tests for the Sendgrid email connector module."""

def test_can_send_email_to_single_recipient(self):
"""Test can send email to single recipient."""
Expand Down
3 changes: 2 additions & 1 deletion tests/notifier/notifiers/email_factory_test.py
Expand Up @@ -14,7 +14,8 @@

"""Tests for Email Factory"""

from google.cloud.forseti.common.util.email import email_factory, mailjet_connector
from google.cloud.forseti.common.util.email import email_factory
from google.cloud.forseti.common.util.email import mailjet_connector
from google.cloud.forseti.common.util.email import sendgrid_connector
from google.cloud.forseti.common.util.errors import InvalidInputError
from tests.unittest_utils import ForsetiTestCase
Expand Down

0 comments on commit b7c3171

Please sign in to comment.