
            zoneid=self.zone.id,
        )

        self.assertEqual(root_vm_new.state.lower(),
                         "running",
                         "Verify that admin should create new VM")

        # Step 5
        # Deletion of zone should fail if resources are running on the zone
        with self.assertRaises(Exception):
            self.pod.delete(self.apiclient)

        return


class TestDisableEnableHost(cloudstackTestCase):

    @classmethod
    def setUpClass(cls):
        testClient = super(TestDisableEnableHost, cls).getClsTestClient()
        cls.apiclient = testClient.getApiClient()
        cls.testdata = testClient.getParsedTestDataConfig()
        cls.hypervisor = cls.testClient.getHypervisorInfo()

        cls.snapshotSupported = True
        if cls.hypervisor.lower() in ["hyperv", "lxc"]:
            cls.snapshotSupported = False

        # Get Zone, Domain and templates
        cls.domain = get_domain(cls.apiclient)
        cls.zone = get_zone(cls.apiclient, testClient.getZoneForTests())
        cls.pod = get_pod(
            cls.apiclient,
            zone_id=cls.zone.id)

        hostList = Host.list(cls.apiclient, zoneid=cls.zone.id, type="routing")
        clusterList = Cluster.list(cls.apiclient, id=hostList[0].clusterid)
        cls.host = Host(hostList[0].__dict__)
        cls.cluster = Cluster(clusterList[0].__dict__)

        cls.template = get_template(
            cls.apiclient,
            cls.zone.id,
            cls.testdata["ostype"])

        cls._cleanup = []
        cls.disabledHosts = []

        try:
            cls.service_offering = ServiceOffering.create(
                cls.apiclient,
                cls.testdata["service_offering"],
            )
            cls._cleanup.append(cls.service_offering)

            cls.disk_offering = DiskOffering.create(
                cls.apiclient,
                cls.testdata["disk_offering"],
            )
            cls._cleanup.append(cls.disk_offering)

            # Create an account
            cls.account = Account.create(
                cls.apiclient,
                cls.testdata["account"],
                domainid=cls.domain.id
            )
            cls._cleanup.append(cls.account)

            # Create root admin account

            cls.admin_account = Account.create(
                cls.apiclient,
                cls.testdata["account2"],
                admin=True

            )
            cls._cleanup.append(cls.admin_account)

            # Create user api client of the account
            cls.userapiclient = testClient.getUserApiClient(
                UserName=cls.account.name,
                DomainName=cls.account.domain
            )

        except Exception as e:
            cls.tearDownClass()
            raise e
        return

    @classmethod
    def tearDownClass(cls):
        try:
            for hostid in cls.disabledHosts:
                hosts = Host.list(cls.apiclient,
                                  id=hostid)
                assert validateList(hosts)[0] == PASS, "hosts\
                        list validation failed"
                if hosts[0].resourcestate.lower() == DISABLED.lower():
                    cmd = updateHost.updateHostCmd()
                    cmd.id = hostid
                    cmd.resourcestate = ENABLED
                    cmd.allocationstate = ENABLE
                    cls.apiclient.updateHost(cmd)
            cleanup_resources(cls.apiclient, cls._cleanup)
        except Exception as e:
            raise Exception("Warning: Exception during cleanup : %s" % e)

    def setUp(self):
        self.apiclient = self.testClient.getApiClient()
        self.dbclient = self.testClient.getDbConnection()
        self.cleanup = []

    def tearDown(self):
        try:
            cleanup_resources(self.apiclient, self.cleanup)
        except Exception as e:
            raise Exception("Warning: Exception during cleanup : %s" % e)
        return

    @attr(tags=["advanced", "basic"], required_hardware="true")
    def test_01_disable_enable_host(self):
        """disable enable host
            1. Disable host and verify following things:
                For admin user:
                    1. Should be able to  stop exsiting vms but can not start.
                    2. Should not be able to  deploy new vm,
                       and create snapshot on the same host
                For Non-admin user:
                    1. Should not be able to stop exsiting vms but
                       cant not start
                    2. Should not be create to deploy new vm,
                    snapshot on the same host
            2. Enable the above disabled host and verify that:
                -All users should be create to deploy new vm,
                snapshot on the same host
            3. Try to reconnect the host :
                -Host should get reconnected successfully
        """
        # Step 1
        vm_root = VirtualMachine.create(
            self.apiclient,
            self.testdata["small"],
            templateid=self.template.id,
            accountid=self.admin_account.name,
            domainid=self.admin_account.domainid,
            serviceofferingid=self.service_offering.id,
            zoneid=self.zone.id,
        )
        hostid = vm_root.hostid

        vm_user = VirtualMachine.create(
            self.userapiclient,
            self.testdata["small"],
            templateid=self.template.id,
            accountid=self.account.name,
            domainid=self.account.domainid,
            serviceofferingid=self.service_offering.id,
            zoneid=self.zone.id,
        )

        cmd = updateHost.updateHostCmd()
        cmd.id = hostid
        cmd.resourcestate = DISABLED
        cmd.allocationstate = DISABLE
        self.apiclient.updateHost(cmd)
        self.disabledHosts.append(hostid)

        hostList = Host.list(self.apiclient, id=hostid)

        self.assertEqual(hostList[0].resourcestate,
                         DISABLED,
                         "Check if the host is in disabled state"
                         )
        # Verify the exsisting vms should be running
        self.assertEqual(vm_user.state.lower(),
                         "running",
                         "Verify that the user vm is running")

        self.assertEqual(vm_root.state.lower(),
                         "running",
                         "Verify that the root vm is running")

        vm_root.stop(self.apiclient)
        vm_user.stop(self.apiclient)
        root_state = self.dbclient.execute(
            "select state from vm_instance where name='%s'" %
            vm_root.name)[0][0]
        user_state = self.dbclient.execute(
            "select state from vm_instance where name='%s'" %
            vm_user.name)[0][0]

        self.assertEqual(root_state.lower(),
                         "stopped",
                         "verify that vm should stop")

        self.assertEqual(user_state.lower(),
                         "stopped",
                         "verify that vm should stop")

        with self.assertRaises(Exception):
            VirtualMachine.create(
                self.apiclient,
                self.testdata["small"],
                templateid=self.template.id,
                accountid=self.admin_account.name,
                domainid=self.admin_account.domainid,
                serviceofferingid=self.service_offering.id,
                zoneid=self.zone.id,
                hostid=hostid)

        root_volume = list_volumes(
            self.apiclient,
            virtualmachineid=vm_root.id,
            type='ROOT',
            listall=True
        )

        self.assertEqual(
            validateList(root_volume)[0],
            PASS,
            "list root volume response is empty for volume id %s" %
            