# -*- coding: utf-8 -*-
'''
    :codeauthor: :email:`Nicole Thomas <nicole@saltstack.com>`
'''

# Import Python Libs
from __future__ import absolute_import
import os

# Import Salt Testing Libs
from tests.support.case import ShellCase
from tests.support.paths import FILES
from tests.support.unit import skipIf
from tests.support.helpers import expensiveTest, generate_random_name

# Import Salt Libs
from salt.config import cloud_providers_config

# Import Third-Party Libs
try:
    import libcloud  # pylint: disable=unused-import
    HAS_LIBCLOUD = True
except ImportError:
    HAS_LIBCLOUD = False

# Create the cloud instance name to be used throughout the tests
INSTANCE_NAME = generate_random_name('CLOUD-TEST-')
PROVIDER_NAME = 'rackspace'
DRIVER_NAME = 'openstack'


@skipIf(HAS_LIBCLOUD is False, 'salt-cloud requires >= libcloud 0.13.2')
class RackspaceTest(ShellCase):
    '''
    Integration tests for the Rackspace cloud provider using the Openstack driver
    '''

    @expensiveTest
    def setUp(self):
        '''
        Sets up the test requirements
        '''
        super(RackspaceTest, self).setUp()

        # check if appropriate cloud provider and profile files are present
        profile_str = 'rackspace-config'
        providers = self.run_cloud('--list-providers')
        if profile_str + ':' not in providers:
            self.skipTest(
                'Configuration file for {0} was not found. Check {0}.conf files '
                'in tests/integration/files/conf/cloud.*.d/ to run these tests.'
                .format(PROVIDER_NAME)
            )

        # check if personal access token, ssh_key_file, and ssh_key_names are present
        config = cloud_providers_config(
            os.path.join(
                FILES,
                'conf',
                'cloud.providers.d',
                PROVIDER_NAME + '.conf'
            )
        )

        user = config[profile_str][DRIVER_NAME]['user']
        tenant = config[profile_str][DRIVER_NAME]['tenant']
        api = config[profile_str][DRIVER_NAME]['apikey']
        if api == '' or tenant == '' or user == '':
            self.skipTest(
                'A user, tenant, and an api key must be provided to run these '
                'tests. Check tests/integration/files/conf/cloud.providers.d/{0}.conf'
                .format(PROVIDER_NAME)
            )

    def test_instance(self):
        '''
        Test creating an instance on rackspace with the openstack driver
        '''
        # check if instance with salt installed returned
        try:
            self.assertIn(
                INSTANCE_NAME,
                [i.strip() for i in self.run_cloud('-p rackspace-test {0}'.format(INSTANCE_NAME), timeout=500)]
            )
        except AssertionError:
            self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME), timeout=500)
            raise

        # delete the instance
        try:
            self.assertIn(
                INSTANCE_NAME + ':',
                [i.strip() for i in self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME), timeout=500)]
            )
        except AssertionError:
            raise

    def tearDown(self):
        '''
        Clean up after tests
        '''
        query = self.run_cloud('--query')
        ret = '        {0}:'.format(INSTANCE_NAME)

        # if test instance is still present, delete it
        if ret in query:
            self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME), timeout=500)

@@ -484,25 +484,21 @@
  import 
-libcloud 
+shade
  # pylin
@@ -530,24 +530,21 @@
     HAS_
-LIBCLOUD
+SHADE
  = True%0A
@@ -571,24 +571,21 @@
     HAS_
-LIBCLOUD
+SHADE
  = False
@@ -775,16 +775,13 @@
 HAS_
-LIBCLOUD
+SHADE
  is 
@@ -792,46 +792,41 @@
 e, '
-salt-cloud requires %3E= libcloud 0.13.2
+openstack driver requires %60shade%60
 ')%0Ac
@@ -1874,20 +1874,27 @@
         
-user
+region_name
  = confi
@@ -1924,16 +1924,27 @@
 AME%5D
-%5B'user'%5D
+.get('region_name')
 %0A   
@@ -1948,22 +1948,20 @@
         
-tenant
+auth
  = confi
@@ -1991,18 +1991,20 @@
 AME%5D
-%5B'tenant'%5D
+.get('auth')
 %0A   
@@ -2008,19 +2008,21 @@
         
-api
+cloud
  = confi
@@ -2052,69 +2052,64 @@
 AME%5D
-%5B'apikey'%5D%0A        if api == '' or tenant == '' or user == ''
+.get('cloud')%0A        if region_name and (auth or cloud)
 :%0A  
@@ -2156,36 +2156,39 @@
  'A 
-user, tenant, and an api key
+region_name and (auth or cloud)
  mus

fix up rackspace/openstack cloud test