# Copyright 2012 OpenStack Foundation
# All Rights Reserved.
#
#    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 import config
from tempest.openstack.common import log as logging
from tempest.scenario import manager
from tempest.scenario import utils as test_utils
from tempest import test

CONF = config.CONF

LOG = logging.getLogger(__name__)

load_tests = test_utils.load_tests_input_scenario_utils


class TestServerBasicOps(manager.OfficialClientTest):

    """
    This smoke test case follows this basic set of operations:

     * Create a keypair for use in launching an instance
     * Create a security group to control network access in instance
     * Add simple permissive rules to the security group
     * Launch an instance
     * Pause/unpause the instance
     * Suspend/resume the instance
     * Terminate the instance
    """

    def setUp(self):
        super(TestServerBasicOps, self).setUp()
        # Setup image and flavor the test instance
        # Support both configured and injected values
        if not hasattr(self, 'image_ref'):
            self.image_ref = CONF.compute.image_ref
        if not hasattr(self, 'flavor_ref'):
            self.flavor_ref = CONF.compute.flavor_ref
        self.image_utils = test_utils.ImageUtils()
        if not self.image_utils.is_flavor_enough(self.flavor_ref,
                                                 self.image_ref):
            raise self.skipException(
                '{image} does not fit in {flavor}'.format(
                    image=self.image_ref, flavor=self.flavor_ref
                )
            )
        self.run_ssh = CONF.compute.run_ssh and \
            self.image_utils.is_sshable_image(self.image_ref)
        self.ssh_user = self.image_utils.ssh_user(self.image_ref)
        LOG.debug('Starting test for i:{image}, f:{flavor}. '
                  'Run ssh: {ssh}, user: {ssh_user}'.format(
                      image=self.image_ref, flavor=self.flavor_ref,
                      ssh=self.run_ssh, ssh_user=self.ssh_user))

    def add_keypair(self):
        self.keypair = self.create_keypair()

    def boot_instance(self):
        # Create server with image and flavor from input scenario
        security_groups = [self.security_group.name]
        create_kwargs = {
            'key_name': self.keypair.id,
            'security_groups': security_groups
        }
        self.instance = self.create_server(image=self.image_ref,
                                           flavor=self.flavor_ref,
                                           create_kwargs=create_kwargs)

    def verify_ssh(self):
        if self.run_ssh:
            # Obtain a floating IP
            floating_ip = self.compute_client.floating_ips.create()
            self.addCleanup(self.delete_wrapper, floating_ip)
            # Attach a floating IP
            self.instance.add_floating_ip(floating_ip)
            # Check ssh
            try:
                self.get_remote_client(
                    server_or_ip=floating_ip.ip,
                    username=self.image_utils.ssh_user(self.image_ref),
                    private_key=self.keypair.private_key)
            except Exception:
                LOG.exception('ssh to server failed')
                self._log_console_output()
                raise

    @test.services('compute', 'network')
    def test_server_basicops(self):
        self.add_keypair()
        self.security_group = self._create_security_group_nova()
        self.boot_instance()
        self.verify_ssh()
        self.instance.delete()

@@ -970,22 +970,16 @@
 ger.
-OfficialClient
+Scenario
 Test
@@ -2768,13 +2768,8 @@
 roup
-.name
 %5D%0A  
@@ -2832,11 +2832,16 @@
 pair
-.id
+%5B'name'%5D
 ,%0A  
@@ -3197,16 +3197,19 @@
         
+ _,
  floatin
@@ -3224,23 +3224,28 @@
 elf.
-compute
+floating_ips
 _client.
 floa
@@ -3240,16 +3240,23 @@
 _client.
+create_
 floating
@@ -3262,16 +3262,8 @@
 g_ip
-s.create
 ()%0A 
@@ -3313,110 +3313,288 @@
 per,
- floating_ip)%0A            # Attach a floating IP%0A            self.instance.add_floating_ip(floating_ip
+%0A                            self.floating_ips_client.delete_floating_ip,%0A                            floating_ip%5B'id'%5D)%0A            # Attach a floating IP%0A            self.floating_ips_client.associate_floating_ip_to_server(%0A                floating_ip%5B'ip'%5D, self.instance%5B'id'%5D
 )%0A  
@@ -3720,11 +3720,14 @@
 g_ip
-.ip
+%5B'ip'%5D
 ,%0A  
@@ -3840,17 +3840,18 @@
 .keypair
-.
+%5B'
 private_
@@ -3853,16 +3853,18 @@
 vate_key
+'%5D
 )%0A      
@@ -4248,22 +4248,54 @@
 elf.
-instance.delete(
+servers_client.delete_server(self.instance%5B'id'%5D
 )%0A

Migrate test_server_basic_ops to tempest client