Skip to content

Commit b3943d7

Browse files
author
NiallBunting
committed
Add image re/deactivate commands
This change allows admins to deactivate and reactivate their images. Currently this has to be done with the REST api or the glanceclient. This change introduces `--deactivate` and `--activate` for the `image set` command. This requires glanceclient 1.2.0. Which got bumped here: https://review.openstack.org/#/c/257512/ Change-Id: I476c44a0343cdc92d58ddc93fb06470242de2345 Depends-On: I2c370c6bf6ff664d94d756cc76aaa983fbdb8869 Closes-Bug: 1516661
1 parent 1ee5191 commit b3943d7

3 files changed

Lines changed: 101 additions & 2 deletions

File tree

doc/source/command-objects/image.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ Set image properties
243243
[--os-distro <os-distro>]
244244
[--os-version <os-version>]
245245
[--ramdisk-id <ramdisk-id>]
246+
[--activate|--deactivate]
246247
<image>
247248
248249
.. option:: --name <name>
@@ -387,6 +388,18 @@ Set image properties
387388
388389
.. versionadded:: 2
389390
391+
.. option:: --activate
392+
393+
Activate the image.
394+
395+
.. versionadded:: 2
396+
397+
.. option:: --deactivate
398+
399+
Deactivate the image.
400+
401+
.. versionadded:: 2
402+
390403
.. describe:: <image>
391404
392405
Image to modify (name or ID)

openstackclient/image/v2/image.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,17 @@ def get_parser(self, prog_name):
693693
metavar="<ramdisk-id>",
694694
help="ID of ramdisk image used to boot this disk image",
695695
)
696+
deactivate_group = parser.add_mutually_exclusive_group()
697+
deactivate_group.add_argument(
698+
"--deactivate",
699+
action="store_true",
700+
help="Deactivate the image",
701+
)
702+
deactivate_group.add_argument(
703+
"--activate",
704+
action="store_true",
705+
help="Activate the image",
706+
)
696707
for deadopt in self.deadopts:
697708
parser.add_argument(
698709
"--%s" % deadopt,
@@ -745,18 +756,35 @@ def take_action(self, parsed_args):
745756
if parsed_args.private:
746757
kwargs['visibility'] = 'private'
747758

748-
if not kwargs:
759+
# Checks if anything that requires getting the image
760+
if not (kwargs or parsed_args.deactivate or parsed_args.activate):
749761
self.log.warning("No arguments specified")
750762
return {}, {}
751763

752764
image = utils.find_resource(
753765
image_client.images, parsed_args.image)
754766

767+
if parsed_args.deactivate:
768+
image_client.images.deactivate(image.id)
769+
activation_status = "deactivated"
770+
if parsed_args.activate:
771+
image_client.images.reactivate(image.id)
772+
activation_status = "activated"
773+
774+
# Check if need to do the actual update
775+
if not kwargs:
776+
return {}, {}
777+
755778
if parsed_args.tags:
756779
# Tags should be extended, but duplicates removed
757780
kwargs['tags'] = list(set(image.tags).union(set(parsed_args.tags)))
758781

759-
image = image_client.images.update(image.id, **kwargs)
782+
try:
783+
image = image_client.images.update(image.id, **kwargs)
784+
except Exception as e:
785+
if activation_status is not None:
786+
print("Image %s was %s." % (image.id, activation_status))
787+
raise e
760788

761789

762790
class ShowImage(show.ShowOne):

openstackclient/tests/image/v2/test_image.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,64 @@ def test_image_set_tag(self):
838838
**kwargs
839839
)
840840

841+
def test_image_set_activate(self):
842+
arglist = [
843+
'--tag', 'test-tag',
844+
'--activate',
845+
image_fakes.image_name,
846+
]
847+
verifylist = [
848+
('tags', ['test-tag']),
849+
('image', image_fakes.image_name),
850+
]
851+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
852+
853+
# DisplayCommandBase.take_action() returns two tuples
854+
self.cmd.take_action(parsed_args)
855+
856+
kwargs = {
857+
'tags': ['test-tag'],
858+
}
859+
860+
self.images_mock.reactivate.assert_called_with(
861+
image_fakes.image_id,
862+
)
863+
864+
# ImageManager.update(image, **kwargs)
865+
self.images_mock.update.assert_called_with(
866+
image_fakes.image_id,
867+
**kwargs
868+
)
869+
870+
def test_image_set_deactivate(self):
871+
arglist = [
872+
'--tag', 'test-tag',
873+
'--deactivate',
874+
image_fakes.image_name,
875+
]
876+
verifylist = [
877+
('tags', ['test-tag']),
878+
('image', image_fakes.image_name),
879+
]
880+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
881+
882+
# DisplayCommandBase.take_action() returns two tuples
883+
self.cmd.take_action(parsed_args)
884+
885+
kwargs = {
886+
'tags': ['test-tag'],
887+
}
888+
889+
self.images_mock.deactivate.assert_called_with(
890+
image_fakes.image_id,
891+
)
892+
893+
# ImageManager.update(image, **kwargs)
894+
self.images_mock.update.assert_called_with(
895+
image_fakes.image_id,
896+
**kwargs
897+
)
898+
841899
def test_image_set_tag_merge(self):
842900
old_image = copy.copy(image_fakes.IMAGE)
843901
old_image['tags'] = ['old1', 'new2']

0 commit comments

Comments
 (0)