From c82f4237e59dd61f180b2bfbd3383c6540cd6cef Mon Sep 17 00:00:00 2001
From: Hongbin Lu <hongbin.lu@huawei.com>
Date: Fri, 27 Jul 2018 16:41:45 -0400
Subject: [PATCH] Support enable/disable uplink status propagation

Add options to enable/disable uplink status propagation on creating
a neutron port.

Related patches:
* neutron: https://review.openstack.org/#/c/571899/
* openstacksdk: https://review.openstack.org/#/c/586687/

Depends-On: https://review.openstack.org/#/c/586687/
Change-Id: I095a98fc5f5aee62d979a16b3cd79d91ec3b9ddb
Related-Bug: #1722720
---
 doc/source/cli/command-objects/port.rst       |  9 +++++
 openstackclient/network/v2/port.py            | 18 +++++++++
 .../tests/unit/network/v2/fakes.py            |  3 ++
 .../tests/unit/network/v2/test_port.py        | 39 +++++++++++++++++++
 ...k_status_propagation-4d37452bcf03e0f8.yaml |  5 +++
 5 files changed, 74 insertions(+)
 create mode 100644 releasenotes/notes/support-uplink_status_propagation-4d37452bcf03e0f8.yaml

diff --git a/doc/source/cli/command-objects/port.rst b/doc/source/cli/command-objects/port.rst
index bb037fa3e5..bc9f5dde02 100644
--- a/doc/source/cli/command-objects/port.rst
+++ b/doc/source/cli/command-objects/port.rst
@@ -26,6 +26,7 @@ Create new port
         [--binding-profile <binding-profile>]
         [--host <host-id>]
         [--enable | --disable]
+        [--enable-uplink-status-propagation | --disable-uplink-status-propagation]
         [--mac-address <mac-address>]
         [--security-group <security-group> | --no-security-group]
         [--dns-domain <dns-domain>]
@@ -87,6 +88,14 @@ Create new port
 
     Disable port
 
+.. option:: --enable-uplink-status-propagation
+
+    Enable uplink status propagate
+
+.. option:: --disable-uplink-status-propagation
+
+    Disable uplink status propagate (default)
+
 .. option:: --mac-address <mac-address>
 
     MAC address of this port
diff --git a/openstackclient/network/v2/port.py b/openstackclient/network/v2/port.py
index 0d276d9da4..1001e6cf41 100644
--- a/openstackclient/network/v2/port.py
+++ b/openstackclient/network/v2/port.py
@@ -163,6 +163,13 @@ def _get_attrs(client_manager, parsed_args):
         attrs['qos_policy_id'] = client_manager.network.find_qos_policy(
             parsed_args.qos_policy, ignore_missing=False).id
 
+    if ('enable_uplink_status_propagation' in parsed_args and
+            parsed_args.enable_uplink_status_propagation):
+        attrs['propagate_uplink_status'] = True
+    if ('disable_uplink_status_propagation' in parsed_args and
+            parsed_args.disable_uplink_status_propagation):
+        attrs['propagate_uplink_status'] = False
+
     return attrs
 
 
@@ -349,6 +356,17 @@ class CreatePort(command.ShowOne):
             action='store_true',
             help=_("Disable port")
         )
+        uplink_status_group = parser.add_mutually_exclusive_group()
+        uplink_status_group.add_argument(
+            '--enable-uplink-status-propagation',
+            action='store_true',
+            help=_("Enable uplink status propagate")
+        )
+        uplink_status_group.add_argument(
+            '--disable-uplink-status-propagation',
+            action='store_true',
+            help=_("Disable uplink status propagate (default)")
+        )
         parser.add_argument(
             '--project',
             metavar='<project>',
diff --git a/openstackclient/tests/unit/network/v2/fakes.py b/openstackclient/tests/unit/network/v2/fakes.py
index aec7402f94..28e92d1196 100644
--- a/openstackclient/tests/unit/network/v2/fakes.py
+++ b/openstackclient/tests/unit/network/v2/fakes.py
@@ -581,6 +581,7 @@ class FakePort(object):
             'tenant_id': 'project-id-' + uuid.uuid4().hex,
             'qos_policy_id': 'qos-policy-id-' + uuid.uuid4().hex,
             'tags': [],
+            'uplink_status_propagation': False,
         }
 
         # Overwrite default attributes.
@@ -600,6 +601,8 @@ class FakePort(object):
         port.project_id = port_attrs['tenant_id']
         port.security_group_ids = port_attrs['security_group_ids']
         port.qos_policy_id = port_attrs['qos_policy_id']
+        port.uplink_status_propagation = port_attrs[
+            'uplink_status_propagation']
 
         return port
 
diff --git a/openstackclient/tests/unit/network/v2/test_port.py b/openstackclient/tests/unit/network/v2/test_port.py
index 78d7fd6ca1..8ac3e54f4a 100644
--- a/openstackclient/tests/unit/network/v2/test_port.py
+++ b/openstackclient/tests/unit/network/v2/test_port.py
@@ -64,6 +64,7 @@ class TestPort(network_fakes.TestNetworkV2):
             'security_group_ids',
             'status',
             'tags',
+            'uplink_status_propagation',
         )
 
         data = (
@@ -93,6 +94,7 @@ class TestPort(network_fakes.TestNetworkV2):
             utils.format_list(fake_port.security_group_ids),
             fake_port.status,
             utils.format_list(fake_port.tags),
+            fake_port.uplink_status_propagation,
         )
 
         return columns, data
@@ -571,6 +573,43 @@ class TestCreatePort(TestPort):
     def test_create_with_no_tag(self):
         self._test_create_with_tag(add_tags=False)
 
+    def _test_create_with_uplink_status_propagation(self, enable=True):
+        arglist = [
+            '--network', self._port.network_id,
+            'test-port',
+        ]
+        if enable:
+            arglist += ['--enable-uplink-status-propagation']
+        else:
+            arglist += ['--disable-uplink-status-propagation']
+        verifylist = [
+            ('network', self._port.network_id,),
+            ('name', 'test-port'),
+        ]
+        if enable:
+            verifylist.append(('enable_uplink_status_propagation', True))
+        else:
+            verifylist.append(('disable_uplink_status_propagation', True))
+        parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+        columns, data = (self.cmd.take_action(parsed_args))
+
+        self.network.create_port.assert_called_once_with(**{
+            'admin_state_up': True,
+            'network_id': self._port.network_id,
+            'propagate_uplink_status': enable,
+            'name': 'test-port',
+        })
+
+        self.assertEqual(self.columns, columns)
+        self.assertEqual(self.data, data)
+
+    def test_create_with_uplink_status_propagation_enabled(self):
+        self._test_create_with_uplink_status_propagation(enable=True)
+
+    def test_create_with_uplink_status_propagation_disabled(self):
+        self._test_create_with_uplink_status_propagation(enable=False)
+
 
 class TestDeletePort(TestPort):
 
diff --git a/releasenotes/notes/support-uplink_status_propagation-4d37452bcf03e0f8.yaml b/releasenotes/notes/support-uplink_status_propagation-4d37452bcf03e0f8.yaml
new file mode 100644
index 0000000000..766e8abbf8
--- /dev/null
+++ b/releasenotes/notes/support-uplink_status_propagation-4d37452bcf03e0f8.yaml
@@ -0,0 +1,5 @@
+---
+features:
+  - |
+    Add ``--enable-uplink-status-propagation`` option and
+    ``--disable-uplink-status-propagation`` option to ``port create`` command.