Add 'port set' command
Add CLI support for the 'port set' command Change-Id: I2bea508e11290284aa64b1ab548a0bb61e7290d3 Partial-bug: #1519909 Partially-implements: blueprint neutron-client
This commit is contained in:
		| @@ -45,7 +45,8 @@ Create new port | ||||
|  | ||||
| .. option:: --vnic-type <vnic-type> | ||||
|  | ||||
|     VNIC type for this port (direct | direct-physical | macvtap | normal(default) | baremetal) | ||||
|     VNIC type for this port (direct | direct-physical | macvtap | normal | baremetal). | ||||
|     If unspecified during port creation, default value will be 'normal'. | ||||
|  | ||||
| .. option:: --binding-profile <binding-profile> | ||||
|  | ||||
| @@ -108,6 +109,65 @@ List ports | ||||
|  | ||||
|     os port list | ||||
|  | ||||
| port set | ||||
| -------- | ||||
|  | ||||
| Set port properties | ||||
|  | ||||
| .. program:: port set | ||||
| .. code:: bash | ||||
|  | ||||
|     os port set | ||||
|         [--fixed-ip subnet=<subnet>,ip-address=<ip-address>] | ||||
|         [--device-id <device-id>] | ||||
|         [--device-owner <device-owner>] | ||||
|         [--vnic-type <vnic-type>] | ||||
|         [--binding-profile <binding-profile>] | ||||
|         [--host-id <host-id>] | ||||
|         [--enable | --disable] | ||||
|         <port> | ||||
|  | ||||
| .. option:: --fixed-ip subnet=<subnet>,ip-address=<ip-address> | ||||
|  | ||||
|     Desired IP and/or subnet for this port: | ||||
|     subnet=<subnet>,ip-address=<ip-address> | ||||
|     (you can repeat this option) | ||||
|  | ||||
| .. option:: --device-id <device-id> | ||||
|  | ||||
|     Device ID of this port | ||||
|  | ||||
| .. option:: --device-owner <device-owner> | ||||
|  | ||||
|     Device owner of this port | ||||
|  | ||||
| .. option:: --vnic-type <vnic-type> | ||||
|  | ||||
|     VNIC type for this port (direct | direct-physical | macvtap | normal | baremetal). | ||||
|     If unspecified during port creation, default value will be 'normal'. | ||||
|  | ||||
| .. option:: --binding-profile <binding-profile> | ||||
|  | ||||
|     Custom data to be passed as binding:profile: <key>=<value> | ||||
|     (this option can be repeated) | ||||
|  | ||||
| .. option:: --host-id <host-id> | ||||
|  | ||||
|     The ID of the host where the port is allocated | ||||
|  | ||||
| .. option:: --enable | ||||
|  | ||||
|     Enable port | ||||
|  | ||||
| .. option:: --disable | ||||
|  | ||||
|     Disable port | ||||
|  | ||||
| .. _port_set-port: | ||||
| .. describe:: <port> | ||||
|  | ||||
|     Port to modify (name or ID) | ||||
|  | ||||
| port show | ||||
| --------- | ||||
|  | ||||
|   | ||||
| @@ -14,6 +14,7 @@ | ||||
| """Port action implementations""" | ||||
|  | ||||
| from openstackclient.common import command | ||||
| from openstackclient.common import exceptions | ||||
| from openstackclient.common import parseractions | ||||
| from openstackclient.common import utils | ||||
| from openstackclient.identity import common as identity_common | ||||
| @@ -56,8 +57,6 @@ def _get_columns(item): | ||||
| def _get_attrs(client_manager, parsed_args): | ||||
|     attrs = {} | ||||
|  | ||||
|     if parsed_args.name is not None: | ||||
|         attrs['name'] = str(parsed_args.name) | ||||
|     if parsed_args.fixed_ip is not None: | ||||
|         attrs['fixed_ips'] = parsed_args.fixed_ip | ||||
|     if parsed_args.device_id is not None: | ||||
| @@ -75,6 +74,8 @@ def _get_attrs(client_manager, parsed_args): | ||||
|  | ||||
|     # The remaining options do not support 'port set' command, so they require | ||||
|     # additional check | ||||
|     if 'name' in parsed_args and parsed_args.name is not None: | ||||
|         attrs['name'] = str(parsed_args.name) | ||||
|     if 'mac_address' in parsed_args and parsed_args.mac_address is not None: | ||||
|         attrs['mac_address'] = parsed_args.mac_address | ||||
|     if 'network' in parsed_args and parsed_args.network is not None: | ||||
| @@ -145,8 +146,9 @@ def _add_updatable_args(parser): | ||||
|             metavar='<vnic-type>', | ||||
|             choices=['direct', 'direct-physical', 'macvtap', | ||||
|                      'normal', 'baremetal'], | ||||
|             help='VNIC type for this port (direct | direct-physical |' | ||||
|                  ' macvtap | normal(default) | baremetal)') | ||||
|             help="VNIC type for this port (direct | direct-physical |" | ||||
|                  " macvtap | normal | baremetal). If unspecified during" | ||||
|                  " port creation, default value will be 'normal'.") | ||||
|         parser.add_argument( | ||||
|             '--binding-profile', | ||||
|             metavar='<binding-profile>', | ||||
| @@ -265,6 +267,48 @@ class ListPort(command.Lister): | ||||
|                 ) for s in data)) | ||||
|  | ||||
|  | ||||
| class SetPort(command.Command): | ||||
|     """Set port properties""" | ||||
|  | ||||
|     def get_parser(self, prog_name): | ||||
|         parser = super(SetPort, self).get_parser(prog_name) | ||||
|         _add_updatable_args(parser) | ||||
|         admin_group = parser.add_mutually_exclusive_group() | ||||
|         admin_group.add_argument( | ||||
|             '--enable', | ||||
|             dest='admin_state', | ||||
|             action='store_true', | ||||
|             default=None, | ||||
|             help='Enable port', | ||||
|         ) | ||||
|         admin_group.add_argument( | ||||
|             '--disable', | ||||
|             dest='admin_state', | ||||
|             action='store_false', | ||||
|             help='Disable port', | ||||
|         ) | ||||
|         parser.add_argument( | ||||
|             'port', | ||||
|             metavar="<port>", | ||||
|             help=("Port to modify (name or ID)") | ||||
|         ) | ||||
|  | ||||
|         return parser | ||||
|  | ||||
|     def take_action(self, parsed_args): | ||||
|         client = self.app.client_manager.network | ||||
|  | ||||
|         _prepare_fixed_ips(self.app.client_manager, parsed_args) | ||||
|         attrs = _get_attrs(self.app.client_manager, parsed_args) | ||||
|  | ||||
|         if attrs == {}: | ||||
|             msg = "Nothing specified to be set" | ||||
|             raise exceptions.CommandError(msg) | ||||
|  | ||||
|         obj = client.find_port(parsed_args.port, ignore_missing=False) | ||||
|         client.update_port(obj, **attrs) | ||||
|  | ||||
|  | ||||
| class ShowPort(command.ShowOne): | ||||
|     """Display port details""" | ||||
|  | ||||
|   | ||||
| @@ -240,6 +240,85 @@ class TestListPort(TestPort): | ||||
|         self.assertEqual(self.data, list(data)) | ||||
|  | ||||
|  | ||||
| class TestSetPort(TestPort): | ||||
|  | ||||
|     _port = network_fakes.FakePort.create_one_port() | ||||
|  | ||||
|     def setUp(self): | ||||
|         super(TestSetPort, self).setUp() | ||||
|  | ||||
|         self.fake_subnet = network_fakes.FakeSubnet.create_one_subnet() | ||||
|         self.network.find_subnet = mock.Mock(return_value=self.fake_subnet) | ||||
|         self.network.find_port = mock.Mock(return_value=self._port) | ||||
|         self.network.update_port = mock.Mock(return_value=None) | ||||
|  | ||||
|         # Get the command object to test | ||||
|         self.cmd = port.SetPort(self.app, self.namespace) | ||||
|  | ||||
|     def test_set_fixed_ip(self): | ||||
|         arglist = [ | ||||
|             '--fixed-ip', 'ip-address=10.0.0.11', | ||||
|             self._port.name, | ||||
|         ] | ||||
|         verifylist = [ | ||||
|             ('fixed_ip', [{'ip-address': '10.0.0.11'}]), | ||||
|         ] | ||||
|  | ||||
|         parsed_args = self.check_parser(self.cmd, arglist, verifylist) | ||||
|         result = self.cmd.take_action(parsed_args) | ||||
|  | ||||
|         attrs = { | ||||
|             'fixed_ips': [{'ip_address': '10.0.0.11'}], | ||||
|         } | ||||
|         self.network.update_port.assert_called_with(self._port, **attrs) | ||||
|         self.assertIsNone(result) | ||||
|  | ||||
|     def test_set_this(self): | ||||
|         arglist = [ | ||||
|             '--disable', | ||||
|             self._port.name, | ||||
|         ] | ||||
|         verifylist = [ | ||||
|             ('admin_state', False), | ||||
|         ] | ||||
|  | ||||
|         parsed_args = self.check_parser(self.cmd, arglist, verifylist) | ||||
|         result = self.cmd.take_action(parsed_args) | ||||
|  | ||||
|         attrs = { | ||||
|             'admin_state_up': False, | ||||
|         } | ||||
|         self.network.update_port.assert_called_with(self._port, **attrs) | ||||
|         self.assertIsNone(result) | ||||
|  | ||||
|     def test_set_that(self): | ||||
|         arglist = [ | ||||
|             '--enable', | ||||
|             '--vnic-type', 'macvtap', | ||||
|             '--binding-profile', 'foo=bar', | ||||
|             '--host-id', 'binding-host-id-xxxx', | ||||
|             self._port.name, | ||||
|         ] | ||||
|         verifylist = [ | ||||
|             ('admin_state', True), | ||||
|             ('vnic_type', 'macvtap'), | ||||
|             ('binding_profile', {'foo': 'bar'}), | ||||
|             ('host_id', 'binding-host-id-xxxx'), | ||||
|         ] | ||||
|  | ||||
|         parsed_args = self.check_parser(self.cmd, arglist, verifylist) | ||||
|         result = self.cmd.take_action(parsed_args) | ||||
|  | ||||
|         attrs = { | ||||
|             'admin_state_up': True, | ||||
|             'binding:vnic_type': 'macvtap', | ||||
|             'binding:profile': {'foo': 'bar'}, | ||||
|             'binding:host_id': 'binding-host-id-xxxx', | ||||
|         } | ||||
|         self.network.update_port.assert_called_with(self._port, **attrs) | ||||
|         self.assertIsNone(result) | ||||
|  | ||||
|  | ||||
| class TestShowPort(TestPort): | ||||
|  | ||||
|     # The port to show. | ||||
|   | ||||
| @@ -0,0 +1,5 @@ | ||||
| --- | ||||
| features: | ||||
|   - | | ||||
|     Add support for the ``port set`` command. | ||||
|     [Bug `1519909 <https://bugs.launchpad.net/python-openstackclient/+bug/1519909>`_] | ||||
| @@ -336,6 +336,7 @@ openstack.network.v2 = | ||||
|     port_create = openstackclient.network.v2.port:CreatePort | ||||
|     port_delete = openstackclient.network.v2.port:DeletePort | ||||
|     port_list = openstackclient.network.v2.port:ListPort | ||||
|     port_set = openstackclient.network.v2.port:SetPort | ||||
|     port_show = openstackclient.network.v2.port:ShowPort | ||||
|  | ||||
|     router_create = openstackclient.network.v2.router:CreateRouter | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Jas
					Jas