Cleanup subnet creation in network api tests

This commit accomplishes 2 things, first it deduplicates the subnet
creation code by changing test_create_update_delete_network_subnet()
to use the common create_subnet() class method. Secondly the common
create_subnet() method is changed to properly use else after the for
loop instead of what was done before.

Change-Id: I04b0516e03eeb8b8f95d72259aa78f573db6ae45
diff --git a/tempest/api/network/base.py b/tempest/api/network/base.py
index 8720985..ba6a617 100644
--- a/tempest/api/network/base.py
+++ b/tempest/api/network/base.py
@@ -152,8 +152,6 @@
             cidr = netaddr.IPNetwork(CONF.network.tenant_network_v6_cidr)
             mask_bits = CONF.network.tenant_network_v6_mask_bits
         # Find a cidr that is not in use yet and create a subnet with it
-        body = None
-        failure = None
         for subnet_cidr in cidr.subnet(mask_bits):
             try:
                 resp, body = cls.client.create_subnet(
@@ -165,12 +163,9 @@
                 is_overlapping_cidr = 'overlaps with another subnet' in str(e)
                 if not is_overlapping_cidr:
                     raise
-                # save the failure in case all of the CIDRs are overlapping
-                failure = e
-
-        if not body and failure:
-            raise failure
-
+        else:
+            message = 'Available CIDR for subnet creation could not be found'
+            raise exceptions.BuildErrorException(message)
         subnet = body['subnet']
         cls.subnets.append(subnet)
         return subnet
diff --git a/tempest/api/network/test_networks.py b/tempest/api/network/test_networks.py
index 654552d..ea802ad 100644
--- a/tempest/api/network/test_networks.py
+++ b/tempest/api/network/test_networks.py
@@ -18,7 +18,6 @@
 from tempest.api.network import base
 from tempest.common.utils import data_utils
 from tempest import config
-from tempest import exceptions
 from tempest.test import attr
 
 CONF = config.CONF
@@ -85,25 +84,7 @@
         updated_net = body['network']
         self.assertEqual(updated_net['name'], new_name)
         # Find a cidr that is not in use yet and create a subnet with it
-        if self._ip_version == 4:
-            cidr = netaddr.IPNetwork(CONF.network.tenant_network_cidr)
-            mask_bits = CONF.network.tenant_network_mask_bits
-        elif self._ip_version == 6:
-            cidr = netaddr.IPNetwork(CONF.network.tenant_network_v6_cidr)
-            mask_bits = CONF.network.tenant_network_v6_mask_bits
-        for subnet_cidr in cidr.subnet(mask_bits):
-            try:
-                resp, body = self.client.create_subnet(
-                    network_id=net_id,
-                    cidr=str(subnet_cidr),
-                    ip_version=self._ip_version)
-                break
-            except exceptions.BadRequest as e:
-                is_overlapping_cidr = 'overlaps with another subnet' in str(e)
-                if not is_overlapping_cidr:
-                    raise
-        self.assertEqual('201', resp['status'])
-        subnet = body['subnet']
+        subnet = self.create_subnet(network)
         subnet_id = subnet['id']
         # Verification of subnet update
         new_subnet = "New_subnet"
@@ -115,6 +96,8 @@
         # Delete subnet and network
         resp, body = self.client.delete_subnet(subnet_id)
         self.assertEqual('204', resp['status'])
+        # Remove subnet from cleanup list
+        self.subnets.pop()
         resp, body = self.client.delete_network(net_id)
         self.assertEqual('204', resp['status'])