Пример #1
0
    def history(self, q=None, sort=None, limit=None, marker=None):
        """Assembles the alarm history requested.

        :param q: Filter rules for the changes to be described.
        :param sort: A list of pairs of sort key and sort dir.
        :param limit: The maximum number of items to be return.
        :param marker: The pagination query marker.
        """

        # Ensure alarm exists
        self._enforce_rbac('alarm_history')

        q = q or []
        # allow history to be returned for deleted alarms, but scope changes
        # returned to those carried out on behalf of the auth'd tenant, to
        # avoid inappropriate cross-tenant visibility of alarm history
        auth_project = rbac.get_limited_to_project(pecan.request.headers,
                                                   pecan.request.enforcer)
        conn = pecan.request.storage
        kwargs = v2_utils.query_to_kwargs(q, conn.get_alarm_changes,
                                          ['on_behalf_of', 'alarm_id'])
        if sort or limit or marker:
            kwargs['pagination'] = v2_utils.get_pagination_options(
                sort, limit, marker, models.AlarmChange)
        return [
            AlarmChange.from_db_model(ac)
            for ac in conn.get_alarm_changes(self._id, auth_project, **kwargs)
        ]
Пример #2
0
 def test_sample_filter_self_always_excluded(self):
     queries = [v2_base.Query(field='user_id', op='eq', value='20')]
     with mock.patch('pecan.request') as request:
         request.headers.return_value = {'X-ProjectId': 'foobar'}
         kwargs = utils.query_to_kwargs(queries,
                                        storage.SampleFilter.__init__)
         self.assertNotIn('self', kwargs)
Пример #3
0
    def get_all(self, q=None, sort=None, limit=None, marker=None):
        """Return all alarms, based on the query provided.

        :param q: Filter rules for the alarms to be returned.
        :param sort: A list of pairs of sort key and sort dir.
        :param limit: The maximum number of items to be return.
        :param marker: The pagination query marker.
        """
        target = rbac.target_from_segregation_rule(pecan.request.headers,
                                                   pecan.request.enforcer)
        rbac.enforce('get_alarms', pecan.request.headers,
                     pecan.request.enforcer, target)

        q = q or []
        # Timestamp is not supported field for Simple Alarm queries
        kwargs = v2_utils.query_to_kwargs(q,
                                          pecan.request.storage.get_alarms,
                                          allow_timestamps=False)
        if sort or limit or marker:
            kwargs['pagination'] = v2_utils.get_pagination_options(
                sort, limit, marker, models.Alarm)
        return [
            Alarm.from_db_model(m)
            for m in pecan.request.storage.get_alarms(**kwargs)
        ]
Пример #4
0
    def history(self, q=None, sort=None, limit=None, marker=None):
        """Assembles the alarm history requested.

        :param q: Filter rules for the changes to be described.
        :param sort: A list of pairs of sort key and sort dir.
        :param limit: The maximum number of items to be return.
        :param marker: The pagination query marker.
        """

        # Ensure alarm exists
        self._enforce_rbac('alarm_history')

        q = q or []
        # allow history to be returned for deleted alarms, but scope changes
        # returned to those carried out on behalf of the auth'd tenant, to
        # avoid inappropriate cross-tenant visibility of alarm history
        auth_project = rbac.get_limited_to_project(pecan.request.headers,
                                                   pecan.request.enforcer)
        conn = pecan.request.storage
        kwargs = v2_utils.query_to_kwargs(
            q, conn.get_alarm_changes, ['on_behalf_of', 'alarm_id'])
        if sort or limit or marker:
            kwargs['pagination'] = v2_utils.get_pagination_options(
                sort, limit, marker, models.AlarmChange)
        return [AlarmChange.from_db_model(ac)
                for ac in conn.get_alarm_changes(self._id, auth_project,
                                                 **kwargs)]
Пример #5
0
 def test_sample_filter_single(self):
     q = [v2_base.Query(field='user_id',
                        op='eq',
                        value='uid')]
     kwargs = utils.query_to_kwargs(q, storage.SampleFilter.__init__)
     self.assertIn('user', kwargs)
     self.assertEqual(1, len(kwargs))
     self.assertEqual('uid', kwargs['user'])
 def test_sample_filter_self_always_excluded(self):
     queries = [v2_base.Query(field='user_id',
                              op='eq',
                              value='20')]
     with mock.patch('pecan.request') as request:
         request.headers.return_value = {'X-ProjectId': 'foobar'}
         kwargs = utils.query_to_kwargs(queries,
                                        storage.SampleFilter.__init__)
         self.assertFalse('self' in kwargs)
Пример #7
0
 def test_sample_filter_translation(self):
     queries = [
         v2_base.Query(field=f, op="eq", value="fake_%s" % f, type="string")
         for f in ["user_id", "project_id", "resource_id"]
     ]
     with mock.patch("pecan.request") as request:
         request.headers.return_value = {"X-ProjectId": "foobar"}
         kwargs = utils.query_to_kwargs(queries, storage.SampleFilter.__init__)
         for o in ["user", "project", "resource"]:
             self.assertEqual("fake_%s_id" % o, kwargs.get(o))
Пример #8
0
 def test_sample_filter_translation(self):
     queries = [
         v2_base.Query(field=f, op='eq', value='fake_%s' % f, type='string')
         for f in ['user_id', 'project_id', 'resource_id']
     ]
     with mock.patch('pecan.request') as request:
         request.headers.return_value = {'X-ProjectId': 'foobar'}
         kwargs = utils.query_to_kwargs(queries,
                                        storage.SampleFilter.__init__)
         for o in ['user', 'project', 'resource']:
             self.assertEqual('fake_%s_id' % o, kwargs.get(o))
Пример #9
0
 def test_sample_filter_meta(self):
     q = [v2_base.Query(field='metadata.size',
                        op='eq',
                        value='20'),
          v2_base.Query(field='resource_metadata.id',
                        op='eq',
                        value='meta_id')]
     kwargs = utils.query_to_kwargs(q, storage.SampleFilter.__init__)
     self.assertEqual(1, len(kwargs))
     self.assertEqual(2, len(kwargs['metaquery']))
     self.assertEqual(20, kwargs['metaquery']['metadata.size'])
     self.assertEqual('meta_id', kwargs['metaquery']['metadata.id'])
 def test_sample_filter_translation(self):
     queries = [v2_base.Query(field=f,
                              op='eq',
                              value='fake_%s' % f,
                              type='string') for f in ['user_id',
                                                       'project_id',
                                                       'resource_id']]
     with mock.patch('pecan.request') as request:
         request.headers.return_value = {'X-ProjectId': 'foobar'}
         kwargs = utils.query_to_kwargs(queries,
                                        storage.SampleFilter.__init__)
         for o in ['user', 'project', 'resource']:
             self.assertEqual('fake_%s_id' % o, kwargs.get(o))
Пример #11
0
 def test_sample_filter_timestamp(self):
     ts_start = timeutils.utcnow()
     ts_end = ts_start + datetime.timedelta(minutes=5)
     q = [
         v2_base.Query(field='timestamp', op='lt', value=str(ts_end)),
         v2_base.Query(field='timestamp', op='gt', value=str(ts_start))
     ]
     kwargs = utils.query_to_kwargs(q, storage.SampleFilter.__init__)
     self.assertEqual(4, len(kwargs))
     self.assertTimestampEqual(kwargs['start_timestamp'], ts_start)
     self.assertTimestampEqual(kwargs['end_timestamp'], ts_end)
     self.assertEqual('gt', kwargs['start_timestamp_op'])
     self.assertEqual('lt', kwargs['end_timestamp_op'])
Пример #12
0
 def test_sample_filter_timestamp(self):
     ts_start = timeutils.utcnow()
     ts_end = ts_start + datetime.timedelta(minutes=5)
     q = [
         v2_base.Query(field="timestamp", op="lt", value=str(ts_end)),
         v2_base.Query(field="timestamp", op="gt", value=str(ts_start)),
     ]
     kwargs = utils.query_to_kwargs(q, storage.SampleFilter.__init__)
     self.assertEqual(4, len(kwargs))
     self.assertTimestampEqual(kwargs["start_timestamp"], ts_start)
     self.assertTimestampEqual(kwargs["end_timestamp"], ts_end)
     self.assertEqual("gt", kwargs["start_timestamp_op"])
     self.assertEqual("lt", kwargs["end_timestamp_op"])
 def test_sample_filter_timestamp(self):
     ts_start = timeutils.utcnow()
     ts_end = ts_start + datetime.timedelta(minutes=5)
     q = [v2_base.Query(field='timestamp',
                        op='lt',
                        value=str(ts_end)),
          v2_base.Query(field='timestamp',
                        op='gt',
                        value=str(ts_start))]
     kwargs = utils.query_to_kwargs(q, storage.SampleFilter.__init__)
     self.assertEqual(4, len(kwargs))
     self.assertTimestampEqual(kwargs['start_timestamp'], ts_start)
     self.assertTimestampEqual(kwargs['end_timestamp'], ts_end)
     self.assertEqual('gt', kwargs['start_timestamp_op'])
     self.assertEqual('lt', kwargs['end_timestamp_op'])
Пример #14
0
    def get_all(self, q=None):
        """Return all alarms, based on the query provided.

        :param q: Filter rules for the alarms to be returned.
        """

        rbac.enforce('get_alarms', pecan.request)

        q = q or []
        # Timestamp is not supported field for Simple Alarm queries
        kwargs = v2_utils.query_to_kwargs(
            q, pecan.request.alarm_storage_conn.get_alarms,
            allow_timestamps=False)
        return [Alarm.from_db_model(m)
                for m in pecan.request.alarm_storage_conn.get_alarms(**kwargs)]
Пример #15
0
 def test_sample_filter_multi(self):
     q = [
         v2_base.Query(field="user_id", op="eq", value="uid"),
         v2_base.Query(field="project_id", op="eq", value="pid"),
         v2_base.Query(field="resource_id", op="eq", value="rid"),
         v2_base.Query(field="source", op="eq", value="source_name"),
         v2_base.Query(field="meter", op="eq", value="meter_name"),
     ]
     kwargs = utils.query_to_kwargs(q, storage.SampleFilter.__init__)
     self.assertEqual(5, len(kwargs))
     self.assertEqual("uid", kwargs["user"])
     self.assertEqual("pid", kwargs["project"])
     self.assertEqual("rid", kwargs["resource"])
     self.assertEqual("source_name", kwargs["source"])
     self.assertEqual("meter_name", kwargs["meter"])
Пример #16
0
 def test_sample_filter_multi(self):
     q = [
         v2_base.Query(field='user_id', op='eq', value='uid'),
         v2_base.Query(field='project_id', op='eq', value='pid'),
         v2_base.Query(field='resource_id', op='eq', value='rid'),
         v2_base.Query(field='source', op='eq', value='source_name'),
         v2_base.Query(field='meter', op='eq', value='meter_name')
     ]
     kwargs = utils.query_to_kwargs(q, storage.SampleFilter.__init__)
     self.assertEqual(5, len(kwargs))
     self.assertEqual('uid', kwargs['user'])
     self.assertEqual('pid', kwargs['project'])
     self.assertEqual('rid', kwargs['resource'])
     self.assertEqual('source_name', kwargs['source'])
     self.assertEqual('meter_name', kwargs['meter'])
Пример #17
0
    def get_all(self, q=None, sort=None, limit=None, marker=None):
        """Return all alarms, based on the query provided.

        :param q: Filter rules for the alarms to be returned.
        :param sort: A list of pairs of sort key and sort dir.
        :param limit: The maximum number of items to be return.
        :param marker: The pagination query marker.
        """
        target = rbac.target_from_segregation_rule(pecan.request.headers, pecan.request.enforcer)
        rbac.enforce("get_alarms", pecan.request.headers, pecan.request.enforcer, target)

        q = q or []
        # Timestamp is not supported field for Simple Alarm queries
        kwargs = v2_utils.query_to_kwargs(q, pecan.request.storage.get_alarms, allow_timestamps=False)
        if sort or limit or marker:
            kwargs["pagination"] = v2_utils.get_pagination_options(sort, limit, marker, models.Alarm)
        return [Alarm.from_db_model(m) for m in pecan.request.storage.get_alarms(**kwargs)]
Пример #18
0
    def history(self, q=None):
        """Assembles the alarm history requested.

        :param q: Filter rules for the changes to be described.
        """

        rbac.enforce('alarm_history', pecan.request)

        q = q or []
        # allow history to be returned for deleted alarms, but scope changes
        # returned to those carried out on behalf of the auth'd tenant, to
        # avoid inappropriate cross-tenant visibility of alarm history
        auth_project = rbac.get_limited_to_project(pecan.request.headers)
        conn = pecan.request.alarm_storage_conn
        kwargs = v2_utils.query_to_kwargs(
            q, conn.get_alarm_changes, ['on_behalf_of', 'alarm_id'])
        return [AlarmChange.from_db_model(ac)
                for ac in conn.get_alarm_changes(self._id, auth_project,
                                                 **kwargs)]
Пример #19
0
    def get_all(self, q=None):
        """Return all alarms, based on the query provided.

        :param q: Filter rules for the alarms to be returned.
        """

        rbac.enforce('get_alarms', pecan.request.headers,
                     pecan.request.enforcer)

        q = q or []
        # Timestamp is not supported field for Simple Alarm queries
        kwargs = v2_utils.query_to_kwargs(
            q,
            pecan.request.alarm_storage_conn.get_alarms,
            allow_timestamps=False)
        return [
            Alarm.from_db_model(m)
            for m in pecan.request.alarm_storage_conn.get_alarms(**kwargs)
        ]
Пример #20
0
    def history(self, q=None):
        """Assembles the alarm history requested.

        :param q: Filter rules for the changes to be described.
        """

        rbac.enforce('alarm_history', pecan.request.headers,
                     pecan.request.enforcer)

        q = q or []
        # allow history to be returned for deleted alarms, but scope changes
        # returned to those carried out on behalf of the auth'd tenant, to
        # avoid inappropriate cross-tenant visibility of alarm history
        auth_project = rbac.get_limited_to_project(pecan.request.headers,
                                                   pecan.request.enforcer)
        conn = pecan.request.alarm_storage_conn
        kwargs = v2_utils.query_to_kwargs(q, conn.get_alarm_changes,
                                          ['on_behalf_of', 'alarm_id'])
        return [
            AlarmChange.from_db_model(ac)
            for ac in conn.get_alarm_changes(self._id, auth_project, **kwargs)
        ]
 def test_sample_filter_multi(self):
     q = [v2_base.Query(field='user_id',
                        op='eq',
                        value='uid'),
          v2_base.Query(field='project_id',
                        op='eq',
                        value='pid'),
          v2_base.Query(field='resource_id',
                        op='eq',
                        value='rid'),
          v2_base.Query(field='source',
                        op='eq',
                        value='source_name'),
          v2_base.Query(field='meter',
                        op='eq',
                        value='meter_name')]
     kwargs = utils.query_to_kwargs(q, storage.SampleFilter.__init__)
     self.assertEqual(5, len(kwargs))
     self.assertEqual('uid', kwargs['user'])
     self.assertEqual('pid', kwargs['project'])
     self.assertEqual('rid', kwargs['resource'])
     self.assertEqual('source_name', kwargs['source'])
     self.assertEqual('meter_name', kwargs['meter'])
Пример #22
0
 def test_sample_filter_self_always_excluded(self):
     queries = [v2_base.Query(field="user_id", op="eq", value="20")]
     with mock.patch("pecan.request") as request:
         request.headers.return_value = {"X-ProjectId": "foobar"}
         kwargs = utils.query_to_kwargs(queries, storage.SampleFilter.__init__)
         self.assertNotIn("self", kwargs)
Пример #23
0
 def test_sample_filter_single(self):
     q = [v2_base.Query(field="user_id", op="eq", value="uid")]
     kwargs = utils.query_to_kwargs(q, storage.SampleFilter.__init__)
     self.assertIn("user", kwargs)
     self.assertEqual(1, len(kwargs))
     self.assertEqual("uid", kwargs["user"])