示例#1
0
    def handle(self, *args, **options):
        brokers = alerts.get_service_classes()
        broker_classes = {}
        for broker in brokers:
            broker_classes[broker] = alerts.get_service_class(broker)()

        target = None
        if options['target_id']:
            try:
                targets = [Target.objects.get(pk=options['target_id'])]
            except ObjectDoesNotExist:
                raise Exception('Invalid target id provided')
        else:
            sources = ReducedDatumSource.objects.filter(
                name__in=broker_classes.keys()).distinct()
            targets = Target.objects.filter(id__in=ReducedDatum.objects.filter(
                source__in=sources).values_list('target').distinct())

        failed_records = {}
        for target in targets:
            for class_name, clazz in broker_classes.items():
                try:
                    clazz.process_reduced_data(target)
                except HTTPError:
                    failed_records[class_name] = target.id

        if len(failed_records) == 0:
            return 'Update completed successfully'
        else:
            return 'Update completed with errors: {0}'.format(
                str(failed_records))
示例#2
0
class BrokerQueryFilter(django_filters.FilterSet):
    broker = django_filters.ChoiceFilter(
        choices=[(k, k) for k in get_service_classes().keys()])
    name = django_filters.CharFilter(lookup_expr='icontains')

    class Meta:
        model = BrokerQuery
        fields = ['broker', 'name']
示例#3
0
    def test_get_service_classes_import_error(self):
        with self.subTest('Invalid import returns an import error.'):
            with patch(
                    'tom_alerts.alerts.import_module') as mock_import_module:
                mock_import_module.side_effect = ImportError()
                with self.assertRaisesRegex(
                        ImportError,
                        'Could not import tom_alerts.fake_broker.'):
                    get_service_classes()

        with self.subTest('Invalid import returns an attribute error.'):
            with patch(
                    'tom_alerts.alerts.import_module') as mock_import_module:
                mock_import_module.side_effect = AttributeError()
                with self.assertRaisesRegex(
                        ImportError,
                        'Could not import tom_alerts.fake_broker.'):
                    get_service_classes()
示例#4
0
    def get_context_data(self, *args, **kwargs):
        """
        Adds the brokers available to the TOM to the context dictionary.

        :returns: context
        :rtype: dict
        """
        context = super().get_context_data(*args, **kwargs)
        context['installed_brokers'] = get_service_classes()
        return context
示例#5
0
class BrokerQueryFilter(FilterSet):
    """
    Defines the available fields for filtering the list of broker queries.
    """
    broker = ChoiceFilter(choices=[(k, k)
                                   for k in get_service_classes().keys()])
    name = CharFilter(lookup_expr='icontains')

    class Meta:
        model = BrokerQuery
        fields = ['broker', 'name']
示例#6
0
    def get_context_data(self, *args, **kwargs):
        """
        Adds the brokers available to the TOM to the context dictionary.
    
        :returns: context
        :rtype: dict
        """
        context = super().get_context_data(*args, **kwargs)
        private_brokers = settings.PRIVATE_BROKERS
        context['private_brokers'] = private_brokers
        context['amon_brokers'] = settings.AMON_BROKERS
        installed_brokers = get_service_classes()
        if not self.request.user.groups.filter(name='AMON members').exists():
            for broker in private_brokers:
                if broker in installed_brokers:
                    del installed_brokers[broker]

        context['installed_brokers'] = installed_brokers
        return context
示例#7
0
 def get_context_data(self, *args, **kwargs):
     context = super().get_context_data(*args, **kwargs)
     context['installed_brokers'] = get_service_classes()
     return context