diff --git a/lower-constraints.txt b/lower-constraints.txt index f387827948..c3dc477eaf 100644 --- a/lower-constraints.txt +++ b/lower-constraints.txt @@ -100,7 +100,7 @@ python-mimeparse==1.6.0 python-mistralclient==3.1.0 python-muranoclient==0.8.2 python-neutronclient==6.7.0 -python-novaclient==14.1.0 +python-novaclient==14.2.0 python-octaviaclient==1.3.0 python-rsdclient==0.1.0 python-saharaclient==1.4.0 diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py index 2792e315f6..240c7cb2a0 100644 --- a/openstackclient/compute/v2/server.py +++ b/openstackclient/compute/v2/server.py @@ -553,6 +553,20 @@ class CreateServer(command.ShowOne): metavar='', help=_('Select an availability zone for the server'), ) + parser.add_argument( + '--host', + metavar='', + help=_('Requested host to create servers. Admin only ' + 'by default. (supported by --os-compute-api-version 2.74 ' + 'or above)'), + ) + parser.add_argument( + '--hypervisor-hostname', + metavar='', + help=_('Requested hypervisor hostname to create servers. Admin ' + 'only by default. (supported by --os-compute-api-version ' + '2.74 or above)'), + ) parser.add_argument( '--block-device-mapping', metavar='', @@ -927,6 +941,21 @@ class CreateServer(command.ShowOne): if parsed_args.description: boot_kwargs['description'] = parsed_args.description + if parsed_args.host: + if compute_client.api_version < api_versions.APIVersion("2.74"): + msg = _("Specifying --host is not supported for " + "--os-compute-api-version less than 2.74") + raise exceptions.CommandError(msg) + boot_kwargs['host'] = parsed_args.host + + if parsed_args.hypervisor_hostname: + if compute_client.api_version < api_versions.APIVersion("2.74"): + msg = _("Specifying --hypervisor-hostname is not supported " + "for --os-compute-api-version less than 2.74") + raise exceptions.CommandError(msg) + boot_kwargs['hypervisor_hostname'] = ( + parsed_args.hypervisor_hostname) + LOG.debug('boot_args: %s', boot_args) LOG.debug('boot_kwargs: %s', boot_kwargs) diff --git a/openstackclient/tests/unit/compute/v2/test_server.py b/openstackclient/tests/unit/compute/v2/test_server.py index 8ea59a3850..dc7ff1aa3f 100644 --- a/openstackclient/tests/unit/compute/v2/test_server.py +++ b/openstackclient/tests/unit/compute/v2/test_server.py @@ -1926,6 +1926,237 @@ class TestServerCreate(TestServer): self.assertRaises(exceptions.CommandError, self.cmd.take_action, parsed_args) + def test_server_create_with_host_v274(self): + + # Explicit host is supported for nova api version 2.74 or above + self.app.client_manager.compute.api_version = 2.74 + + arglist = [ + '--image', 'image1', + '--flavor', 'flavor1', + '--host', 'host1', + self.new_server.name, + ] + verifylist = [ + ('image', 'image1'), + ('flavor', 'flavor1'), + ('host', 'host1'), + ('config_drive', False), + ('server_name', self.new_server.name), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + with mock.patch.object(api_versions, + 'APIVersion', + return_value=2.74): + # In base command class ShowOne in cliff, abstract method + # take_action() returns a two-part tuple with a tuple of + # column names and a tuple of data to be shown. + columns, data = self.cmd.take_action(parsed_args) + + # Set expected values + kwargs = dict( + meta=None, + files={}, + reservation_id=None, + min_count=1, + max_count=1, + security_groups=[], + userdata=None, + key_name=None, + availability_zone=None, + block_device_mapping_v2=[], + nics='auto', + scheduler_hints={}, + config_drive=None, + host='host1', + ) + # ServerManager.create(name, image, flavor, **kwargs) + self.servers_mock.create.assert_called_with( + self.new_server.name, + self.image, + self.flavor, + **kwargs + ) + + self.assertEqual(self.columns, columns) + self.assertEqual(self.datalist(), data) + self.assertFalse(self.images_mock.called) + self.assertFalse(self.flavors_mock.called) + + def test_server_create_with_host_pre_v274(self): + + # Host is not supported for nova api version below 2.74 + self.app.client_manager.compute.api_version = 2.73 + + arglist = [ + '--image', 'image1', + '--flavor', 'flavor1', + '--host', 'host1', + self.new_server.name, + ] + verifylist = [ + ('image', 'image1'), + ('flavor', 'flavor1'), + ('host', 'host1'), + ('config_drive', False), + ('server_name', self.new_server.name), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + with mock.patch.object(api_versions, + 'APIVersion', + return_value=2.74): + self.assertRaises(exceptions.CommandError, self.cmd.take_action, + parsed_args) + + def test_server_create_with_hypervisor_hostname_v274(self): + + # Explicit hypervisor_hostname is supported for nova api version + # 2.74 or above + self.app.client_manager.compute.api_version = 2.74 + + arglist = [ + '--image', 'image1', + '--flavor', 'flavor1', + '--hypervisor-hostname', 'node1', + self.new_server.name, + ] + verifylist = [ + ('image', 'image1'), + ('flavor', 'flavor1'), + ('hypervisor_hostname', 'node1'), + ('config_drive', False), + ('server_name', self.new_server.name), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + with mock.patch.object(api_versions, + 'APIVersion', + return_value=2.74): + # In base command class ShowOne in cliff, abstract method + # take_action() returns a two-part tuple with a tuple of + # column names and a tuple of data to be shown. + columns, data = self.cmd.take_action(parsed_args) + + # Set expected values + kwargs = dict( + meta=None, + files={}, + reservation_id=None, + min_count=1, + max_count=1, + security_groups=[], + userdata=None, + key_name=None, + availability_zone=None, + block_device_mapping_v2=[], + nics='auto', + scheduler_hints={}, + config_drive=None, + hypervisor_hostname='node1', + ) + # ServerManager.create(name, image, flavor, **kwargs) + self.servers_mock.create.assert_called_with( + self.new_server.name, + self.image, + self.flavor, + **kwargs + ) + + self.assertEqual(self.columns, columns) + self.assertEqual(self.datalist(), data) + self.assertFalse(self.images_mock.called) + self.assertFalse(self.flavors_mock.called) + + def test_server_create_with_hypervisor_hostname_pre_v274(self): + + # Hypervisor_hostname is not supported for nova api version below 2.74 + self.app.client_manager.compute.api_version = 2.73 + + arglist = [ + '--image', 'image1', + '--flavor', 'flavor1', + '--hypervisor-hostname', 'node1', + self.new_server.name, + ] + verifylist = [ + ('image', 'image1'), + ('flavor', 'flavor1'), + ('hypervisor_hostname', 'node1'), + ('config_drive', False), + ('server_name', self.new_server.name), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + with mock.patch.object(api_versions, + 'APIVersion', + return_value=2.74): + self.assertRaises(exceptions.CommandError, self.cmd.take_action, + parsed_args) + + def test_server_create_with_host_and_hypervisor_hostname_v274(self): + + # Explicit host and hypervisor_hostname is supported for nova api + # version 2.74 or above + self.app.client_manager.compute.api_version = 2.74 + + arglist = [ + '--image', 'image1', + '--flavor', 'flavor1', + '--host', 'host1', + '--hypervisor-hostname', 'node1', + self.new_server.name, + ] + verifylist = [ + ('image', 'image1'), + ('flavor', 'flavor1'), + ('host', 'host1'), + ('hypervisor_hostname', 'node1'), + ('config_drive', False), + ('server_name', self.new_server.name), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + with mock.patch.object(api_versions, + 'APIVersion', + return_value=2.74): + # In base command class ShowOne in cliff, abstract method + # take_action() returns a two-part tuple with a tuple of + # column names and a tuple of data to be shown. + columns, data = self.cmd.take_action(parsed_args) + + # Set expected values + kwargs = dict( + meta=None, + files={}, + reservation_id=None, + min_count=1, + max_count=1, + security_groups=[], + userdata=None, + key_name=None, + availability_zone=None, + block_device_mapping_v2=[], + nics='auto', + scheduler_hints={}, + config_drive=None, + host='host1', + hypervisor_hostname='node1', + ) + # ServerManager.create(name, image, flavor, **kwargs) + self.servers_mock.create.assert_called_with( + self.new_server.name, + self.image, + self.flavor, + **kwargs + ) + + self.assertEqual(self.columns, columns) + self.assertEqual(self.datalist(), data) + self.assertFalse(self.images_mock.called) + self.assertFalse(self.flavors_mock.called) + class TestServerDelete(TestServer): diff --git a/releasenotes/notes/add-host-and-hypervisor-hostname-flag-to-create-server-cb8b39a9f9311d42.yaml b/releasenotes/notes/add-host-and-hypervisor-hostname-flag-to-create-server-cb8b39a9f9311d42.yaml new file mode 100644 index 0000000000..fbd4c70402 --- /dev/null +++ b/releasenotes/notes/add-host-and-hypervisor-hostname-flag-to-create-server-cb8b39a9f9311d42.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + Add ``--host`` and ``--hypervisor-hostname`` options to + ``server create`` command. + [Blueprint `add-host-and-hypervisor-hostname-flag-to-create-server `_] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 3db6caef0b..cf67906646 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,5 +13,5 @@ oslo.i18n>=3.15.3 # Apache-2.0 oslo.utils>=3.33.0 # Apache-2.0 python-glanceclient>=2.8.0 # Apache-2.0 python-keystoneclient>=3.17.0 # Apache-2.0 -python-novaclient>=14.1.0 # Apache-2.0 +python-novaclient>=14.2.0 # Apache-2.0 python-cinderclient>=3.3.0 # Apache-2.0