Skip to content

Commit 0148359

Browse files
Huanxuan Aostevemar
authored andcommitted
Add "--project" option to the "flavor create" command.
Add ``--project`` and ``--project-domain`` options to the ``flavor create`` command. We can use these options to add the flavor access to a givin project when we create the flavor. Change-Id: Ic1907272c1d1ae526f9c9e86f32ba06c6da147c0
1 parent 7cda2b2 commit 0148359

4 files changed

Lines changed: 70 additions & 1 deletion

File tree

doc/source/command-objects/flavor.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Create new flavor
2121
[--vcpus <num-cpu>]
2222
[--rxtx-factor <factor>]
2323
[--public | --private]
24+
[--project <project>]
25+
[--project-domain <project-domain>]
2426
<flavor-name>
2527
2628
.. option:: --id <id>
@@ -59,6 +61,16 @@ Create new flavor
5961

6062
Flavor is not available to other projects
6163

64+
.. option:: --project <project>
65+
66+
Allow <project> to access private flavor (name or ID)
67+
(Must be used with :option:`--private` option)
68+
69+
.. option:: --project-domain <project-domain>
70+
71+
Domain the project belongs to (name or ID).
72+
This can be used in case collisions between project names exist.
73+
6274
.. _flavor_create-flavor-name:
6375
.. describe:: <flavor-name>
6476

openstackclient/compute/v2/flavor.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,22 @@ def get_parser(self, prog_name):
121121
action="store_false",
122122
help=_("Flavor is not available to other projects")
123123
)
124+
parser.add_argument(
125+
'--project',
126+
metavar='<project>',
127+
help=_("Allow <project> to access private flavor (name or ID) "
128+
"(Must be used with --private option)"),
129+
)
130+
identity_common.add_project_domain_option_to_parser(parser)
124131
return parser
125132

126133
def take_action(self, parsed_args):
127134
compute_client = self.app.client_manager.compute
135+
identity_client = self.app.client_manager.identity
136+
137+
if parsed_args.project and parsed_args.public:
138+
msg = _("--project is only allowed with --private")
139+
raise exceptions.CommandError(msg)
128140

129141
args = (
130142
parsed_args.name,
@@ -141,6 +153,20 @@ def take_action(self, parsed_args):
141153
flavor = compute_client.flavors.create(*args)._info.copy()
142154
flavor.pop("links")
143155

156+
if parsed_args.project:
157+
try:
158+
project_id = identity_common.find_project(
159+
identity_client,
160+
parsed_args.project,
161+
parsed_args.project_domain,
162+
).id
163+
compute_client.flavor_access.add_tenant_access(
164+
parsed_args.id, project_id)
165+
except Exception as e:
166+
msg = _("Failed to add project %(project)s access to "
167+
"flavor: %(e)s")
168+
LOG.error(msg % {'project': parsed_args.project, 'e': e})
169+
144170
return zip(*sorted(six.iteritems(flavor)))
145171

146172

openstackclient/tests/compute/v2/test_flavor.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ class TestFlavorCreate(TestFlavor):
7575
def setUp(self):
7676
super(TestFlavorCreate, self).setUp()
7777

78+
# Return a project
79+
self.projects_mock.get.return_value = fakes.FakeResource(
80+
None,
81+
copy.deepcopy(identity_fakes.PROJECT),
82+
loaded=True,
83+
)
7884
self.flavors_mock.create.return_value = self.flavor
7985
self.cmd = flavor.CreateFlavor(self.app, None)
8086

@@ -161,6 +167,7 @@ def test_flavor_create_other_options(self):
161167
'--vcpus', str(self.flavor.vcpus),
162168
'--rxtx-factor', str(self.flavor.rxtx_factor),
163169
'--private',
170+
'--project', identity_fakes.project_id,
164171
]
165172
verifylist = [
166173
('name', self.flavor.name),
@@ -172,6 +179,7 @@ def test_flavor_create_other_options(self):
172179
('vcpus', self.flavor.vcpus),
173180
('rxtx_factor', self.flavor.rxtx_factor),
174181
('public', False),
182+
('project', identity_fakes.project_id),
175183
]
176184
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
177185

@@ -188,10 +196,28 @@ def test_flavor_create_other_options(self):
188196
)
189197
columns, data = self.cmd.take_action(parsed_args)
190198
self.flavors_mock.create.assert_called_once_with(*args)
191-
199+
self.flavor_access_mock.add_tenant_access.assert_called_with(
200+
self.flavor.id,
201+
identity_fakes.project_id,
202+
)
192203
self.assertEqual(self.columns, columns)
193204
self.assertEqual(self.data, data)
194205

206+
def test_public_flavor_create_with_project(self):
207+
arglist = [
208+
'--project', identity_fakes.project_id,
209+
self.flavor.name,
210+
]
211+
verifylist = [
212+
('project', identity_fakes.project_id),
213+
('name', self.flavor.name),
214+
]
215+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
216+
217+
self.assertRaises(exceptions.CommandError,
218+
self.cmd.take_action,
219+
parsed_args)
220+
195221
def test_flavor_create_no_options(self):
196222
arglist = []
197223
verifylist = None
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
features:
3+
- Add ``--project`` and ``--project-domain`` options to the ``flavor create``
4+
command. We can use these options to add the flavor access to a given project
5+
when we create the flavor.

0 commit comments

Comments
 (0)