diff options
author | Andrey Pavlov <apavlov@mirantis.com> | 2015-10-27 15:52:05 +0300 |
---|---|---|
committer | Andrey Pavlov <apavlov@mirantis.com> | 2015-11-09 17:39:58 +0000 |
commit | ce767108694288fd6cd574f2a2796322e6ee8c09 (patch) | |
tree | 5fc88302cf3c6a9cdf7df3d9a96aaa294c718bd7 /saharaclient | |
parent | 205b981d06ae96e5d957aa3dc4338536869e8d2e (diff) | |
download | python-saharaclient-ce767108694288fd6cd574f2a2796322e6ee8c09.tar.gz |
Images CLI improvement
* fixing representation after image is registered
* adding ability to register images by name
* adding additional ut
Partially implements: blueprint cli-as-openstackclient-plugin
Change-Id: Idda6aefa6406b49c8aa6c4de67d1de73878c5661
Diffstat (limited to 'saharaclient')
-rw-r--r-- | saharaclient/osc/v1/images.py | 33 | ||||
-rw-r--r-- | saharaclient/tests/unit/osc/v1/test_images.py | 26 |
2 files changed, 38 insertions, 21 deletions
diff --git a/saharaclient/osc/v1/images.py b/saharaclient/osc/v1/images.py index 377d5a9..f01d247 100644 --- a/saharaclient/osc/v1/images.py +++ b/saharaclient/osc/v1/images.py @@ -21,6 +21,8 @@ from oslo_log import log as logging from saharaclient.osc.v1 import utils +IMAGE_FIELDS = ['name', 'id', 'username', 'tags', 'status', 'description'] + class ListImages(lister.Lister): """Lists registered images""" @@ -68,8 +70,7 @@ class ListImages(lister.Lister): data = [i for i in data if parsed_args.username in i.username] if parsed_args.long: - columns = ('name', 'id', 'username', 'tags', 'status', - 'description') + columns = IMAGE_FIELDS column_headers = [c.capitalize() for c in columns] else: @@ -111,8 +112,7 @@ class ShowImage(show.ShowOne): client.images, parsed_args.image).to_dict() data['tags'] = osc_utils.format_list(data['tags']) - fields = ['name', 'id', 'username', 'tags', 'status', 'description'] - data = utils.prepare_data(data, fields) + data = utils.prepare_data(data, IMAGE_FIELDS) return self.dict2columns(data) @@ -126,8 +126,8 @@ class RegisterImage(show.ShowOne): parser = super(RegisterImage, self).get_parser(prog_name) parser.add_argument( "image", - metavar="<image-id>", - help="Id of the image to register", + metavar="<image>", + help="Name or ID of the image to register", ) parser.add_argument( "--username", @@ -147,16 +147,18 @@ class RegisterImage(show.ShowOne): def take_action(self, parsed_args): self.log.debug("take_action(%s)" % parsed_args) client = self.app.client_manager.data_processing + image_client = self.app.client_manager.image + + image_id = osc_utils.find_resource( + image_client.images, parsed_args.image).id - description = parsed_args.description or '' data = client.images.update_image( - parsed_args.image, user_name=parsed_args.username, - desc=description).to_dict() + image_id, user_name=parsed_args.username, + desc=parsed_args.description).image data['tags'] = osc_utils.format_list(data['tags']) - fields = ['name', 'id', 'username', 'tags', 'status', 'description'] - data = utils.prepare_data(data, fields) + data = utils.prepare_data(data, IMAGE_FIELDS) return self.dict2columns(data) @@ -215,8 +217,7 @@ class SetImageTags(show.ShowOne): data['tags'] = osc_utils.format_list(data['tags']) - fields = ['name', 'id', 'username', 'tags', 'status', 'description'] - data = utils.prepare_data(data, fields) + data = utils.prepare_data(data, IMAGE_FIELDS) return self.dict2columns(data) @@ -253,8 +254,7 @@ class AddImageTags(show.ShowOne): data['tags'] = osc_utils.format_list(data['tags']) - fields = ['name', 'id', 'username', 'tags', 'status', 'description'] - data = utils.prepare_data(data, fields) + data = utils.prepare_data(data, IMAGE_FIELDS) return self.dict2columns(data) @@ -301,7 +301,6 @@ class RemoveImageTags(show.ShowOne): data['tags'] = osc_utils.format_list(data['tags']) - fields = ['name', 'id', 'username', 'tags', 'status', 'description'] - data = utils.prepare_data(data, fields) + data = utils.prepare_data(data, IMAGE_FIELDS) return self.dict2columns(data) diff --git a/saharaclient/tests/unit/osc/v1/test_images.py b/saharaclient/tests/unit/osc/v1/test_images.py index 8812d2c..e94a1d0 100644 --- a/saharaclient/tests/unit/osc/v1/test_images.py +++ b/saharaclient/tests/unit/osc/v1/test_images.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +import mock + from openstackclient.tests import utils as osc_utils from saharaclient.api import images as api_images @@ -171,8 +173,11 @@ class TestShowImage(TestImages): class TestRegisterImage(TestImages): def setUp(self): super(TestRegisterImage, self).setUp() - self.image_mock.update_image.return_value = api_images.Image( - None, IMAGE_INFO) + self.image_mock.update_image.return_value = mock.Mock( + image=IMAGE_INFO.copy()) + self.app.client_manager.image = mock.Mock() + self.image_client = self.app.client_manager.image.images + self.image_client.get.return_value = mock.Mock(id='id') # Command to test self.cmd = osc_images.RegisterImage(self.app, None) @@ -184,7 +189,7 @@ class TestRegisterImage(TestImages): self.assertRaises(osc_utils.ParserException, self.check_parser, self.cmd, arglist, verifylist) - def test_image_register(self): + def test_image_register_required_options(self): arglist = ['id', '--username', 'ubuntu'] verifylist = [('image', 'id'), ('username', 'ubuntu')] @@ -194,7 +199,7 @@ class TestRegisterImage(TestImages): # Check that correct arguments were passed self.image_mock.update_image.assert_called_once_with( - 'id', desc='', user_name='ubuntu') + 'id', desc=None, user_name='ubuntu') # Check that columns are correct expected_columns = ('Description', 'Id', 'Name', 'Status', 'Tags', @@ -206,6 +211,19 @@ class TestRegisterImage(TestImages): '0.1, fake', 'ubuntu'] self.assertEqual(expected_data, list(data)) + def test_image_register_all_options(self): + arglist = ['id', '--username', 'ubuntu', '--description', 'descr'] + verifylist = [('image', 'id'), ('username', 'ubuntu'), + ('description', 'descr')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.image_mock.update_image.assert_called_once_with( + 'id', desc='descr', user_name='ubuntu') + class TestUnregisterImage(TestImages): def setUp(self): |