From 8b507b1635068ebbc1611df57e2f2b31f7d954be Mon Sep 17 00:00:00 2001 From: "Gareth J. Greenaway" Date: Mon, 3 Jul 2017 17:44:00 -0700 Subject: [PATCH] Updating twilio_txt_msg & memusage beacons to ensure list based configuration. Adding/Updating unit tests for both. Updating twilio related things to support newer version of twilio python library. --- salt/beacons/__init__.py | 3 ++- salt/beacons/memusage.py | 7 ++++--- salt/beacons/twilio_txt_msg.py | 19 +++++++++++++++++-- salt/modules/twilio_notify.py | 16 +++++++++++++--- tests/unit/modules/test_twilio_notify.py | 17 ++++++++++++++++- 5 files changed, 52 insertions(+), 10 deletions(-) diff --git a/salt/beacons/__init__.py b/salt/beacons/__init__.py index f6d3c842d3..d41cf3d54d 100644 --- a/salt/beacons/__init__.py +++ b/salt/beacons/__init__.py @@ -97,7 +97,8 @@ class Beacon(object): # Update __grains__ on the beacon self.beacons[fun_str].__globals__['__grains__'] = grains - # Run the validate function + # Run the validate function if it's available, + # otherwise there is a warning about it being missing if validate_str in self.beacons: valid, vcomment = self.beacons[validate_str](b_config[mod]) diff --git a/salt/beacons/memusage.py b/salt/beacons/memusage.py index d779f9c58e..8bf8db415f 100644 --- a/salt/beacons/memusage.py +++ b/salt/beacons/memusage.py @@ -31,14 +31,14 @@ def __virtual__(): return __virtualname__ -def __validate__(config): +def validate(config): ''' Validate the beacon configuration ''' # Configuration for memusage beacon should be a list of dicts - if not isinstance(config, dict): + if not isinstance(config, list): return False, ('Configuration for memusage ' - 'beacon must be a dictionary.') + 'beacon must be a list.') return True, 'Valid beacon configuration' @@ -58,6 +58,7 @@ def beacon(config): for memusage in config: mount = memusage.keys()[0] _current_usage = psutil.virtual_memory() + log.debug('_current_usage {}'.format(_current_usage)) current_usage = _current_usage.percent monitor_usage = memusage[mount] diff --git a/salt/beacons/twilio_txt_msg.py b/salt/beacons/twilio_txt_msg.py index a0216b6385..cd0663e5df 100644 --- a/salt/beacons/twilio_txt_msg.py +++ b/salt/beacons/twilio_txt_msg.py @@ -9,7 +9,11 @@ import logging # Import 3rd Party libs try: - from twilio.rest import TwilioRestClient + import twilio + if twilio.__version__ > 5: + from twilio.rest import Client as TwilioRestClient + else: + from twilio.rest import TwilioRestClient HAS_TWILIO = True except ImportError: HAS_TWILIO = False @@ -33,7 +37,18 @@ def validate(config): # Configuration for twilio_txt_msg beacon should be a list of dicts if not isinstance(config, list): return False, ('Configuration for twilio_txt_msg beacon ' - 'must be a dictionary.') + 'must be a list.') + else: + _config = {} + list(map(_config.update, config)) + + log.debug('_config {}'.format(_config)) + if not all(x in _config for x in ('account_sid', + 'auth_token', + 'twilio_number')): + return False, ('Configuration for twilio_txt_msg beacon ' + 'must contain account_sid, auth_token ' + 'and twilio_number items.') return True, 'Valid beacon configuration' diff --git a/salt/modules/twilio_notify.py b/salt/modules/twilio_notify.py index 520a7c0cc3..334ecd167e 100644 --- a/salt/modules/twilio_notify.py +++ b/salt/modules/twilio_notify.py @@ -21,8 +21,15 @@ import logging HAS_LIBS = False try: - from twilio.rest import TwilioRestClient - from twilio import TwilioRestException + import twilio + if twilio.__version__ > 5: + TWILIO_5 = False + from twilio.rest import Client as TwilioRestClient + from twilio.rest import TwilioException as TwilioRestException + else: + TWILIO_5 = True + from twilio.rest import TwilioRestClient + from twilio import TwilioRestException HAS_LIBS = True except ImportError: pass @@ -67,7 +74,10 @@ def send_sms(profile, body, to, from_): ret['message']['sid'] = None client = _get_twilio(profile) try: - message = client.sms.messages.create(body=body, to=to, from_=from_) + if TWILIO_5: + message = client.sms.messages.create(body=body, to=to, from_=from_) + else: + message = client.messages.create(body=body, to=to, from_=from_) except TwilioRestException as exc: ret['_error'] = {} ret['_error']['code'] = exc.code diff --git a/tests/unit/modules/test_twilio_notify.py b/tests/unit/modules/test_twilio_notify.py index 6aaaaae573..370b45d690 100644 --- a/tests/unit/modules/test_twilio_notify.py +++ b/tests/unit/modules/test_twilio_notify.py @@ -19,6 +19,17 @@ from tests.support.mock import ( # Import Salt Libs import salt.modules.twilio_notify as twilio_notify +HAS_LIBS = False +try: + import twilio + if twilio.__version__ > 5: + TWILIO_5 = False + else: + TWILIO_5 = True + HAS_LIBS = True +except ImportError: + pass + class MockTwilioRestException(Exception): ''' @@ -75,9 +86,13 @@ class MockTwilioRestClient(object): Mock TwilioRestClient class ''' def __init__(self): - self.sms = MockSMS() + if TWILIO_5: + self.sms = MockSMS() + else: + self.messages = MockMessages() +@skipIf(not HAS_LIBS, 'twilio.rest is not available') @skipIf(NO_MOCK, NO_MOCK_REASON) class TwilioNotifyTestCase(TestCase, LoaderModuleMockMixin): '''