resource: Set correct attribute type

Currently all resources attributes are transmitted as string to the
server. But if the attribute type is a number or a bool, the json must
have the value as float or bool too.

This change does that.

Closes-bug: #1649269
Change-Id: I0f739f782bf081761d289398bcd4185af3d0e7b4
(cherry picked from commit c77109d8b3)
This commit is contained in:
Mehdi Abaakouk 2017-01-04 16:12:24 +01:00
parent faf0a99a94
commit 8957c54e28
2 changed files with 45 additions and 0 deletions

View File

@ -17,6 +17,8 @@ from gnocchiclient.tests.functional import base
class ResourceTypeClientTest(base.ClientTestBase):
RESOURCE_TYPE = str(uuid.uuid4())
RESOURCE_TYPE2 = str(uuid.uuid4())
RESOURCE_ID = str(uuid.uuid4())
def test_help(self):
self.gnocchi("help", params="resource list")
@ -47,6 +49,39 @@ class ResourceTypeClientTest(base.ClientTestBase):
"max_length=16, min_length=0, required=True, type=string",
resource["attributes/foo"])
# PATCH
result = self.gnocchi(
u'resource-type',
params=u"create "
"-a new:number:no:max=16 %s" % self.RESOURCE_TYPE2)
resource = self.details_multiple(result)[0]
self.assertEqual(self.RESOURCE_TYPE2, resource["name"])
self.assertNotIn("attributes/foo", resource)
self.assertEqual(
"max=16, min=None, required=False, type=number",
resource["attributes/new"])
# SHOW
result = self.gnocchi(
u'resource-type', params=u"show %s" % self.RESOURCE_TYPE2)
resource = self.details_multiple(result)[0]
self.assertEqual(self.RESOURCE_TYPE2, resource["name"])
self.assertNotIn("attributes/foo", resource)
self.assertEqual(
"max=16, min=None, required=False, type=number",
resource["attributes/new"])
# Create a resource for this type
result = self.gnocchi(
u'resource', params=(u"create %s -t %s -a new:5") %
(self.RESOURCE_ID, self.RESOURCE_TYPE2))
resource = self.details_multiple(result)[0]
self.assertEqual(self.RESOURCE_ID, resource["id"])
self.assertEqual('5.0', resource["new"])
# Delete the resource
self.gnocchi('resource', params="delete %s" % self.RESOURCE_ID)
# DELETE
result = self.gnocchi('resource-type',
params="delete %s" % self.RESOURCE_TYPE)

View File

@ -14,6 +14,8 @@ from cliff import command
from cliff import lister
from cliff import show
from oslo_utils import strutils
from gnocchiclient import exceptions
from gnocchiclient import utils
@ -140,12 +142,20 @@ class CliResourceCreate(show.ShowOne):
return parser
def _resource_from_args(self, parsed_args, update=False):
# Get the resource type to set the correct type
rt_attrs = utils.get_client(self).resource_type.get(
name=parsed_args.resource_type)['attributes']
resource = {}
if not update:
resource['id'] = parsed_args.resource_id
if parsed_args.attribute:
for attr in parsed_args.attribute:
attr, __, value = attr.partition(":")
attr_type = rt_attrs.get(attr, {}).get('type')
if attr_type == "number":
value = float(value)
elif attr_type == "bool":
value = strutils.bool_from_string(value)
resource[attr] = value
if (parsed_args.add_metric
or parsed_args.create_metric