예제 #1
0
def get_notifier_config(name):
    """
    Get the notifier config object from the notifier name
    :param name: the name of the notifier defined in the config file
    :rtype: stackdio.core.notifications.utils.NotifierConfig
    :return: the config object
    """
    if name not in notifier_configs:
        notifier_config = settings.STACKDIO_CONFIG.get('notifiers', {})

        if name not in notifier_config:
            raise StackdioConfigException('Notifier {} not found.'.format(name))

        ret = notifier_config[name]

        if not isinstance(ret, dict):
            raise StackdioConfigException('Notifier config for {} was not a dict.'.format(name))

        if 'class' not in ret:
            raise StackdioConfigException('Notifier config for {} is missing '
                                          'a `class` attribute.'.format(name))

        # options are optional, don't fail if they're not there
        options = ret.get('options', {})

        notifier_configs[name] = NotifierConfig(name, ret['class'], options)

    return notifier_configs[name]
예제 #2
0
def get_notifier_class(name):
    """
    Get the imported notifier class
    :param name: the notifier name
    :rtype: abc.ABCMeta
    :return: the notifier class object
    """
    if name not in notifier_classes:
        notifier_config = get_notifier_config(name)

        try:
            module_path, class_name = notifier_config.class_path.rsplit('.', 1)
            module = importlib.import_module(module_path)
            notifier_class = getattr(module, class_name)
        except (ImportError, ValueError):
            msg = 'Could not import notifier: {0}'.format(notifier_config.class_path)
            raise StackdioConfigException(msg)

        if not issubclass(notifier_class, BaseNotifier):
            raise StackdioConfigException('The provided class for {} was not a '
                                          'subclass of BaseNotifier.'.format(name))

        notifier_classes[name] = notifier_class

    return notifier_classes[name]
예제 #3
0
def validate_model_class(model_class):
    if not isinstance(model_class, ModelBase):
        raise StackdioConfigException('Object %r is not a Model class.' %
                                      model_class)
    if model_class._meta.abstract:
        raise StackdioConfigException(
            'The model %r is abstract, so it cannot be registered with '
            'actstream.' % model_class)
    if not model_class._meta.installed:
        raise StackdioConfigException(
            'The model %r is not installed, please put the app "%s" in your '
            'INSTALLED_APPS setting.' %
            (model_class, model_class._meta.app_label))
    return model_class
예제 #4
0
 def get_queryset(self):
     if settings.LDAP_ENABLED and 'username' in self.request.query_params:
         if LDAPBackend is None:
             raise StackdioConfigException('LDAP is enabled, but django_auth_ldap isn\'t '
                                           'installed.  Please install django_auth_ldap')
         # Try populating the user first
         LDAPBackend().populate_user(self.request.query_params['username'])
     return super(UserListAPIView, self).get_queryset()
예제 #5
0
    def __init__(self, slack_api_token, post_as_user=True):
        super(SlackNotifier, self).__init__()

        if SlackClient is None:
            raise StackdioConfigException('Could not load the slack client.  Be sure you have '
                                          'installed stackdio-server with the `slack` extra.  '
                                          '(pip install stackdio-server[slack])')

        self.slackclient = SlackClient(slack_api_token)
        self.post_as_user = post_as_user
예제 #6
0
 def get_from_ldap_module(attr, module=ldap, fail_on_error=False):
     try:
         return getattr(module, attr)
     except (AttributeError, TypeError):
         if fail_on_error:
             raise StackdioConfigException(
                 'Invalid config value: {}'.format(attr))
         else:
             # if we get an exception, just return the raw attribute
             return attr
예제 #7
0
    def get_ui_url(self, content_object):
        model_class = content_object._meta.model
        if model_class not in self:
            raise StackdioConfigException(
                'Model %r is not registered with the '
                'notification registry.' % model_class)

        url_name = self[model_class].url_name
        return reverse(url_name,
                       request=self.serializer_context['request'],
                       kwargs={'pk': content_object.pk})
예제 #8
0
def get_cloud_providers():
    check_cloud_provider_settings()

    providers = []
    for class_path in settings.CLOUD_PROVIDERS:
        try:
            module_path, class_name = class_path.rsplit('.', 1)
            module = importlib.import_module(module_path)
            providers.append(getattr(module, class_name))
        except ImportError as e:
            msg = 'Could not import {0} from settings.CLOUD_PROVIDERS'.format(
                class_path)
            logger.error(e)
            raise StackdioConfigException(msg)

    return providers
예제 #9
0
    def get_search_object(user_or_group):
        search_base = LDAP_CONFIG.get('{}_search_base'.format(user_or_group))
        if not search_base:
            raise StackdioConfigException(
                'Missing ldap.{}_search_base '
                'config parameter'.format(user_or_group))

        search_scope_str = LDAP_CONFIG.get(
            '{}_search_scope'.format(user_or_group), 'SCOPE_SUBTREE')
        search_scope = get_from_ldap_module(search_scope_str,
                                            fail_on_error=True)
        search_filter = LDAP_CONFIG.get(
            '{}_search_filter'.format(user_or_group))

        if search_filter is None:
            return LDAPSearch(search_base, search_scope)
        else:
            return LDAPSearch(search_base, search_scope, search_filter)
예제 #10
0
def check_cloud_provider_settings():
    if not hasattr(settings, 'CLOUD_PROVIDERS'):
        raise StackdioConfigException(
            'settings.CLOUD_PROVIDERS must set with a list of supported cloud providers.'
        )
예제 #11
0
def validate_serializer_class(serializer_class):
    if not issubclass(serializer_class, BaseSerializer):
        raise StackdioConfigException('Object %r is not a Serializer class.' %
                                      serializer_class)
    return serializer_class
예제 #12
0
 def get_model_serializer_class(self, model_class):
     if model_class not in self:
         raise StackdioConfigException(
             'Model %r is not registered with the '
             'notification registry.' % model_class)
     return self[model_class].serializer_class
예제 #13
0
        'propagate': False,
    }

##
# LDAP configuration. To enable this, you should set ldap: enabled: true in your config file.
##

# Throw in the rest of our LDAP config if ldap is enabled
if LDAP_ENABLED:
    try:
        import ldap
        import django_auth_ldap.config
        from django_auth_ldap.config import LDAPSearch
    except ImportError:
        raise StackdioConfigException(
            'LDAP is enabled, but django_auth_ldap is missing.  '
            'Please install django_auth_ldap.')

    auth_ldap_search = ('group_type', )
    call_value = ('group_type', )

    def get_from_ldap_module(attr, module=ldap, fail_on_error=False):
        try:
            return getattr(module, attr)
        except (AttributeError, TypeError):
            if fail_on_error:
                raise StackdioConfigException(
                    'Invalid config value: {}'.format(attr))
            else:
                # if we get an exception, just return the raw attribute
                return attr