Add Email/URL address validator to python monasca-api

Using validate_email library for checking invalid email address.

Change-Id: Ic03841fb66e9d2baf346ce6967be81f9128048c1
This commit is contained in:
Shinya Kawabata 2016-01-13 16:49:10 +09:00
parent 9970b6275b
commit 68e8b7d835
3 changed files with 42 additions and 12 deletions

View File

@ -213,12 +213,10 @@ class TestNotificationValidation(unittest.TestCase):
self.fail("shouldn't happen")
def test_validation_exception_for_email(self):
# ToDo: this function is not implemented yet.
# notification = {"name": "MyEmail", "type": "EMAIL", "address": "name@domain."}
# self.assertRaises(
# schemas_exceptions.ValidationException,
# schemas_notifications.validate, notification)
pass
notification = {"name": "MyEmail", "type": "EMAIL", "address": "name@domain."}
self.assertRaises(
schemas_exceptions.ValidationException,
schemas_notifications.validate, notification)
def test_validation_for_webhook(self):
notification = {"name": "MyWebhook", "type": "WEBHOOK", "address": "http://somedomain.com"}
@ -228,12 +226,10 @@ class TestNotificationValidation(unittest.TestCase):
self.fail("shouldn't happen")
def test_validation_exception_for_webhook(self):
# ToDo: this function is not implemented yet.
# notification = {"name": "MyWebhook", "type": "WEBHOOK", "address": "ftp://localhost"}
# self.assertRaises(
# schemas_exceptions.ValidationException,
# schemas_notifications.validate, notification)
pass
notification = {"name": "MyWebhook", "type": "WEBHOOK", "address": "ftp://localhost"}
self.assertRaises(
schemas_exceptions.ValidationException,
schemas_notifications.validate, notification)
def test_validation_for_pagerduty(self):
notification = {"name": "MyPagerduty", "type": "PAGERDUTY",

View File

@ -13,11 +13,21 @@
# under the License.
from oslo_log import log
import sys
from validate_email import validate_email
import voluptuous
from monasca_api.v2.common.schemas import exceptions
if sys.version_info >= (3,):
import urllib.parse as urlparse
else:
import urlparse
LOG = log.getLogger(__name__)
ADDRESS_ERR_MESSAGE = "Address {} is not of correct format"
schemes = ['http', 'https']
notification_schema = {
voluptuous.Required('name'): voluptuous.Schema(
@ -39,3 +49,26 @@ def validate(msg):
except Exception as ex:
LOG.debug(ex)
raise exceptions.ValidationException(str(ex))
notification_type = str(msg['type']).upper()
if notification_type == 'EMAIL':
_validate_email(msg['address'])
elif notification_type == 'WEBHOOK':
_validate_url(msg['address'])
def _validate_email(address):
if not validate_email(address):
raise exceptions.ValidationException(ADDRESS_ERR_MESSAGE.format(address))
def _validate_url(address):
try:
parsed = urlparse.urlparse(address)
except:
raise exceptions.ValidationException(ADDRESS_ERR_MESSAGE.format(address))
if not parsed.scheme or not parsed.netloc:
raise exceptions.ValidationException(ADDRESS_ERR_MESSAGE.format(address))
if parsed.scheme not in schemes:
raise exceptions.ValidationException(ADDRESS_ERR_MESSAGE.format(address))

View File

@ -21,3 +21,4 @@ eventlet
kafka-python>=0.9.2,<0.9.3
simplejson
simport
validate_email>=1.3