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.

This commit is contained in:
Gareth J. Greenaway 2017-07-03 17:44:00 -07:00
parent 366bed8998
commit 8b507b1635
5 changed files with 52 additions and 10 deletions

View File

@ -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])

View File

@ -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]

View File

@ -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'

View File

@ -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

View File

@ -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):
'''