Add api resource field
This file is used for manage the various fields that a resource contains, such as Chassis, Node, System. An individual field consists of a 'field_id'(key) and 'label'(value). Change-Id: I6a9a460abb328f34f6098fbe475fc8656dcd3cdc
This commit is contained in:
parent
fcdf14c395
commit
abf1f61565
70
valenceclient/tests/unit/v1/test_resource_fields.py
Normal file
70
valenceclient/tests/unit/v1/test_resource_fields.py
Normal file
@ -0,0 +1,70 @@
|
||||
# Copyright 2017 99cloud Inc.
|
||||
# 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 testtools
|
||||
|
||||
from valenceclient.v1 import resource_fields
|
||||
|
||||
|
||||
class ResourceTest(testtools.TestCase):
|
||||
def setUp(self):
|
||||
super(ResourceTest, self).setUp()
|
||||
self._saved_ids = resource_fields.Resource.FIELDS
|
||||
resource_fields.Resource.FIELDS = {
|
||||
'item1': 'ITEM1',
|
||||
'2nd_item': 'A second item',
|
||||
'item_3': 'Third item',
|
||||
}
|
||||
|
||||
def tearDown(self):
|
||||
super(ResourceTest, self).tearDown()
|
||||
resource_fields.Resource.FIELDS = self._saved_ids
|
||||
|
||||
def test_fields_single_value(self):
|
||||
# Make sure single value is what we expect
|
||||
foo = resource_fields.Resource(['item1'])
|
||||
self.assertEqual(('item1',), foo.fields)
|
||||
self.assertEqual(('ITEM1',), foo.labels)
|
||||
self.assertEqual(('item1',), foo.sort_fields)
|
||||
self.assertEqual(('ITEM1',), foo.sort_labels)
|
||||
|
||||
def test_fields_multiple_value_order(self):
|
||||
# Make sure order is maintained
|
||||
foo = resource_fields.Resource(['2nd_item', 'item1'])
|
||||
self.assertEqual(('2nd_item', 'item1'), foo.fields)
|
||||
self.assertEqual(('A second item', 'ITEM1'), foo.labels)
|
||||
self.assertEqual(('2nd_item', 'item1'), foo.sort_fields)
|
||||
self.assertEqual(('A second item', 'ITEM1'), foo.sort_labels)
|
||||
|
||||
def test_sort_excluded(self):
|
||||
# Test excluding of fields for sort purposes
|
||||
foo = resource_fields.Resource(['item_3', 'item1', '2nd_item'],
|
||||
sort_excluded=['item1'])
|
||||
self.assertEqual(('item_3', '2nd_item'), foo.sort_fields)
|
||||
self.assertEqual(('Third item', 'A second item'), foo.sort_labels)
|
||||
|
||||
def test_sort_excluded_unknown(self):
|
||||
# Test sort_excluded value not in the field_ids
|
||||
self.assertRaises(
|
||||
ValueError,
|
||||
resource_fields.Resource,
|
||||
['item_3', 'item1', '2nd_item'],
|
||||
sort_excluded=['item1', 'foo'])
|
||||
|
||||
def test_unknown_field_id(self):
|
||||
self.assertRaises(
|
||||
KeyError,
|
||||
resource_fields.Resource,
|
||||
['item1', 'unknown_id'])
|
127
valenceclient/v1/resource_fields.py
Normal file
127
valenceclient/v1/resource_fields.py
Normal file
@ -0,0 +1,127 @@
|
||||
# Copyright 2017 99cloud, Inc.
|
||||
# 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.
|
||||
|
||||
from valenceclient.common.i18n import _
|
||||
|
||||
|
||||
class Resource(object):
|
||||
"""Resource class
|
||||
|
||||
This class is used to manage the various fields that a resource (e.g.
|
||||
Chassis, Node, Port) contains. An individual field consists of a
|
||||
'field_id' (key) and a 'label' (value). The caller only provides the
|
||||
'field_ids' when instantiating the object.
|
||||
Ordering of the 'field_ids' will be preserved as specified by the caller.
|
||||
It also provides the ability to exclude some of these fields when they are
|
||||
being used for sorting.
|
||||
"""
|
||||
|
||||
FIELDS = {
|
||||
'id': 'uuid',
|
||||
'uuid': 'id',
|
||||
'bmcmac': 'bmcmac',
|
||||
'bmcip': 'bmcip',
|
||||
'nw': 'nw',
|
||||
'location': 'location',
|
||||
'ram': 'ram',
|
||||
'storage': 'storage',
|
||||
'cpu': 'cpu',
|
||||
'arch': 'arch',
|
||||
'systemurl': 'systemurl'
|
||||
|
||||
}
|
||||
|
||||
def __init__(self, field_ids, sort_excluded=None):
|
||||
"""Create a Resource object
|
||||
|
||||
:param field_ids: A list of strings that the Resource object will
|
||||
contain. Each string must match an existing key in
|
||||
FIELDS.
|
||||
:param sort_excluded: Optional. A list of strings that will not be used
|
||||
for sorting. Must be a subset of 'field_ids'.
|
||||
:raises: ValueError if sort_excluded contains value not in field_ids
|
||||
"""
|
||||
|
||||
self._fields = tuple(field_ids)
|
||||
self._labels = tuple([self.FIELDS[x] for x in field_ids])
|
||||
if sort_excluded is None:
|
||||
sort_excluded = []
|
||||
not_existing = set(sort_excluded) - set(field_ids)
|
||||
if not_existing:
|
||||
raise ValueError(
|
||||
_("sort_excluded specified with value not contained in "
|
||||
"field_ids. Unknown value(s): %s") % ','.join(not_existing))
|
||||
self._sort_fields = tuple(
|
||||
[x for x in field_ids if x not in sort_excluded])
|
||||
self._sort_labels = tuple([self.FIELDS[x] for x in self._sort_fields])
|
||||
|
||||
@property
|
||||
def fields(self):
|
||||
return self._fields
|
||||
|
||||
@property
|
||||
def labels(self):
|
||||
return self._labels
|
||||
|
||||
@property
|
||||
def sort_fields(self):
|
||||
return self._sort_fields
|
||||
|
||||
@property
|
||||
def sort_labels(self):
|
||||
return self._sort_labels
|
||||
|
||||
|
||||
NODE_DETAIL_RESOURCE = Resource(
|
||||
['id',
|
||||
'uuid',
|
||||
'bmcmac',
|
||||
'bmcip',
|
||||
'nw',
|
||||
'location',
|
||||
'ram',
|
||||
'storage',
|
||||
'cpu',
|
||||
'arch',
|
||||
'systemurl'
|
||||
])
|
||||
|
||||
|
||||
NODE_RESOURCE = Resource(
|
||||
['id',
|
||||
'uuid',
|
||||
'bmcip',
|
||||
'location',
|
||||
])
|
||||
|
||||
SYSTEM_DETAIL_RESOURCE = Resource(
|
||||
['id',
|
||||
'uuid',
|
||||
'bmcmac',
|
||||
'bmcip',
|
||||
'nw',
|
||||
'location',
|
||||
'ram',
|
||||
'storage',
|
||||
'cpu',
|
||||
'arch',
|
||||
])
|
||||
|
||||
SYSTEM_RESOURCE = Resource(
|
||||
['id',
|
||||
'uuid',
|
||||
'bmcip',
|
||||
'location',
|
||||
])
|
Loading…
x
Reference in New Issue
Block a user