Allow create resource type

Change-Id: I4c76bfe882ccb7cc20ccb31d87d07e1b7984a914
This commit is contained in:
Mehdi Abaakouk 2016-04-12 16:57:24 +02:00
parent 6e9e005859
commit 749ed545a1
6 changed files with 92 additions and 7 deletions

View File

@ -48,6 +48,7 @@ class GnocchiCommandManager(commandmanager.CommandManager):
"resource delete": resource_cli.CliResourceDelete,
"resource list-types": resource_cli.CliResourceTypeList,
"resource-type list": resource_type_cli.CliResourceTypeList,
"resource-type create": resource_type_cli.CliResourceTypeCreate,
"archive-policy list": archive_policy_cli.CliArchivePolicyList,
"archive-policy show": archive_policy_cli.CliArchivePolicyShow,
"archive-policy create": archive_policy_cli.CliArchivePolicyCreate,

View File

@ -179,10 +179,8 @@ class ResourceClientTest(base.ClientTestBase):
result = self.gnocchi(
'resource', params="list-types")
r = self.parser.listing(result)
self.assertEqual([
{
'resource_controller_url':
'http://localhost:8041/v1/resource/generic',
'resource_type': 'generic'
},
], r)
self.assertIn({
'resource_controller_url':
'http://localhost:8041/v1/resource/generic',
'resource_type': 'generic'
}, r)

View File

@ -10,10 +10,13 @@
# License for the specific language governing permissions and limitations
# under the License.
import uuid
from gnocchiclient.tests.functional import base
class ResourceTypeClientTest(base.ClientTestBase):
RESOURCE_TYPE = str(uuid.uuid4())
def test_help(self):
self.gnocchi("help", params="resource list")
@ -23,3 +26,14 @@ class ResourceTypeClientTest(base.ClientTestBase):
result = self.gnocchi('resource-type', params="list")
r = self.parser.listing(result)
self.assertEqual([{'attributes': '', 'name': 'generic'}], r)
# CREATE
result = self.gnocchi(
u'resource-type',
params=u"create -a foo:string:1:max_length=16 "
"-a bar:number:no:max=32 %s" % self.RESOURCE_TYPE)
resource = self.details_multiple(result)[0]
self.assertEqual(self.RESOURCE_TYPE, resource["name"])
self.assertEqual(
"max_length=16, min_length=0, required=True, type=string",
resource["attributes/foo"])

View File

@ -141,6 +141,14 @@ def format_move_dict_to_root(obj, field):
del obj[field]
def format_resource_type(rt):
format_move_dict_to_root(rt, "attributes")
for key in rt:
if key.startswith("attributes"):
rt[key] = ", ".join(
"%s=%s" % (k, v) for k, v in sorted(rt[key].items()))
def format_archive_policy(ap):
format_dict_list(ap, "definition")
format_string_list(ap, "aggregation_methods")

View File

@ -11,6 +11,8 @@
# License for the specific language governing permissions and limitations
# under the License.
from oslo_serialization import jsonutils
from gnocchiclient.v1 import base
@ -20,3 +22,14 @@ class ResourceTypeManager(base.Manager):
def list(self):
"""List resource types."""
return self._get(self.url).json()
def create(self, resource_type):
"""Create a resource type
:param resource_type: Resource type
:type resource_type: dict
"""
return self._post(
self.url,
headers={'Content-Type': "application/json"},
data=jsonutils.dumps(resource_type)).json()

View File

@ -11,6 +11,8 @@
# License for the specific language governing permissions and limitations
# under the License.
from cliff import lister
from cliff import show
from oslo_utils import strutils
from gnocchiclient import utils
@ -26,3 +28,52 @@ class CliResourceTypeList(lister.Lister):
resource_type['attributes'] = utils.format_dict_dict(
resource_type['attributes'])
return utils.list2cols(self.COLS, resource_types)
class CliResourceTypeCreate(show.ShowOne):
"""Create a resource type"""
def get_parser(self, prog_name):
parser = super(CliResourceTypeCreate, self).get_parser(prog_name)
parser.add_argument("name", help="name of the resource type")
parser.add_argument("-a", "--attribute", action='append',
type=self._resource_attribute,
default=[],
help=(u"attribute definition, "
u"attribute_name:"
u"attribute_type:"
u"attribute_is_required:"
u"attribute_type_option_name="
u"attribute_type_option_value:\u2026 "
u"For example: "
u"display_name:string:true:max_length=255"))
return parser
@classmethod
def _resource_attribute(cls, value):
config = value.split(":")
name = config.pop(0)
attrs = {}
if config:
attrs["type"] = config.pop(0)
if config:
attrs["required"] = strutils.bool_from_string(config.pop(0),
strict=True)
while config:
param, _, value = config.pop(0).partition("=")
try:
attrs[param] = int(value)
except ValueError:
try:
attrs[param] = float(value)
except ValueError:
attrs[param] = value
return (name, attrs)
def take_action(self, parsed_args):
resource_type = {'name': parsed_args.name}
if parsed_args.attribute:
resource_type['attributes'] = dict(parsed_args.attribute)
res = self.app.client.resource_type.create(resource_type=resource_type)
utils.format_resource_type(res)
return self.dict2columns(res)