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,
                 test_data_filepath=None,
                 zone=None,
                 hypervisor_type=None,
                 halt_on_failure=False):
        self.__mgmtDetails = mgmt_details
        self.__dbSvrDetails = dbsvr_details
        self.__csConnection = None
        self.__dbConnection = None
        self.__testClient = None
        self.__asyncTimeOut = async_timeout
        self.__logger = MarvinLog('marvin').get_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()
        self.__scenarioManager = None
        self.__halt_on_failure = halt_on_failure

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

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

    def getHaltOnFailure(self):
        return self.__halt_on_failure

    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:
            self.__logger.exception("Set hypervisor failed: %s" % 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 \
                        (validate_list(list_user_res)[0] != PASS):
                    self.__logger.error("API Client Creation Failed")
                    return FAILED
                user_id = list_user_res[0].id
                ret = self.__getKeys(user_id)
                if ret != FAILED:
                    api_key = ret[0]
                    security_key = ret[1]
                else:
                    self.__logger.error(
                        "API Client Creation Failed while Registering User")
                    return FAILED

                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("Create API client failed: %s" % 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
            return (register_user_res.apikey, register_user_res.secretkey)
        except Exception as e:
            self.__logger.exception("Key retrival failed: %s" % 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(
                    "Either Hypervisor is None or Not able to create ConfigManager Object"
                )
                return FAILED

            self.__parsedTestDataConfig = self.__configObj.getConfig()
            self.__logger.info("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.info("=== Test Client Creation Successful ===")
            return ret
        except Exception as e:
            self.__logger.exception("Test Client creation failed: %s" % 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
            apiKey = listuserRes[0].apikey
            securityKey = listuserRes[0].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(
                        "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("API user creation failed: %s" % 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)

    def getScenarioManager(self,
                           scenarioName,
                           randomizeNames=True,
                           cleanup=True):
        if not self.__scenarioManager:
            scenarioHumanReadableName = self.getParsedTestDataConfig(
            )['scenarios'][scenarioName]['metadata']['name']
            self.__logger.info(
                "=== Scenario Manager, Scenario \"%s\" Started ===",
                scenarioHumanReadableName)

            self.__scenarioManager = TestScenarioManager(
                api_client=self.getApiClient(),
                scenario_data=self.getParsedTestDataConfig()['scenarios']
                [scenarioName],
                test_data=self.getParsedTestDataConfig()['test_data'],
                zone=get_zone(self.getApiClient(), self.getZoneForTests()),
                randomizeNames=randomizeNames,
                cleanup=cleanup)

            try:
                self.__scenarioManager.setup_infra()
            except:
                self.__logger.exception(">>>>>>>>>>>>>>>>>>>>>>> %s ",
                                        traceback.format_exc())
                raise
            self.__logger.info(
                "=== Scenario Manager, Scenario \"%s\" Created ===",
                scenarioHumanReadableName)
        return self.__scenarioManager

    def finalize(self):
        if self.__scenarioManager:
            self.__scenarioManager.finalize()
Exemple #2
0
class MarvinPlugin(Plugin):
    """
    Custom plugin for the cloudstackTestCases to be run using nose
    """

    name = "marvin"

    def __init__(self):
        self.__identifier = None
        self.__testClient = None
        self.__logFolderPath = None
        self.__parsedConfig = None
        '''
        Contains Config File
        '''
        self.__configFile = None
        '''
        Signifies the Zone against which all tests will be Run
        '''
        self.__zoneForTests = None
        '''
        Signifies the flag whether to deploy the New DC or Not
        '''
        self.__deployDcFlag = None
        self.conf = None
        self.__resultStream = stdout
        self.__testRunner = None
        self.__testResult = SUCCESS
        self.__startTime = None
        self.__testName = None
        self.__tcRunLogger = MarvinLog('marvin').get_logger()
        self.__testModName = ''
        self.__hypervisorType = None

        self.__halt_on_failure = False

        Plugin.__init__(self)

    def configure(self, options, conf):
        """enable the marvin plugin when the --with-marvin directive is given
        to nose. The enableOpt value is set from the command line directive and
        self.enabled (True|False) determines whether marvin's tests will run.
        By default non-default plugins like marvin will be disabled
        """
        self.enabled = True
        if hasattr(options, self.enableOpt):
            if not getattr(options, self.enableOpt):
                self.enabled = False
                return
        self.__configFile = options.configFile
        self.__deployDcFlag = options.deployDc
        self.__zoneForTests = options.zone
        self.__hypervisorType = options.hypervisor_type
        self.__halt_on_failure = options.haltOnFailure
        self.conf = conf
        if self.startMarvin() == FAILED:
            self.__tcRunLogger.error(
                'Starting Marvin Failed, exiting. Please Check')
            exit(1)

    def options(self, parser, env):
        """
        Register command line options
        """
        parser.add_option("--marvin-config",
                          action="store",
                          default=env.get('MARVIN_CONFIG', './datacenter.cfg'),
                          dest="configFile",
                          help="Marvin's configuration file is required."
                          "The config file containing the datacenter and "
                          "other management server "
                          "information is specified")
        parser.add_option("--deploy",
                          action="store_true",
                          default=False,
                          dest="deployDc",
                          help="Deploys the DC with Given Configuration."
                          "Requires only when DC needs to be deployed")
        parser.add_option("--zone",
                          action="store",
                          default=None,
                          dest="zone",
                          help="Runs all tests against this specified zone")
        parser.add_option("--hypervisor",
                          action="store",
                          default=None,
                          dest="hypervisor_type",
                          help="Runs all tests against the specified "
                          "zone and hypervisor Type")
        parser.add_option("--log-folder-path",
                          action="store",
                          default=None,
                          dest="logFolder",
                          help="Collects all logs under the user specified"
                          "folder")
        parser.add_option("--halt-on-failure",
                          action="store_true",
                          default=False,
                          dest="haltOnFailure",
                          help="Halt the test on failure")
        Plugin.options(self, parser, env)

    def wantClass(self, cls):
        if cls.__name__ == 'cloudstackTestCase':
            return False
        if issubclass(cls, cloudstackTestCase):
            return True
        return None

    def __checkImport(self, filename):
        '''
        @Name : __checkImport
        @Desc : Verifies if a test module is importable.
                Returns False or True based upon the result.
        '''
        try:
            if os.path.isfile(filename):
                ret = os.path.splitext(filename)
                if ret[1] == ".py":
                    os.system("python " + filename)
                    return True
            return False
        except ImportError as e:
            self.__tcRunLogger.exception("File %s has import errors: %s" %
                                         (filename, e))
            return False

    def wantFile(self, filename):
        '''
        @Desc : Only python files will be used as test modules
        '''
        return self.__checkImport(filename)

    def loadTestsFromTestCase(self, cls):
        if cls.__name__ != 'cloudstackTestCase':
            self.__identifier = cls.__name__
            self._injectClients(cls)

    def beforeTest(self, test):
        self.__testModName = test.__str__()
        self.__testName = test.__str__().split()[0]
        if not self.__testName:
            self.__testName = "test"
        self.__testClient.identifier = '-'. \
            join([self.__identifier, self.__testName])
        if self.__tcRunLogger:
            self.__tcRunLogger.name = test.__str__()

    def startTest(self, test):
        """
        Currently used to record start time for tests
        Dump Start Msg of TestCase to Log
        """
        self.__tcRunLogger.info("=== Started Test %s ===" %
                                str(self.__testName))
        self.__startTime = time.time()

    def printMsg(self, status, tname, err):
        if status in [FAILED, EXCEPTION] and self.__tcRunLogger:
            self.__tcRunLogger.fatal("%s: %s: %s" % (status, tname, err))
        write_str = "=== TestName: %s | Status : %s ===" % (tname, status)
        self.__resultStream.write(write_str)
        self.__tcRunLogger.info(write_str)

    def addSuccess(self, test, capt):
        '''
        Adds the Success Messages to logs
        '''
        self.printMsg(SUCCESS, self.__testName, "Test Case Passed")
        self.__testResult = SUCCESS

    def handleError(self, test, err):
        '''
        Adds Exception throwing test cases and information to log.
        '''
        self.printMsg(EXCEPTION, self.__testName, err)
        self.__testResult = EXCEPTION

    def prepareTestRunner(self, runner):
        if self.__testRunner:
            return self.__testRunner

    def handleFailure(self, test, err):
        '''
        Adds Failing test cases and information to log.
        '''
        self.printMsg(FAILED, self.__testName, err)
        self.__testResult = FAILED

    def startMarvin(self):
        '''
        @Name : startMarvin
        @Desc : Initializes the Marvin
                creates the test Client
                creates the runlogger for logging
                Parses the config and creates a parsedconfig
                Creates a debugstream for tc debug log
        '''
        try:
            obj_marvininit = MarvinInit(self.__configFile, self.__deployDcFlag,
                                        None, self.__zoneForTests,
                                        self.__hypervisorType)
            if obj_marvininit and obj_marvininit.init() == SUCCESS:
                self.__testClient = obj_marvininit.getTestClient()
                self.__parsedConfig = obj_marvininit.getParsedConfig()
                self.__resultStream = obj_marvininit.getResultFile()
                self.__testRunner = nose.core.TextTestRunner(
                    stream=self.__resultStream,
                    descriptions=True,
                    verbosity=2,
                    config=self.conf)
                return SUCCESS
            return FAILED
        except Exception as e:
            self.__tcRunLogger.exception(
                ("=== Start Marvin failed: %s ===" % e))
            return FAILED

    def stopTest(self, test):
        """
        Currently used to record end time for tests
        """
        endTime = time.time()
        if self.__startTime:
            totTime = int(endTime - self.__startTime)
            self.__tcRunLogger.info(
                "TestCaseName: %s; Time Taken: %s Seconds; StartTime: %s; EndTime: %s; Result: %s"
                % (self.__testName, str(totTime),
                   str(time.ctime(self.__startTime)), str(
                       time.ctime(endTime)), self.__testResult))

    def _injectClients(self, test):
        setattr(test, "debug", self.__tcRunLogger.debug)
        setattr(test, "info", self.__tcRunLogger.info)
        setattr(test, "warn", self.__tcRunLogger.warning)
        setattr(test, "error", self.__tcRunLogger.error)
        setattr(test, "testClient", self.__testClient)
        setattr(test, "config", self.__parsedConfig)
        if self.__testClient.identifier is None:
            self.__testClient.identifier = self.__identifier
        setattr(test, "clstestclient", self.__testClient)
        if hasattr(test, "user"):
            # when the class-level attr applied. all test runs as 'user'
            self.__testClient.getUserApiClient(test.UserName, test.DomainName,
                                               test.AcctType)

    def finalize(self, result):
        self.__tcRunLogger.info('=== Finalizing... ===')
        self.__testClient.finalize()
Exemple #3
0
class MarvinInit:
    def __init__(self,
                 config_file,
                 deploy_dc_flag=False,
                 test_mod_name="deploydc",
                 zone=None,
                 hypervisor_type=None,
                 halt_on_failure=False):
        self.__configFile = config_file
        self.__deployFlag = deploy_dc_flag
        self.__tcRunLogger = MarvinLog('marvin').get_logger()
        self.__tcRunLogger.info("=== Marvin Init Logging Successful ===")
        self.__testModName = test_mod_name
        self.__testClient = None
        self.__tcResultFile = None
        self.__testDataFilePath = None
        self.__zoneForTests = zone
        self.__parsedConfig = None
        self.__hypervisorType = hypervisor_type
        self.__halt_on_failure = halt_on_failure

    def __parseConfig(self):
        '''
        @Name: __parseConfig
        @Desc : Parses the configuration file passed and assigns
        the parsed configuration
        @Output : SUCCESS or FAILED
        '''
        try:
            if not os.path.isfile(self.__configFile):
                self.__tcRunLogger.error(
                    "=== Marvin Parse Config Init Failed ===")
                return FAILED
            self.__parsedConfig = getSetupConfig(self.__configFile)
            self.__tcRunLogger.info("=== Marvin Parse Config Successful ===")
            return SUCCESS
        except Exception as e:
            self.__tcRunLogger.exception(
                "=== Marvin Parse Config Init Failed: %s ===" % e)
            return FAILED

    def getParsedConfig(self):
        return self.__parsedConfig

    def getLogFolderPath(self):
        return self.__logFolderPath

    def getTestClient(self):
        return self.__testClient

    def getLogger(self):
        return self.__tcRunLogger

    def getResultFile(self):
        '''
        @Name : getDebugFile
        @Desc : Creates the result file at a given path.
        @Output : Returns the Result file to be used for writing
                test outputs
        '''
        self.__tcResultFile = open("results.txt", "w")
        return self.__tcResultFile

    def __setHypervisorAndZoneInfo(self):
        '''
        @Name : __setHypervisorAndZoneInfo
        @Desc:  Set the HyperVisor and Zone details;
                default to XenServer
        '''
        try:
            if not self.__hypervisorType:
                if self.__parsedConfig and self.__parsedConfig.zones is not None:
                    for zone in self.__parsedConfig.zones:
                        for pod in zone.pods:
                            if pod is not None:
                                for cluster in pod.clusters:
                                    if cluster is not None and cluster.hypervisor is not None:
                                        self.__hypervisorType = cluster.hypervisor
                                        break
            if not self.__zoneForTests:
                if self.__parsedConfig and self.__parsedConfig.zones is not None:
                    for zone in self.__parsedConfig.zones:
                        self.__zoneForTests = zone.name
                        break
            if not self.__hypervisorType:
                self.__hypervisorType = XEN_SERVER
            return SUCCESS
        except Exception as e:
            self.__tcRunLogger.exception(
                "=== Set Hypervizor and Zone info Failed: %s ===" % e)
            return FAILED

    def init(self):
        '''
        @Name : init
        @Desc :Initializes the marvin by
               1. Parsing the configuration and creating a parsed config
                  structure
               2. Creates a timestamped log folder and provides all logs
                  to be dumped there
               3. Creates the DataCenter based upon configuration provided
        @Output : SUCCESS or FAILED
        '''
        try:
            self.__tcRunLogger.info("=== Marvin Init Started ===")
            if ((self.__parseConfig() != FAILED)
                    and (self.__setHypervisorAndZoneInfo())
                    and (self.__setTestDataPath() != FAILED)
                    and (self.__createTestClient() != FAILED)
                    and (self.__deployDC() != FAILED)):
                self.__tcRunLogger.info("=== Marvin Init Successful ===")
                return SUCCESS
            self.__tcRunLogger.error("=== Marvin Init Failed ===")
            return FAILED
        except Exception as e:
            self.__tcRunLogger.exception(
                "=== Marvin Init Failed with exception: %s ===" % e)
            return FAILED

    def __createTestClient(self):
        '''
        @Name : __createTestClient
        @Desc : Creates the TestClient during init
                based upon the parameters provided
        @Output: Returns SUCCESS or FAILED
        '''
        try:
            mgt_details = self.__parsedConfig.mgtSvr[0]
            dbsvr_details = self.__parsedConfig.dbSvr
            self.__testClient = CSTestClient(
                mgt_details,
                dbsvr_details,
                test_data_filepath=self.__testDataFilePath,
                zone=self.__zoneForTests,
                hypervisor_type=self.__hypervisorType,
                halt_on_failure=self.__halt_on_failure)
            if self.__testClient:
                return self.__testClient.createTestClient()
            return FAILED
        except Exception as e:
            self.__tcRunLogger.exception(
                "=== Marvin Create Test Client Failed: %s ===" % e)
            return FAILED

    def __setTestDataPath(self):
        '''
        @Name : __setTestDataPath
        @Desc : Sets the TestData Path for tests to run
        @Output:Returns SUCCESS or FAILED
        '''
        try:
            if ((self.__parsedConfig.TestData is not None)
                    and (self.__parsedConfig.TestData.Path is not None)):
                self.__testDataFilePath = self.__parsedConfig.TestData.Path
            self.__tcRunLogger.info(
                "=== Marvin Setting TestData Successful ===")
            return SUCCESS
        except Exception as e:
            self.__tcRunLogger.exception(
                "=== Marvin Setting TestData Successful Failed: %s ===" % e)
            return FAILED

    def __deployDC(self):
        '''
        @Name : __deployDC
        @Desc : Deploy the DataCenter and returns accordingly.
        @Output: SUCCESS or FAILED
        '''
        try:
            ret = SUCCESS
            if self.__deployFlag:
                deploy_obj = DeployDataCenters(self.__testClient,
                                               self.__parsedConfig,
                                               self.__tcRunLogger)
                ret = deploy_obj.deploy()
                if ret != SUCCESS:
                    self.__tcRunLogger.error("=== Deploy DC Failed ===")
            return ret
        except Exception as e:
            self.__tcRunLogger.exception(
                "=== Deploy DC Failed with exception: %s ===" % 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,
                 test_data_filepath=None,
                 zone=None,
                 hypervisor_type=None,
                 halt_on_failure=False):
        self.__mgmtDetails = mgmt_details
        self.__dbSvrDetails = dbsvr_details
        self.__csConnection = None
        self.__dbConnection = None
        self.__testClient = None
        self.__asyncTimeOut = async_timeout
        self.__logger = MarvinLog('marvin').get_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()
        self.__scenarioManager = None
        self.__halt_on_failure = halt_on_failure

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

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

    def getHaltOnFailure(self):
        return self.__halt_on_failure

    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:
            self.__logger.exception("Set hypervisor failed: %s" % 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 \
                        (validate_list(list_user_res)[0] != PASS):
                    self.__logger.error("API Client Creation Failed")
                    return FAILED
                user_id = list_user_res[0].id
                ret = self.__getKeys(user_id)
                if ret != FAILED:
                    api_key = ret[0]
                    security_key = ret[1]
                else:
                    self.__logger.error("API Client Creation Failed while Registering User")
                    return FAILED

                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("Create API client failed: %s" % 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
            return (register_user_res.apikey, register_user_res.secretkey)
        except Exception as e:
            self.__logger.exception("Key retrival failed: %s" % 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("Either Hypervisor is None or Not able to create ConfigManager Object")
                return FAILED

            self.__parsedTestDataConfig = self.__configObj.getConfig()
            self.__logger.info("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.info("=== Test Client Creation Successful ===")
            return ret
        except Exception as e:
            self.__logger.exception("Test Client creation failed: %s" % 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
            apiKey = listuserRes[0].apikey
            securityKey = listuserRes[0].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("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("API user creation failed: %s" % 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)

    def getScenarioManager(self, scenarioName, randomizeNames=True, cleanup=True):
        if not self.__scenarioManager:
            scenarioHumanReadableName = self.getParsedTestDataConfig()['scenarios'][scenarioName]['metadata']['name']
            self.__logger.info("=== Scenario Manager, Scenario \"%s\" Started ===", scenarioHumanReadableName)

            self.__scenarioManager = TestScenarioManager(
                api_client=self.getApiClient(),
                scenario_data=self.getParsedTestDataConfig()['scenarios'][scenarioName],
                test_data=self.getParsedTestDataConfig()['test_data'],
                zone=get_zone(self.getApiClient(), self.getZoneForTests()),
                randomizeNames=randomizeNames,
                cleanup=cleanup
            )

            try:
                self.__scenarioManager.setup_infra()
            except:
                self.__logger.exception(">>>>>>>>>>>>>>>>>>>>>>> %s ", traceback.format_exc())
                raise
            self.__logger.info("=== Scenario Manager, Scenario \"%s\" Created ===", scenarioHumanReadableName)
        return self.__scenarioManager

    def finalize(self):
        if self.__scenarioManager:
            self.__scenarioManager.finalize()
class DeleteDataCenters:
    """
    @Desc : Deletes the Data Center using the settings provided.
            test_client :Client for deleting the DC.
            dc_cfg_file : obj file exported by DeployDataCenter
            when successfully created DC.
                          This file is serialized one containing
                          entries with successful DC.
            dc_cfg: If dc_cfg_file, is not available, we can use
            the dictionary of elements to delete.
            tc_run_logger: Logger to dump log messages.
    """

    def __init__(self, test_client, cfg):
        self.__cfg = cfg
        self.__test_client = test_client
        self.__logger = MarvinLog('marvin').get_logger()
        self.__apiClient = None

    def __deleteCmds(self, cmd_name, cmd_obj):
        """
        @Name : __deleteCmds
        @Desc : Deletes the entities provided by cmd
        """
        if cmd_name.lower() == "deletehostcmd":
            cmd_obj.forcedestroylocalstorage = "true"
            cmd_obj.force = "true"
            '''
            Step1 : Prepare Host For Maintenance
            '''
            host_maint_cmd = prepareHostForMaintenance.prepareHostForMaintenanceCmd()
            host_maint_cmd.id = cmd_obj.id
            host_maint_resp = self.__apiClient.prepareHostForMaintenance(host_maint_cmd)
            if host_maint_resp:
                '''
                Step2 : List Hosts for Resource State
                '''
                list_host_cmd = listHosts.listHostsCmd()
                list_host_cmd.id = cmd_obj.id
                retries = 3
                for i in xrange(retries):
                    list_host_resp = self.__apiClient.listHosts(list_host_cmd)
                    if list_host_resp and (list_host_resp[0].resourcestate == 'Maintenance'):
                        break
                    sleep(30)
        if cmd_name.lower() == "deletestoragepoolcmd":
            cmd_obj.forced = "true"
            store_maint_cmd = enableStorageMaintenance.enableStorageMaintenanceCmd()
            store_maint_cmd.id = cmd_obj.id
            store_maint_resp = self.__apiClient.enableStorageMaintenance(store_maint_cmd)
            if store_maint_resp:
                list_store_cmd = listStoragePools.listStoragePoolsCmd()
                list_store_cmd.id = cmd_obj.id
                retries = 3
                for i in xrange(retries):
                    store_maint_resp = self.__apiClient.listStoragePools(list_store_cmd)
                    if store_maint_resp and (store_maint_resp[0].state == 'Maintenance'):
                        break
                    sleep(30)
        return cmd_obj

    def __setClient(self):
        self.__apiClient = self.__test_client.getApiClient()

    def __cleanEntries(self):
        """
        @Name : __cleanAndEntries
        @Description: Cleans up the created DC in order of creation
        """
        try:
            ret = FAILED
            if "order" in self.__cfg.keys() and len(self.__cfg["order"]):
                self.__cfg["order"].reverse()
            self.__logger.info("=== Clean Up Entries: %s ===" % self.__cfg)
            for type in self.__cfg["order"]:
                self.__logger.info("=== CleanUp Started For Type: %s ===" % type)
                if type:
                    temp_ids = self.__cfg[type]
                    ids = [items for items in temp_ids if items]
                    for id in ids:
                        del_mod = "delete" + type
                        del_cmd = getattr(globals()[del_mod], del_mod + "Cmd")
                        del_cmd_obj = del_cmd()
                        del_cmd_obj.id = id
                        del_cmd_obj = self.__deleteCmds(del_mod + "Cmd", del_cmd_obj)
                        del_func = getattr(self.__apiClient, del_mod)
                        del_cmd_resp = del_func(del_cmd_obj)
                        if del_cmd_resp:
                            self.__logger.error("=== %s CleanUp Failed. ID: %s ===" % (type, id))
                        else:
                            self.__logger.info("=== %s CleanUp Successful. ID : %s ===" % (type, id))
            ret = SUCCESS
        except Exception as e:
            self.__logger.exception("=== Clean Up Entries failed: %s ===" % e)
        finally:
            return ret

    def removeDataCenter(self):
        """
        @Name : removeDataCenter
        @Desc : Removes the Data Center provided by Configuration
                If Input dc file configuration is None, uses the cfg provided
                else uses the dc file to get the configuration
        """
        try:
            self.__setClient()
            self.__logger.info("=== DeployDC: CleanUp Started ===")
            ret = self.__cleanEntries()
        except Exception as e:
            self.__logger.exception("=== DeployDC: CleanUp failed: %s ===" % e)
        finally:
            return ret
class DeployDataCenters(object):
    """
    @Desc : Deploys the Data Center with information provided.
            Once the Deployment is successful, it will export
            the DataCenter settings to an obj file
            ( can be used if wanted to delete the created DC)
    """

    def __init__(self, test_client, cfg):
        self.__test_client = test_client
        self.__config = cfg
        self.__logger = MarvinLog('marvin').get_logger()
        self.__apiClient = None
        self.__cleanUp = { }

    def __persistDcConfig(self):
        try:
            ts = strftime("%b_%d_%Y_%H_%M_%S", localtime())
            dc_file_path = "dc_entries_" + str(ts) + ".obj"
            file_to_write = open(dc_file_path, 'w')
            if file_to_write:
                pickle.dump(self.__cleanUp, file_to_write)
                self.__logger.info("=== Data Center Settings are dumped to %s ===" % dc_file_path)
        except Exception as e:
            self.__logger.exception("=== Persisting DataCenter config failed: %s ===" % e)

    def __cleanAndExit(self):
        try:
            self.__logger.info("=== deploy dc failed, so cleaning the created entries ===")
            if not test_data.get("deleteDC", None):
                self.__logger.info("=== Deploy DC Clean Up flag not set. So, exiting ===")
                exit(1)
            self.__logger.info("=== Deploy DC Failed, So Cleaning to Exit ===")
            remove_dc_obj = DeleteDataCenters(self.__test_client, self.__cleanUp)
            if remove_dc_obj:
                if remove_dc_obj.removeDataCenter() == FAILED:
                    self.__logger.error("=== Removing DataCenter Failed ===")
                else:
                    self.__logger.info("=== Removing DataCenter Successful ===")
        except Exception as e:
            self.__logger.exception("=== Clean up failed: %s ===" % e)
        finally:
            exit(1)

    def __addToCleanUp(self, type, id):
        if type not in self.__cleanUp.keys():
            self.__cleanUp[type] = []
        self.__cleanUp[type].append(id)
        if "order" not in self.__cleanUp.keys():
            self.__cleanUp["order"] = []
        if type not in self.__cleanUp["order"]:
            self.__cleanUp["order"].append(type)

    def addHosts(self, hosts, zoneId, podId, clusterId, hypervisor):
        if hosts is None:
            self.__logger.warn("=== Invalid Hosts Information ===")
            return
        failed_cnt = 0
        for host in hosts:
            try:
                hostcmd = addHost.addHostCmd()
                hostcmd.clusterid = clusterId
                hostcmd.hosttags = host.hosttags
                hostcmd.hypervisor = host.hypervisor
                hostcmd.password = host.password
                hostcmd.podid = podId
                hostcmd.url = host.url
                hostcmd.username = host.username
                hostcmd.zoneid = zoneId
                hostcmd.hypervisor = hypervisor
                hostcmd.hosttags = hypervisor
                ret = self.__apiClient.addHost(hostcmd)
                if ret:
                    self.__logger.info("=== Add Host Successful ===")
                    self.__addToCleanUp("Host", ret[0].id)
            except Exception as e:
                failed_cnt += 1
                self.__logger.exception("=== Adding Host (%s) Failed: %s ===" % (str(host.url), e))
                if failed_cnt == len(hosts):
                    self.__cleanAndExit()

    def createClusters(self, clusters, zoneId, podId):
        try:
            if clusters is None:
                return

            for cluster in clusters:
                clustercmd = addCluster.addClusterCmd()
                clustercmd.clustername = cluster.clustername
                clustercmd.clustertype = cluster.clustertype
                clustercmd.hypervisor = cluster.hypervisor
                clustercmd.password = cluster.password
                clustercmd.podid = podId
                clustercmd.url = cluster.url
                clustercmd.username = cluster.username
                clustercmd.zoneid = zoneId
                clusterresponse = self.__apiClient.addCluster(clustercmd)
                if clusterresponse[0].id:
                    clusterId = clusterresponse[0].id
                    self.__logger.debug(
                        "Cluster Name : %s Id : %s Created Successfully" % (str(cluster.clustername), str(clusterId)))
                    self.__addToCleanUp("Cluster", clusterId)
                self.addHosts(cluster.hosts, zoneId, podId, clusterId, cluster.hypervisor)
                self.waitForHost(zoneId, clusterId)
                self.createPrimaryStorages(cluster.primaryStorages, zoneId, podId, clusterId)
        except Exception as e:
            self.__logger.exception("=== Cluster %s Creation Failed: %s ===" % (str(cluster.clustername), e))
            self.__cleanAndExit()

    def waitForHost(self, zoneId, clusterId):
        """
        Wait for the hosts in the zoneid, clusterid to be up
        2 retries with 30s delay
        """
        try:
            retry, timeout = 2, 30
            cmd = listHosts.listHostsCmd()
            cmd.clusterid, cmd.zoneid = clusterId, zoneId
            hosts = self.__apiClient.listHosts(cmd)
            while retry != 0:
                for host in hosts:
                    if host.state != 'Up':
                        break
                sleep(timeout)
                retry -= 1
        except Exception as e:
            self.__logger.exception("=== List Hosts Failed: %s ===" % e)
            self.__cleanAndExit()

    def createPrimaryStorages(self, primaryStorages, zoneId, podId=None, clusterId=None):
        try:
            if primaryStorages is None:
                return
            for primary in primaryStorages:
                primarycmd = createStoragePool.createStoragePoolCmd()
                if primary.details:
                    for key, value in vars(primary.details).iteritems():
                        primarycmd.details.append({ key: value })
                primarycmd.name = primary.name

                primarycmd.tags = primary.tags
                primarycmd.url = primary.url
                if primary.scope == 'zone' or clusterId is None:
                    primarycmd.scope = 'zone'
                    primarycmd.hypervisor = primary.hypervisor
                    primarycmd.tags = 'zone'
                else:
                    primarycmd.podid = podId
                    primarycmd.clusterid = clusterId
                    primarycmd.tags = 'cluster'
                primarycmd.zoneid = zoneId

                ret = self.__apiClient.createStoragePool(primarycmd)
                if ret.id:
                    self.__logger.info("=== Creating Storage Pool Successful ===")
                    self.__addToCleanUp("StoragePool", ret.id)
        except Exception as e:
            self.__logger.exception("=== Create Storage Pool Failed: %s ===" % e)
            self.__cleanAndExit()

    def createPods(self, pods, zoneId, networkId=None):
        try:
            if pods is None:
                return
            for pod in pods:
                createpod = createPod.createPodCmd()
                createpod.name = pod.name
                createpod.gateway = pod.gateway
                createpod.netmask = pod.netmask
                createpod.startip = pod.startip
                createpod.endip = pod.endip
                createpod.zoneid = zoneId
                createpodResponse = self.__apiClient.createPod(createpod)
                if createpodResponse.id:
                    podId = createpodResponse.id
                    self.__logger.debug("Pod Name : %s Id : %s Created Successfully" % (str(pod.name), str(podId)))
                    self.__addToCleanUp("Pod", podId)
                if pod.guestIpRanges is not None and networkId is not None:
                    self.createVlanIpRanges("Basic", pod.guestIpRanges, zoneId, podId, networkId)
                self.createClusters(pod.clusters, zoneId, podId)
        except Exception as e:
            self.__logger.exception("=== Pod: %s Creation Failed: %s ===" % (str(pod.name), e))
            self.__cleanAndExit()

    def createVlanIpRanges(self, mode, ipranges, zoneId, podId=None, networkId=None, forvirtualnetwork=None):
        try:
            if ipranges is None:
                return
            for iprange in ipranges:
                vlanipcmd = createVlanIpRange.createVlanIpRangeCmd()
                vlanipcmd.account = iprange.account
                vlanipcmd.domainid = iprange.domainid
                vlanipcmd.endip = iprange.endip
                vlanipcmd.gateway = iprange.gateway
                vlanipcmd.netmask = iprange.netmask
                vlanipcmd.networkid = networkId
                vlanipcmd.podid = podId
                vlanipcmd.startip = iprange.startip
                vlanipcmd.zoneid = zoneId
                vlanipcmd.vlan = iprange.vlan
                if mode == "Basic":
                    if forvirtualnetwork:
                        vlanipcmd.forvirtualnetwork = "true"
                    else:
                        vlanipcmd.forvirtualnetwork = "false"
                else:
                    vlanipcmd.forvirtualnetwork = "true"
                ret = self.__apiClient.createVlanIpRange(vlanipcmd)
                if ret.id:
                    self.__logger.info("=== Creating Vlan Ip Range Successful ===")
                    self.__addToCleanUp("VlanIpRange", ret.id)
        except Exception as e:
            self.__logger.exception("=== Create Vlan Ip Range Failed: %s ===" % e)
            self.__cleanAndExit()

    def createSecondaryStorages(self, secondaryStorages, zoneId):
        try:
            if secondaryStorages is None:
                return
            for secondary in secondaryStorages:
                secondarycmd = addImageStore.addImageStoreCmd()
                secondarycmd.url = secondary.url
                secondarycmd.provider = secondary.provider
                secondarycmd.details = []

                if secondarycmd.provider.lower() in ("smb"):
                    for key, value in vars(secondary.details).iteritems():
                        secondarycmd.details.append({
                            'key': key,
                            'value': value
                        })
                if secondarycmd.provider.lower() in ("nfs", "smb"):
                    secondarycmd.zoneid = zoneId
                ret = self.__apiClient.addImageStore(secondarycmd)
                if ret.id:
                    self.__logger.info("=== Add Image Store Successful ===")
                    self.__addToCleanUp("ImageStore", ret.id)
        except Exception as e:
            self.__logger.exception("=== Add Image Store Failed: %s ===" % e)
            self.__cleanAndExit()

    def createCacheStorages(self, cacheStorages, zoneId):
        try:
            if cacheStorages is None:
                return
            for cache in cacheStorages:
                cachecmd = createSecondaryStagingStore.createSecondaryStagingStoreCmd()
                cachecmd.url = cache.url
                cachecmd.provider = cache.provider
                cachecmd.zoneid = zoneId
                cachecmd.details = []

                if cache.details:
                    for key, value in vars(cache.details).iteritems():
                        cachecmd.details.append({
                            'key': key,
                            'value': value
                        })
                ret = self.__apiClient.createSecondaryStagingStore(cachecmd)
                if ret.id:
                    self.__logger.info("=== Creating Secondary StagingStore Successful ===")
                    self.__addToCleanUp("SecondaryStagingStore", ret.id)
        except Exception as e:
            self.__logger.exception("=== Creating Secondary Staging Storage Failed: %s ===" % e)
            self.__cleanAndExit()

    def createNetworks(self, networks, zoneId):
        try:
            if networks is None:
                return
            for network in networks:
                networkcmd = createNetwork.createNetworkCmd()
                networkcmd.displaytext = network.displaytext
                networkcmd.name = network.name
                networkcmd.networkofferingid = network.networkofferingid
                networkcmd.zoneid = zoneId

                ipranges = network.ipranges
                if ipranges:
                    iprange = ipranges.pop()
                    networkcmd.startip = iprange.startip
                    networkcmd.endip = iprange.endip
                    networkcmd.gateway = iprange.gateway
                    networkcmd.netmask = iprange.netmask
                networkcmdresponse = self.__apiClient.createNetwork(networkcmd)
                if networkcmdresponse.id:
                    networkId = networkcmdresponse.id
                    self.__logger.info(
                        "=== Creating Network Name : %s Id : %s Successful ===" % (str(network.name), str(networkId)))
                    self.__addToCleanUp("Network", networkId)
                    return networkId
        except Exception as e:
            self.__logger.exception("=== Network : %s Creation Failed: %s ===" % (str(network.name), e))
            self.__cleanAndExit()

    def createPhysicalNetwork(self, net, zoneid):
        try:
            phynet = createPhysicalNetwork.createPhysicalNetworkCmd()
            phynet.zoneid = zoneid
            phynet.name = net.name
            phynet.isolationmethods = net.isolationmethods
            phynetwrk = self.__apiClient.createPhysicalNetwork(phynet)
            if phynetwrk.id:
                self.__logger.info("=== Creating Physical Network Name : %s Id : %s Successful ===" % (
                    str(phynet.name), str(phynetwrk.id)))
                self.__addToCleanUp("PhysicalNetwork", phynetwrk.id)
            self.addTrafficTypes(phynetwrk.id, net.traffictypes)
            return phynetwrk
        except Exception as e:
            self.__logger.exception("=== Physical Network Creation Failed: %s ===" % e)
            self.__cleanAndExit()

    def updatePhysicalNetwork(self, networkid, state="Enabled", vlan=None):
        try:
            upnet = updatePhysicalNetwork.updatePhysicalNetworkCmd()
            upnet.id = networkid
            upnet.state = state
            if vlan:
                upnet.vlan = vlan
            ret = self.__apiClient.updatePhysicalNetwork(upnet)
            return ret
        except Exception as e:
            self.__logger.exception("=== Update Physical Network Failed: %s ===" % e)
            self.__cleanAndExit()

    def enableProvider(self, provider_id):
        try:
            upnetprov = \
                updateNetworkServiceProvider.updateNetworkServiceProviderCmd()
            upnetprov.id = provider_id
            upnetprov.state = "Enabled"
            ret = self.__apiClient.updateNetworkServiceProvider(upnetprov)
            if ret.id:
                self.__logger.info("=== Update Network Service Provider Successfull ===")
        except Exception as e:
            self.__logger.exception("=== Update Network Service Provider Failed: %s ===" % e)
            self.__cleanAndExit()

    def configureProviders(self, phynetwrk, providers):
        """
        We will enable the virtualrouter elements for all zones.
        """
        try:
            for provider in providers:
                pnetprov = listNetworkServiceProviders.listNetworkServiceProvidersCmd()
                pnetprov.physicalnetworkid = phynetwrk.id
                pnetprov.state = "Disabled"
                pnetprov.name = provider.name
                pnetprovres = self.__apiClient.listNetworkServiceProviders(
                    pnetprov)
                if pnetprovres and len(pnetprovres) > 0:
                    if provider.name == 'VirtualRouter' or provider.name == 'VpcVirtualRouter':
                        vrprov = listVirtualRouterElements.listVirtualRouterElementsCmd()
                        vrprov.nspid = pnetprovres[0].id
                        vrprovresponse = self.__apiClient.listVirtualRouterElements(vrprov)
                        vrprovid = vrprovresponse[0].id
                        vrconfig = configureVirtualRouterElement.configureVirtualRouterElementCmd()
                        vrconfig.enabled = "true"
                        vrconfig.id = vrprovid
                        self.__apiClient.configureVirtualRouterElement(vrconfig)
                        self.enableProvider(pnetprovres[0].id)
                elif provider.name in ['NiciraNvp']:
                    netprov = addNetworkServiceProvider.addNetworkServiceProviderCmd()
                    netprov.name = provider.name
                    netprov.physicalnetworkid = phynetwrk.id
                    result = self.__apiClient.addNetworkServiceProvider(
                        netprov)
                    if result.id:
                        self.__logger.info("=== AddNetworkServiceProvider Successful ===")
                        self.__addToCleanUp("NetworkServiceProvider", result.id)
                    if provider.devices is not None:
                        for device in provider.devices:
                            if provider.name == 'NiciraNvp':
                                cmd = addNiciraNvpDevice.addNiciraNvpDeviceCmd()
                                cmd.hostname = device.hostname
                                cmd.username = device.username
                                cmd.password = device.password
                                cmd.transportzoneuuid = device.transportzoneuuid
                                cmd.physicalnetworkid = phynetwrk.id
                                ret = self.__apiClient.addNiciraNvpDevice(cmd)
                                self.__logger.info("=== Add NiciraNvp Successful ===")
                                self.__addToCleanUp("NiciraNvp", ret.id)
                            else:
                                raise InvalidParameterException(
                                    "Device %s doesn't match any know provider type" % device)
                    self.enableProvider(result.id)
        except Exception as e:
            self.__logger.exception("=== List Network Service Providers Failed: %s ===" % e)
            self.__cleanAndExit()

    def addTrafficTypes(self, physical_network_id, traffictypes):
        [self.addTrafficType(physical_network_id, traffic_type) for traffic_type in traffictypes]

    def addTrafficType(self, physical_network_id, traffictype):
        try:
            traffic_type = addTrafficType.addTrafficTypeCmd()
            traffic_type.physicalnetworkid = physical_network_id
            traffic_type.traffictype = traffictype.typ
            traffic_type.kvmnetworklabel = traffictype.kvm \
                if traffictype.kvm is not None else None
            traffic_type.xennetworklabel = traffictype.xen \
                if traffictype.xen is not None else None
            traffic_type.simulatorlabel = traffictype.simulator \
                if traffictype.simulator is not None else None
            ret = self.__apiClient.addTrafficType(traffic_type)
            if ret.id:
                self.__logger.info("=== Add TrafficType Successful ===")
                self.__addToCleanUp("TrafficType", ret.id)
                return ret
        except Exception as e:
            self.__logger.exception("=== Add TrafficType Failed: %s ===" % e)
            self.__cleanAndExit()

    def enableZone(self, zoneid, allocation_state="Enabled"):
        try:
            zoneCmd = updateZone.updateZoneCmd()
            zoneCmd.id = zoneid
            zoneCmd.allocationstate = allocation_state
            ret = self.__apiClient.updateZone(zoneCmd)
            if ret.id:
                self.__logger.info("=== Enable Zone Successful ===")
                return ret
        except Exception as e:
            self.__logger.exception("=== Enable Zone Failed: %s ===" % e)
            self.__cleanAndExit()

    def updateZoneDetails(self, zoneid, details):
        try:
            zoneCmd = updateZone.updateZoneCmd()
            zoneCmd.id = zoneid
            zoneCmd.details = details
            ret = self.__apiClient.updateZone(zoneCmd)
            if ret.id:
                self.__logger.info("=== Update Zone Successful ===")
                return ret
        except Exception as e:
            self.__logger.exception("=== Update Zone  Failed: %s ===" % e)
            self.__cleanAndExit()

    def createZone(self, zone, rec=0):
        try:
            zoneresponse = self.__apiClient.createZone(zone)
            if zoneresponse.id:
                self.__logger.info("=== Create Zone Successful ===")
                self.__logger.debug("Zone Name : %s Id : %s " % (str(zone.name), str(zoneresponse.id)))
                self.__addToCleanUp("Zone", zoneresponse.id)
                return zoneresponse.id
            else:
                self.__logger.exception("=== Zone : %s Creation Failed ===" % str(zone.name))
                if not rec:
                    zone.name = zone.name + "_" + random_gen()
                    self.__logger.info("=== Recreating Zone With New Name : %s ===" % zone.name)
                    return self.createZone(zone, 1)
        except Exception as e:
            self.__logger.exception("=== Create Zone Failed: %s ===" % e)
            return FAILED

    def createZones(self, zones):
        try:
            for zone in zones:
                zonecmd = createZone.createZoneCmd()
                zonecmd.dns1 = zone.dns1
                zonecmd.dns2 = zone.dns2
                zonecmd.internaldns1 = zone.internaldns1
                zonecmd.internaldns2 = zone.internaldns2
                zonecmd.name = zone.name
                zonecmd.localstorageenabled = zone.localstorageenabled
                zonecmd.networktype = zone.networktype
                zonecmd.domain = zone.domain
                zonecmd.guestcidraddress = zone.guestcidraddress
                zoneId = self.createZone(zonecmd)
                if zoneId == FAILED:
                    self.__logger.error("=== Zone: %s Creation Failed. So Exiting ===" % str(zone.name))
                    self.__cleanAndExit()
                for pnet in zone.physical_networks:
                    phynetwrk = self.createPhysicalNetwork(pnet, zoneId)
                    self.configureProviders(phynetwrk, pnet.providers)
                    self.updatePhysicalNetwork(phynetwrk.id, "Enabled", vlan=pnet.vlan)
                if zone.networktype == "Basic":
                    listnetworkoffering = listNetworkOfferings.listNetworkOfferingsCmd()
                    if len(filter(lambda x: x.typ == 'Public', zone.physical_networks[0].traffictypes)) > 0:
                        listnetworkoffering.name = "DefaultSharedNetscalerEIPandELBNetworkOffering"
                    else:
                        listnetworkoffering.name = "DefaultSharedNetworkOfferingWithSGService"
                    if zone.networkofferingname is not None:
                        listnetworkoffering.name = zone.networkofferingname
                    listnetworkofferingresponse = self.__apiClient.listNetworkOfferings(listnetworkoffering)
                    guestntwrk = configGenerator.network()
                    guestntwrk.displaytext = "guestNetworkForBasicZone"
                    guestntwrk.name = "guestNetworkForBasicZone"
                    guestntwrk.zoneid = zoneId
                    guestntwrk.networkofferingid = listnetworkofferingresponse[0].id
                    networkid = self.createNetworks([guestntwrk], zoneId)
                    self.createPods(zone.pods, zoneId, networkid)
                    if self.isEipElbZone(zone):
                        self.createVlanIpRanges(zone.networktype, zone.ipranges, zoneId, forvirtualnetwork=True)
                isPureAdvancedZone = zone.networktype == "Advanced"
                if isPureAdvancedZone:
                    self.createPods(zone.pods, zoneId)
                    self.createVlanIpRanges(zone.networktype, zone.ipranges, zoneId)
                '''Note: Swift needs cache storage first'''
                self.createCacheStorages(zone.cacheStorages, zoneId)
                self.createSecondaryStorages(zone.secondaryStorages, zoneId)
                if zone.primaryStorages:
                    self.createPrimaryStorages(zone.primaryStorages, zoneId)
                enabled = getattr(zone, 'enabled', 'True')
                if enabled == 'True' or enabled is None:
                    self.enableZone(zoneId, "Enabled")
                details = getattr(zone, 'details')
                if details is not None:
                    det = [d.__dict__ for d in details]
                    self.updateZoneDetails(zoneId, det)
            return
        except Exception as e:
            self.__logger.exception("=== Create Zones Failed: %e ===" % e)

    def isEipElbZone(self, zone):
        if zone.networktype == "Basic" and len(
                filter(lambda x: x.typ == 'Public', zone.physical_networks[0].traffictypes)) > 0:
            return True
        return False

    def setClient(self):
        """
        @Name : setClient
        @Desc : Sets the API Client retrieved from test client
        """
        self.__apiClient = self.__test_client.getApiClient()

    def updateConfiguration(self, globalCfg):
        try:
            if globalCfg is None or self.__apiClient is None:
                return None
            for config in globalCfg:
                updateCfg = updateConfiguration.updateConfigurationCmd()
                updateCfg.name = config.name
                updateCfg.value = config.value
                ret = self.__apiClient.updateConfiguration(updateCfg)
                if ret.id:
                    self.__logger.info("=== Update Configuration Successfull ===")
        except Exception as e:
            self.__logger.exception("=== Update Configuration Failed: %s ===" % e)
            self.__cleanAndExit()

    @staticmethod
    def copyAttributesToCommand(source, command):
        map(lambda attr: setattr(command, attr, getattr(source, attr, None)),
            filter(lambda attr: not attr.startswith("__") and attr not in ["required", "isAsync"], dir(command)))

    def deploy(self):
        try:
            self.__logger.info("=== Deploy DC Started ===")
            '''
            Step1 : Set the Client
            '''
            self.setClient()
            '''
            Step2: Update the Configuration
            '''
            self.updateConfiguration(self.__config.globalConfig)
            '''
            Step3 :Deploy the Zone
            '''
            self.createZones(self.__config.zones)
            '''
            Persist the Configuration to an external file post DC creation
            '''
            self.__persistDcConfig()
            self.__logger.info("=== Deploy DC Successful ===")
            return SUCCESS
        except Exception as e:
            self.__logger.exception("=== Deploy DC Failed: %s ===" % e)
            self.__cleanAndExit()
            return FAILED
Exemple #7
0
class DeleteDataCenters:
    """
    @Desc : Deletes the Data Center using the settings provided.
            test_client :Client for deleting the DC.
            dc_cfg_file : obj file exported by DeployDataCenter
            when successfully created DC.
                          This file is serialized one containing
                          entries with successful DC.
            dc_cfg: If dc_cfg_file, is not available, we can use
            the dictionary of elements to delete.
            tc_run_logger: Logger to dump log messages.
    """
    def __init__(self, test_client, cfg):
        self.__cfg = cfg
        self.__test_client = test_client
        self.__logger = MarvinLog('marvin').get_logger()
        self.__apiClient = None

    def __deleteCmds(self, cmd_name, cmd_obj):
        """
        @Name : __deleteCmds
        @Desc : Deletes the entities provided by cmd
        """
        if cmd_name.lower() == "deletehostcmd":
            cmd_obj.forcedestroylocalstorage = "true"
            cmd_obj.force = "true"
            '''
            Step1 : Prepare Host For Maintenance
            '''
            host_maint_cmd = prepareHostForMaintenance.prepareHostForMaintenanceCmd(
            )
            host_maint_cmd.id = cmd_obj.id
            host_maint_resp = self.__apiClient.prepareHostForMaintenance(
                host_maint_cmd)
            if host_maint_resp:
                '''
                Step2 : List Hosts for Resource State
                '''
                list_host_cmd = listHosts.listHostsCmd()
                list_host_cmd.id = cmd_obj.id
                retries = 3
                for i in xrange(retries):
                    list_host_resp = self.__apiClient.listHosts(list_host_cmd)
                    if list_host_resp and (list_host_resp[0].resourcestate
                                           == 'Maintenance'):
                        break
                    sleep(30)
        if cmd_name.lower() == "deletestoragepoolcmd":
            cmd_obj.forced = "true"
            store_maint_cmd = enableStorageMaintenance.enableStorageMaintenanceCmd(
            )
            store_maint_cmd.id = cmd_obj.id
            store_maint_resp = self.__apiClient.enableStorageMaintenance(
                store_maint_cmd)
            if store_maint_resp:
                list_store_cmd = listStoragePools.listStoragePoolsCmd()
                list_store_cmd.id = cmd_obj.id
                retries = 3
                for i in xrange(retries):
                    store_maint_resp = self.__apiClient.listStoragePools(
                        list_store_cmd)
                    if store_maint_resp and (store_maint_resp[0].state
                                             == 'Maintenance'):
                        break
                    sleep(30)
        return cmd_obj

    def __setClient(self):
        self.__apiClient = self.__test_client.getApiClient()

    def __cleanEntries(self):
        """
        @Name : __cleanAndEntries
        @Description: Cleans up the created DC in order of creation
        """
        try:
            ret = FAILED
            if "order" in self.__cfg.keys() and len(self.__cfg["order"]):
                self.__cfg["order"].reverse()
            self.__logger.info("=== Clean Up Entries: %s ===" % self.__cfg)
            for type in self.__cfg["order"]:
                self.__logger.info("=== CleanUp Started For Type: %s ===" %
                                   type)
                if type:
                    temp_ids = self.__cfg[type]
                    ids = [items for items in temp_ids if items]
                    for id in ids:
                        del_mod = "delete" + type
                        del_cmd = getattr(globals()[del_mod], del_mod + "Cmd")
                        del_cmd_obj = del_cmd()
                        del_cmd_obj.id = id
                        del_cmd_obj = self.__deleteCmds(
                            del_mod + "Cmd", del_cmd_obj)
                        del_func = getattr(self.__apiClient, del_mod)
                        del_cmd_resp = del_func(del_cmd_obj)
                        if del_cmd_resp:
                            self.__logger.error(
                                "=== %s CleanUp Failed. ID: %s ===" %
                                (type, id))
                        else:
                            self.__logger.info(
                                "=== %s CleanUp Successful. ID : %s ===" %
                                (type, id))
            ret = SUCCESS
        except Exception as e:
            self.__logger.exception("=== Clean Up Entries failed: %s ===" % e)
        finally:
            return ret

    def removeDataCenter(self):
        """
        @Name : removeDataCenter
        @Desc : Removes the Data Center provided by Configuration
                If Input dc file configuration is None, uses the cfg provided
                else uses the dc file to get the configuration
        """
        try:
            self.__setClient()
            self.__logger.info("=== DeployDC: CleanUp Started ===")
            ret = self.__cleanEntries()
        except Exception as e:
            self.__logger.exception("=== DeployDC: CleanUp failed: %s ===" % e)
        finally:
            return ret
Exemple #8
0
class DeployDataCenters(object):
    """
    @Desc : Deploys the Data Center with information provided.
            Once the Deployment is successful, it will export
            the DataCenter settings to an obj file
            ( can be used if wanted to delete the created DC)
    """
    def __init__(self, test_client, cfg):
        self.__test_client = test_client
        self.__config = cfg
        self.__logger = MarvinLog('marvin').get_logger()
        self.__apiClient = None
        self.__cleanUp = {}

    def __persistDcConfig(self):
        try:
            ts = strftime("%b_%d_%Y_%H_%M_%S", localtime())
            dc_file_path = "dc_entries_" + str(ts) + ".obj"
            file_to_write = open(dc_file_path, 'w')
            if file_to_write:
                pickle.dump(self.__cleanUp, file_to_write)
                self.__logger.info(
                    "=== Data Center Settings are dumped to %s ===" %
                    dc_file_path)
        except Exception as e:
            self.__logger.exception(
                "=== Persisting DataCenter config failed: %s ===" % e)

    def __cleanAndExit(self):
        try:
            self.__logger.info(
                "=== deploy dc failed, so cleaning the created entries ===")
            if not test_data.get("deleteDC", None):
                self.__logger.info(
                    "=== Deploy DC Clean Up flag not set. So, exiting ===")
                exit(1)
            self.__logger.info("=== Deploy DC Failed, So Cleaning to Exit ===")
            remove_dc_obj = DeleteDataCenters(self.__test_client,
                                              self.__cleanUp)
            if remove_dc_obj:
                if remove_dc_obj.removeDataCenter() == FAILED:
                    self.__logger.error("=== Removing DataCenter Failed ===")
                else:
                    self.__logger.info(
                        "=== Removing DataCenter Successful ===")
        except Exception as e:
            self.__logger.exception("=== Clean up failed: %s ===" % e)
        finally:
            exit(1)

    def __addToCleanUp(self, type, id):
        if type not in self.__cleanUp.keys():
            self.__cleanUp[type] = []
        self.__cleanUp[type].append(id)
        if "order" not in self.__cleanUp.keys():
            self.__cleanUp["order"] = []
        if type not in self.__cleanUp["order"]:
            self.__cleanUp["order"].append(type)

    def addHosts(self, hosts, zoneId, podId, clusterId, hypervisor):
        if hosts is None:
            self.__logger.warn("=== Invalid Hosts Information ===")
            return
        failed_cnt = 0
        for host in hosts:
            try:
                hostcmd = addHost.addHostCmd()
                hostcmd.clusterid = clusterId
                hostcmd.hosttags = host.hosttags
                hostcmd.hypervisor = host.hypervisor
                hostcmd.password = host.password
                hostcmd.podid = podId
                hostcmd.url = host.url
                hostcmd.username = host.username
                hostcmd.zoneid = zoneId
                hostcmd.hypervisor = hypervisor
                ret = self.__apiClient.addHost(hostcmd)
                if ret:
                    self.__logger.info("=== Add Host Successful ===")
                    self.__addToCleanUp("Host", ret[0].id)
            except Exception as e:
                failed_cnt += 1
                self.__logger.exception("=== Adding Host (%s) Failed: %s ===" %
                                        (str(host.url), e))
                if failed_cnt == len(hosts):
                    self.__cleanAndExit()

    def createClusters(self, clusters, zoneId, podId):
        try:
            if clusters is None:
                return

            for cluster in clusters:
                clustercmd = addCluster.addClusterCmd()
                clustercmd.clustername = cluster.clustername
                clustercmd.clustertype = cluster.clustertype
                clustercmd.hypervisor = cluster.hypervisor
                clustercmd.password = cluster.password
                clustercmd.podid = podId
                clustercmd.url = cluster.url
                clustercmd.username = cluster.username
                clustercmd.zoneid = zoneId
                clusterresponse = self.__apiClient.addCluster(clustercmd)
                if clusterresponse[0].id:
                    clusterId = clusterresponse[0].id
                    self.__logger.debug(
                        "Cluster Name : %s Id : %s Created Successfully" %
                        (str(cluster.clustername), str(clusterId)))
                    self.__addToCleanUp("Cluster", clusterId)
                self.addHosts(cluster.hosts, zoneId, podId, clusterId,
                              cluster.hypervisor)
                self.waitForHost(zoneId, clusterId)
                self.createPrimaryStorages(cluster.primaryStorages, zoneId,
                                           podId, clusterId)
        except Exception as e:
            self.__logger.exception("=== Cluster %s Creation Failed: %s ===" %
                                    (str(cluster.clustername), e))
            self.__cleanAndExit()

    def waitForHost(self, zoneId, clusterId):
        """
        Wait for the hosts in the zoneid, clusterid to be up
        2 retries with 30s delay
        """
        try:
            retry, timeout = 2, 30
            cmd = listHosts.listHostsCmd()
            cmd.clusterid, cmd.zoneid = clusterId, zoneId
            hosts = self.__apiClient.listHosts(cmd)
            while retry != 0:
                for host in hosts:
                    if host.state != 'Up':
                        break
                sleep(timeout)
                retry -= 1
        except Exception as e:
            self.__logger.exception("=== List Hosts Failed: %s ===" % e)
            self.__cleanAndExit()

    def createPrimaryStorages(self,
                              primaryStorages,
                              zoneId,
                              podId=None,
                              clusterId=None):
        try:
            if primaryStorages is None:
                return
            for primary in primaryStorages:
                primarycmd = createStoragePool.createStoragePoolCmd()
                if primary.details:
                    for key, value in vars(primary.details).iteritems():
                        primarycmd.details.append({key: value})
                primarycmd.name = primary.name

                primarycmd.tags = primary.tags
                primarycmd.url = primary.url
                if primary.scope == 'zone' or clusterId is None:
                    primarycmd.scope = 'zone'
                    primarycmd.hypervisor = primary.hypervisor
                else:
                    primarycmd.podid = podId
                    primarycmd.clusterid = clusterId
                primarycmd.zoneid = zoneId

                ret = self.__apiClient.createStoragePool(primarycmd)
                if ret.id:
                    self.__logger.info(
                        "=== Creating Storage Pool Successful ===")
                    self.__addToCleanUp("StoragePool", ret.id)
        except Exception as e:
            self.__logger.exception("=== Create Storage Pool Failed: %s ===" %
                                    e)
            self.__cleanAndExit()

    def createPods(self, pods, zoneId, networkId=None):
        try:
            if pods is None:
                return
            for pod in pods:
                createpod = createPod.createPodCmd()
                createpod.name = pod.name
                createpod.gateway = pod.gateway
                createpod.netmask = pod.netmask
                createpod.startip = pod.startip
                createpod.endip = pod.endip
                createpod.zoneid = zoneId
                createpodResponse = self.__apiClient.createPod(createpod)
                if createpodResponse.id:
                    podId = createpodResponse.id
                    self.__logger.debug(
                        "Pod Name : %s Id : %s Created Successfully" %
                        (str(pod.name), str(podId)))
                    self.__addToCleanUp("Pod", podId)
                if pod.guestIpRanges is not None and networkId is not None:
                    self.createVlanIpRanges("Basic", pod.guestIpRanges, zoneId,
                                            podId, networkId)
                self.createClusters(pod.clusters, zoneId, podId)
        except Exception as e:
            self.__logger.exception("=== Pod: %s Creation Failed: %s ===" %
                                    (str(pod.name), e))
            self.__cleanAndExit()

    def createVlanIpRanges(self,
                           mode,
                           ipranges,
                           zoneId,
                           podId=None,
                           networkId=None,
                           forvirtualnetwork=None):
        try:
            if ipranges is None:
                return
            for iprange in ipranges:
                vlanipcmd = createVlanIpRange.createVlanIpRangeCmd()
                vlanipcmd.account = iprange.account
                vlanipcmd.domainid = iprange.domainid
                vlanipcmd.endip = iprange.endip
                vlanipcmd.gateway = iprange.gateway
                vlanipcmd.netmask = iprange.netmask
                vlanipcmd.networkid = networkId
                vlanipcmd.podid = podId
                vlanipcmd.startip = iprange.startip
                vlanipcmd.zoneid = zoneId
                vlanipcmd.vlan = iprange.vlan
                if mode == "Basic":
                    if forvirtualnetwork:
                        vlanipcmd.forvirtualnetwork = "true"
                    else:
                        vlanipcmd.forvirtualnetwork = "false"
                else:
                    vlanipcmd.forvirtualnetwork = "true"
                ret = self.__apiClient.createVlanIpRange(vlanipcmd)
                if ret.id:
                    self.__logger.info(
                        "=== Creating Vlan Ip Range Successful ===")
                    self.__addToCleanUp("VlanIpRange", ret.id)
        except Exception as e:
            self.__logger.exception("=== Create Vlan Ip Range Failed: %s ===" %
                                    e)
            self.__cleanAndExit()

    def createSecondaryStorages(self, secondaryStorages, zoneId):
        try:
            if secondaryStorages is None:
                return
            for secondary in secondaryStorages:
                secondarycmd = addImageStore.addImageStoreCmd()
                secondarycmd.url = secondary.url
                secondarycmd.provider = secondary.provider
                secondarycmd.details = []

                if secondarycmd.provider.lower() in ("smb"):
                    for key, value in vars(secondary.details).iteritems():
                        secondarycmd.details.append({
                            'key': key,
                            'value': value
                        })
                if secondarycmd.provider.lower() in ("nfs", "smb"):
                    secondarycmd.zoneid = zoneId
                ret = self.__apiClient.addImageStore(secondarycmd)
                if ret.id:
                    self.__logger.info("=== Add Image Store Successful ===")
                    self.__addToCleanUp("ImageStore", ret.id)
        except Exception as e:
            self.__logger.exception("=== Add Image Store Failed: %s ===" % e)
            self.__cleanAndExit()

    def createCacheStorages(self, cacheStorages, zoneId):
        try:
            if cacheStorages is None:
                return
            for cache in cacheStorages:
                cachecmd = createSecondaryStagingStore.createSecondaryStagingStoreCmd(
                )
                cachecmd.url = cache.url
                cachecmd.provider = cache.provider
                cachecmd.zoneid = zoneId
                cachecmd.details = []

                if cache.details:
                    for key, value in vars(cache.details).iteritems():
                        cachecmd.details.append({'key': key, 'value': value})
                ret = self.__apiClient.createSecondaryStagingStore(cachecmd)
                if ret.id:
                    self.__logger.info(
                        "=== Creating Secondary StagingStore Successful ===")
                    self.__addToCleanUp("SecondaryStagingStore", ret.id)
        except Exception as e:
            self.__logger.exception(
                "=== Creating Secondary Staging Storage Failed: %s ===" % e)
            self.__cleanAndExit()

    def createNetworks(self, networks, zoneId):
        try:
            if networks is None:
                return
            for network in networks:
                networkcmd = createNetwork.createNetworkCmd()
                networkcmd.displaytext = network.displaytext
                networkcmd.name = network.name
                networkcmd.networkofferingid = network.networkofferingid
                networkcmd.zoneid = zoneId

                ipranges = network.ipranges
                if ipranges:
                    iprange = ipranges.pop()
                    networkcmd.startip = iprange.startip
                    networkcmd.endip = iprange.endip
                    networkcmd.gateway = iprange.gateway
                    networkcmd.netmask = iprange.netmask
                networkcmdresponse = self.__apiClient.createNetwork(networkcmd)
                if networkcmdresponse.id:
                    networkId = networkcmdresponse.id
                    self.__logger.info(
                        "=== Creating Network Name : %s Id : %s Successful ==="
                        % (str(network.name), str(networkId)))
                    self.__addToCleanUp("Network", networkId)
                    return networkId
        except Exception as e:
            self.__logger.exception(
                "=== Network : %s Creation Failed: %s ===" %
                (str(network.name), e))
            self.__cleanAndExit()

    def createPhysicalNetwork(self, net, zoneid):
        try:
            phynet = createPhysicalNetwork.createPhysicalNetworkCmd()
            phynet.zoneid = zoneid
            phynet.name = net.name
            phynet.isolationmethods = net.isolationmethods
            phynetwrk = self.__apiClient.createPhysicalNetwork(phynet)
            if phynetwrk.id:
                self.__logger.info(
                    "=== Creating Physical Network Name : %s Id : %s Successful ==="
                    % (str(phynet.name), str(phynetwrk.id)))
                self.__addToCleanUp("PhysicalNetwork", phynetwrk.id)
            self.addTrafficTypes(phynetwrk.id, net.traffictypes)
            return phynetwrk
        except Exception as e:
            self.__logger.exception(
                "=== Physical Network Creation Failed: %s ===" % e)
            self.__cleanAndExit()

    def updatePhysicalNetwork(self, networkid, state="Enabled", vlan=None):
        try:
            upnet = updatePhysicalNetwork.updatePhysicalNetworkCmd()
            upnet.id = networkid
            upnet.state = state
            if vlan:
                upnet.vlan = vlan
            ret = self.__apiClient.updatePhysicalNetwork(upnet)
            return ret
        except Exception as e:
            self.__logger.exception(
                "=== Update Physical Network Failed: %s ===" % e)
            self.__cleanAndExit()

    def enableProvider(self, provider_id):
        try:
            upnetprov = \
                updateNetworkServiceProvider.updateNetworkServiceProviderCmd()
            upnetprov.id = provider_id
            upnetprov.state = "Enabled"
            ret = self.__apiClient.updateNetworkServiceProvider(upnetprov)
            if ret.id:
                self.__logger.info(
                    "=== Update Network Service Provider Successfull ===")
        except Exception as e:
            self.__logger.exception(
                "=== Update Network Service Provider Failed: %s ===" % e)
            self.__cleanAndExit()

    def configureProviders(self, phynetwrk, providers):
        """
        We will enable the virtualrouter elements for all zones.
        """
        try:
            for provider in providers:
                pnetprov = listNetworkServiceProviders.listNetworkServiceProvidersCmd(
                )
                pnetprov.physicalnetworkid = phynetwrk.id
                pnetprov.state = "Disabled"
                pnetprov.name = provider.name
                pnetprovres = self.__apiClient.listNetworkServiceProviders(
                    pnetprov)
                if pnetprovres and len(pnetprovres) > 0:
                    if provider.name == 'VirtualRouter' or provider.name == 'VpcVirtualRouter':
                        vrprov = listVirtualRouterElements.listVirtualRouterElementsCmd(
                        )
                        vrprov.nspid = pnetprovres[0].id
                        vrprovresponse = self.__apiClient.listVirtualRouterElements(
                            vrprov)
                        vrprovid = vrprovresponse[0].id
                        vrconfig = configureVirtualRouterElement.configureVirtualRouterElementCmd(
                        )
                        vrconfig.enabled = "true"
                        vrconfig.id = vrprovid
                        self.__apiClient.configureVirtualRouterElement(
                            vrconfig)
                        self.enableProvider(pnetprovres[0].id)
                    elif provider.name == 'InternalLbVm':
                        internallbprov = listInternalLoadBalancerElements.listInternalLoadBalancerElementsCmd(
                        )
                        internallbprov.nspid = pnetprovres[0].id
                        internallbresponse = self.__apiClient.listInternalLoadBalancerElements(
                            internallbprov)
                        internallbid = internallbresponse[0].id
                        internallbconfig = configureInternalLoadBalancerElement.configureInternalLoadBalancerElementCmd(
                        )
                        internallbconfig.enabled = "true"
                        internallbconfig.id = internallbid
                        self.__apiClient.configureInternalLoadBalancerElement(
                            internallbconfig)
                        self.enableProvider(pnetprovres[0].id)
                    elif provider.name == 'SecurityGroupProvider':
                        self.enableProvider(pnetprovres[0].id)
                elif provider.name in ['NiciraNvp']:
                    netprov = addNetworkServiceProvider.addNetworkServiceProviderCmd(
                    )
                    netprov.name = provider.name
                    netprov.physicalnetworkid = phynetwrk.id
                    result = self.__apiClient.addNetworkServiceProvider(
                        netprov)
                    if result.id:
                        self.__logger.info(
                            "=== AddNetworkServiceProvider Successful ===")
                        self.__addToCleanUp("NetworkServiceProvider",
                                            result.id)
                    if provider.devices is not None:
                        for device in provider.devices:
                            if provider.name == 'NiciraNvp':
                                cmd = addNiciraNvpDevice.addNiciraNvpDeviceCmd(
                                )
                                cmd.hostname = device.hostname
                                cmd.username = device.username
                                cmd.password = device.password
                                cmd.transportzoneuuid = device.transportzoneuuid
                                cmd.physicalnetworkid = phynetwrk.id
                                ret = self.__apiClient.addNiciraNvpDevice(cmd)
                                self.__logger.info(
                                    "=== Add NiciraNvp Successful ===")
                                self.__addToCleanUp("NiciraNvp", ret.id)
                            else:
                                raise InvalidParameterException(
                                    "Device %s doesn't match any know provider type"
                                    % device)
                    self.enableProvider(result.id)
        except Exception as e:
            self.__logger.exception(
                "=== List Network Service Providers Failed: %s ===" % e)
            self.__cleanAndExit()

    def addTrafficTypes(self, physical_network_id, traffictypes):
        [
            self.addTrafficType(physical_network_id, traffic_type)
            for traffic_type in traffictypes
        ]

    def addTrafficType(self, physical_network_id, traffictype):
        try:
            traffic_type = addTrafficType.addTrafficTypeCmd()
            traffic_type.physicalnetworkid = physical_network_id
            traffic_type.traffictype = traffictype.typ
            traffic_type.kvmnetworklabel = traffictype.kvm \
                if traffictype.kvm is not None else None
            traffic_type.xennetworklabel = traffictype.xen \
                if traffictype.xen is not None else None
            traffic_type.simulatorlabel = traffictype.simulator \
                if traffictype.simulator is not None else None
            ret = self.__apiClient.addTrafficType(traffic_type)
            if ret.id:
                self.__logger.info("=== Add TrafficType Successful ===")
                self.__addToCleanUp("TrafficType", ret.id)
                return ret
        except Exception as e:
            self.__logger.exception("=== Add TrafficType Failed: %s ===" % e)
            self.__cleanAndExit()

    def enableZone(self, zoneid, allocation_state="Enabled"):
        try:
            zoneCmd = updateZone.updateZoneCmd()
            zoneCmd.id = zoneid
            zoneCmd.allocationstate = allocation_state
            ret = self.__apiClient.updateZone(zoneCmd)
            if ret.id:
                self.__logger.info("=== Enable Zone Successful ===")
                return ret
        except Exception as e:
            self.__logger.exception("=== Enable Zone Failed: %s ===" % e)
            self.__cleanAndExit()

    def updateZoneDetails(self, zoneid, details):
        try:
            zoneCmd = updateZone.updateZoneCmd()
            zoneCmd.id = zoneid
            zoneCmd.details = details
            ret = self.__apiClient.updateZone(zoneCmd)
            if ret.id:
                self.__logger.info("=== Update Zone Successful ===")
                return ret
        except Exception as e:
            self.__logger.exception("=== Update Zone  Failed: %s ===" % e)
            self.__cleanAndExit()

    def createZone(self, zone, rec=0):
        try:
            zoneresponse = self.__apiClient.createZone(zone)
            if zoneresponse.id:
                self.__logger.info("=== Create Zone Successful ===")
                self.__logger.debug("Zone Name : %s Id : %s " %
                                    (str(zone.name), str(zoneresponse.id)))
                self.__addToCleanUp("Zone", zoneresponse.id)
                return zoneresponse.id
            else:
                self.__logger.exception("=== Zone : %s Creation Failed ===" %
                                        str(zone.name))
                if not rec:
                    zone.name = zone.name + "_" + random_gen()
                    self.__logger.info(
                        "=== Recreating Zone With New Name : %s ===" %
                        zone.name)
                    return self.createZone(zone, 1)
        except Exception as e:
            self.__logger.exception("=== Create Zone Failed: %s ===" % e)
            return FAILED

    def createZones(self, zones):
        try:
            for zone in zones:
                zonecmd = createZone.createZoneCmd()
                zonecmd.dns1 = zone.dns1
                zonecmd.dns2 = zone.dns2
                zonecmd.internaldns1 = zone.internaldns1
                zonecmd.internaldns2 = zone.internaldns2
                zonecmd.name = zone.name
                zonecmd.securitygroupenabled = zone.securitygroupenabled
                zonecmd.localstorageenabled = zone.localstorageenabled
                zonecmd.networktype = zone.networktype
                zonecmd.domain = zone.domain
                if zone.securitygroupenabled != "true":
                    zonecmd.guestcidraddress = zone.guestcidraddress
                zoneId = self.createZone(zonecmd)
                if zoneId == FAILED:
                    self.__logger.error(
                        "=== Zone: %s Creation Failed. So Exiting ===" %
                        str(zone.name))
                    self.__cleanAndExit()
                for pnet in zone.physical_networks:
                    phynetwrk = self.createPhysicalNetwork(pnet, zoneId)
                    self.configureProviders(phynetwrk, pnet.providers)
                    self.updatePhysicalNetwork(phynetwrk.id,
                                               "Enabled",
                                               vlan=pnet.vlan)
                if zone.networktype == "Basic":
                    listnetworkoffering = listNetworkOfferings.listNetworkOfferingsCmd(
                    )
                    if len(
                            filter(
                                lambda x: x.typ == 'Public',
                                zone.physical_networks[0].traffictypes)) > 0:
                        listnetworkoffering.name = "DefaultSharedNetscalerEIPandELBNetworkOffering"
                    else:
                        listnetworkoffering.name = "DefaultSharedNetworkOfferingWithSGService"
                    if zone.networkofferingname is not None:
                        listnetworkoffering.name = zone.networkofferingname
                    listnetworkofferingresponse = self.__apiClient.listNetworkOfferings(
                        listnetworkoffering)
                    guestntwrk = configGenerator.network()
                    guestntwrk.displaytext = "guestNetworkForBasicZone"
                    guestntwrk.name = "guestNetworkForBasicZone"
                    guestntwrk.zoneid = zoneId
                    guestntwrk.networkofferingid = listnetworkofferingresponse[
                        0].id
                    networkid = self.createNetworks([guestntwrk], zoneId)
                    self.createPods(zone.pods, zoneId, networkid)
                    if self.isEipElbZone(zone):
                        self.createVlanIpRanges(zone.networktype,
                                                zone.ipranges,
                                                zoneId,
                                                forvirtualnetwork=True)
                isPureAdvancedZone = (zone.networktype == "Advanced"
                                      and zone.securitygroupenabled != "true")
                if isPureAdvancedZone:
                    self.createPods(zone.pods, zoneId)
                    self.createVlanIpRanges(zone.networktype, zone.ipranges,
                                            zoneId)
                elif zone.networktype == "Advanced" and zone.securitygroupenabled == "true":
                    listnetworkoffering = listNetworkOfferings.listNetworkOfferingsCmd(
                    )
                    listnetworkoffering.name = "DefaultSharedNetworkOfferingWithSGService"
                    if zone.networkofferingname is not None:
                        listnetworkoffering.name = zone.networkofferingname
                    listnetworkofferingresponse = self.__apiClient.listNetworkOfferings(
                        listnetworkoffering)
                    networkcmd = createNetwork.createNetworkCmd()
                    networkcmd.displaytext = "Shared SG enabled network"
                    networkcmd.name = "Shared SG enabled network"
                    networkcmd.networkofferingid = listnetworkofferingresponse[
                        0].id
                    networkcmd.zoneid = zoneId
                    ipranges = zone.ipranges
                    if ipranges:
                        iprange = ipranges.pop()
                        networkcmd.startip = iprange.startip
                        networkcmd.endip = iprange.endip
                        networkcmd.gateway = iprange.gateway
                        networkcmd.netmask = iprange.netmask
                        networkcmd.vlan = iprange.vlan
                    networkcmdresponse = self.__apiClient.createNetwork(
                        networkcmd)
                    if networkcmdresponse.id:
                        self.__addToCleanUp("Network", networkcmdresponse.id)
                        self.__logger.debug(
                            "create Network Successful. NetworkId : %s " %
                            str(networkcmdresponse.id))
                    self.createPods(zone.pods, zoneId, networkcmdresponse.id)
                '''Note: Swift needs cache storage first'''
                self.createCacheStorages(zone.cacheStorages, zoneId)
                self.createSecondaryStorages(zone.secondaryStorages, zoneId)
                if zone.primaryStorages:
                    self.createPrimaryStorages(zone.primaryStorages, zoneId)
                enabled = getattr(zone, 'enabled', 'True')
                if enabled == 'True' or enabled is None:
                    self.enableZone(zoneId, "Enabled")
                details = getattr(zone, 'details')
                if details is not None:
                    det = [d.__dict__ for d in details]
                    self.updateZoneDetails(zoneId, det)
            return
        except Exception as e:
            self.__logger.exception("=== Create Zones Failed: %e ===" % e)

    def isEipElbZone(self, zone):
        if zone.networktype == "Basic" and len(
                filter(lambda x: x.typ == 'Public',
                       zone.physical_networks[0].traffictypes)) > 0:
            return True
        return False

    def setClient(self):
        """
        @Name : setClient
        @Desc : Sets the API Client retrieved from test client
        """
        self.__apiClient = self.__test_client.getApiClient()

    def updateConfiguration(self, globalCfg):
        try:
            if globalCfg is None or self.__apiClient is None:
                return None
            for config in globalCfg:
                updateCfg = updateConfiguration.updateConfigurationCmd()
                updateCfg.name = config.name
                updateCfg.value = config.value
                ret = self.__apiClient.updateConfiguration(updateCfg)
                if ret.id:
                    self.__logger.info(
                        "=== Update Configuration Successfull ===")
        except Exception as e:
            self.__logger.exception("=== Update Configuration Failed: %s ===" %
                                    e)
            self.__cleanAndExit()

    @staticmethod
    def copyAttributesToCommand(source, command):
        map(
            lambda attr: setattr(command, attr, getattr(source, attr, None)),
            filter(
                lambda attr: not attr.startswith("__") and attr not in
                ["required", "isAsync"], dir(command)))

    def deploy(self):
        try:
            self.__logger.info("=== Deploy DC Started ===")
            '''
            Step1 : Set the Client
            '''
            self.setClient()
            '''
            Step2: Update the Configuration
            '''
            self.updateConfiguration(self.__config.globalConfig)
            '''
            Step3 :Deploy the Zone
            '''
            self.createZones(self.__config.zones)
            '''
            Persist the Configuration to an external file post DC creation
            '''
            self.__persistDcConfig()
            self.__logger.info("=== Deploy DC Successful ===")
            return SUCCESS
        except Exception as e:
            self.__logger.exception("=== Deploy DC Failed: %s ===" % e)
            self.__cleanAndExit()
            return FAILED
class MarvinInit:
    def __init__(self, config_file, deploy_dc_flag=False, test_mod_name="deploydc", zone=None, hypervisor_type=None, halt_on_failure=False):
        self.__configFile = config_file
        self.__deployFlag = deploy_dc_flag
        self.__tcRunLogger = MarvinLog('marvin').get_logger()
        self.__tcRunLogger.info("=== Marvin Init Logging Successful ===")
        self.__testModName = test_mod_name
        self.__testClient = None
        self.__tcResultFile = None
        self.__testDataFilePath = None
        self.__zoneForTests = zone
        self.__parsedConfig = None
        self.__hypervisorType = hypervisor_type
        self.__halt_on_failure = halt_on_failure

    def __parseConfig(self):
        '''
        @Name: __parseConfig
        @Desc : Parses the configuration file passed and assigns
        the parsed configuration
        @Output : SUCCESS or FAILED
        '''
        try:
            if not os.path.isfile(self.__configFile):
                self.__tcRunLogger.error("=== Marvin Parse Config Init Failed ===")
                return FAILED
            self.__parsedConfig = getSetupConfig(self.__configFile)
            self.__tcRunLogger.info("=== Marvin Parse Config Successful ===")
            return SUCCESS
        except Exception as e:
            self.__tcRunLogger.exception("=== Marvin Parse Config Init Failed: %s ===" % e)
            return FAILED

    def getParsedConfig(self):
        return self.__parsedConfig

    def getLogFolderPath(self):
        return self.__logFolderPath

    def getTestClient(self):
        return self.__testClient

    def getLogger(self):
        return self.__tcRunLogger

    def getResultFile(self):
        '''
        @Name : getDebugFile
        @Desc : Creates the result file at a given path.
        @Output : Returns the Result file to be used for writing
                test outputs
        '''
        self.__tcResultFile = open("results.txt", "w")
        return self.__tcResultFile

    def __setHypervisorAndZoneInfo(self):
        '''
        @Name : __setHypervisorAndZoneInfo
        @Desc:  Set the HyperVisor and Zone details;
                default to XenServer
        '''
        try:
            if not self.__hypervisorType:
                if self.__parsedConfig and self.__parsedConfig.zones is not None:
                    for zone in self.__parsedConfig.zones:
                        for pod in zone.pods:
                            if pod is not None:
                                for cluster in pod.clusters:
                                    if cluster is not None and cluster.hypervisor is not None:
                                        self.__hypervisorType = cluster.hypervisor
                                        break
            if not self.__zoneForTests:
                if self.__parsedConfig and self.__parsedConfig.zones is not None:
                    for zone in self.__parsedConfig.zones:
                        self.__zoneForTests = zone.name
                        break
            if not self.__hypervisorType:
                self.__hypervisorType = XEN_SERVER
            return SUCCESS
        except Exception as e:
            self.__tcRunLogger.exception("=== Set Hypervizor and Zone info Failed: %s ===" % e)
            return FAILED

    def init(self):
        '''
        @Name : init
        @Desc :Initializes the marvin by
               1. Parsing the configuration and creating a parsed config
                  structure
               2. Creates a timestamped log folder and provides all logs
                  to be dumped there
               3. Creates the DataCenter based upon configuration provided
        @Output : SUCCESS or FAILED
        '''
        try:
            self.__tcRunLogger.info("=== Marvin Init Started ===")
            if ((self.__parseConfig() != FAILED) and
                    (self.__setHypervisorAndZoneInfo()) and
                    (self.__setTestDataPath() != FAILED) and
                    (self.__createTestClient() != FAILED) and
                    (self.__deployDC() != FAILED)):
                self.__tcRunLogger.info("=== Marvin Init Successful ===")
                return SUCCESS
            self.__tcRunLogger.error("=== Marvin Init Failed ===")
            return FAILED
        except Exception as e:
            self.__tcRunLogger.exception("=== Marvin Init Failed with exception: %s ===" % e)
            return FAILED

    def __createTestClient(self):
        '''
        @Name : __createTestClient
        @Desc : Creates the TestClient during init
                based upon the parameters provided
        @Output: Returns SUCCESS or FAILED
        '''
        try:
            mgt_details = self.__parsedConfig.mgtSvr[0]
            dbsvr_details = self.__parsedConfig.dbSvr
            self.__testClient = CSTestClient(
                mgt_details,
                dbsvr_details,
                test_data_filepath=self.__testDataFilePath,
                zone=self.__zoneForTests,
                hypervisor_type=self.__hypervisorType,
                halt_on_failure=self.__halt_on_failure
            )
            if self.__testClient:
                return self.__testClient.createTestClient()
            return FAILED
        except Exception as e:
            self.__tcRunLogger.exception("=== Marvin Create Test Client Failed: %s ===" % e)
            return FAILED

    def __setTestDataPath(self):
        '''
        @Name : __setTestDataPath
        @Desc : Sets the TestData Path for tests to run
        @Output:Returns SUCCESS or FAILED
        '''
        try:
            if ((self.__parsedConfig.TestData is not None) and
                    (self.__parsedConfig.TestData.Path is not None)):
                self.__testDataFilePath = self.__parsedConfig.TestData.Path
            self.__tcRunLogger.info("=== Marvin Setting TestData Successful ===")
            return SUCCESS
        except Exception as e:
            self.__tcRunLogger.exception("=== Marvin Setting TestData Successful Failed: %s ===" % e)
            return FAILED

    def __deployDC(self):
        '''
        @Name : __deployDC
        @Desc : Deploy the DataCenter and returns accordingly.
        @Output: SUCCESS or FAILED
        '''
        try:
            ret = SUCCESS
            if self.__deployFlag:
                deploy_obj = DeployDataCenters(self.__testClient,
                                               self.__parsedConfig,
                                               self.__tcRunLogger)
                ret = deploy_obj.deploy()
                if ret != SUCCESS:
                    self.__tcRunLogger.error("=== Deploy DC Failed ===")
            return ret
        except Exception as e:
            self.__tcRunLogger.exception("=== Deploy DC Failed with exception: %s ===" % e)
            return FAILED