Add segments client

Segments client is necessary to test the routed provider networks
feature.
the following client methods are now available for Tempest:
* create_segment
* update_segment
* show_segment
* delete_segment
* list_segments

Change-Id: Ie89468351fc9cf68ccf356d35ee9ba4b5037c499
diff --git a/releasenotes/notes/segments-client-866f02948f40d4ff.yaml b/releasenotes/notes/segments-client-866f02948f40d4ff.yaml
new file mode 100644
index 0000000..90ac3e8
--- /dev/null
+++ b/releasenotes/notes/segments-client-866f02948f40d4ff.yaml
@@ -0,0 +1,12 @@
+---
+features:
+  - |
+    Add ``segments`` client to Tempest to make possible the testing of the
+    Routed Provider Networks feature.
+    The following API calls are available for tempest from now:
+
+    * POST /segments
+    * PUT /segments/{segment_id}
+    * GET /segment/{segment_id}
+    * DELETE /segments/{segment_id}
+    * GET /segments
diff --git a/tempest/clients.py b/tempest/clients.py
index f7a83be..6aed92e 100644
--- a/tempest/clients.py
+++ b/tempest/clients.py
@@ -71,6 +71,7 @@
         self.tags_client = self.network.TagsClient()
         self.qos_client = self.network.QosClient()
         self.qos_min_bw_client = self.network.QosMinimumBandwidthRulesClient()
+        self.segments_client = self.network.SegmentsClient()
 
     def _set_image_clients(self):
         if CONF.service_available.glance:
diff --git a/tempest/lib/services/network/__init__.py b/tempest/lib/services/network/__init__.py
index 69f178e..f7ac046 100644
--- a/tempest/lib/services/network/__init__.py
+++ b/tempest/lib/services/network/__init__.py
@@ -30,6 +30,7 @@
     SecurityGroupRulesClient
 from tempest.lib.services.network.security_groups_client import \
     SecurityGroupsClient
+from tempest.lib.services.network.segments_client import SegmentsClient
 from tempest.lib.services.network.service_providers_client import \
     ServiceProvidersClient
 from tempest.lib.services.network.subnetpools_client import SubnetpoolsClient
@@ -42,5 +43,5 @@
            'NetworksClient', 'NetworkVersionsClient', 'PortsClient',
            'QosClient', 'QosMinimumBandwidthRulesClient', 'QuotasClient',
            'RoutersClient', 'SecurityGroupRulesClient', 'SecurityGroupsClient',
-           'ServiceProvidersClient', 'SubnetpoolsClient', 'SubnetsClient',
-           'TagsClient']
+           'SegmentsClient', 'ServiceProvidersClient', 'SubnetpoolsClient',
+           'SubnetsClient', 'TagsClient']
diff --git a/tempest/lib/services/network/segments_client.py b/tempest/lib/services/network/segments_client.py
new file mode 100644
index 0000000..dfdc418
--- /dev/null
+++ b/tempest/lib/services/network/segments_client.py
@@ -0,0 +1,63 @@
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
+#    not use this file except in compliance with the License. You may obtain
+#    a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+#    License for the specific language governing permissions and limitations
+#    under the License.
+
+from tempest.lib.services.network import base
+
+
+class SegmentsClient(base.BaseNetworkClient):
+
+    def create_segment(self, **kwargs):
+        """Creates a segment.
+
+        For a full list of available parameters, please refer to the official
+        API reference:
+        http://developer.openstack.org/api-ref/networking/v2/index.html#create-segment
+        """
+        uri = '/segments'
+        post_data = {'segment': kwargs}
+        return self.create_resource(uri, post_data)
+
+    def update_segment(self, segment_id, **kwargs):
+        """Updates a segment.
+
+        For a full list of available parameters, please refer to the official
+        API reference:
+        http://developer.openstack.org/api-ref/networking/v2/index.html#update-segment
+        """
+        uri = '/segments/%s' % segment_id
+        post_data = {'segment': kwargs}
+        return self.update_resource(uri, post_data)
+
+    def show_segment(self, segment_id, **fields):
+        """Shows details of a segment.
+
+        For a full list of available parameters, please refer to the official
+        API reference:
+        http://developer.openstack.org/api-ref/networking/v2/index.html#show-segment
+        """
+        uri = '/segments/%s' % segment_id
+        return self.show_resource(uri, **fields)
+
+    def delete_segment(self, segment_id):
+        """Deletes a segment"""
+        uri = '/segments/%s' % segment_id
+        return self.delete_resource(uri)
+
+    def list_segments(self, **filters):
+        """Lists segments.
+
+        For a full list of available parameters, please refer to the official
+        API reference:
+        http://developer.openstack.org/api-ref/networking/v2/index.html#list-segments
+        """
+        uri = '/segments'
+        return self.list_resources(uri, **filters)
diff --git a/tempest/tests/lib/services/network/test_segments_client.py b/tempest/tests/lib/services/network/test_segments_client.py
new file mode 100644
index 0000000..579c78f
--- /dev/null
+++ b/tempest/tests/lib/services/network/test_segments_client.py
@@ -0,0 +1,140 @@
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
+#    not use this file except in compliance with the License. You may obtain
+#    a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+#    License for the specific language governing permissions and limitations
+#    under the License.
+
+import copy
+
+from tempest.lib.services.network import segments_client
+from tempest.tests.lib import fake_auth_provider
+from tempest.tests.lib.services import base
+
+
+class TestSegmentsClient(base.BaseServiceTest):
+
+    FAKE_SEGMENT_ID = '83a59912-a473-11e9-a012-af494c35c9c2'
+    FAKE_NETWORK_ID = '913ab0e4-a473-11e9-84a3-af1c16fc05de'
+
+    FAKE_SEGMENT_REQUEST = {
+        'segment': {
+            'network_id': FAKE_NETWORK_ID,
+            'segmentation_id': 2000,
+            'network_type': 'vlan',
+            'physical_network': 'segment-1'
+        }
+    }
+
+    FAKE_SEGMENT_RESPONSE = {
+        'segment': {
+            'name': 'foo',
+            'network_id': FAKE_NETWORK_ID,
+            'segmentation_id': 2000,
+            'network_type': 'vlan',
+            'physical_network': 'segment-1',
+            'revision_number': 1,
+            'id': FAKE_SEGMENT_ID,
+            'created_at': '2019-07-12T09:13:56Z',
+            'updated_at': '2019-07-12T09:13:56Z',
+            'description': 'bar'
+        }
+    }
+
+    FAKE_SEGMENTS = {
+        'segments': [
+            FAKE_SEGMENT_RESPONSE['segment']
+        ]
+    }
+
+    def setUp(self):
+        super(TestSegmentsClient, self).setUp()
+        fake_auth = fake_auth_provider.FakeAuthProvider()
+        self.segments_client = segments_client.SegmentsClient(
+            fake_auth, 'compute', 'regionOne')
+
+    def _test_create_segment(self, bytes_body=False):
+        self.check_service_client_function(
+            self.segments_client.create_segment,
+            'tempest.lib.common.rest_client.RestClient.post',
+            self.FAKE_SEGMENT_RESPONSE,
+            bytes_body,
+            201,
+            **self.FAKE_SEGMENT_REQUEST['segment']
+        )
+
+    def _test_list_segments(self, bytes_body=False):
+        self.check_service_client_function(
+            self.segments_client.list_segments,
+            'tempest.lib.common.rest_client.RestClient.get',
+            self.FAKE_SEGMENTS,
+            bytes_body,
+            200
+        )
+
+    def _test_show_segment(self, bytes_body=False):
+        self.check_service_client_function(
+            self.segments_client.show_segment,
+            'tempest.lib.common.rest_client.RestClient.get',
+            self.FAKE_SEGMENT_RESPONSE,
+            bytes_body,
+            200,
+            segment_id=self.FAKE_SEGMENT_ID
+        )
+
+    def _test_update_segment(self, bytes_body=False):
+        update_kwargs = {
+            'name': 'notfoo'
+        }
+
+        resp_body = {
+            'segment': copy.deepcopy(self.FAKE_SEGMENT_RESPONSE['segment'])
+        }
+        resp_body['segment'].update(update_kwargs)
+
+        self.check_service_client_function(
+            self.segments_client.update_segment,
+            'tempest.lib.common.rest_client.RestClient.put',
+            resp_body,
+            bytes_body,
+            200,
+            segment_id=self.FAKE_SEGMENT_ID,
+            **update_kwargs
+        )
+
+    def test_create_segment_with_str_body(self):
+        self._test_create_segment()
+
+    def test_create_segment_with_bytes_body(self):
+        self._test_create_segment(bytes_body=True)
+
+    def test_update_segment_with_str_body(self):
+        self._test_update_segment()
+
+    def test_update_segment_with_bytes_body(self):
+        self._test_update_segment(bytes_body=True)
+
+    def test_show_segment_with_str_body(self):
+        self._test_show_segment()
+
+    def test_show_segment_with_bytes_body(self):
+        self._test_show_segment(bytes_body=True)
+
+    def test_delete_segment(self):
+        self.check_service_client_function(
+            self.segments_client.delete_segment,
+            'tempest.lib.common.rest_client.RestClient.delete',
+            {},
+            status=204,
+            segment_id=self.FAKE_SEGMENT_ID)
+
+    def test_list_segment_with_str_body(self):
+        self._test_list_segments()
+
+    def test_list_segment_with_bytes_body(self):
+        self._test_list_segments(bytes_body=True)