Esempio n. 1
0
    def get_one(self, message_id):
        """Return a single event with the given message id.

        :param message_id: Message ID of the Event to be returned
        """
        rbac.enforce("events:show", pecan.request)
        filters = _build_rbac_query_filters()
        t_filter = filters['t_filter']
        admin_proj = filters['admin_proj']
        event_filter = storage.EventFilter(traits_filter=t_filter,
                                           admin_proj=admin_proj,
                                           message_id=message_id)
        events = [
            event for event in pecan.request.event_storage_conn.get_events(
                event_filter)
        ]
        if not events:
            raise base.EntityNotFound(_("Event"), message_id)

        if len(events) > 1:
            LOG.error(
                _("More than one event with "
                  "id %s returned from storage driver") % message_id)

        event = events[0]

        return Event(message_id=event.message_id,
                     event_type=event.event_type,
                     generated=event.generated,
                     traits=event.traits,
                     raw=event.raw)
Esempio n. 2
0
    def test_simple_get_event_type(self):
        expected_trait_values = {
            'id_Bar_100': {
                'trait_A': 'my_Bar_text',
                'trait_B': 101,
                'trait_C': 100.123456,
                'trait_D': self.start + datetime.timedelta(hours=1)
            },
            'id_Bar_400': {
                'trait_A': 'my_Bar_text',
                'trait_B': 401,
                'trait_C': 400.123456,
                'trait_D': self.start + datetime.timedelta(hours=4)
            }
        }

        event_filter = event_storage.EventFilter(self.start, self.end, "Bar")
        events = [event for event in self.event_conn.get_events(event_filter)]
        self.assertEqual(2, len(events))
        self.assertEqual("Bar", events[0].event_type)
        self.assertEqual("Bar", events[1].event_type)
        self.assertEqual(4, len(events[0].traits))
        self.assertEqual(4, len(events[1].traits))
        for event in events:
            trait_values = expected_trait_values.get(event.message_id,
                                                     None)
            if not trait_values:
                self.fail("Unexpected event ID returned:" % event.message_id)

            for trait in event.traits:
                expected_val = trait_values.get(trait.name)
                if not expected_val:
                    self.fail("Unexpected trait type: %s" % trait.dtype)
                self.assertEqual(expected_val, trait.value)
Esempio n. 3
0
    def _get_pagination_query(self, query, pagination, api_model, model):
        limit = pagination.get('limit')

        marker = None
        if pagination.get('marker'):
            marker_filter = event_storage.EventFilter(
                message_id=pagination.get('marker'))
            markers = list(self.get_events(marker_filter))
            if markers:
                marker = markers[0]
            else:
                raise storage.InvalidMarker('Marker %s not found.' %
                                            pagination['marker'])

        if not pagination.get('sort'):
            pagination['sort'] = api_model.DEFAULT_SORT
        sort_keys = [s[0] for s in pagination['sort']]
        sort_dirs = [s[1] for s in pagination['sort']]

        return oslo_sql_utils.paginate_query(query,
                                             model,
                                             limit,
                                             sort_keys,
                                             sort_dirs=sort_dirs,
                                             marker=marker)
Esempio n. 4
0
 def test_get_event_multiple_trait_filter_expect_none(self):
     trait_filters = [{'key': 'trait_B', 'integer': 1},
                      {'key': 'trait_A', 'string': 'my_Zoo_text'}]
     event_filter = event_storage.EventFilter(self.start, self.end,
                                              traits_filter=trait_filters)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(0, len(events))
Esempio n. 5
0
 def test_get_event_trait_filter(self):
     trait_filters = [{'key': 'trait_B', 'integer': 101}]
     event_filter = event_storage.EventFilter(self.start, self.end,
                                              traits_filter=trait_filters)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(1, len(events))
     self.assertEqual("Bar", events[0].event_type)
     self.assertEqual(4, len(events[0].traits))
Esempio n. 6
0
def _event_query_to_event_filter(q):
    evt_model_filter = {
        'event_type': None,
        'message_id': None,
        'start_timestamp': None,
        'end_timestamp': None
    }
    filters = _build_rbac_query_filters()
    traits_filter = filters['t_filter']
    admin_proj = filters['admin_proj']

    for i in q:
        if not i.op:
            i.op = 'eq'
        elif i.op not in base.operation_kind:
            error = (_('Operator %(operator)s is not supported. The supported'
                       ' operators are: %(supported)s') % {
                           'operator': i.op,
                           'supported': base.operation_kind
                       })
            raise base.ClientSideError(error)
        if i.field in evt_model_filter:
            if i.op != 'eq' and i.field in ('event_type', 'message_id'):
                error = (_('Operator %(operator)s is not supported. Only'
                           ' `eq\' operator is available for field'
                           ' %(field)s') % {
                               'operator': i.op,
                               'field': i.field
                           })
                raise base.ClientSideError(error)
            if i.op != 'ge' and i.field == 'start_timestamp':
                error = (_('Operator %(operator)s is not supported. Only'
                           ' `ge\' operator is available for field'
                           ' %(field)s') % {
                               'operator': i.op,
                               'field': i.field
                           })
                raise base.ClientSideError(error)
            if i.op != 'le' and i.field == 'end_timestamp':
                error = (_('Operator %(operator)s is not supported. Only'
                           ' `le\' operator is available for field'
                           ' %(field)s') % {
                               'operator': i.op,
                               'field': i.field
                           })
                raise base.ClientSideError(error)
            evt_model_filter[i.field] = i.value
        else:
            trait_type = i.type or 'string'
            traits_filter.append({
                "key": i.field,
                trait_type: i._get_value_as_type(),
                "op": i.op
            })
    return storage.EventFilter(traits_filter=traits_filter,
                               admin_proj=admin_proj,
                               **evt_model_filter)
Esempio n. 7
0
 def test_get_event_multiple_trait_filter(self):
     trait_filters = [{'key': 'trait_B', 'integer': 1},
                      {'key': 'trait_A', 'string': 'my_Foo_text'},
                      {'key': 'trait_C', 'float': 0.123456}]
     event_filter = event_storage.EventFilter(self.start, self.end,
                                              traits_filter=trait_filters)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(1, len(events))
     self.assertEqual("Foo", events[0].event_type)
     self.assertEqual(4, len(events[0].traits))
Esempio n. 8
0
 def test_simple_get_event_no_traits(self):
     new_events = [event_models.Event("id_notraits", "NoTraits",
                   self.start, [], {})]
     self.event_conn.record_events(new_events)
     event_filter = event_storage.EventFilter(
         self.start, self.end, "NoTraits")
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(1, len(events))
     self.assertEqual("id_notraits", events[0].message_id)
     self.assertEqual("NoTraits", events[0].event_type)
     self.assertEqual(0, len(events[0].traits))
Esempio n. 9
0
    def test_get_by_message_id(self):
        new_events = [event_models.Event("id_testid",
                                         "MessageIDTest",
                                         self.start,
                                         [], {})]

        self.event_conn.record_events(new_events)
        event_filter = event_storage.EventFilter(message_id="id_testid")
        events = [event for event in self.event_conn.get_events(event_filter)]
        self.assertEqual(1, len(events))
        event = events[0]
        self.assertEqual("id_testid", event.message_id)
Esempio n. 10
0
 def test_simple_get(self):
     event_filter = event_storage.EventFilter(self.start, self.end)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(6, len(events))
     start_time = None
     for i, type in enumerate(['Foo', 'Bar', 'Zoo']):
         self.assertEqual(type, events[i].event_type)
         self.assertEqual(4, len(events[i].traits))
         # Ensure sorted results ...
         if start_time is not None:
             # Python 2.6 has no assertLess :(
             self.assertTrue(start_time < events[i].generated)
         start_time = events[i].generated
Esempio n. 11
0
 def test_generated_is_datetime(self):
     event_filter = event_storage.EventFilter(self.start, self.end)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(6, len(events))
     for i, event in enumerate(events):
         self.assertIsInstance(event.generated, datetime.datetime)
         self.assertEqual(event.generated,
                          self.event_models[i].generated)
         model_traits = self.event_models[i].traits
         for j, trait in enumerate(event.traits):
             if trait.dtype == event_models.Trait.DATETIME_TYPE:
                 self.assertIsInstance(trait.value, datetime.datetime)
                 self.assertEqual(trait.value, model_traits[j].value)
Esempio n. 12
0
    def test_clear_expired_event_data(self, mock_utcnow):
        mock_utcnow.return_value = datetime.datetime(2013, 12, 31, 10, 0)
        self.event_conn.clear_expired_event_data(3600)

        events = list(self.event_conn.get_events(event_storage.EventFilter()))
        self.assertEqual(2, len(events))
        event_types = list(self.event_conn.get_event_types())
        self.assertEqual(['Bar', 'Zoo'], event_types)
        for event_type in event_types:
            trait_types = list(self.event_conn.get_trait_types(event_type))
            self.assertEqual(4, len(trait_types))
            traits = list(self.event_conn.get_traits(event_type))
            self.assertEqual(4, len(traits))
Esempio n. 13
0
 def test_trait_type_enforced_on_none(self):
     new_events = [event_models.Event(
         "id_testid", "MessageIDTest", self.start,
         [event_models.Trait('text', event_models.Trait.TEXT_TYPE, ''),
          event_models.Trait('int', event_models.Trait.INT_TYPE, 0),
          event_models.Trait('float', event_models.Trait.FLOAT_TYPE, 0.0)],
         {})]
     self.event_conn.record_events(new_events)
     event_filter = event_storage.EventFilter(message_id="id_testid")
     events = [event for event in self.event_conn.get_events(event_filter)]
     options = [(event_models.Trait.TEXT_TYPE, ''),
                (event_models.Trait.INT_TYPE, 0.0),
                (event_models.Trait.FLOAT_TYPE, 0.0)]
     for trait in events[0].traits:
         options.remove((trait.dtype, trait.value))
Esempio n. 14
0
 def test_get_event_trait_filter_op_datetime(self):
     trait_filters = [{'key': 'trait_D',
                       'datetime': self.start + datetime.timedelta(hours=2),
                       'op': 'eq'}]
     event_filter = event_storage.EventFilter(self.start, self.end,
                                              traits_filter=trait_filters)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(1, len(events))
     self.assertEqual("Zoo", events[0].event_type)
     self.assertEqual(4, len(events[0].traits))
     trait_filters[0].update({'key': 'trait_D', 'op': 'lt'})
     event_filter = event_storage.EventFilter(self.start, self.end,
                                              traits_filter=trait_filters)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(2, len(events))
     trait_filters[0].update({'key': 'trait_D', 'op': 'le'})
     self.assertEqual("Bar", events[1].event_type)
     event_filter = event_storage.EventFilter(self.start, self.end,
                                              traits_filter=trait_filters)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(3, len(events))
     self.assertEqual("Bar", events[1].event_type)
     trait_filters[0].update({'key': 'trait_D', 'op': 'ne'})
     event_filter = event_storage.EventFilter(self.start, self.end,
                                              traits_filter=trait_filters)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(5, len(events))
     self.assertEqual("Foo", events[2].event_type)
     trait_filters[0].update({'key': 'trait_D', 'op': 'gt'})
     event_filter = event_storage.EventFilter(self.start, self.end,
                                              traits_filter=trait_filters)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(3, len(events))
     self.assertEqual("Zoo", events[2].event_type)
     trait_filters[0].update({'key': 'trait_D', 'op': 'ge'})
     event_filter = event_storage.EventFilter(self.start, self.end,
                                              traits_filter=trait_filters)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(4, len(events))
     self.assertEqual("Bar", events[2].event_type)
Esempio n. 15
0
 def test_get_event_trait_filter_op_string(self):
     trait_filters = [{'key': 'trait_A', 'string': 'my_Foo_text',
                       'op': 'eq'}]
     event_filter = event_storage.EventFilter(self.start, self.end,
                                              traits_filter=trait_filters)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(2, len(events))
     self.assertEqual("Foo", events[0].event_type)
     self.assertEqual(4, len(events[0].traits))
     trait_filters[0].update({'key': 'trait_A', 'op': 'lt'})
     event_filter = event_storage.EventFilter(self.start, self.end,
                                              traits_filter=trait_filters)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(2, len(events))
     self.assertEqual("Bar", events[0].event_type)
     trait_filters[0].update({'key': 'trait_A', 'op': 'le'})
     event_filter = event_storage.EventFilter(self.start, self.end,
                                              traits_filter=trait_filters)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(4, len(events))
     self.assertEqual("Bar", events[1].event_type)
     trait_filters[0].update({'key': 'trait_A', 'op': 'ne'})
     event_filter = event_storage.EventFilter(self.start, self.end,
                                              traits_filter=trait_filters)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(4, len(events))
     self.assertEqual("Zoo", events[3].event_type)
     trait_filters[0].update({'key': 'trait_A', 'op': 'gt'})
     event_filter = event_storage.EventFilter(self.start, self.end,
                                              traits_filter=trait_filters)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(2, len(events))
     self.assertEqual("Zoo", events[0].event_type)
     trait_filters[0].update({'key': 'trait_A', 'op': 'ge'})
     event_filter = event_storage.EventFilter(self.start, self.end,
                                              traits_filter=trait_filters)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(4, len(events))
     self.assertEqual("Foo", events[2].event_type)
Esempio n. 16
0
 def test_get_event_trait_filter_op_float(self):
     trait_filters = [{'key': 'trait_C', 'float': 300.123456, 'op': 'eq'}]
     event_filter = event_storage.EventFilter(self.start, self.end,
                                              traits_filter=trait_filters)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(1, len(events))
     self.assertEqual("Foo", events[0].event_type)
     self.assertEqual(4, len(events[0].traits))
     trait_filters[0].update({'key': 'trait_C', 'op': 'lt'})
     event_filter = event_storage.EventFilter(self.start, self.end,
                                              traits_filter=trait_filters)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(3, len(events))
     self.assertEqual("Zoo", events[2].event_type)
     trait_filters[0].update({'key': 'trait_C', 'op': 'le'})
     event_filter = event_storage.EventFilter(self.start, self.end,
                                              traits_filter=trait_filters)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(4, len(events))
     self.assertEqual("Bar", events[1].event_type)
     trait_filters[0].update({'key': 'trait_C', 'op': 'ne'})
     event_filter = event_storage.EventFilter(self.start, self.end,
                                              traits_filter=trait_filters)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(5, len(events))
     self.assertEqual("Zoo", events[2].event_type)
     trait_filters[0].update({'key': 'trait_C', 'op': 'gt'})
     event_filter = event_storage.EventFilter(self.start, self.end,
                                              traits_filter=trait_filters)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(2, len(events))
     self.assertEqual("Bar", events[0].event_type)
     trait_filters[0].update({'key': 'trait_C', 'op': 'ge'})
     event_filter = event_storage.EventFilter(self.start, self.end,
                                              traits_filter=trait_filters)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(3, len(events))
     self.assertEqual("Zoo", events[2].event_type)
Esempio n. 17
0
 def test_simple_get_raw(self):
     event_filter = event_storage.EventFilter()
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertTrue(events)
     self.assertEqual({'status': {'nested': 'started'}}, events[0].raw)
Esempio n. 18
0
 def test_simple_get_no_filters(self):
     event_filter = event_storage.EventFilter(None, None, None)
     events = [event for event in self.event_conn.get_events(event_filter)]
     self.assertEqual(6, len(events))