blob: ce857cdcc9a76a38923e8b4aa0875a7f31210c3d [file] [log] [blame]
import datetime
import hashlib
import logging
import os
from exceptions import SfNotifierError
from settings import (ALLOWED_HASHING, CONFIG_FIELD_MAP,
FEED_UPDATE_INTERVAL)
logger = logging.getLogger(__name__)
class SalesforceMixin(object):
@staticmethod
def _hash_func():
name = os.environ.get('SF_NOTIFIER_ALERT_ID_HASH_FUNC', 'sha256')
if name in ALLOWED_HASHING:
return getattr(hashlib, name)
return hashlib.sha256
@staticmethod
def _feed_update_ready(update_time):
now = datetime.datetime.now()
feed_update_interval = datetime.timedelta(hours=FEED_UPDATE_INTERVAL)
return now - update_time >= feed_update_interval
@staticmethod
def _validate_config(config):
kwargs = {}
for param, field in CONFIG_FIELD_MAP.iteritems():
setting_var = param.upper()
env_var = 'SFDC_{}'.format(setting_var)
kwargs[field] = os.environ.get(
env_var, config.get(setting_var))
if field == 'domain':
if kwargs[field] in ['true', 'True', True]:
kwargs[field] = 'test'
else:
del kwargs[field]
continue
if kwargs[field] is None:
msg = ('Invalid config: missing "{}" field or "{}" environment'
' variable.').format(param, env_var)
logger.error(msg)
raise SfNotifierError(msg)
return kwargs
@staticmethod
def _load_session(session_file):
lines = session_file.readlines()
if lines == []:
return
return lines[0]
@staticmethod
def _fmt_alert_update(case_id, error_code, error_msg=None, now=None):
now = now or datetime.datetime.now()
row = {
'id': case_id,
'error_code': error_code,
'last_update': now
}
if error_msg is not None:
row.update({'error': error_msg})
return row
def get_alert_id(self, labels, hash_func=None):
hash_func = hash_func or self.hash_func
alert_id_data = ''
for key in sorted(labels):
alert_id_data += labels[key].replace(".", "\\.")
return hash_func(alert_id_data).hexdigest()