Merge "Migrate security_groups_basic to tempest clients"
diff --git a/tempest/scenario/manager.py b/tempest/scenario/manager.py
index dfd4658..cabefc8 100644
--- a/tempest/scenario/manager.py
+++ b/tempest/scenario/manager.py
@@ -114,6 +114,11 @@
                                     'user')
 
     @classmethod
+    def alt_credentials(cls):
+        return cls._get_credentials(cls.isolated_creds.get_alt_creds,
+                                    'alt_user')
+
+    @classmethod
     def admin_credentials(cls):
         return cls._get_credentials(cls.isolated_creds.get_admin_creds,
                                     'identity_admin')
@@ -188,11 +193,13 @@
     # The create_[resource] functions only return body and discard the
     # resp part which is not used in scenario tests
 
-    def create_keypair(self):
+    def create_keypair(self, client=None):
+        if not client:
+            client = self.keypairs_client
         name = data_utils.rand_name(self.__class__.__name__)
         # We don't need to create a keypair by pubkey in scenario
-        resp, body = self.keypairs_client.create_keypair(name)
-        self.addCleanup(self.keypairs_client.delete_keypair, name)
+        resp, body = client.create_keypair(name)
+        self.addCleanup(client.delete_keypair, name)
         return body
 
     def create_server(self, name=None, image=None, flavor=None,
@@ -474,11 +481,15 @@
         cls.tenant_id = cls.manager.identity_client.tenant_id
         cls.check_preconditions()
 
-    def _create_network(self, tenant_id, namestart='network-smoke-'):
+    def _create_network(self, client=None, tenant_id=None,
+                        namestart='network-smoke-'):
+        if not client:
+            client = self.network_client
+        if not tenant_id:
+            tenant_id = client.rest_client.tenant_id
         name = data_utils.rand_name(namestart)
-        _, result = self.network_client.create_network(name=name,
-                                                       tenant_id=tenant_id)
-        network = net_resources.DeletableNetwork(client=self.network_client,
+        _, result = client.create_network(name=name, tenant_id=tenant_id)
+        network = net_resources.DeletableNetwork(client=client,
                                                  **result['network'])
         self.assertEqual(network.name, name)
         self.addCleanup(self.delete_wrapper, network.delete)
@@ -508,11 +519,14 @@
             return resource_list[resource_type]
         return temp
 
-    def _create_subnet(self, network, namestart='subnet-smoke-', **kwargs):
+    def _create_subnet(self, network, client=None, namestart='subnet-smoke',
+                       **kwargs):
         """
         Create a subnet for the given network within the cidr block
         configured for tenant networks.
         """
+        if not client:
+            client = self.network_client
 
         def cidr_in_use(cidr, tenant_id):
             """
@@ -541,27 +555,29 @@
                 **kwargs
             )
             try:
-                _, result = self.network_client.create_subnet(**subnet)
+                _, result = client.create_subnet(**subnet)
                 break
-            except exc.NeutronClientException as e:
+            except exceptions.Conflict as e:
                 is_overlapping_cidr = 'overlaps with another subnet' in str(e)
                 if not is_overlapping_cidr:
                     raise
         self.assertIsNotNone(result, 'Unable to allocate tenant network')
-        subnet = net_resources.DeletableSubnet(client=self.network_client,
+        subnet = net_resources.DeletableSubnet(client=client,
                                                **result['subnet'])
         self.assertEqual(subnet.cidr, str_cidr)
         self.addCleanup(self.delete_wrapper, subnet.delete)
         return subnet
 
-    def _create_port(self, network, namestart='port-quotatest'):
+    def _create_port(self, network, client=None, namestart='port-quotatest'):
+        if not client:
+            client = self.network_client
         name = data_utils.rand_name(namestart)
-        _, result = self.network_client.create_port(
+        _, result = client.create_port(
             name=name,
             network_id=network.id,
             tenant_id=network.tenant_id)
         self.assertIsNotNone(result, 'Unable to allocate port')
-        port = net_resources.DeletablePort(client=self.network_client,
+        port = net_resources.DeletablePort(client=client,
                                            **result['port'])
         self.addCleanup(self.delete_wrapper, port.delete)
         return port
@@ -577,16 +593,19 @@
         net = self._list_networks(name=network_name)
         return net_common.AttributeDict(net[0])
 
-    def _create_floating_ip(self, thing, external_network_id, port_id=None):
+    def _create_floating_ip(self, thing, external_network_id, port_id=None,
+                            client=None):
+        if not client:
+            client = self.network_client
         if not port_id:
             port_id = self._get_server_port_id(thing)
-        _, result = self.network_client.create_floatingip(
+        _, result = client.create_floatingip(
             floating_network_id=external_network_id,
             port_id=port_id,
             tenant_id=thing['tenant_id']
         )
         floating_ip = net_resources.DeletableFloatingIp(
-            client=self.network_client,
+            client=client,
             **result['floatingip'])
         self.addCleanup(self.delete_wrapper, floating_ip.delete)
         return floating_ip
@@ -716,10 +735,12 @@
                                             CONF.compute.ping_timeout,
                                             1)
 
-    def _create_security_group(self, tenant_id, client=None,
+    def _create_security_group(self, client=None, tenant_id=None,
                                namestart='secgroup-smoke'):
         if client is None:
             client = self.network_client
+        if tenant_id is None:
+            tenant_id = client.rest_client.tenant_id
         secgroup = self._create_empty_security_group(namestart=namestart,
                                                      client=client,
                                                      tenant_id=tenant_id)
@@ -731,7 +752,7 @@
             self.assertEqual(secgroup.id, rule.security_group_id)
         return secgroup
 
-    def _create_empty_security_group(self, tenant_id, client=None,
+    def _create_empty_security_group(self, client=None, tenant_id=None,
                                      namestart='secgroup-smoke'):
         """Create a security group without rules.
 
@@ -744,6 +765,8 @@
         """
         if client is None:
             client = self.network_client
+        if not tenant_id:
+            tenant_id = client.rest_client.tenant_id
         sg_name = data_utils.rand_name(namestart)
         sg_desc = sg_name + " description"
         sg_dict = dict(name=sg_name,
@@ -760,13 +783,15 @@
         self.addCleanup(self.delete_wrapper, secgroup.delete)
         return secgroup
 
-    def _default_security_group(self, tenant_id, client=None):
+    def _default_security_group(self, client=None, tenant_id=None):
         """Get default secgroup for given tenant_id.
 
         :returns: DeletableSecurityGroup -- default secgroup for given tenant
         """
         if client is None:
             client = self.network_client
+        if not tenant_id:
+            tenant_id = client.rest_client.tenant_id
         sgs = [
             sg for sg in client.list_security_groups().values()[0]
             if sg['tenant_id'] == tenant_id and sg['name'] == 'default'
@@ -779,7 +804,7 @@
         return net_resources.DeletableSecurityGroup(client=client,
                                                     **sgs[0])
 
-    def _create_security_group_rule(self, client=None, secgroup=None,
+    def _create_security_group_rule(self, secgroup=None, client=None,
                                     tenant_id=None, **kwargs):
         """Create a rule from a dictionary of rule parameters.
 
@@ -787,8 +812,6 @@
         default secgroup in tenant_id.
 
         :param secgroup: type DeletableSecurityGroup.
-        :param secgroup_id: search for secgroup by id
-            default -- choose default secgroup for given tenant_id
         :param tenant_id: if secgroup not passed -- the tenant in which to
             search for default secgroup
         :param kwargs: a dictionary containing rule parameters:
@@ -802,8 +825,11 @@
         """
         if client is None:
             client = self.network_client
+        if not tenant_id:
+            tenant_id = client.rest_client.tenant_id
         if secgroup is None:
-            secgroup = self._default_security_group(tenant_id)
+            secgroup = self._default_security_group(client=client,
+                                                    tenant_id=tenant_id)
 
         ruleset = dict(security_group_id=secgroup.id,
                        tenant_id=secgroup.tenant_id)
@@ -865,7 +891,7 @@
                                       username=ssh_login,
                                       private_key=private_key)
 
-    def _get_router(self, tenant_id):
+    def _get_router(self, client=None, tenant_id=None):
         """Retrieve a router for the given tenant id.
 
         If a public router has been configured, it will be returned.
@@ -874,31 +900,40 @@
         network has, a tenant router will be created and returned that
         routes traffic to the public network.
         """
+        if not client:
+            client = self.network_client
+        if not tenant_id:
+            tenant_id = client.rest_client.tenant_id
         router_id = CONF.network.public_router_id
         network_id = CONF.network.public_network_id
         if router_id:
-            result = self.network_client.show_router(router_id)
+            result = client.show_router(router_id)
             return net_resources.AttributeDict(**result['router'])
         elif network_id:
-            router = self._create_router(tenant_id)
+            router = self._create_router(client, tenant_id)
             router.set_gateway(network_id)
             return router
         else:
             raise Exception("Neither of 'public_router_id' or "
                             "'public_network_id' has been defined.")
 
-    def _create_router(self, tenant_id, namestart='router-smoke-'):
+    def _create_router(self, client=None, tenant_id=None,
+                       namestart='router-smoke'):
+        if not client:
+            client = self.network_client
+        if not tenant_id:
+            tenant_id = client.rest_client.tenant_id
         name = data_utils.rand_name(namestart)
-        _, result = self.network_client.create_router(name=name,
-                                                      admin_state_up=True,
-                                                      tenant_id=tenant_id, )
-        router = net_resources.DeletableRouter(client=self.network_client,
+        _, result = client.create_router(name=name,
+                                         admin_state_up=True,
+                                         tenant_id=tenant_id)
+        router = net_resources.DeletableRouter(client=client,
                                                **result['router'])
         self.assertEqual(router.name, name)
         self.addCleanup(self.delete_wrapper, router.delete)
         return router
 
-    def create_networks(self, tenant_id=None):
+    def create_networks(self, client=None, tenant_id=None):
         """Create a network with a subnet connected to a router.
 
         The baremetal driver is a special case since all nodes are
@@ -917,11 +952,9 @@
             router = None
             subnet = None
         else:
-            if tenant_id is None:
-                tenant_id = self.tenant_id
-            network = self._create_network(tenant_id)
-            router = self._get_router(tenant_id)
-            subnet = self._create_subnet(network)
+            network = self._create_network(client=client, tenant_id=tenant_id)
+            router = self._get_router(client=client, tenant_id=tenant_id)
+            subnet = self._create_subnet(network=network, client=client)
             subnet.add_to_router(router.id)
         return network, subnet, router
 
diff --git a/tempest/scenario/test_network_basic_ops.py b/tempest/scenario/test_network_basic_ops.py
index 21a5d1b..e8dba6a 100644
--- a/tempest/scenario/test_network_basic_ops.py
+++ b/tempest/scenario/test_network_basic_ops.py
@@ -203,7 +203,7 @@
             floating_ip, server)
 
     def _create_new_network(self):
-        self.new_net = self._create_network(self.tenant_id)
+        self.new_net = self._create_network(tenant_id=self.tenant_id)
         self.new_subnet = self._create_subnet(
             network=self.new_net,
             gateway_ip=None)
diff --git a/tempest/scenario/test_security_groups_basic_ops.py b/tempest/scenario/test_security_groups_basic_ops.py
index e9ca770..520c232 100644
--- a/tempest/scenario/test_security_groups_basic_ops.py
+++ b/tempest/scenario/test_security_groups_basic_ops.py
@@ -26,7 +26,7 @@
 LOG = logging.getLogger(__name__)
 
 
-class TestSecurityGroupsBasicOps(manager.NetworkScenarioTest):
+class TestSecurityGroupsBasicOps(manager.NeutronScenarioTest):
 
     """
     This test suite assumes that Nova has been configured to
@@ -99,7 +99,7 @@
         """
 
         def __init__(self, credentials):
-            self.manager = clients.OfficialClientManager(credentials)
+            self.manager = clients.Manager(credentials)
             # Credentials from manager are filled with both names and IDs
             self.creds = self.manager.credentials
             self.network = None
@@ -113,13 +113,18 @@
             self.subnet = subnet
             self.router = router
 
-        def _get_tenant_credentials(self):
-            # FIXME(andreaf) Unused method
-            return self.creds
-
     @classmethod
     def check_preconditions(cls):
+        if CONF.baremetal.driver_enabled:
+            msg = ('Not currently supported by baremetal.')
+            cls.enabled = False
+            raise cls.skipException(msg)
         super(TestSecurityGroupsBasicOps, cls).check_preconditions()
+        # need alt_creds here to check preconditions
+        cls.alt_creds = cls.alt_credentials()
+        cls.alt_manager = clients.Manager(cls.alt_creds)
+        # Credentials from the manager are filled with both IDs and Names
+        cls.alt_creds = cls.alt_manager.credentials
         if (cls.alt_creds is None) or \
                 (cls.tenant_id is cls.alt_creds.tenant_id):
             msg = 'No alt_tenant defined'
@@ -131,21 +136,12 @@
                    'public_network_id must be defined.')
             cls.enabled = False
             raise cls.skipException(msg)
-        if CONF.baremetal.driver_enabled:
-            msg = ('Not currently supported by baremetal.')
-            cls.enabled = False
-            raise cls.skipException(msg)
 
     @classmethod
     def setUpClass(cls):
         # Create no network resources for these tests.
         cls.set_network_resources()
         super(TestSecurityGroupsBasicOps, cls).setUpClass()
-        cls.alt_creds = cls.alt_credentials()
-        cls.alt_manager = clients.OfficialClientManager(cls.alt_creds)
-        # Credentials from the manager are filled with both IDs and Names
-        cls.alt_creds = cls.alt_manager.credentials
-        cls.check_preconditions()
         # TODO(mnewby) Consider looking up entities as needed instead
         # of storing them as collections on the class.
         cls.floating_ips = {}
@@ -166,21 +162,22 @@
         self._verify_network_details(self.primary_tenant)
         self._verify_mac_addr(self.primary_tenant)
 
-    def _create_tenant_keypairs(self, tenant_id):
-        keypair = self.create_keypair(
-            name=data_utils.rand_name('keypair-smoke-'))
-        self.tenants[tenant_id].keypair = keypair
+    def _create_tenant_keypairs(self, tenant):
+        keypair = self.create_keypair(tenant.manager.keypairs_client)
+        tenant.keypair = keypair
 
     def _create_tenant_security_groups(self, tenant):
         access_sg = self._create_empty_security_group(
             namestart='secgroup_access-',
-            tenant_id=tenant.creds.tenant_id
+            tenant_id=tenant.creds.tenant_id,
+            client=tenant.manager.network_client
         )
 
         # don't use default secgroup since it allows in-tenant traffic
         def_sg = self._create_empty_security_group(
             namestart='secgroup_general-',
-            tenant_id=tenant.creds.tenant_id
+            tenant_id=tenant.creds.tenant_id,
+            client=tenant.manager.network_client
         )
         tenant.security_groups.update(access=access_sg, default=def_sg)
         ssh_rule = dict(
@@ -189,7 +186,9 @@
             port_range_max=22,
             direction='ingress',
         )
-        self._create_security_group_rule(secgroup=access_sg, **ssh_rule)
+        self._create_security_group_rule(secgroup=access_sg,
+                                         client=tenant.manager.network_client,
+                                         **ssh_rule)
 
     def _verify_network_details(self, tenant):
         # Checks that we see the newly created network/subnet/router via
@@ -216,7 +215,7 @@
 
         myport = (tenant.router.id, tenant.subnet.id)
         router_ports = [(i['device_id'], i['fixed_ips'][0]['subnet_id']) for i
-                        in self.network_client.list_ports()['ports']
+                        in self._list_ports()
                         if self._is_router_port(i)]
 
         self.assertIn(myport, router_ports)
@@ -233,17 +232,16 @@
         """
         self._set_compute_context(tenant)
         if security_groups is None:
-            security_groups = [tenant.security_groups['default'].name]
+            security_groups = [tenant.security_groups['default']]
         create_kwargs = {
             'nics': [
                 {'net-id': tenant.network.id},
             ],
-            'key_name': tenant.keypair.name,
+            'key_name': tenant.keypair['name'],
             'security_groups': security_groups,
             'tenant_id': tenant.creds.tenant_id
         }
-        server = self.create_server(name=name, create_kwargs=create_kwargs)
-        return server
+        return self.create_server(name=name, create_kwargs=create_kwargs)
 
     def _create_tenant_servers(self, tenant, num=1):
         for i in range(num):
@@ -261,27 +259,30 @@
         in order to access tenant internal network
         workaround ip namespace
         """
-        secgroups = [sg.name for sg in tenant.security_groups.values()]
+        secgroups = tenant.security_groups.values()
         name = 'server-{tenant}-access_point-'.format(
             tenant=tenant.creds.tenant_name)
         name = data_utils.rand_name(name)
         server = self._create_server(name, tenant,
                                      security_groups=secgroups)
         tenant.access_point = server
-        self._assign_floating_ips(server)
+        self._assign_floating_ips(tenant, server)
 
-    def _assign_floating_ips(self, server):
+    def _assign_floating_ips(self, tenant, server):
         public_network_id = CONF.network.public_network_id
-        floating_ip = self._create_floating_ip(server, public_network_id)
-        self.floating_ips.setdefault(server, floating_ip)
+        floating_ip = self._create_floating_ip(
+            server, public_network_id,
+            client=tenant.manager.network_client)
+        self.floating_ips.setdefault(server['id'], floating_ip)
 
     def _create_tenant_network(self, tenant):
-        network, subnet, router = self.create_networks(tenant.creds.tenant_id)
+        network, subnet, router = self.create_networks(
+            client=tenant.manager.network_client)
         tenant.set_network(network, subnet, router)
 
     def _set_compute_context(self, tenant):
-        self.compute_client = tenant.manager.compute_client
-        return self.compute_client
+        self.servers_client = tenant.manager.servers_client
+        return self.servers_client
 
     def _deploy_tenant(self, tenant_or_id):
         """
@@ -294,12 +295,10 @@
         """
         if not isinstance(tenant_or_id, self.TenantProperties):
             tenant = self.tenants[tenant_or_id]
-            tenant_id = tenant_or_id
         else:
             tenant = tenant_or_id
-            tenant_id = tenant.creds.tenant_id
         self._set_compute_context(tenant)
-        self._create_tenant_keypairs(tenant_id)
+        self._create_tenant_keypairs(tenant)
         self._create_tenant_network(tenant)
         self._create_tenant_security_groups(tenant)
         self._set_access_point(tenant)
@@ -309,12 +308,12 @@
         returns the ip (floating/internal) of a server
         """
         if floating:
-            server_ip = self.floating_ips[server].floating_ip_address
+            server_ip = self.floating_ips[server['id']].floating_ip_address
         else:
             server_ip = None
-            network_name = self.tenants[server.tenant_id].network.name
-            if network_name in server.networks:
-                server_ip = server.networks[network_name][0]
+            network_name = self.tenants[server['tenant_id']].network.name
+            if network_name in server['addresses']:
+                server_ip = server['addresses'][network_name][0]['addr']
         return server_ip
 
     def _connect_to_access_point(self, tenant):
@@ -322,8 +321,8 @@
         create ssh connection to tenant access point
         """
         access_point_ssh = \
-            self.floating_ips[tenant.access_point].floating_ip_address
-        private_key = tenant.keypair.private_key
+            self.floating_ips[tenant.access_point['id']].floating_ip_address
+        private_key = tenant.keypair['private_key']
         access_point_ssh = self._ssh_to_server(access_point_ssh,
                                                private_key=private_key)
         return access_point_ssh
@@ -387,6 +386,7 @@
         )
         self._create_security_group_rule(
             secgroup=dest_tenant.security_groups['default'],
+            client=dest_tenant.manager.network_client,
             **ruleset
         )
         access_point_ssh = self._connect_to_access_point(source_tenant)
@@ -400,6 +400,7 @@
         # allow reverse traffic and check
         self._create_security_group_rule(
             secgroup=source_tenant.security_groups['default'],
+            client=source_tenant.manager.network_client,
             **ruleset
         )
 
@@ -418,8 +419,7 @@
         mac_addr = mac_addr.strip().lower()
         # Get the fixed_ips and mac_address fields of all ports. Select
         # only those two columns to reduce the size of the response.
-        port_list = self.network_client.list_ports(
-            fields=['fixed_ips', 'mac_address'])['ports']
+        port_list = self._list_ports(fields=['fixed_ips', 'mac_address'])
         port_detail_list = [
             (port['fixed_ips'][0]['subnet_id'],
              port['fixed_ips'][0]['ip_address'],