Merge "Add "--project" option to the "flavor create" command."

This commit is contained in:
Jenkins 2016-06-21 20:15:19 +00:00 committed by Gerrit Code Review
commit 94534e26cf
4 changed files with 70 additions and 1 deletions

View File

@ -21,6 +21,8 @@ Create new flavor
[--vcpus <num-cpu>] [--vcpus <num-cpu>]
[--rxtx-factor <factor>] [--rxtx-factor <factor>]
[--public | --private] [--public | --private]
[--project <project>]
[--project-domain <project-domain>]
<flavor-name> <flavor-name>
.. option:: --id <id> .. option:: --id <id>
@ -59,6 +61,16 @@ Create new flavor
Flavor is not available to other projects Flavor is not available to other projects
.. option:: --project <project>
Allow <project> to access private flavor (name or ID)
(Must be used with :option:`--private` option)
.. option:: --project-domain <project-domain>
Domain the project belongs to (name or ID).
This can be used in case collisions between project names exist.
.. _flavor_create-flavor-name: .. _flavor_create-flavor-name:
.. describe:: <flavor-name> .. describe:: <flavor-name>

View File

@ -121,10 +121,22 @@ class CreateFlavor(command.ShowOne):
action="store_false", action="store_false",
help=_("Flavor is not available to other projects") help=_("Flavor is not available to other projects")
) )
parser.add_argument(
'--project',
metavar='<project>',
help=_("Allow <project> to access private flavor (name or ID) "
"(Must be used with --private option)"),
)
identity_common.add_project_domain_option_to_parser(parser)
return parser return parser
def take_action(self, parsed_args): def take_action(self, parsed_args):
compute_client = self.app.client_manager.compute compute_client = self.app.client_manager.compute
identity_client = self.app.client_manager.identity
if parsed_args.project and parsed_args.public:
msg = _("--project is only allowed with --private")
raise exceptions.CommandError(msg)
args = ( args = (
parsed_args.name, parsed_args.name,
@ -141,6 +153,20 @@ class CreateFlavor(command.ShowOne):
flavor = compute_client.flavors.create(*args)._info.copy() flavor = compute_client.flavors.create(*args)._info.copy()
flavor.pop("links") flavor.pop("links")
if parsed_args.project:
try:
project_id = identity_common.find_project(
identity_client,
parsed_args.project,
parsed_args.project_domain,
).id
compute_client.flavor_access.add_tenant_access(
parsed_args.id, project_id)
except Exception as e:
msg = _("Failed to add project %(project)s access to "
"flavor: %(e)s")
LOG.error(msg % {'project': parsed_args.project, 'e': e})
return zip(*sorted(six.iteritems(flavor))) return zip(*sorted(six.iteritems(flavor)))

View File

@ -75,6 +75,12 @@ class TestFlavorCreate(TestFlavor):
def setUp(self): def setUp(self):
super(TestFlavorCreate, self).setUp() super(TestFlavorCreate, self).setUp()
# Return a project
self.projects_mock.get.return_value = fakes.FakeResource(
None,
copy.deepcopy(identity_fakes.PROJECT),
loaded=True,
)
self.flavors_mock.create.return_value = self.flavor self.flavors_mock.create.return_value = self.flavor
self.cmd = flavor.CreateFlavor(self.app, None) self.cmd = flavor.CreateFlavor(self.app, None)
@ -161,6 +167,7 @@ class TestFlavorCreate(TestFlavor):
'--vcpus', str(self.flavor.vcpus), '--vcpus', str(self.flavor.vcpus),
'--rxtx-factor', str(self.flavor.rxtx_factor), '--rxtx-factor', str(self.flavor.rxtx_factor),
'--private', '--private',
'--project', identity_fakes.project_id,
] ]
verifylist = [ verifylist = [
('name', self.flavor.name), ('name', self.flavor.name),
@ -172,6 +179,7 @@ class TestFlavorCreate(TestFlavor):
('vcpus', self.flavor.vcpus), ('vcpus', self.flavor.vcpus),
('rxtx_factor', self.flavor.rxtx_factor), ('rxtx_factor', self.flavor.rxtx_factor),
('public', False), ('public', False),
('project', identity_fakes.project_id),
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@ -188,10 +196,28 @@ class TestFlavorCreate(TestFlavor):
) )
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.flavors_mock.create.assert_called_once_with(*args) self.flavors_mock.create.assert_called_once_with(*args)
self.flavor_access_mock.add_tenant_access.assert_called_with(
self.flavor.id,
identity_fakes.project_id,
)
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, data) self.assertEqual(self.data, data)
def test_public_flavor_create_with_project(self):
arglist = [
'--project', identity_fakes.project_id,
self.flavor.name,
]
verifylist = [
('project', identity_fakes.project_id),
('name', self.flavor.name),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.assertRaises(exceptions.CommandError,
self.cmd.take_action,
parsed_args)
def test_flavor_create_no_options(self): def test_flavor_create_no_options(self):
arglist = [] arglist = []
verifylist = None verifylist = None

View File

@ -0,0 +1,5 @@
---
features:
- Add ``--project`` and ``--project-domain`` options to the ``flavor create``
command. We can use these options to add the flavor access to a given project
when we create the flavor.