Overwrite/Clear Flavor property

This patch adds support to overwrite/clear the flavor's
property using the new ``--no-property`` option in the
``flavor set`` command.

Change-Id: I873c96fcf223bbd638a19b908766d904a84e8431
Implements: blueprint allow-overwrite-set-options
Co-Authored By: zhiyong.dai@easystack.cn
This commit is contained in:
Reedip 2016-12-28 01:45:42 -05:00 committed by Dean Troyer
parent 1e3dc48c64
commit 6a3c7c2a68
4 changed files with 46 additions and 0 deletions

View File

@ -144,6 +144,7 @@ Set flavor properties
.. code:: bash .. code:: bash
openstack flavor set openstack flavor set
[--no-property]
[--property <key=value> [...] ] [--property <key=value> [...] ]
[--project <project>] [--project <project>]
[--project-domain <project-domain>] [--project-domain <project-domain>]
@ -162,6 +163,11 @@ Set flavor properties
Domain the project belongs to (name or ID). Domain the project belongs to (name or ID).
This can be used in case collisions between project names exist. This can be used in case collisions between project names exist.
.. option:: --no-property
Remove all properties from this flavor (specify both --no-property and --property
to remove the current properties before setting new properties.)
.. describe:: <flavor> .. describe:: <flavor>
Flavor to modify (name or ID) Flavor to modify (name or ID)

View File

@ -312,6 +312,14 @@ class SetFlavor(command.Command):
metavar="<flavor>", metavar="<flavor>",
help=_("Flavor to modify (name or ID)") help=_("Flavor to modify (name or ID)")
) )
parser.add_argument(
"--no-property",
action="store_true",
help=_("Remove all properties from this flavor "
"(specify both --no-property and --property"
" to remove the current properties before setting"
" new properties.)"),
)
parser.add_argument( parser.add_argument(
"--property", "--property",
metavar="<key=value>", metavar="<key=value>",
@ -336,6 +344,15 @@ class SetFlavor(command.Command):
flavor = _find_flavor(compute_client, parsed_args.flavor) flavor = _find_flavor(compute_client, parsed_args.flavor)
result = 0 result = 0
key_list = []
if parsed_args.no_property:
try:
for key in flavor.get_keys().keys():
key_list.append(key)
flavor.unset_keys(key_list)
except Exception as e:
LOG.error(_("Failed to clear flavor property: %s"), e)
result += 1
if parsed_args.property: if parsed_args.property:
try: try:
flavor.set_keys(parsed_args.property) flavor.set_keys(parsed_args.property)

View File

@ -528,6 +528,23 @@ class TestFlavorSet(TestFlavor):
self.flavor.set_keys.assert_called_with({'FOO': '"B A R"'}) self.flavor.set_keys.assert_called_with({'FOO': '"B A R"'})
self.assertIsNone(result) self.assertIsNone(result)
def test_flavor_set_no_property(self):
arglist = [
'--no-property',
'baremetal'
]
verifylist = [
('no_property', True),
('flavor', 'baremetal')
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
result = self.cmd.take_action(parsed_args)
self.flavors_mock.find.assert_called_with(name=parsed_args.flavor,
is_public=None)
self.flavor.unset_keys.assert_called_with(['property'])
self.assertIsNone(result)
def test_flavor_set_project(self): def test_flavor_set_project(self):
arglist = [ arglist = [
'--project', self.project.id, '--project', self.project.id,

View File

@ -0,0 +1,6 @@
---
features:
- |
Add support to clear/overwrite all flavor properties using
``--no-property`` option with ``flavor set`` command.
[ Blueprint `allow-overwrite-set-options <https://blueprints.launchpad.net/python-openstackclient/+spec/allow-overwrite-set-options>` _]