Merge "Introduce shadow resources for NeutronCommands"

This commit is contained in:
Jenkins
2014-08-04 14:41:52 +00:00
committed by Gerrit Code Review
2 changed files with 97 additions and 52 deletions

View File

@@ -45,9 +45,12 @@ def _get_resource_plural(resource, client):
return resource + 's'
def find_resourceid_by_id(client, resource, resource_id):
def find_resourceid_by_id(client, resource, resource_id, cmd_resource=None):
if not cmd_resource:
cmd_resource = resource
cmd_resource_plural = _get_resource_plural(cmd_resource, client)
resource_plural = _get_resource_plural(resource, client)
obj_lister = getattr(client, "list_%s" % resource_plural)
obj_lister = getattr(client, "list_%s" % cmd_resource_plural)
# perform search by id only if we are passing a valid UUID
match = re.match(UUID_PATTERN, resource_id)
collection = resource_plural
@@ -63,9 +66,13 @@ def find_resourceid_by_id(client, resource, resource_id):
message=not_found_message, status_code=404)
def _find_resourceid_by_name(client, resource, name, project_id=None):
def _find_resourceid_by_name(client, resource, name, project_id=None,
cmd_resource=None):
if not cmd_resource:
cmd_resource = resource
cmd_resource_plural = _get_resource_plural(cmd_resource, client)
resource_plural = _get_resource_plural(resource, client)
obj_lister = getattr(client, "list_%s" % resource_plural)
obj_lister = getattr(client, "list_%s" % cmd_resource_plural)
params = {'name': name, 'fields': 'id'}
if project_id:
params['tenant_id'] = project_id
@@ -87,12 +94,13 @@ def _find_resourceid_by_name(client, resource, name, project_id=None):
def find_resourceid_by_name_or_id(client, resource, name_or_id,
project_id=None):
project_id=None, cmd_resource=None):
try:
return find_resourceid_by_id(client, resource, name_or_id)
return find_resourceid_by_id(client, resource, name_or_id,
cmd_resource)
except exceptions.NeutronClientException:
return _find_resourceid_by_name(client, resource, name_or_id,
project_id)
project_id, cmd_resource)
def add_show_list_common_argument(parser):
@@ -360,6 +368,8 @@ class NeutronCommand(command.OpenStackCommand):
api = 'network'
values_specs = []
json_indent = None
resource = None
shadow_resource = None
def __init__(self, app, app_args):
super(NeutronCommand, self).__init__(app, app_args)
@@ -369,6 +379,12 @@ class NeutronCommand(command.OpenStackCommand):
#if hasattr(self, 'formatters'):
#self.formatters['table'] = TableFormater()
@property
def cmd_resource(self):
if self.shadow_resource:
return self.shadow_resource
return self.resource
def get_client(self):
return self.app.client_manager.neutron
@@ -414,7 +430,6 @@ class CreateCommand(NeutronCommand, show.ShowOne):
"""
api = 'network'
resource = None
log = None
def get_parser(self, prog_name):
@@ -438,7 +453,7 @@ class CreateCommand(NeutronCommand, show.ShowOne):
body = self.args2body(parsed_args)
body[self.resource].update(_extra_values)
obj_creator = getattr(neutron_client,
"create_%s" % self.resource)
"create_%s" % self.cmd_resource)
data = obj_creator(body)
self.format_output_data(data)
info = self.resource in data and data[self.resource] or None
@@ -455,7 +470,6 @@ class UpdateCommand(NeutronCommand):
"""
api = 'network'
resource = None
log = None
allow_names = True
@@ -481,15 +495,18 @@ class UpdateCommand(NeutronCommand):
body[self.resource] = _extra_values
if not body[self.resource]:
raise exceptions.CommandError(
_("Must specify new values to update %s") % self.resource)
_("Must specify new values to update %s") %
self.cmd_resource)
if self.allow_names:
_id = find_resourceid_by_name_or_id(
neutron_client, self.resource, parsed_args.id)
neutron_client, self.resource, parsed_args.id,
cmd_resource=self.cmd_resource)
else:
_id = find_resourceid_by_id(
neutron_client, self.resource, parsed_args.id)
neutron_client, self.resource, parsed_args.id,
self.cmd_resource)
obj_updator = getattr(neutron_client,
"update_%s" % self.resource)
"update_%s" % self.cmd_resource)
obj_updator(_id, body)
print((_('Updated %(resource)s: %(id)s') %
{'id': parsed_args.id, 'resource': self.resource}),
@@ -503,7 +520,6 @@ class DeleteCommand(NeutronCommand):
"""
api = 'network'
resource = None
log = None
allow_names = True
@@ -523,12 +539,16 @@ class DeleteCommand(NeutronCommand):
neutron_client = self.get_client()
neutron_client.format = parsed_args.request_format
obj_deleter = getattr(neutron_client,
"delete_%s" % self.resource)
"delete_%s" % self.cmd_resource)
if self.allow_names:
_id = find_resourceid_by_name_or_id(neutron_client, self.resource,
parsed_args.id)
params = {'cmd_resource': self.cmd_resource}
_id = find_resourceid_by_name_or_id(neutron_client,
self.resource,
parsed_args.id,
**params)
else:
_id = parsed_args.id
obj_deleter(_id)
print((_('Deleted %(resource)s: %(id)s')
% {'id': parsed_args.id,
@@ -543,7 +563,6 @@ class ListCommand(NeutronCommand, lister.Lister):
"""
api = 'network'
resource = None
log = None
_formatters = {}
list_columns = []
@@ -570,7 +589,8 @@ class ListCommand(NeutronCommand, lister.Lister):
return search_opts
def call_server(self, neutron_client, search_opts, parsed_args):
resource_plural = _get_resource_plural(self.resource, neutron_client)
resource_plural = _get_resource_plural(self.cmd_resource,
neutron_client)
obj_lister = getattr(neutron_client, "list_%s" % resource_plural)
data = obj_lister(**search_opts)
return data
@@ -642,7 +662,6 @@ class ShowCommand(NeutronCommand, show.ShowOne):
"""
api = 'network'
resource = None
log = None
allow_names = True
@@ -670,11 +689,12 @@ class ShowCommand(NeutronCommand, show.ShowOne):
params = {'fields': parsed_args.fields}
if self.allow_names:
_id = find_resourceid_by_name_or_id(neutron_client, self.resource,
parsed_args.id)
parsed_args.id,
cmd_resource=self.cmd_resource)
else:
_id = parsed_args.id
obj_shower = getattr(neutron_client, "show_%s" % self.resource)
obj_shower = getattr(neutron_client, "show_%s" % self.cmd_resource)
data = obj_shower(_id, **params)
self.format_output_data(data)
resource = data[self.resource]

View File

@@ -162,7 +162,8 @@ class CLITestV20Base(base.BaseTestCase):
test_id = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
id_field = 'id'
def _find_resourceid(self, client, resource, name_or_id):
def _find_resourceid(self, client, resource, name_or_id,
cmd_resource=None):
return name_or_id
def _get_attr_metadata(self):
@@ -197,11 +198,10 @@ class CLITestV20Base(base.BaseTestCase):
self._get_attr_metadata))
self.client = client.Client(token=TOKEN, endpoint_url=self.endurl)
def _test_create_resource(self, resource, cmd,
name, myid, args,
position_names, position_values, tenant_id=None,
tags=None, admin_state_up=True, extra_body=None,
**kwargs):
def _test_create_resource(self, resource, cmd, name, myid, args,
position_names, position_values,
tenant_id=None, tags=None, admin_state_up=True,
extra_body=None, cmd_resource=None, **kwargs):
self.mox.StubOutWithMock(cmd, "get_client")
self.mox.StubOutWithMock(self.client.httpclient, "request")
cmd.get_client().MultipleTimes().AndReturn(self.client)
@@ -212,6 +212,8 @@ class CLITestV20Base(base.BaseTestCase):
'policy_profile', 'ikepolicy',
'ipsecpolicy', 'metering_label',
'metering_label_rule', 'net_partition']
if not cmd_resource:
cmd_resource = resource
if (resource in non_admin_status_resources):
body = {resource: {}, }
else:
@@ -233,7 +235,7 @@ class CLITestV20Base(base.BaseTestCase):
self.client.format = self.format
resstr = self.client.serialize(ress)
# url method body
resource_plural = neutronV2_0._get_resource_plural(resource,
resource_plural = neutronV2_0._get_resource_plural(cmd_resource,
self.client)
path = getattr(self.client, resource_plural + "_path")
# Work around for LP #1217791. XML deserializer called from
@@ -258,15 +260,19 @@ class CLITestV20Base(base.BaseTestCase):
if name:
self.assertIn(name, _str)
def _test_list_columns(self, cmd, resources_collection,
resources_out, args=['-f', 'json']):
def _test_list_columns(self, cmd, resources,
resources_out, args=['-f', 'json'],
cmd_resources=None):
self.mox.StubOutWithMock(cmd, "get_client")
self.mox.StubOutWithMock(self.client.httpclient, "request")
cmd.get_client().MultipleTimes().AndReturn(self.client)
self.client.format = self.format
if not cmd_resources:
cmd_resources = resources
resstr = self.client.serialize(resources_out)
path = getattr(self.client, resources_collection + "_path")
path = getattr(self.client, cmd_resources + "_path")
self.client.httpclient.request(
end_url(path, format=self.format), 'GET',
body=None,
@@ -274,7 +280,7 @@ class CLITestV20Base(base.BaseTestCase):
'X-Auth-Token', TOKEN)).AndReturn((MyResp(200), resstr))
args.extend(['--request-format', self.format])
self.mox.ReplayAll()
cmd_parser = cmd.get_parser("list_" + resources_collection)
cmd_parser = cmd.get_parser("list_" + cmd_resources)
shell.run_command(cmd, cmd_parser, args)
self.mox.VerifyAll()
self.mox.UnsetStubs()
@@ -282,10 +288,12 @@ class CLITestV20Base(base.BaseTestCase):
def _test_list_resources(self, resources, cmd, detail=False, tags=[],
fields_1=[], fields_2=[], page_size=None,
sort_key=[], sort_dir=[], response_contents=None,
base_args=None, path=None):
base_args=None, path=None, cmd_resources=None):
self.mox.StubOutWithMock(cmd, "get_client")
self.mox.StubOutWithMock(self.client.httpclient, "request")
cmd.get_client().MultipleTimes().AndReturn(self.client)
if not cmd_resources:
cmd_resources = resources
if response_contents is None:
contents = [{self.id_field: 'myid1', },
{self.id_field: 'myid2', }, ]
@@ -357,7 +365,7 @@ class CLITestV20Base(base.BaseTestCase):
query += '&'
query += 'sort_dir=%s' % dir
if path is None:
path = getattr(self.client, resources + "_path")
path = getattr(self.client, cmd_resources + "_path")
self.client.httpclient.request(
MyUrlComparator(end_url(path, query, format=self.format),
self.client),
@@ -366,7 +374,7 @@ class CLITestV20Base(base.BaseTestCase):
headers=mox.ContainsKeyValue(
'X-Auth-Token', TOKEN)).AndReturn((MyResp(200), resstr))
self.mox.ReplayAll()
cmd_parser = cmd.get_parser("list_" + resources)
cmd_parser = cmd.get_parser("list_" + cmd_resources)
shell.run_command(cmd, cmd_parser, args)
self.mox.VerifyAll()
self.mox.UnsetStubs()
@@ -375,11 +383,15 @@ class CLITestV20Base(base.BaseTestCase):
self.assertIn('myid1', _str)
return _str
def _test_list_resources_with_pagination(self, resources, cmd):
def _test_list_resources_with_pagination(self, resources, cmd,
cmd_resources=None):
self.mox.StubOutWithMock(cmd, "get_client")
self.mox.StubOutWithMock(self.client.httpclient, "request")
cmd.get_client().MultipleTimes().AndReturn(self.client)
path = getattr(self.client, resources + "_path")
if not cmd_resources:
cmd_resources = resources
path = getattr(self.client, cmd_resources + "_path")
fake_query = "marker=myid2&limit=2"
reses1 = {resources: [{'id': 'myid1', },
{'id': 'myid2', }],
@@ -401,18 +413,22 @@ class CLITestV20Base(base.BaseTestCase):
headers=mox.ContainsKeyValue(
'X-Auth-Token', TOKEN)).AndReturn((MyResp(200), resstr2))
self.mox.ReplayAll()
cmd_parser = cmd.get_parser("list_" + resources)
cmd_parser = cmd.get_parser("list_" + cmd_resources)
args = ['--request-format', self.format]
shell.run_command(cmd, cmd_parser, args)
self.mox.VerifyAll()
self.mox.UnsetStubs()
def _test_update_resource(self, resource, cmd, myid, args, extrafields):
def _test_update_resource(self, resource, cmd, myid, args, extrafields,
cmd_resource=None):
self.mox.StubOutWithMock(cmd, "get_client")
self.mox.StubOutWithMock(self.client.httpclient, "request")
cmd.get_client().MultipleTimes().AndReturn(self.client)
if not cmd_resource:
cmd_resource = resource
body = {resource: extrafields}
path = getattr(self.client, resource + "_path")
path = getattr(self.client, cmd_resource + "_path")
self.client.format = self.format
# Work around for LP #1217791. XML deserializer called from
# MyComparator does not decodes XML string correctly.
@@ -429,24 +445,28 @@ class CLITestV20Base(base.BaseTestCase):
'X-Auth-Token', TOKEN)).AndReturn((MyResp(204), None))
args.extend(['--request-format', self.format])
self.mox.ReplayAll()
cmd_parser = cmd.get_parser("update_" + resource)
cmd_parser = cmd.get_parser("update_" + cmd_resource)
shell.run_command(cmd, cmd_parser, args)
self.mox.VerifyAll()
self.mox.UnsetStubs()
_str = self.fake_stdout.make_string()
self.assertIn(myid, _str)
def _test_show_resource(self, resource, cmd, myid, args, fields=[]):
def _test_show_resource(self, resource, cmd, myid, args, fields=[],
cmd_resource=None):
self.mox.StubOutWithMock(cmd, "get_client")
self.mox.StubOutWithMock(self.client.httpclient, "request")
cmd.get_client().MultipleTimes().AndReturn(self.client)
if not cmd_resource:
cmd_resource = resource
query = "&".join(["fields=%s" % field for field in fields])
expected_res = {resource:
{self.id_field: myid,
'name': 'myname', }, }
self.client.format = self.format
resstr = self.client.serialize(expected_res)
path = getattr(self.client, resource + "_path")
path = getattr(self.client, cmd_resource + "_path")
self.client.httpclient.request(
end_url(path % myid, query, format=self.format), 'GET',
body=None,
@@ -454,7 +474,7 @@ class CLITestV20Base(base.BaseTestCase):
'X-Auth-Token', TOKEN)).AndReturn((MyResp(200), resstr))
args.extend(['--request-format', self.format])
self.mox.ReplayAll()
cmd_parser = cmd.get_parser("show_" + resource)
cmd_parser = cmd.get_parser("show_" + cmd_resource)
shell.run_command(cmd, cmd_parser, args)
self.mox.VerifyAll()
self.mox.UnsetStubs()
@@ -462,11 +482,14 @@ class CLITestV20Base(base.BaseTestCase):
self.assertIn(myid, _str)
self.assertIn('myname', _str)
def _test_delete_resource(self, resource, cmd, myid, args):
def _test_delete_resource(self, resource, cmd, myid, args,
cmd_resource=None):
self.mox.StubOutWithMock(cmd, "get_client")
self.mox.StubOutWithMock(self.client.httpclient, "request")
cmd.get_client().MultipleTimes().AndReturn(self.client)
path = getattr(self.client, resource + "_path")
if not cmd_resource:
cmd_resource = resource
path = getattr(self.client, cmd_resource + "_path")
self.client.httpclient.request(
end_url(path % myid, format=self.format), 'DELETE',
body=None,
@@ -474,7 +497,7 @@ class CLITestV20Base(base.BaseTestCase):
'X-Auth-Token', TOKEN)).AndReturn((MyResp(204), None))
args.extend(['--request-format', self.format])
self.mox.ReplayAll()
cmd_parser = cmd.get_parser("delete_" + resource)
cmd_parser = cmd.get_parser("delete_" + cmd_resource)
shell.run_command(cmd, cmd_parser, args)
self.mox.VerifyAll()
self.mox.UnsetStubs()
@@ -482,11 +505,13 @@ class CLITestV20Base(base.BaseTestCase):
self.assertIn(myid, _str)
def _test_update_resource_action(self, resource, cmd, myid, action, args,
body, retval=None):
body, retval=None, cmd_resource=None):
self.mox.StubOutWithMock(cmd, "get_client")
self.mox.StubOutWithMock(self.client.httpclient, "request")
cmd.get_client().MultipleTimes().AndReturn(self.client)
path = getattr(self.client, resource + "_path")
if not cmd_resource:
cmd_resource = resource
path = getattr(self.client, cmd_resource + "_path")
path_action = '%s/%s' % (myid, action)
self.client.httpclient.request(
end_url(path % path_action, format=self.format), 'PUT',
@@ -495,7 +520,7 @@ class CLITestV20Base(base.BaseTestCase):
'X-Auth-Token', TOKEN)).AndReturn((MyResp(204), retval))
args.extend(['--request-format', self.format])
self.mox.ReplayAll()
cmd_parser = cmd.get_parser("delete_" + resource)
cmd_parser = cmd.get_parser("delete_" + cmd_resource)
shell.run_command(cmd, cmd_parser, args)
self.mox.VerifyAll()
self.mox.UnsetStubs()