v1-api: Adapted resouce/user api.

Added project-id support in resource api and source-id support in user
api.

Change-Id: I3295a36a4b3a57e9451cc042d542ead1354f8e61
This commit is contained in:
Lianhao Lu
2013-02-20 13:38:14 +08:00
parent d740e37672
commit feb4bcc614
5 changed files with 177 additions and 5 deletions

View File

@@ -32,7 +32,12 @@ class UserManager(base.Manager):
resource_class = User
def list(self, **kwargs):
return self._list('/v1/users', 'users')
s = kwargs.get('source')
if s:
path = '/sources/%s/users' % (s)
else:
path = '/users'
return self._list('/v1%s' % path, 'users')
class Project(base.Resource):
@@ -57,7 +62,7 @@ class ProjectManager(base.Manager):
else:
path = '/projects'
return self._list('/v1/%s' % path, 'projects')
return self._list('/v1%s' % path, 'projects')
class Resource(base.Resource):
@@ -74,11 +79,14 @@ class ResourceManager(base.Manager):
def list(self, **kwargs):
u = kwargs.get('user_id')
s = kwargs.get('source')
p = kwargs.get('project_id')
opts = kwargs.get('metaquery')
if u:
path = '/users/%s/resources' % (u)
elif s:
path = '/sources/%s/resources' % (s)
elif p:
path = '/projects/%s/resources' % (p)
else:
path = '/resources'
if opts:

View File

@@ -71,9 +71,11 @@ def do_meter_list(cc, args={}):
sortby=0)
@utils.arg('-s', '--source', metavar='<SOURCE>',
help='ID of the resource to show projects for.')
def do_user_list(cc, args={}):
'''List the users'''
kwargs = {}
kwargs = {'source': args.source}
users = cc.users.list(**kwargs)
field_labels = ['User ID']
fields = ['user_id']
@@ -85,10 +87,16 @@ def do_user_list(cc, args={}):
help='ID of the resource to show for.')
@utils.arg('-u', '--user_id', metavar='<USER_ID>',
help='ID of the user to show resources for.')
@utils.arg('-p', '--project_id', metavar='<PROJECT_ID>',
help='ID of the project to show samples for.')
@utils.arg('-m', '--metaquery', metavar='<METAQUERY>',
help='Query into the metadata metadata.key=value:..')
def do_resource_list(cc, args={}):
'''List the users'''
kwargs = {'source': args.source,
'user_id': args.user_id}
'user_id': args.user_id,
'project_id': args.project_id,
'metaquery': args.metaquery}
resources = cc.resources.list(**kwargs)
field_labels = ['Resource ID', 'Source', 'User ID', 'Project ID']

66
tests/v1/test_projects.py Normal file
View File

@@ -0,0 +1,66 @@
# Copyright 2012 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import unittest
import ceilometerclient.v1.meters
from tests import utils
fixtures = {
'/v1/projects': {
'GET': (
{},
{'projects': [
'a',
'b',
]},
),
},
'/v1/sources/source_b/projects': {
'GET': (
{},
{'projects': [
'b',
]},
),
},
}
class ProjectManagerTest(unittest.TestCase):
def setUp(self):
self.api = utils.FakeAPI(fixtures)
self.mgr = ceilometerclient.v1.meters.ProjectManager(self.api)
def test_list_all(self):
projects = list(self.mgr.list())
expect = [
('GET', '/v1/projects', {}, None),
]
self.assertEqual(self.api.calls, expect)
self.assertEqual(len(projects), 2)
self.assertEqual(projects[0].project_id, 'a')
self.assertEqual(projects[1].project_id, 'b')
def test_list_by_source(self):
projects = list(self.mgr.list(source='source_b'))
expect = [
('GET', '/v1/sources/source_b/projects', {}, None),
]
self.assertEqual(self.api.calls, expect)
self.assertEqual(len(projects), 1)
self.assertEqual(projects[0].project_id, 'b')

View File

@@ -26,7 +26,7 @@ fixtures = {
{'resources': [
{
'resource_id': 'a',
'project_id': 'dig_the_ditch',
'project_id': 'project_bla',
'user_id': 'freddy',
'timestamp': 'now',
'meter': ['this', 'that'],
@@ -73,6 +73,21 @@ fixtures = {
]},
),
},
'/v1/projects/project_bla/resources': {
'GET': (
{},
{'resources': [
{
'resource_id': 'a',
'project_id': 'project_bla',
'user_id': 'freddy',
'timestamp': 'now',
'meter': ['this', 'that'],
'metadata': {'zxc_id': 'bla'},
},
]},
),
},
}
@@ -109,3 +124,12 @@ class ResourceManagerTest(unittest.TestCase):
self.assertEqual(self.api.calls, expect)
self.assertEqual(len(resources), 1)
self.assertEqual(resources[0].resource_id, 'b')
def test_list_by_project(self):
resources = list(self.mgr.list(project_id='project_bla'))
expect = [
('GET', '/v1/projects/project_bla/resources', {}, None),
]
self.assertEqual(self.api.calls, expect)
self.assertEqual(len(resources), 1)
self.assertEqual(resources[0].resource_id, 'a')

66
tests/v1/test_users.py Normal file
View File

@@ -0,0 +1,66 @@
# Copyright 2012 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import unittest
import ceilometerclient.v1.meters
from tests import utils
fixtures = {
'/v1/users': {
'GET': (
{},
{'users': [
'a',
'b',
]},
),
},
'/v1/sources/source_b/users': {
'GET': (
{},
{'users': [
'b',
]},
),
},
}
class UserManagerTest(unittest.TestCase):
def setUp(self):
self.api = utils.FakeAPI(fixtures)
self.mgr = ceilometerclient.v1.meters.UserManager(self.api)
def test_list_all(self):
users = list(self.mgr.list())
expect = [
('GET', '/v1/users', {}, None),
]
self.assertEqual(self.api.calls, expect)
self.assertEqual(len(users), 2)
self.assertEqual(users[0].user_id, 'a')
self.assertEqual(users[1].user_id, 'b')
def test_list_by_source(self):
users = list(self.mgr.list(source='source_b'))
expect = [
('GET', '/v1/sources/source_b/users', {}, None),
]
self.assertEqual(self.api.calls, expect)
self.assertEqual(len(users), 1)
self.assertEqual(users[0].user_id, 'b')