Rebase telemetry resources to resource2/proxy2

Change-Id: If82e9753098ee104aacebe7ad7ce37bf6813ac88
This commit is contained in:
tengqm 2016-09-05 01:11:39 -04:00
parent b535bc833f
commit d435684465
12 changed files with 103 additions and 172 deletions

View File

@ -10,7 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.
from openstack import proxy
from openstack import proxy2
from openstack.telemetry.v2 import capability
from openstack.telemetry.v2 import meter as _meter
from openstack.telemetry.v2 import resource as _resource
@ -18,7 +18,7 @@ from openstack.telemetry.v2 import sample
from openstack.telemetry.v2 import statistics
class Proxy(proxy.BaseProxy):
class Proxy(proxy2.BaseProxy):
""".. caution:: This API is a work in progress and is subject to change."""
def find_capability(self, name_or_id, ignore_missing=True):
@ -147,9 +147,8 @@ class Proxy(proxy.BaseProxy):
:returns: A generator of sample objects
:rtype: :class:`~openstack.telemetry.v2.sample.Sample`
"""
meter_name = _meter.Meter.from_name(meter).name
return self._list(sample.Sample, paginated=False,
path_args={'counter_name': meter_name}, **query)
counter_name=meter, **query)
def find_statistics(self, name_or_id, ignore_missing=True):
"""Find a single statistics
@ -176,6 +175,5 @@ class Proxy(proxy.BaseProxy):
:returns: A generator of statistics objects
:rtype: :class:`~openstack.telemetry.v2.statistics.Statistics`
"""
meter_name = _meter.Meter.from_name(meter).name
return self._list(statistics.Statistics, paginated=False,
path_args={'meter_name': meter_name}, **query)
meter_name=meter, **query)

View File

@ -12,7 +12,7 @@
import six
from openstack import resource
from openstack import resource2 as resource
from openstack.telemetry import telemetry_service
@ -27,11 +27,10 @@ class Capability(resource.Resource):
allow_list = True
# Properties
is_enabled = resource.prop('enabled', type=bool)
is_enabled = resource.Body('enabled', type=bool)
@classmethod
def list(cls, session, limit=None, marker=None, path_args=None,
paginated=False, **params):
def list(cls, session, paginated=False, **params):
resp = session.get(cls.base_path, endpoint_filter=cls.service,
params=params)
resp = resp.json()

View File

@ -10,13 +10,12 @@
# License for the specific language governing permissions and limitations
# under the License.
from openstack import resource
from openstack import resource2 as resource
from openstack.telemetry import telemetry_service
class Meter(resource.Resource):
""".. caution:: This API is a work in progress and is subject to change."""
id_attribute = 'meter_id'
resource_key = 'meter'
base_path = '/meters'
service = telemetry_service.TelemetryService()
@ -26,18 +25,18 @@ class Meter(resource.Resource):
# Properties
#: The ID of the meter
meter_id = resource.prop('meter_id')
meter_id = resource.Body('meter_id', alternate_id=True)
#: The unique name for the meter
name = resource.prop('name')
name = resource.Body('name')
#: The ID of the project that owns the resource
project_id = resource.prop('project_id')
project_id = resource.Body('project_id')
#: The ID of the resource for which the measurements are taken
resource_id = resource.prop('resource_id')
resource_id = resource.Body('resource_id')
#: The name of the source where the meter comes from
source = resource.prop('source')
source = resource.Body('source')
#: The meter type
type = resource.prop('type')
type = resource.Body('type')
#: The unit of measure
unit = resource.prop('unit')
unit = resource.Body('unit')
#: The ID of the user who last triggered an update to the resource
user_id = resource.prop('user_id')
user_id = resource.Body('user_id')

View File

@ -10,36 +10,35 @@
# License for the specific language governing permissions and limitations
# under the License.
from openstack import resource
from openstack import resource2 as resource
from openstack.telemetry import telemetry_service
class Resource(resource.Resource):
""".. caution:: This API is a work in progress and is subject to change."""
id_attribute = 'resource_id'
base_path = '/resources'
service = telemetry_service.TelemetryService()
# Supported Operations
allow_retrieve = True
allow_get = True
allow_list = True
# Properties
#: UTC date & time not later than the first sample known
#: for this resource.
first_sample_at = resource.prop('first_sample_timestamp')
first_sample_at = resource.Body('first_sample_timestamp')
#: UTC date & time not earlier than the last sample known
#: for this resource.
last_sample_at = resource.prop('last_sample_timestamp')
last_sample_at = resource.Body('last_sample_timestamp')
#: A list containing a self link and associated meter links
links = resource.prop('links')
links = resource.Body('links')
#: Arbitrary metadata associated with the resource
metadata = resource.prop('metadata')
metadata = resource.Body('metadata')
#: The ID of the owning project
project_id = resource.prop('project_id')
project_id = resource.Body('project_id')
#: The ID for the resource
resource_id = resource.prop('resource_id')
resource_id = resource.Body('resource_id', alternate_id=True)
#: The name of the source where the resource comes from
source = resource.prop('source')
source = resource.Body('source')
#: The ID of the user who created the resource or updated it last
user_id = resource.prop('user_id')
user_id = resource.Body('user_id')

View File

@ -10,60 +10,43 @@
# License for the specific language governing permissions and limitations
# under the License.
from openstack import resource
from openstack import resource2 as resource
from openstack.telemetry import telemetry_service
class Sample(resource.Resource):
""".. caution:: This API is a work in progress and is subject to change."""
id_attribute = 'sample_id'
base_path = '/meters/%(counter_name)s'
service = telemetry_service.TelemetryService()
# Supported Operations
allow_create = True
allow_get = True
allow_list = True
# Properties
#: The meter name this sample is for
counter_name = resource.prop('meter', alias='counter_name')
#: When the sample has been generated.
generated_at = resource.prop('timestamp')
generated_at = resource.Body('timestamp')
#: The message ID
message_id = resource.Body('message_id', alternate_id=True)
#: Arbitrary metadata associated with the sample
metadata = resource.prop('metadata', alias='resource_metadata')
metadata = resource.Body('metadata')
#: The meter name this sample is for
counter_name = resource.Body('counter_name')
#: The meter name this sample is for
counter_type = resource.Body('counter_type')
#: The ID of the project this sample was taken for
project_id = resource.prop('project_id')
project_id = resource.Body('project_id')
#: When the sample has been recorded.
recorded_at = resource.prop('recorded_at')
recorded_at = resource.Body('recorded_at')
#: The ID of the resource this sample was taken for
resource_id = resource.prop('resource_id')
resource_id = resource.Body('resource_id')
#: The name of the source that identifies where the sample comes from
source = resource.prop('source')
source = resource.Body('source')
#: The meter type
type = resource.prop('type', alias='counter_type')
type = resource.Body('type')
#: The unit of measure
unit = resource.prop('unit', alias='counter_unit')
unit = resource.Body('unit')
#: The ID of the user this sample was taken for
user_id = resource.prop('user_id')
user_id = resource.Body('user_id')
#: The metered value
volume = resource.prop('volume', alias='counter_volume')
@classmethod
def list(cls, session, limit=None, marker=None, path_args=None,
paginated=False, **params):
url = cls._get_url(path_args)
resp = session.get(url, endpoint_filter=cls.service, params=params)
for item in resp.json():
yield cls.existing(**item)
def create(self, session):
url = self._get_url(self)
# telemetry expects a list of samples
attrs = self._attrs.copy()
attrs.pop('meter', None)
resp = session.post(url, endpoint_filter=self.service,
json=[attrs])
resp = resp.json()
self.update_attrs(**resp.pop())
self._reset_dirty()
return self
volume = resource.Body('volume')

View File

@ -10,13 +10,12 @@
# License for the specific language governing permissions and limitations
# under the License.
from openstack import resource
from openstack import resource2 as resource
from openstack.telemetry import telemetry_service
class Statistics(resource.Resource):
""".. caution:: This API is a work in progress and is subject to change."""
id_attribute = 'meter_name'
resource_key = 'statistics'
base_path = '/meters/%(meter_name)s/statistics'
service = telemetry_service.TelemetryService()
@ -24,43 +23,40 @@ class Statistics(resource.Resource):
# Supported Operations
allow_list = True
# Path Parameter
meter_name = resource.prop('meter_name')
# Properties
#: The selectable aggregate value(s)
aggregate = resource.prop('aggregate')
aggregate = resource.Body('aggregate')
#: The average of all of the volume values seen in the data
avg = resource.prop('avg')
avg = resource.Body('avg')
#: The number of samples seen
count = resource.prop('count')
count = resource.Body('count')
#: The difference, in seconds, between the oldest and newest timestamp
duration = resource.prop('duration')
duration = resource.Body('duration')
#: UTC date and time of the oldest timestamp, or the query end time.
duration_end_at = resource.prop('duration_end')
duration_end_at = resource.Body('duration_end')
#: UTC date and time of the earliest timestamp, or the query start time.
duration_start_at = resource.prop('duration_start')
duration_start_at = resource.Body('duration_start')
#: Dictionary of field names for group, if groupby statistics are requested
group_by = resource.prop('groupby')
group_by = resource.Body('groupby')
#: The maximum volume seen in the data
max = resource.prop('max')
max = resource.Body('max')
#: The minimum volume seen in the data
min = resource.prop('min')
min = resource.Body('min')
#: The difference, in seconds, between the period start and end
period = resource.prop('period')
period = resource.Body('period')
#: UTC date and time of the period end.
period_end_at = resource.prop('period_end')
period_end_at = resource.Body('period_end')
#: UTC date and time of the period start.
period_start_at = resource.prop('period_start')
period_start_at = resource.Body('period_start')
#: The total of all of the volume values seen in the data
sum = resource.prop('sum')
sum = resource.Body('sum')
#: The unit type of the data set
unit = resource.prop('unit')
#: TODO(Qiming): This is still incorrect
unit = resource.Body('unit', alternate_id=True)
@classmethod
def list(cls, session, limit=None, marker=None, path_args=None,
paginated=False, **params):
url = cls._get_url(path_args)
def list(cls, session, paginated=False, **params):
url = cls.base_path % {'meter_name': params.pop('meter_name')}
resp = session.get(url, endpoint_filter=cls.service, params=params)
for stat in resp.json():
yield cls.existing(**stat)

View File

@ -38,13 +38,13 @@ class TestCapability(testtools.TestCase):
self.assertEqual('/capabilities', sot.base_path)
self.assertEqual('metering', sot.service.service_type)
self.assertFalse(sot.allow_create)
self.assertFalse(sot.allow_retrieve)
self.assertFalse(sot.allow_get)
self.assertFalse(sot.allow_update)
self.assertFalse(sot.allow_delete)
self.assertTrue(sot.allow_list)
def test_make_it(self):
sot = capability.Capability(EXAMPLE)
sot = capability.Capability(**EXAMPLE)
self.assertEqual(EXAMPLE['id'], sot.id)
self.assertEqual(EXAMPLE['enabled'], sot.is_enabled)

View File

@ -36,13 +36,13 @@ class TestMeter(testtools.TestCase):
self.assertEqual('/meters', sot.base_path)
self.assertEqual('metering', sot.service.service_type)
self.assertFalse(sot.allow_create)
self.assertFalse(sot.allow_retrieve)
self.assertFalse(sot.allow_get)
self.assertFalse(sot.allow_update)
self.assertFalse(sot.allow_delete)
self.assertTrue(sot.allow_list)
def test_make_it(self):
sot = meter.Meter(EXAMPLE)
sot = meter.Meter(**EXAMPLE)
self.assertEqual(EXAMPLE['meter_id'], sot.id)
self.assertEqual(EXAMPLE['meter_id'], sot.meter_id)
self.assertEqual(EXAMPLE['name'], sot.name)

View File

@ -16,10 +16,10 @@ from openstack.telemetry.v2 import meter
from openstack.telemetry.v2 import resource
from openstack.telemetry.v2 import sample
from openstack.telemetry.v2 import statistics
from openstack.tests.unit import test_proxy_base
from openstack.tests.unit import test_proxy_base2
class TestTelemetryProxy(test_proxy_base.TestProxyBase):
class TestTelemetryProxy(test_proxy_base2.TestProxyBase):
def setUp(self):
super(TestTelemetryProxy, self).setUp()
self.proxy = _proxy.Proxy(self.session)
@ -54,18 +54,16 @@ class TestTelemetryProxy(test_proxy_base.TestProxyBase):
self.verify_find(self.proxy.find_sample, sample.Sample)
def test_samples(self):
met = meter.Meter.existing(name='meterone')
expected_kwargs = {'path_args': {'counter_name': 'meterone'}}
expected_kwargs = {'counter_name': 'meterone'}
self.verify_list(self.proxy.samples, sample.Sample,
method_args=[met],
method_args=['meterone'],
paginated=False, expected_kwargs=expected_kwargs)
def test_statistics_find(self):
self.verify_find(self.proxy.find_statistics, statistics.Statistics)
def test_statistics(self):
met = meter.Meter.existing(name='meterone')
expected_kwargs = {'path_args': {'meter_name': 'meterone'}}
expected_kwargs = {'meter_name': 'meterone'}
self.verify_list(self.proxy.statistics, statistics.Statistics,
method_args=[met],
method_args=['meterone'],
paginated=False, expected_kwargs=expected_kwargs)

View File

@ -32,20 +32,21 @@ EXAMPLE = {
class TestResource(testtools.TestCase):
def test_basic(self):
sot = resource.Resource(EXAMPLE)
sot = resource.Resource()
self.assertIsNone(sot.resource_key)
self.assertIsNone(sot.resources_key)
self.assertEqual('/resources', sot.base_path)
self.assertEqual('metering', sot.service.service_type)
self.assertFalse(sot.allow_create)
self.assertTrue(sot.allow_retrieve)
self.assertTrue(sot.allow_get)
self.assertFalse(sot.allow_update)
self.assertFalse(sot.allow_delete)
self.assertTrue(sot.allow_list)
def test_make_it(self):
sot = resource.Resource(EXAMPLE)
sot = resource.Resource(**EXAMPLE)
self.assertEqual(EXAMPLE['resource_id'], sot.id)
self.assertEqual(EXAMPLE['resource_id'], sot.resource_id)
self.assertEqual(EXAMPLE['first_sample_timestamp'],
sot.first_sample_at)
self.assertEqual(EXAMPLE['last_sample_timestamp'],

View File

@ -19,6 +19,7 @@ SAMPLE = {
'sample_id': '0',
'metadata': {'1': 'one'},
'counter_name': '2',
'message_id': '4',
'project_id': '3',
'recorded_at': '2015-03-09T12:15:57.233772',
'resource_id': '5',
@ -30,39 +31,24 @@ SAMPLE = {
'volume': '11.1',
}
OLD_SAMPLE = {
'counter_name': '1',
'counter_type': '2',
'counter_unit': '3',
'counter_volume': '4',
'message_id': '0',
'project_id': '5',
'recorded_at': '2015-03-09T12:15:57.233772',
'resource_id': '7',
'resource_metadata': '8',
'source': '9',
'timestamp': '2015-03-09T12:15:57.233772',
'user_id': '11',
}
class TestSample(testtools.TestCase):
def test_basic(self):
sot = sample.Sample(SAMPLE)
sot = sample.Sample()
self.assertIsNone(sot.resource_key)
self.assertIsNone(sot.resources_key)
self.assertEqual('/meters/%(counter_name)s', sot.base_path)
self.assertEqual('metering', sot.service.service_type)
self.assertTrue(sot.allow_create)
self.assertFalse(sot.allow_retrieve)
self.assertFalse(sot.allow_create)
self.assertTrue(sot.allow_get)
self.assertFalse(sot.allow_update)
self.assertFalse(sot.allow_delete)
self.assertTrue(sot.allow_list)
def test_make_new(self):
sot = sample.Sample(SAMPLE)
self.assertEqual(SAMPLE['sample_id'], sot.id)
sot = sample.Sample(**SAMPLE)
self.assertEqual(SAMPLE['message_id'], sot.id)
self.assertEqual(SAMPLE['metadata'], sot.metadata)
self.assertEqual(SAMPLE['counter_name'], sot.counter_name)
self.assertEqual(SAMPLE['project_id'], sot.project_id)
@ -75,31 +61,15 @@ class TestSample(testtools.TestCase):
self.assertEqual(SAMPLE['user_id'], sot.user_id)
self.assertEqual(SAMPLE['volume'], sot.volume)
def test_make_old(self):
sot = sample.Sample(OLD_SAMPLE)
self.assertIsNone(sot.id)
self.assertEqual(OLD_SAMPLE['counter_name'], sot.counter_name)
self.assertEqual(OLD_SAMPLE['counter_type'], sot.type)
self.assertEqual(OLD_SAMPLE['counter_unit'], sot.unit)
self.assertEqual(OLD_SAMPLE['counter_volume'], sot.volume)
self.assertEqual(OLD_SAMPLE['project_id'], sot.project_id)
self.assertEqual(OLD_SAMPLE['recorded_at'], sot.recorded_at)
self.assertEqual(OLD_SAMPLE['resource_id'], sot.resource_id)
self.assertEqual(OLD_SAMPLE['resource_metadata'], sot.metadata)
self.assertEqual(OLD_SAMPLE['source'], sot.source)
self.assertEqual(OLD_SAMPLE['timestamp'], sot.generated_at)
self.assertEqual(OLD_SAMPLE['user_id'], sot.user_id)
def test_list(self):
sess = mock.Mock()
resp = mock.Mock()
resp.json = mock.Mock(return_value=[SAMPLE, OLD_SAMPLE])
resp.json = mock.Mock(return_value=[SAMPLE])
sess.get = mock.Mock(return_value=resp)
path_args = {'counter_name': 'name_of_meter'}
found = sample.Sample.list(sess, path_args=path_args)
found = sample.Sample.list(sess, counter_name='name_of_meter')
first = next(found)
self.assertEqual(SAMPLE['sample_id'], first.id)
self.assertEqual(SAMPLE['message_id'], first.id)
self.assertEqual(SAMPLE['metadata'], first.metadata)
self.assertEqual(SAMPLE['counter_name'], first.counter_name)
self.assertEqual(SAMPLE['project_id'], first.project_id)
@ -111,26 +81,3 @@ class TestSample(testtools.TestCase):
self.assertEqual(SAMPLE['unit'], first.unit)
self.assertEqual(SAMPLE['user_id'], first.user_id)
self.assertEqual(SAMPLE['volume'], first.volume)
second = next(found)
self.assertEqual(OLD_SAMPLE['counter_name'], second.counter_name)
def test_create(self):
sess = mock.Mock()
resp = mock.Mock()
resp.json = mock.Mock(return_value=[SAMPLE])
sess.post = mock.Mock(return_value=resp)
data = {'sample_id': None,
'counter_name': 'temperature',
'project_id': 'project',
'resource_id': 'resource',
'type': 'gauge',
'unit': 'instance',
'volume': '98.6'}
new_sample = sample.Sample.new(**data)
new_sample.create(sess)
url = '/meters/temperature'
sess.post.assert_called_with(url, endpoint_filter=new_sample.service,
json=[data])
self.assertIsNone(new_sample.id)

View File

@ -42,14 +42,14 @@ class TestStatistics(testtools.TestCase):
self.assertEqual('/meters/%(meter_name)s/statistics', sot.base_path)
self.assertEqual('metering', sot.service.service_type)
self.assertFalse(sot.allow_create)
self.assertFalse(sot.allow_retrieve)
self.assertFalse(sot.allow_get)
self.assertFalse(sot.allow_update)
self.assertFalse(sot.allow_delete)
self.assertTrue(sot.allow_list)
def test_make_it(self):
sot = statistics.Statistics(EXAMPLE)
self.assertIsNone(sot.id)
sot = statistics.Statistics(**EXAMPLE)
self.assertEqual(EXAMPLE['unit'], sot.id)
self.assertEqual(EXAMPLE['aggregate'], sot.aggregate)
self.assertEqual(EXAMPLE['avg'], sot.avg)
self.assertEqual(EXAMPLE['count'], sot.count)
@ -70,13 +70,24 @@ class TestStatistics(testtools.TestCase):
resp = mock.Mock()
resp.json = mock.Mock(return_value=[EXAMPLE])
sess.get = mock.Mock(return_value=resp)
args = {'meter_name': 'example'}
reply = statistics.Statistics.list(sess, path_args=args)
reply = statistics.Statistics.list(sess, meter_name='example')
url = '/meters/example/statistics'
stat = next(reply)
sess.get.assert_called_with(url, endpoint_filter=stat.service,
params={})
self.assertEqual(EXAMPLE, stat)
self.assertEqual(EXAMPLE['aggregate'], stat.aggregate)
self.assertEqual(EXAMPLE['avg'], stat.avg)
self.assertEqual(EXAMPLE['count'], stat.count)
self.assertEqual(EXAMPLE['duration'], stat.duration)
self.assertEqual(EXAMPLE['duration_end'], stat.duration_end_at)
self.assertEqual(EXAMPLE['duration_start'], stat.duration_start_at)
self.assertEqual(EXAMPLE['groupby'], stat.group_by)
self.assertEqual(EXAMPLE['max'], stat.max)
self.assertEqual(EXAMPLE['min'], stat.min)
self.assertEqual(EXAMPLE['period'], stat.period)
self.assertEqual(EXAMPLE['period_end'], stat.period_end_at)
self.assertEqual(EXAMPLE['period_start'], stat.period_start_at)
self.assertEqual(EXAMPLE['sum'], stat.sum)
self.assertEqual(EXAMPLE['unit'], stat.unit)
self.assertRaises(StopIteration, next, reply)