def __createApiClient(self):
        try:
            '''
            Step1 : Create a CS Connection Object
            '''
            mgmt_details = self.__mgmtDetails
            self.__csConnection = CSConnection(mgmt_details,
                                               self.__asyncTimeOut,
                                               self.__logger)

            '''
            Step2 : Create API Client with earlier created connection object
            '''
            self.__apiClient = CloudStackAPIClient(self.__csConnection)

            '''
            Step3:  If API Key is not provided as part of Management Details,
                    then verify and register
            '''
            if mgmt_details.apiKey is None:
                list_user = listUsers.listUsersCmd()
                list_user.account = "admin"
                list_user_res = self.__apiClient.listUsers(list_user)
                if list_user_res is None or\
                        (validateList(list_user_res)[0] != PASS):
                    self.__logger.error("__createApiClient: API "
                                        "Client Creation Failed")
                    return FAILED
                user_id = list_user_res[0].id
                api_key = list_user_res[0].apikey
                security_key = list_user_res[0].secretkey
                if api_key is None:
                    ret = self.__getKeys(user_id)
                    if ret != FAILED:
                        mgmt_details.apiKey = ret[0]
                        mgmt_details.securityKey = ret[1]
                    else:
                        self.__logger.error("__createApiClient: API Client "
                                            "Creation Failed while "
                                            "Registering User")
                        return FAILED
                else:
                    mgmt_details.port = 8080
                    mgmt_details.apiKey = api_key
                    mgmt_details.securityKey = security_key
                '''
                Now Create the Connection objects and Api Client using
                new details
                '''
                self.__csConnection = CSConnection(mgmt_details,
                                                   self.__asyncTimeOut,
                                                   self.__logger)
                self.__apiClient = CloudStackAPIClient(self.__csConnection)
            return SUCCESS
        except Exception as e:
            self.__logger.exception(" Exception Occurred Under "
                                    "__createApiClient: %s" %
                                    GetDetailExceptionInfo(e))
            return FAILED
    def __createApiClient(self):
        try:
            '''
            Step1 : Create a CS Connection Object
            '''
            self.__csConnection = CSConnection(self.__mgmtDetails,
                                               self.__asyncTimeOut,
                                               self.__logger)

            '''
            Step2 : Create API Client with earlier created connection object
            '''
            self.__apiClient = CloudStackAPIClient(self.__csConnection)

            '''
            Step3:  If API Key is not provided as part of Management Details,
                    then verify and register
            '''
            if self.__mgmtDetails.apiKey is None:
                list_user = listUsers.listUsersCmd()
                list_user.account = "admin"
                list_user_res = self.__apiClient.listUsers(list_user)
                if list_user_res is None or\
                        (validateList(list_user_res)[0] != PASS):
                    self.__logger.error("__createApiClient: API "
                                        "Client Creation Failed")
                    return FAILED
                user_id = list_user_res[0].id
                api_key = list_user_res[0].apikey
                security_key = list_user_res[0].secretkey
                if api_key is None:
                    ret = self.__getKeys(user_id)
                    if ret != FAILED:
                        self.__mgmtDetails.port = 8080
                        self.__mgmtDetails.apiKey = ret[0]
                        self.__mgmtDetails.securityKey = ret[1]
                    else:
                        self.__logger.error("__createApiClient: API Client "
                                            "Creation Failed while "
                                            "Registering User")
                        return FAILED
                else:
                    self.__mgmtDetails.port = 8080
                    self.__mgmtDetails.apiKey = api_key
                    self.__mgmtDetails.securityKey = security_key
                '''
                Now Create the Connection objects and Api Client using
                new details
                '''
                self.__csConnection = CSConnection(self.__mgmtDetails,
                                                   self.__asyncTimeOut,
                                                   self.__logger)
                self.__apiClient = CloudStackAPIClient(self.__csConnection)
            return SUCCESS
        except Exception as e:
            self.__logger.exception(" Exception Occurred Under "
                                    "__createApiClient: %s" %
                                    GetDetailExceptionInfo(e))
            return FAILED
 def __init__(self, l, options):
     self.options = options
     self.log = l
     utils = CSUtils()
     conn = utils.getConnection()
     self.apiclient = CloudStackAPIClient(conn)
     self.get_zone()
     self.vms = []
     self.networks = []
     self.vpcs = []
     self.isolated = []
    def __createUserApiClient(self, UserName, DomainName, acctType=0):
        '''
        @Name : ___createUserApiClient
        @Desc : Creates a User API Client with given
                UserName\DomainName Parameters
        @Input: UserName: Username to be created in cloudstack
                DomainName: Domain under which the above account be created
                accType: Type of Account EX: Root,Non Root etc
        @Output: Return the API client for the user
        '''
        try:
            if not self.isAdminContext():
                return self.__apiClient
            mgmt_details = self.__mgmtDetails
            listDomain = listDomains.listDomainsCmd()
            listDomain.listall = True
            listDomain.name = DomainName
            try:
                domains = self.__apiClient.listDomains(listDomain)
                domId = domains[0].id
            except:
                cdomain = createDomain.createDomainCmd()
                cdomain.name = DomainName
                domain = self.__apiClient.createDomain(cdomain)
                domId = domain.id

            cmd = listAccounts.listAccountsCmd()
            cmd.name = UserName
            cmd.domainid = domId
            cmd.listall = True
            try:
                accounts = self.__apiClient.listAccounts(cmd)
                acctId = accounts[0].id
            except:
                createAcctCmd = createAccount.createAccountCmd()
                createAcctCmd.accounttype = acctType
                createAcctCmd.domainid = domId
                createAcctCmd.email = "test-" + random_gen()\
                    + "@cloudstack.org"
                createAcctCmd.firstname = UserName
                createAcctCmd.lastname = UserName
                createAcctCmd.password = '******'
                createAcctCmd.username = UserName
                acct = self.__apiClient.createAccount(createAcctCmd)
                acctId = acct.id

            listuser = listUsers.listUsersCmd()
            listuser.username = UserName
            listuser.domainid = domId
            listuser.listall = True

            listuserRes = self.__apiClient.listUsers(listuser)
            userId = listuserRes[0].id

            getuser_keys = getUserKeys.getUserKeysCmd()
            getuser_keys.id = listuserRes[0].id
            getuser_keys_res = self.__apiClient.getUserKeys(getuser_keys)
            if getuser_keys_res is None or\
                (validateList(getuser_keys_res) != PASS):
                self.__logger.error("__createApiClient: API "
                                "Client Creation Failed")

            apiKey = getuser_keys_res.apikey
            securityKey = getuser_keys_res.secretkey

            if apiKey is None:
                ret = self.__getKeys(userId)
                if ret != FAILED:
                    mgmt_details.apiKey = ret[0]
                    mgmt_details.securityKey = ret[1]
                else:
                    self.__logger.error("__createUserApiClient: "
                                        "User API Client Creation."
                                        " While Registering User Failed")
                    return FAILED
            else:
                mgmt_details.port = 8080
                mgmt_details.apiKey = apiKey
                mgmt_details.securityKey = securityKey

            newUserConnection =\
                CSConnection(mgmt_details,
                             self.__csConnection.asyncTimeout,
                             self.__csConnection.logger)
            self.__userApiClient = CloudStackAPIClient(newUserConnection)
            self.__userApiClient.connection = newUserConnection
            self.__userApiClient.hypervisor = self.__hypervisor
            return self.__userApiClient
        except Exception as e:
            self.__logger.exception("Exception Occurred "
                                    "Under getUserApiClient : %s" %
                                    GetDetailExceptionInfo(e))
            return FAILED
class CSTestClient(object):

    '''
    @Desc  : CloudStackTestClient is encapsulated entity for creating and
         getting various clients viz., apiclient,
         user api client, dbconnection, test Data parsed
         information etc
    @Input :
         mgmt_details : Management Server Details
         dbsvr_details: Database Server details of Management \
                       Server. Retrieved from configuration file.
         async_timeout : Timeout for Async queries
         default_worker_threads : Number of worker threads
         logger : provides logging facilities for this library
         zone : The zone on which test suites using this test client will run
    '''

    def __init__(self, mgmt_details,
                 dbsvr_details,
                 async_timeout=3600,
                 logger=None,
                 test_data_filepath=None,
                 zone=None,
                 hypervisor_type=None):
        self.__mgmtDetails = mgmt_details
        self.__dbSvrDetails = dbsvr_details
        self.__csConnection = None
        self.__dbConnection = None
        self.__testClient = None
        self.__asyncTimeOut = async_timeout
        self.__logger = logger
        self.__apiClient = None
        self.__userApiClient = None
        self.__asyncJobMgr = None
        self.__id = None
        self.__hypervisor = hypervisor_type
        self.__testDataFilePath = test_data_filepath
        self.__parsedTestDataConfig = None
        self.__zone = zone
        self.__setHypervisorInfo()

    @property
    def identifier(self):
        return self.__id

    @identifier.setter
    def identifier(self, id):
        self.__id = id

    def getParsedTestDataConfig(self):
        '''
        @Name : getParsedTestDataConfig
        @Desc : Provides the TestData Config needed for
                Tests are to Run
        @Output : Returns the Parsed Test Data Dictionary
        '''
        return copy.deepcopy(self.__parsedTestDataConfig)

    def getZoneForTests(self):
        '''
        @Name : getZoneForTests
        @Desc : Provides the Zone against which Tests are to run
                If zone name provided to marvin plugin is none
                it will get it from Test Data Config File
                Even, if  it is not available, return None
        @Output : Returns the Zone Name
        '''
        return self.__zone

    def getHypervisorInfo(self):
        '''
        @Name : getHypervisorInfo
        @Desc : Provides the hypervisor Information to test users
        @Output : Return Hypervisor Information
        '''
        return self.__hypervisor

    def __setHypervisorInfo(self):
        '''
        @Name : __setHypervisorInfo
        @Desc:  Set the HyperVisor details;
                default to XenServer
        '''
        try:
            if not self.__hypervisor:
                self.__hypervisor = XEN_SERVER
            return SUCCESS
        except Exception as e:
            print("\n Exception Occurred Under __setHypervisorInfo " \
                  "%s" % GetDetailExceptionInfo(e))
            return FAILED

    def __createApiClient(self):
        try:
            '''
            Step1 : Create a CS Connection Object
            '''
            mgmt_details = self.__mgmtDetails
            self.__csConnection = CSConnection(mgmt_details,
                                               self.__asyncTimeOut,
                                               self.__logger)

            '''
            Step2 : Create API Client with earlier created connection object
            '''
            self.__apiClient = CloudStackAPIClient(self.__csConnection)

            '''
            Step3:  If API Key is not provided as part of Management Details,
                    then verify and register
            '''
            if mgmt_details.apiKey is None:
                list_user = listUsers.listUsersCmd()
                list_user.account = "admin"
                list_user_res = self.__apiClient.listUsers(list_user)
                if list_user_res is None or\
                        (validateList(list_user_res)[0] != PASS):
                    self.__logger.error("__createApiClient: API "
                                        "Client Creation Failed")
                    return FAILED

                getuser_keys = getUserKeys.getUserKeysCmd()
                getuser_keys.id = list_user_res[0].id
                getuser_keys_res = self.__apiClient.getUserKeys(getuser_keys)
                if getuser_keys_res is None :
                    self.__logger.error("__createApiClient: API "
                                        "Client Creation Failed")
                    return FAILED

                api_key = getuser_keys_res.apikey
                security_key = getuser_keys_res.secretkey

                user_id = list_user_res[0].id
                if api_key is None:
                    ret = self.__getKeys(user_id)
                    if ret != FAILED:
                        mgmt_details.apiKey = ret[0]
                        mgmt_details.securityKey = ret[1]
                    else:
                        self.__logger.error("__createApiClient: API Client "
                                            "Creation Failed while "
                                            "Registering User")
                        return FAILED
                else:
                    mgmt_details.port = 8080
                    mgmt_details.apiKey = api_key
                    mgmt_details.securityKey = security_key
                '''
                Now Create the Connection objects and Api Client using
                new details
                '''
                self.__csConnection = CSConnection(mgmt_details,
                                                   self.__asyncTimeOut,
                                                   self.__logger)
                self.__apiClient = CloudStackAPIClient(self.__csConnection)
            return SUCCESS
        except Exception as e:
            self.__logger.exception(" Exception Occurred Under "
                                    "__createApiClient: %s" %
                                    GetDetailExceptionInfo(e))
            return FAILED

    def __createDbConnection(self):
        '''
        @Name : ___createDbConnection
        @Desc : Creates the CloudStack DB Connection
        '''
        host = "localhost" if self.__dbSvrDetails.dbSvr is None \
            else self.__dbSvrDetails.dbSvr
        port = 3306 if self.__dbSvrDetails.port is None \
            else self.__dbSvrDetails.port
        user = "******" if self.__dbSvrDetails.user is None \
            else self.__dbSvrDetails.user
        passwd = 'cloud' if self.__dbSvrDetails.passwd is None \
            else self.__dbSvrDetails.passwd
        db = 'cloud' if self.__dbSvrDetails.db is None \
            else self.__dbSvrDetails.db
        self.__dbConnection = DbConnection(host, port, user, passwd, db)

    def __getKeys(self, userid):
        '''
        @Name : ___getKeys
        @Desc : Retrieves the API and Secret Key for the provided Userid
        @Input: userid: Userid to register
        @Output: FAILED or tuple with apikey and secretkey
        '''
        try:
            register_user = registerUserKeys.registerUserKeysCmd()
            register_user.id = userid
            register_user_res = \
                self.__apiClient.registerUserKeys(register_user)
            if not register_user_res:
                return FAILED

            getuser_keys = getUserKeys.getUserKeysCmd()
            getuser_keys.id = userid
            getuser_keys_res = self.__apiClient.getUserKeys(getuser_keys)
            if getuser_keys_res is None :
                self.__logger.error("__createApiClient: API "
                                "Client Creation Failed")
                return FAILED

            api_key = getuser_keys_res.apikey
            security_key = getuser_keys_res.secretkey
            return (api_key, security_key)
        except Exception as e:
            self.__logger.exception("Exception Occurred Under __geKeys : "
                                    "%s" % GetDetailExceptionInfo(e))
            return FAILED

    def createTestClient(self):
        '''
        @Name : createTestClient
        @Desc : Creates the Test Client.
                The test Client is used by test suites
                Here we create ParsedTestData Config.
                Creates a DB Connection.
                Creates an API Client
        @Output : FAILED In case of an issue\Failure
                  SUCCESS in case of Success of this function
        '''
        try:
            '''
            1. Create Config Object
               Provides the Configuration Object to test suites through
               getConfigParser. The purpose of this config object is to
               parse the default config and provide dictionary of the
               config so users can use that configuration.
               Users can later call getConfig on this object and it will
               return the default parsed config dictionary from default
               configuration file. They can overwrite it with
               providing their own configuration file as well.
            '''
            '''
            1. Check Config,Zone,Hypervisor Information
            '''
            self.__configObj = ConfigManager(self.__testDataFilePath)

            if not self.__configObj or not self.__hypervisor:
                self.__logger.error("createTestClient : "
                                    "Either Hypervisor is None or "
                                    "Not able to create "
                                    "ConfigManager Object")
                return FAILED

            self.__parsedTestDataConfig = self.__configObj.getConfig()
            self.__logger.debug("Parsing Test data successful")

            '''
            2. Create DB Connection
            '''
            self.__createDbConnection()
            '''
            3. Creates API Client
            '''
            ret = self.__createApiClient()
            if ret == FAILED:
                self.__logger.\
                    error("==== Test Client Creation Failed ====")
            else:
                self.__logger.\
                    debug("==== Test Client Creation Successful ====")
            return ret
        except Exception as e:
            self.__logger.exception("Exception Occurred "
                                    "Under createTestClient "
                                    ": %s" % GetDetailExceptionInfo(e))
            return FAILED

    def isAdminContext(self):
        """
        @Name : isAdminContext
        @Desc:A user is a regular user if he fails to listDomains;
        if he is a domain-admin, he can list only domains that are non-ROOT;
        if he is an admin, he can list the ROOT domain successfully
        """
        try:
            listdom = listDomains.listDomainsCmd()
            listdom.name = 'ROOT'
            listdomres = self.__apiClient.listDomains(listdom)
            if listdomres != FAILED:
                rootdom = listdomres[0].name
                if rootdom == 'ROOT':
                    return ADMIN
                else:
                    return DOMAIN_ADMIN
            return USER
        except:
            return USER

    def __createUserApiClient(self, UserName, DomainName, acctType=0):
        '''
        @Name : ___createUserApiClient
        @Desc : Creates a User API Client with given
                UserName\DomainName Parameters
        @Input: UserName: Username to be created in cloudstack
                DomainName: Domain under which the above account be created
                accType: Type of Account EX: Root,Non Root etc
        @Output: Return the API client for the user
        '''
        try:
            if not self.isAdminContext():
                return self.__apiClient
            mgmt_details = self.__mgmtDetails
            listDomain = listDomains.listDomainsCmd()
            listDomain.listall = True
            listDomain.name = DomainName
            try:
                domains = self.__apiClient.listDomains(listDomain)
                domId = domains[0].id
            except:
                cdomain = createDomain.createDomainCmd()
                cdomain.name = DomainName
                domain = self.__apiClient.createDomain(cdomain)
                domId = domain.id

            cmd = listAccounts.listAccountsCmd()
            cmd.name = UserName
            cmd.domainid = domId
            cmd.listall = True
            try:
                accounts = self.__apiClient.listAccounts(cmd)
                acctId = accounts[0].id
            except:
                createAcctCmd = createAccount.createAccountCmd()
                createAcctCmd.accounttype = acctType
                createAcctCmd.domainid = domId
                createAcctCmd.email = "test-" + random_gen()\
                    + "@cloudstack.org"
                createAcctCmd.firstname = UserName
                createAcctCmd.lastname = UserName
                createAcctCmd.password = '******'
                createAcctCmd.username = UserName
                acct = self.__apiClient.createAccount(createAcctCmd)
                acctId = acct.id

            listuser = listUsers.listUsersCmd()
            listuser.username = UserName
            listuser.domainid = domId
            listuser.listall = True

            listuserRes = self.__apiClient.listUsers(listuser)
            userId = listuserRes[0].id

            getuser_keys = getUserKeys.getUserKeysCmd()
            getuser_keys.id = listuserRes[0].id
            getuser_keys_res = self.__apiClient.getUserKeys(getuser_keys)
            if getuser_keys_res is None or\
                (validateList(getuser_keys_res) != PASS):
                self.__logger.error("__createApiClient: API "
                                "Client Creation Failed")

            apiKey = getuser_keys_res.apikey
            securityKey = getuser_keys_res.secretkey

            if apiKey is None:
                ret = self.__getKeys(userId)
                if ret != FAILED:
                    mgmt_details.apiKey = ret[0]
                    mgmt_details.securityKey = ret[1]
                else:
                    self.__logger.error("__createUserApiClient: "
                                        "User API Client Creation."
                                        " While Registering User Failed")
                    return FAILED
            else:
                mgmt_details.port = 8080
                mgmt_details.apiKey = apiKey
                mgmt_details.securityKey = securityKey

            newUserConnection =\
                CSConnection(mgmt_details,
                             self.__csConnection.asyncTimeout,
                             self.__csConnection.logger)
            self.__userApiClient = CloudStackAPIClient(newUserConnection)
            self.__userApiClient.connection = newUserConnection
            self.__userApiClient.hypervisor = self.__hypervisor
            return self.__userApiClient
        except Exception as e:
            self.__logger.exception("Exception Occurred "
                                    "Under getUserApiClient : %s" %
                                    GetDetailExceptionInfo(e))
            return FAILED

    def close(self):
        if self.__csConnection is not None:
            self.__csConnection.close()

    def getDbConnection(self):
        '''
        @Name : getDbConnection
        @Desc : Retrieves the DB Connection Handle
        '''
        return self.__dbConnection

    def getConfigParser(self):
        '''
        @Name : getConfigParser
        @Desc : Provides the ConfigManager Interface to TestClients
        '''
        return self.__configObj

    def getApiClient(self):
        if self.__apiClient:
            self.__apiClient.id = self.identifier
            return self.__apiClient
        return None

    def getUserApiClient(self, UserName=None, DomainName=None, type=0):
        """
        @Name : getUserApiClient
        @Desc : Provides the User API Client to test Users
        0 - user ; 1 - admin;2 - domain admin
        @OutPut : FAILED In case of an issue
                  else User API Client
        """
        if UserName is None or DomainName is None:
            return FAILED
        return self.__createUserApiClient(UserName, DomainName, type)

    def submitCmdsAndWait(self, cmds, workers=1, apiclient=None):
        '''
        @Desc : FixME, httplib has issue if more than one thread submitted
        '''
        if not apiclient:
            apiclient = self.__apiClient
        if self.__asyncJobMgr is None:
            self.__asyncJobMgr = asyncJobMgr(apiclient,
                                             self.__dbConnection)
        return self.__asyncJobMgr.submitCmdsAndWait(cmds, workers)

    def submitJob(self, job, ntimes=1, nums_threads=10, interval=1):
        '''
        @Desc : submit one job and execute the same job
                ntimes, with nums_threads of threads
        '''
        if self.__asyncJobMgr is None:
            self.__asyncJobMgr = asyncJobMgr(self.__apiClient,
                                             self.__dbConnection)
        self.__asyncJobMgr.submitJobExecuteNtimes(job, ntimes,
                                                  nums_threads,
                                                  interval)

    def submitJobs(self, jobs, nums_threads=10, interval=1):
        '''
        @Desc :submit n jobs, execute them with nums_threads
               of threads
        '''
        if self.__asyncJobMgr is None:
            self.__asyncJobMgr = asyncJobMgr(self.__apiClient,
                                             self.__dbConnection)
        self.__asyncJobMgr.submitJobs(jobs, nums_threads, interval)
from marvin.configGenerator import ConfigManager
from marvin.lib.utils import (random_gen, validateList)


from CSUtils import *

config = {
    'management_server' : 'localhost',
    'hypervisor_type' : 'XenServer',
    'primarystorage' : 'mccdxpl2_primary'
}
  

utils = CSUtils()  
conn = utils.getConnection()
apiclient = CloudStackAPIClient(conn)

configuration = {
   'cpu.overprovisioning.factor'     : 10,
   'mem.overprovisioning.factor'     : 10,
   'storage.overprovisioning.factor' : 4,
   'expunge.delay'                   : 120,
   'expunge.interval'                : 60,
   'network.gc.interval'             : 60,
   'network.gc.wait'                 : 120
   }

listconfig = listConfigurations.listConfigurationsCmd()
try:
   resp = apiclient.listConfigurations(listconfig)
   for item in resp:
class CSTestClient(object):

    '''
    @Desc  : CloudStackTestClient is encapsulated entity for creating and
         getting various clients viz., apiclient,
         user api client, dbconnection, test Data parsed
         information etc
    @Input :
         mgmt_details : Management Server Details
         dbsvr_details: Database Server details of Management \
                       Server. Retrieved from configuration file.
         async_timeout : Timeout for Async queries
         default_worker_threads : Number of worker threads
         logger : provides logging facilities for this library
         zone : The zone on which test suites using this test client will run
    '''

    def __init__(self, mgmt_details,
                 dbsvr_details,
                 async_timeout=3600,
                 logger=None,
                 test_data_filepath=None,
                 zone=None,
                 hypervisor_type=None):
        self.__mgmtDetails = mgmt_details
        self.__dbSvrDetails = dbsvr_details
        self.__csConnection = None
        self.__dbConnection = None
        self.__testClient = None
        self.__asyncTimeOut = async_timeout
        self.__logger = logger
        self.__apiClient = None
        self.__userApiClient = None
        self.__asyncJobMgr = None
        self.__id = None
        self.__hypervisor = hypervisor_type
        self.__testDataFilePath = test_data_filepath
        self.__parsedTestDataConfig = None
        self.__zone = zone
        self.__setHypervisorInfo()

    @property
    def identifier(self):
        return self.__id

    @identifier.setter
    def identifier(self, id):
        self.__id = id

    def getParsedTestDataConfig(self):
        '''
        @Name : getParsedTestDataConfig
        @Desc : Provides the TestData Config needed for
                Tests are to Run
        @Output : Returns the Parsed Test Data Dictionary
        '''
        return self.__parsedTestDataConfig

    def getZoneForTests(self):
        '''
        @Name : getZoneForTests
        @Desc : Provides the Zone against which Tests are to run
                If zone name provided to marvin plugin is none
                it will get it from Test Data Config File
                Even, if  it is not available, return None
        @Output : Returns the Zone Name
        '''
        return self.__zone

    def getHypervisorInfo(self):
        '''
        @Name : getHypervisorInfo
        @Desc : Provides the hypervisor Information to test users
        @Output : Return Hypervisor Information
        '''
        return self.__hypervisor

    def __setHypervisorInfo(self):
        '''
        @Name : __setHypervisorInfo
        @Desc:  Set the HyperVisor details;
                default to XenServer
        '''
        try:
            if not self.__hypervisor:
                self.__hypervisor = XEN_SERVER
            return SUCCESS
        except Exception as e:
            print "\n Exception Occurred Under __setHypervisorInfo " \
                  "%s" % GetDetailExceptionInfo(e)
            return FAILED

    def __createApiClient(self):
        try:
            '''
            Step1 : Create a CS Connection Object
            '''
            self.__csConnection = CSConnection(self.__mgmtDetails,
                                               self.__asyncTimeOut,
                                               self.__logger)

            '''
            Step2 : Create API Client with earlier created connection object
            '''
            self.__apiClient = CloudStackAPIClient(self.__csConnection)

            '''
            Step3:  If API Key is not provided as part of Management Details,
                    then verify and register
            '''
            if self.__mgmtDetails.apiKey is None:
                list_user = listUsers.listUsersCmd()
                list_user.account = "admin"
                list_user_res = self.__apiClient.listUsers(list_user)
                if list_user_res is None or\
                        (validateList(list_user_res)[0] != PASS):
                    self.__logger.error("__createApiClient: API "
                                        "Client Creation Failed")
                    return FAILED
                user_id = list_user_res[0].id
                api_key = list_user_res[0].apikey
                security_key = list_user_res[0].secretkey
                if api_key is None:
                    ret = self.__getKeys(user_id)
                    if ret != FAILED:
                        self.__mgmtDetails.port = 8080
                        self.__mgmtDetails.apiKey = ret[0]
                        self.__mgmtDetails.securityKey = ret[1]
                    else:
                        self.__logger.error("__createApiClient: API Client "
                                            "Creation Failed while "
                                            "Registering User")
                        return FAILED
                else:
                    self.__mgmtDetails.port = 8080
                    self.__mgmtDetails.apiKey = api_key
                    self.__mgmtDetails.securityKey = security_key
                '''
                Now Create the Connection objects and Api Client using
                new details
                '''
                self.__csConnection = CSConnection(self.__mgmtDetails,
                                                   self.__asyncTimeOut,
                                                   self.__logger)
                self.__apiClient = CloudStackAPIClient(self.__csConnection)
            return SUCCESS
        except Exception as e:
            self.__logger.exception(" Exception Occurred Under "
                                    "__createApiClient: %s" %
                                    GetDetailExceptionInfo(e))
            return FAILED

    def __createDbConnection(self):
        '''
        @Name : ___createDbConnection
        @Desc : Creates the CloudStack DB Connection
        '''
        host = "localhost" if self.__dbSvrDetails.dbSvr is None \
            else self.__dbSvrDetails.dbSvr
        port = 3306 if self.__dbSvrDetails.port is None \
            else self.__dbSvrDetails.port
        user = "******" if self.__dbSvrDetails.user is None \
            else self.__dbSvrDetails.user
        passwd = 'cloud' if self.__dbSvrDetails.passd is None \
            else self.__dbSvrDetails.passd
        db = 'cloud' if self.__dbSvrDetails.db is None \
            else self.__dbSvrDetails.db
        self.__dbConnection = DbConnection(host, port, user, passwd, db)

    def __getKeys(self, userid):
        '''
        @Name : ___getKeys
        @Desc : Retrieves the API and Secret Key for the provided Userid
        @Input: userid: Userid to register
        @Output: FAILED or tuple with apikey and secretkey
        '''
        try:
            register_user = registerUserKeys.registerUserKeysCmd()
            register_user.id = userid
            register_user_res = \
                self.__apiClient.registerUserKeys(register_user)
            if not register_user_res:
                return FAILED
            return (register_user_res.apikey, register_user_res.secretkey)
        except Exception as e:
            self.__logger.exception("Exception Occurred Under __geKeys : "
                                    "%s" % GetDetailExceptionInfo(e))
            return FAILED

    def createTestClient(self):
        '''
        @Name : createTestClient
        @Desc : Creates the Test Client.
                The test Client is used by test suites
                Here we create ParsedTestData Config.
                Creates a DB Connection.
                Creates an API Client
        @Output : FAILED In case of an issue\Failure
                  SUCCESS in case of Success of this function
        '''
        try:
            '''
            1. Create Config Object
               Provides the Configuration Object to test suites through
               getConfigParser. The purpose of this config object is to
               parse the default config and provide dictionary of the
               config so users can use that configuration.
               Users can later call getConfig on this object and it will
               return the default parsed config dictionary from default
               configuration file. They can overwrite it with
               providing their own configuration file as well.
            '''
            '''
            1. Check Config,Zone,Hypervisor Information
            '''
            self.__configObj = ConfigManager(self.__testDataFilePath)

            if not self.__configObj or not self.__hypervisor:
                self.__logger.error("createTestClient : "
                                    "Either Hypervisor is None or "
                                    "Not able to create "
                                    "ConfigManager Object")
                return FAILED

            self.__parsedTestDataConfig = self.__configObj.getConfig()
            self.__logger.debug("Parsing Test data successful")

            '''
            2. Create DB Connection
            '''
            self.__createDbConnection()
            '''
            3. Creates API Client
            '''
            ret = self.__createApiClient()
            if ret == FAILED:
                self.__logger.\
                    error("==== Test Client Creation Failed ====")
            else:
                self.__logger.\
                    debug("==== Test Client Creation Successful ====")
            return ret
        except Exception as e:
            self.__logger.exception("Exception Occurred "
                                    "Under createTestClient "
                                    ": %s" % GetDetailExceptionInfo(e))
            return FAILED

    def isAdminContext(self):
        """
        @Name : isAdminContext
        @Desc:A user is a regular user if he fails to listDomains;
        if he is a domain-admin, he can list only domains that are non-ROOT;
        if he is an admin, he can list the ROOT domain successfully
        """
        try:
            listdom = listDomains.listDomainsCmd()
            listdom.name = 'ROOT'
            listdomres = self.__apiClient.listDomains(listdom)
            if listdomres != FAILED:
                rootdom = listdomres[0].name
                if rootdom == 'ROOT':
                    return ADMIN
                else:
                    return DOMAIN_ADMIN
            return USER
        except:
            return USER

    def __createUserApiClient(self, UserName, DomainName, acctType=0):
        '''
        @Name : ___createUserApiClient
        @Desc : Creates a User API Client with given
                UserName\DomainName Parameters
        @Input: UserName: Username to be created in cloudstack
                DomainName: Domain under which the above account be created
                accType: Type of Account EX: Root,Non Root etc
        @Output: Return the API client for the user
        '''
        try:
            if not self.isAdminContext():
                return self.__apiClient

            listDomain = listDomains.listDomainsCmd()
            listDomain.listall = True
            listDomain.name = DomainName
            try:
                domains = self.__apiClient.listDomains(listDomain)
                domId = domains[0].id
            except:
                cdomain = createDomain.createDomainCmd()
                cdomain.name = DomainName
                domain = self.__apiClient.createDomain(cdomain)
                domId = domain.id

            cmd = listAccounts.listAccountsCmd()
            cmd.name = UserName
            cmd.domainid = domId
            try:
                accounts = self.__apiClient.listAccounts(cmd)
                acctId = accounts[0].id
            except:
                createAcctCmd = createAccount.createAccountCmd()
                createAcctCmd.accounttype = acctType
                createAcctCmd.domainid = domId
                createAcctCmd.email = "test-" + random_gen()\
                    + "@cloudstack.org"
                createAcctCmd.firstname = UserName
                createAcctCmd.lastname = UserName
                createAcctCmd.password = '******'
                createAcctCmd.username = UserName
                acct = self.__apiClient.createAccount(createAcctCmd)
                acctId = acct.id

            listuser = listUsers.listUsersCmd()
            listuser.username = UserName

            listuserRes = self.__apiClient.listUsers(listuser)
            userId = listuserRes[0].id
            apiKey = listuserRes[0].apikey
            securityKey = listuserRes[0].secretkey

            if apiKey is None:
                ret = self.__getKeys(userId)
                if ret != FAILED:
                    mgtDetails = self.__mgmtDetails
                    mgtDetails.apiKey = ret[0]
                    mgtDetails.securityKey = ret[1]
                else:
                    self.__logger.error("__createUserApiClient: "
                                        "User API Client Creation."
                                        " While Registering User Failed")
                    return FAILED
            else:
                mgtDetails = self.__mgmtDetails
                mgtDetails.apiKey = apiKey
                mgtDetails.securityKey = securityKey

            newUserConnection =\
                CSConnection(mgtDetails,
                             self.__csConnection.asyncTimeout,
                             self.__csConnection.logger)
            self.__userApiClient = CloudStackAPIClient(newUserConnection)
            self.__userApiClient.connection = newUserConnection
            self.__userApiClient.hypervisor = self.__hypervisor
            return self.__userApiClient
        except Exception as e:
            self.__logger.exception("Exception Occurred "
                                    "Under getUserApiClient : %s" %
                                    GetDetailExceptionInfo(e))
            return FAILED

    def close(self):
        if self.__csConnection is not None:
            self.__csConnection.close()

    def getDbConnection(self):
        '''
        @Name : getDbConnection
        @Desc : Retrieves the DB Connection Handle
        '''
        return self.__dbConnection

    def getConfigParser(self):
        '''
        @Name : getConfigParser
        @Desc : Provides the ConfigManager Interface to TestClients
        '''
        return self.__configObj

    def getApiClient(self):
        if self.__apiClient:
            self.__apiClient.id = self.identifier
            return self.__apiClient
        return None

    def getUserApiClient(self, UserName=None, DomainName=None, type=0):
        """
        @Name : getUserApiClient
        @Desc : Provides the User API Client to test Users
        0 - user ; 1 - admin;2 - domain admin
        @OutPut : FAILED In case of an issue
                  else User API Client
        """
        if UserName is None or DomainName is None:
            return FAILED
        return self.__createUserApiClient(UserName, DomainName, type)

    def submitCmdsAndWait(self, cmds, workers=1):
        '''
        @Desc : FixME, httplib has issue if more than one thread submitted
        '''
        if self.__asyncJobMgr is None:
            self.__asyncJobMgr = asyncJobMgr(self.__apiClient,
                                             self.__dbConnection)
        return self.__asyncJobMgr.submitCmdsAndWait(cmds, workers)

    def submitJob(self, job, ntimes=1, nums_threads=10, interval=1):
        '''
        @Desc : submit one job and execute the same job
                ntimes, with nums_threads of threads
        '''
        if self.__asyncJobMgr is None:
            self.__asyncJobMgr = asyncJobMgr(self.__apiClient,
                                             self.__dbConnection)
        self.__asyncJobMgr.submitJobExecuteNtimes(job, ntimes,
                                                  nums_threads,
                                                  interval)

    def submitJobs(self, jobs, nums_threads=10, interval=1):
        '''
        @Desc :submit n jobs, execute them with nums_threads
               of threads
        '''
        if self.__asyncJobMgr is None:
            self.__asyncJobMgr = asyncJobMgr(self.__apiClient,
                                             self.__dbConnection)
        self.__asyncJobMgr.submitJobs(jobs, nums_threads, interval)
from marvin.cloudstackException import GetDetailExceptionInfo
from marvin.cloudstackConnection import CSConnection
from marvin.configGenerator import ConfigManager
from marvin.lib.utils import (random_gen, validateList)

from CSUtils import *

config = {
    'management_server': 'localhost',
    'hypervisor_type': 'XenServer',
    'primarystorage': '192.168.56.5'
}

utils = CSUtils()
conn = utils.getConnection()
apiclient = CloudStackAPIClient(conn)

configuration = {
    'cpu.overprovisioning.factor': 10,
    'mem.overprovisioning.factor': 10,
    'storage.overprovisioning.factor': 4,
    'expunge.delay': 120,
    'expunge.interval': 60,
    'network.gc.interval': 60,
    'network.gc.wait': 120
}

listconfig = listConfigurations.listConfigurationsCmd()
try:
    resp = apiclient.listConfigurations(listconfig)
    for item in resp:
class RedVPCRouter():

    templateName = "tiny Linux"
    hypervisor = "XenServer"
    serviceOffering = "tinyOffering"

    def __init__(self, l, options):
        self.options = options
        self.log = l
        utils = CSUtils()
        conn = utils.getConnection()
        self.apiclient = CloudStackAPIClient(conn)
        self.get_zone()
        self.vms = []
        self.networks = []
        self.vpcs = []
        self.isolated = []

    def add_comm(self, comm):
        self.comm = comm

    def get_zone(self):
        self.log.ptest("Getting Zone info")
        cmd = listZones.listZonesCmd()
        ret = self.apiclient.listZones(cmd)

        if ret is None:
            self.log.extra("No zones")
            self.log.failure()

        for zone in ret:
            self.zone = zone
        self.log.success()

    def list_routers(self, rid='', is_vpc=IS_VPC):
        self.log.ptest("Finding routers")
        cmd = listRouters.listRoutersCmd()
        ret = self.apiclient.listRouters(cmd)
        rtrs = []
        if ret is None:
            self.log.extra("No routers found it has gone really wrong")
            self.log.failure()
        for router in ret:
            if is_vpc:
                if router.vpcid == rid:
                    rtrs.append(router)
            else:
                if router.networkdomain == rid:
                    rtrs.append(router)
        self.log.success()
        return rtrs

    def set_vpc_offerings(self, name):
        self.log.ptest("Getting VPC service offering")
        cmd = listVPCOfferings.listVPCOfferingsCmd()
        ret = self.apiclient.listVPCOfferings(cmd)
        self.vpc_off = None
        for off in ret:
            if off.displaytext == name:
                self.vpc_off = off
        if self.vpc_off is None:
            self.log.extra("No VPC offering found with name %s" % name)
            self.log.failure()
        self.log.success()

    def list_network_offerings(self, name):
        cmd = listNetworkOfferings.listNetworkOfferingsCmd()
        ret = self.apiclient.listNetworkOfferings(cmd)
        for off in ret:
            if off.name == name:
                return off
        return None

    def set_isolated_offerings(self, name):
        self.log.ptest("Getting Isolated service offering")
        cmd = listNetworkOfferings.listNetworkOfferingsCmd()
        ret = self.apiclient.listNetworkOfferings(cmd)
        self.isolated_off = None
        for off in ret:
            if off.name == name:
                self.isolated_off = off
        if self.isolated_off is None:
            self.log.extra("No Isolated offering found with name %s" % name)
            self.log.failure()
        self.log.success()

    def list_templates(self, name):
        cmd = listTemplates.listTemplatesCmd()
        cmd.templatefilter = "all"
        cmd.name = name
        cmd.listAll = True
        cmd.zone = self.zone.id
        return self.apiclient.listTemplates(cmd)

    def list_instances(self):
        cmd = listVirtualMachines.listVirtualMachinesCmd()
        return self.apiclient.listVirtualMachines(cmd)

    def instance_exists(self, name):
        vms = self.list_instances()
        if vms is None:
            return False
        for vm in vms:
            if vm.name == name:
                return vm.id
        return False

    def getServiceOffering(self, name):
        cmd = listServiceOfferings.listServiceOfferingsCmd()
        ret = self.apiclient.listServiceOfferings(cmd)
        for t in ret:
            if t.name.startswith(name):
                return t.id
        return False

    def list_vpc(self):
        cmd = listVPCs.listVPCsCmd()
        ret = self.apiclient.listVPCs(cmd)
        return ret

    def vpc_exists(self, name):
        vpcs = self.list_vpc()
        if vpcs is None:
            return False
        for vpc in vpcs:
            if vpc.name == name:
                return vpc
        return False

    def get_network(self, name):
        nets = self.list_networks()
        if nets is None:
            return None
        for net in nets:
            if net.name == name:
                return net
        return None

    def list_networks(self):
        cmd = listNetworks.listNetworksCmd()
        ret = self.apiclient.listNetworks(cmd)
        if ret is None:
            return None
        return ret

    def list_ips(self, net):
        self.ips = []
        cmd = listPublicIpAddresses.listPublicIpAddressesCmd()
        cmd.listAll = True
        ret = self.apiclient.listPublicIpAddresses(cmd)
        self.log.ptest("Getting public ips for network %s" % net)
        for ip in ret:
            if hasattr(ip, "associatednetworkname") and ip.associatednetworkname == net:
                self.ips.append(ip)
        if len(self.ips) > 0:
            self.log.success()
        else:
            self.log.extra("No public IP")
            self.log.failure()

    def get_acl(self, traffictype, action):
        cmd = listNetworkACLs.listNetworkACLsCmd()
        ret = self.apiclient.listNetworkACLs(cmd)
        if ret is None:
            self.log.extra("No Network ACLS found")
            self.log.failure()
        for acl in ret:
            if acl.traffictype == traffictype and acl.action == action:
                return acl

    def create_network(self, vpc, name, gateway, netmask):
        self.log.ptest("Creating network %s (%s)" % (name, gateway))
        vid = self.vpc_exists(vpc).id
        netname = "%s-%s" % (vpc, name)
        if not vid:
            self.log.extra("No vpc called %s" % vpc)
            self.log.failure(True)
        n = self.get_network(netname)
        if n is not None:
            self.log.extra("Network %s already exists" % netname)
            self.networks.append(n.id)
            self.log.success()
            return n
        cmd = createNetwork.createNetworkCmd()
        cmd.zoneid = self.zone.id
        cmd.name = netname
        cmd.displaytext = netname
        cmd.gateway = gateway
        cmd.netmask = netmask
        cmd.vpcid = vid
        cmd.aclid = self.get_acl("Egress", "Allow").aclid
        cmd.networkofferingid = self.list_network_offerings("DefaultIsolatedNetworkOfferingForVpcNetworks").id
        ret = self.apiclient.createNetwork(cmd)
        self.networks.append(ret.id)
        time.sleep(20)
        self.log.success()
        return ret

    def create_isolated_network(self, name, gw, mask):
        self.log.ptest("Creating Isolated Network %s (%s/%s)" % (name, gw, mask))
        cmd = listNetworks.listNetworksCmd()
        networks = self.apiclient.listNetworks(cmd)
        if networks is not None:
            for net in networks:
                if net.name == name:
                    self.networks.append(net.id)
                    self.log.extra("Exists")
                    self.log.success()
                    return
        cmd = createNetwork.createNetworkCmd()
        cmd.zoneid = self.zone.id
        cmd.name = name
        cmd.displaytext = name
        cmd.networkofferingid = self.isolated_off.id
        cmd.gateway = gw
        cmd.netmask = mask
        cmd.networkdomain = "%s.local" % name
        ret = self.apiclient.createNetwork(cmd)
        self.networks.append(ret.id)
        self.log.success()
        return ret.id

    def create_vpc(self, name, cidr):
        self.log.ptest("Creating Redundant VPC %s (%s)" % (name, cidr))
        vpc = self.vpc_exists(name)
        if vpc:
            self.log.extra("Already exists")
            self.log.success()
            self.vpcs.append(vpc.id)
            return vpc.id
        cmd = createVPC.createVPCCmd()
        cmd.name = name
        cmd.displaytext = name
        cmd.vpcofferingid = self.vpc_off.id
        cmd.zoneid = self.zone.id
        cmd.cidr = cidr
        # cmd.account = account
        # cmd.domainid = domainid
        # cmd.networkDomain = networkDomain
        ret = self.apiclient.createVPC(cmd)
        self.vpcs.append(ret.id)
        self.log.success()
        return ret.id

    def list_acls(self, name):
        cmd = listNetworkACLLists.listNetworkACLListsCmd()
        ret = self.apiclient.listNetworkACLLists(cmd)
        for acl in ret:
            if acl.name == name:
                return acl
        return None

    def create_acl_list(self, name, vid):
        self.log.ptest("Creating acl list %s" % name)
        acl = self.list_acls(name)
        if acl is None:
            cmd = createNetworkACLList.createNetworkACLListCmd()
            cmd.vpcid = vid
            cmd.name = name
            cmd.description = name
            acl = self.apiclient.createNetworkACLList(cmd)
            self.log.success()
        else:
            self.log.extra("exists")
            self.log.skipped()
        return acl

    def add_acl_rule(self, number, acl, port, direction):
        self.log.ptest("Adding %s rule for port %s" % (direction, port))
        cmd = listNetworkACLs.listNetworkACLsCmd()
        cmd.aclid = acl.id
        ret = self.apiclient.listNetworkACLs(cmd)
        if ret is not None:
            for rule in ret:
                if rule.number == number:
                    self.log.extra("Exists")
                    self.log.skipped()
                    return
        cmd = createNetworkACL.createNetworkACLCmd()
        cmd.aclid = acl.id
        cmd.startport = port
        cmd.endport = port
        cmd.cidrlist = ''
        cmd.number = number
        cmd.traffictype = direction
        cmd.protocol = 'tcp'
        self.apiclient.createNetworkACL(cmd)
        self.log.success()

    def replace_acl(self, acl, net):
        self.log.ptest("Setting acl on %s to %s" % (net.name, acl.name))
        cmd = replaceNetworkACLList.replaceNetworkACLListCmd()
        cmd.networkid = net.id
        cmd.aclid = acl.id
        self.apiclient.replaceNetworkACLList(cmd)
        self.log.success()

    def create_instance(self, name, network):
        self.log.ptest("Creating virtual machine %s in %s" % (name, network))
        v = self.instance_exists(name)
        if v:
            self.vms.append(v)
            self.log.extra("Already exists")
            self.log.success()
            return
        cmd = deployVirtualMachine.deployVirtualMachineCmd()
        cmd.name = name
        cmd.displayname = name
        cmd.zoneid = self.zone.id
        so = self.getServiceOffering(self.serviceOffering)
        if so is False:
            self.log.extra("Cannot find service Offering %s]" % self.serviceOffering)
            self.log.failure()
        cmd.serviceofferingid = so
        temp = self.list_templates(self.templateName)
        if temp is None:
            self.log.extra("Cannot find template %s" % self.templateName)
            self.log.failure()
        neto = self.get_network(network)
        if neto is None:
            self.log.extra("Cannot find network %s" % network)
            self.log.failure()
        cmd.networkids.append(neto.id)
        cmd.templateid = temp[0].id
        cmd.hypervisor = self.hypervisor
        ret = self.apiclient.deployVirtualMachine(cmd)
        self.vms.append(ret.id)
        time.sleep(20)
        self.log.success()

    def instances_in_network(self, name):
        instances = []
        self.log.ptest("Getting virtual machines in %s" % name)
        for inst in self.list_instances():
            for nic in inst.nic:
                if nic.networkname.startswith(name):
                    instances.append(inst)
        self.log.extra("[Found %s]" % len(instances))
        self.log.success()
        return instances

    def destroy_instance(self, id):
        if self.options.no_destroy:
            return
        self.log.ptest("Destroy virtual machine %s" % (id))
        cmd = destroyVirtualMachine.destroyVirtualMachineCmd()
        cmd.id = id
        cmd.expunge = True
        self.apiclient.destroyVirtualMachine(cmd)
        time.sleep(20)
        self.log.success()

    def destroy_network(self, id):
        if self.options.no_destroy:
            return
        self.log.ptest("Destroy network %s" % (id))
        cmd = deleteNetwork.deleteNetworkCmd()
        cmd.id = id
        cmd.expunge = True
        self.apiclient.deleteNetwork(cmd)
        time.sleep(20)
        self.log.success()

    def destroy_vpc(self, id):
        if self.options.no_destroy:
            return
        self.log.ptest("Detroying VPC %s" % id)
        cmd = deleteVPC.deleteVPCCmd()
        cmd.id = id
        ret = self.apiclient.deleteVPC(cmd)
        time.sleep(20)
        self.log.success()

    def destroy_all_vpcs(self):
        for id in self.vpcs:
            self.destroy_vpc(id)

    def destroy_all(self):
        for id in self.vms:
            self.destroy_instance(id)
        for id in self.networks:
            self.destroy_network(id)
        self.vms = []
        self.networks = []

    def list_firewall_rules(self, index, sport, eport):
        cmd = listFirewallRules.listFirewallRulesCmd()
        ret = self.apiclient.listFirewallRules(cmd)
        if ret is None:
            return ret
        for rule in ret:
            if rule.startport == str(sport) and rule.endport == str(eport) and self.ips[index].id == rule.ipaddressid:
                return rule
        return None

    def delete_firewall_rule(self, index, sport, eport):
        self.log.ptest("Deleting Firewall rule %s %s %s" % (self.ips[index].ipaddress, sport, eport))
        self.comm.update_firewall(self.ips[index].ipaddress, sport, eport, False)
        rule = self.list_firewall_rules(index, sport, eport)
        if rule is None:
            self.log.extra("Not There")
            self.log.success()
            return
        cmd = deleteFirewallRule.deleteFirewallRuleCmd()
        cmd.id = rule.id
        self.apiclient.deleteFirewallRule(cmd)
        self.log.success()

    def add_firewall_rule(self, index, sport, eport):
        self.log.ptest("Adding Firewall rule %s %s %s" % (self.ips[index].ipaddress, sport, eport))
        self.comm.update_firewall(self.ips[index].ipaddress, sport, eport, True)
        if self.list_firewall_rules(index, sport, eport) is not None:
            self.log.extra("Exists")
            self.log.success()
            return
        cmd = createFirewallRule.createFirewallRuleCmd()
        cmd.cidrlist = "0.0.0.0/0"
        cmd.protocol = "tcp"
        cmd.startport = sport
        cmd.endport = eport
        cmd.ipaddressid = self.ips[index].id
        self.apiclient.createFirewallRule(cmd)
        self.log.success()

    def enable_offering(self, off):
        self.log.ptest("Enabling network offering %s" % off.name)
        if off.state == "Enabled":
            self.log.extra("Already Enabled")
            self.log.success()
            return
        cmd = updateNetworkOffering.updateNetworkOfferingCmd()
        cmd.id = off.id
        cmd.state = "Enabled"
        self.apiclient.updateNetworkOffering(cmd)
        self.log.success()

    def add_redundant_service_offering(self, name="isored1"):
        self.log.ptest("Adding redundant isolated network offering (%s)" % name)
        off = self.list_network_offerings(name)
        if off is not None:
            self.log.extra("Exists")
            self.log.success()
            self.enable_offering(off)
            return off
        cmd = createNetworkOffering.createNetworkOfferingCmd()
        cmd.name = name
        cmd.displaytext = name
        cmd.ispersistent = "true"
        cmd.guestiptype = "Isolated"
        cmd.supportedservices = "Vpn,Dhcp,Dns,Firewall,Lb,SourceNat,StaticNat,PortForwarding"
        cmd.servicecapabilitylist = []
        cmd.servicecapabilitylist.append({
            "service": "SourceNat",
            "capabilitytype": "RedundantRouter",
            "capabilityvalue": "true"
            })
        cmd.servicecapabilitylist.append({
            "service": "SourceNat",
            "capabilitytype": "SupportedSourceNatTypes",
            "capabilityvalue": "peraccount"
            })
        cmd.servicecapabilitylist.append({
            "service": "lb",
            "capabilitytype": "SupportedLbIsolation",
            "capabilityvalue": "dedicated"
            })
        cmd.serviceproviderlist = []
        cmd.serviceproviderlist.append({"service": "Vpn", "provider": "VirtualRouter"})
        cmd.serviceproviderlist.append({"service": "Dhcp", "provider": "VirtualRouter"})
        cmd.serviceproviderlist.append({"service": "Dns", "provider": "VirtualRouter"})
        cmd.serviceproviderlist.append({"service": "Firewall", "provider": "VirtualRouter"})
        cmd.serviceproviderlist.append({"service": "Lb", "provider": "VirtualRouter"})
        cmd.serviceproviderlist.append({"service": "SourceNat", "provider": "VirtualRouter"})
        cmd.serviceproviderlist.append({"service": "StaticNat", "provider": "VirtualRouter"})
        cmd.serviceproviderlist.append({"service": "PortForwarding", "provider": "VirtualRouter"})
        cmd.traffictype = "GUEST"
        off = self.apiclient.createNetworkOffering(cmd)
        self.log.success()
        self.enable_offering(off)
        return off
from marvin.cloudstackAPI import *
from marvin.cloudstackAPI.cloudstackAPIClient import CloudStackAPIClient
from marvin.cloudstackException import CloudstackAPIException
from marvin.cloudstackException import GetDetailExceptionInfo
from marvin.cloudstackConnection import CSConnection
from marvin.configGenerator import ConfigManager
from marvin.lib.utils import (random_gen, validateList)

from CSUtils import *
import json
import time
import sys

utils = CSUtils()
conn = utils.getConnection()
apiclient = CloudStackAPIClient(conn)


# Query Zone
lz = listZones.listZonesCmd()
lz.available = True
resp = apiclient.listZones(lz)
zone = resp[0]

# Create Network
lno = listNetworkOfferings.listNetworkOfferingsCmd()
lno.name="DefaultIsolatedNetworkOfferingWithSourceNatService"
resp = apiclient.listNetworkOfferings(lno)
offering = resp[0]

cn = createNetwork.createNetworkCmd()
from marvin.cloudstackAPI import *
from marvin.cloudstackAPI.cloudstackAPIClient import CloudStackAPIClient
from marvin.cloudstackException import CloudstackAPIException
from marvin.cloudstackException import GetDetailExceptionInfo
from marvin.cloudstackConnection import CSConnection
from marvin.configGenerator import ConfigManager
from marvin.lib.utils import (random_gen, validateList)

from CSUtils import *
import json
import time
import sys

utils = CSUtils()
conn = utils.getConnection()
apiclient = CloudStackAPIClient(conn)

listsvm = listSystemVms.listSystemVmsCmd()

count = 40
while count > 0:
    # Sleep 15 seconds
    time.sleep(15)
    count = count - 1
    ssvm = None

    # Check if we have an ssvm
    print "Looking for systemvm of type secondarystoragevm"
    try:
        resp = apiclient.listSystemVms(listsvm)
        if resp == None or len(resp) == 0: