def __init__(self):
        py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
        cloudshell_data_retriever_service = CloudshellDataRetrieverService()
        resource_connection_details_retriever = ResourceConnectionDetailsRetriever(
            helpers, cloudshell_data_retriever_service)
        resource_remover = CloudshellResourceRemover(helpers)
        command_wrapper = CommandWrapper(getLogger, py_vmomi_service)
        template_deployer = VirtualMachineDeployer(py_vmomi_service,
                                                   generate_unique_name)

        deploy_from_template_command = DeployFromTemplateCommand(
            template_deployer)

        resource_model_parser = ResourceModelParser()
        vc_model_retriever = VCenterDataModelRetriever(helpers,
                                                       resource_model_parser)
        vc_data_model = vc_model_retriever.get_vcenter_data_model()

        #vnic_to_network_mapper = VnicToNetworkMapper(name_generator, vc_data_model.default_network)
        port_group_name_generator = DvPortGroupNameGenerator()
        vnic_to_network_mapper = VnicToNetworkMapper(port_group_name_generator)

        # Virtual Switch Connect
        synchronous_task_waiter = SynchronousTaskWaiter()
        dv_port_group_creator = DvPortGroupCreator(py_vmomi_service,
                                                   synchronous_task_waiter)
        virtual_machine_port_group_configurer = VirtualMachinePortGroupConfigurer(
            py_vmomi_service, synchronous_task_waiter, vnic_to_network_mapper,
            VNicService())
        virtual_switch_to_machine_connector = VirtualSwitchToMachineConnector(
            dv_port_group_creator, virtual_machine_port_group_configurer)

        virtual_switch_connect_command = VirtualSwitchConnectCommand(
            py_vmomi_service, virtual_switch_to_machine_connector,
            DvPortGroupNameGenerator(), VlanSpecFactory(), VLanIdRangeParser(),
            getLogger('VirtualSwitchConnectCommand'))

        # Virtual Switch Revoke
        virtual_switch_disconnect_command = \
            VirtualSwitchToMachineDisconnectCommand(py_vmomi_service,
                                                    virtual_machine_port_group_configurer,
                                                    vc_data_model.default_network)

        destroy_virtual_machine_command = DestroyVirtualMachineCommand(
            py_vmomi_service, resource_remover,
            virtual_switch_disconnect_command)
        # Power Command
        vm_power_management_command = VirtualMachinePowerManagementCommand(
            py_vmomi_service, synchronous_task_waiter)
        # Refresh IP command
        refresh_ip_command = RefreshIpCommand(
            py_vmomi_service, cloudshell_data_retriever_service, helpers,
            resource_model_parser)

        self.commandExecuterService = CommandExecuterService(
            jsonpickle, helpers, command_wrapper,
            resource_connection_details_retriever, vc_data_model,
            destroy_virtual_machine_command, deploy_from_template_command,
            virtual_switch_connect_command, virtual_switch_disconnect_command,
            vm_power_management_command, refresh_ip_command)
    def __init__(self):
        update_environment()

        self.py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
        self.data_retriever_service = CloudshellDataRetrieverService()
        self.connection_details_retriever = ResourceConnectionDetailsRetriever(
            self.data_retriever_service)
        self.si = None
    def setUp(self):
        self._si = None
        self.virtual_machine_path = 'SergiiT'
        self.virtual_machine_name = 'JustTestNeedToBeRemoved'
        self.vm_uuid = "422254d5-5226-946e-26fb-60c21898b731"

        self.vcenter_name = "QualiSB"
        self.port_group_path = 'QualiSB'
        self.dv_switch_path = 'QualiSB'
        self.network_path = 'QualiSB'

        self.dv_switch_name = 'dvSwitch-SergiiT'
        self.dv_port_group_name = 'aa-dvPortGroup3B'

        self.network = Mock()
        self.network.key = "network-key"
        self.network.config.distributedVirtualSwitch.uuid = "422254d5-5226-946e-26fb-60c21898b73f"
        self.py_vmomi_service = Mock()

        self.vm = Mock()
        self.vm.config.hardware = Mock()
        self.vnic = Mock(spec=vim.vm.device.VirtualEthernetCard)
        self.vnic.deviceInfo = Mock()
        self.vm.config.hardware.device = [self.vnic]

        self.py_vmomi_service.find_by_uuid = lambda a, b, c: self.vm
        self.py_vmomi_service.find_network_by_name = Mock(
            return_value=self.network)

        self.synchronous_task_waiter = Mock()
        self.synchronous_task_waiter.wait_for_task = Mock(
            return_value="TASK OK")
        self.si = Mock()

        resource_model_parser = ResourceModelParser()
        #vc_model_retriever = VCenterDataModelRetriever(helpers, resource_model_parser, cloudshell_data_retriever_service)
        #vc_data_model = vc_model_retriever.get_vcenter_data_model()
        vc_data_model = Mock()
        name_generator = generate_unique_name
        #vnic_to_network_mapper = VnicToNetworkMapper(name_generator, vc_data_model.default_network)
        vnic_to_network_mapper = VnicToNetworkMapper(name_generator)

        helpers = Mock()
        cs_retriever_service = Mock()
        session = Mock()
        resource_context = Mock()
        connection_details = Mock()

        helpers.get_resource_context_details = Mock(
            return_value=resource_context)
        helpers.get_api_session = Mock(return_value=session)
        cs_retriever_service.getVCenterConnectionDetails = Mock(
            return_value=connection_details)
        retriever = ResourceConnectionDetailsRetriever(helpers,
                                                       cs_retriever_service)

        self.data_retriever_service = CloudshellDataRetrieverService()
        self.connection_details_retriever = ResourceConnectionDetailsRetriever(
            helpers,
            self.data_retriever_service,
        )
        self.configurer = VirtualMachinePortGroupConfigurer(
            self.py_vmomi_service, self.synchronous_task_waiter,
            vnic_to_network_mapper, Mock())

        # pyvmomi_service, synchronous_task_waiter, vnic_to_network_mapper, vnic_common

        self.creator = DvPortGroupCreator(self.py_vmomi_service,
                                          self.synchronous_task_waiter)
        self.connector = VirtualSwitchToMachineConnector(
            self.creator, self.configurer)
def main():
    cs = CloudshellDataRetrieverService()
    dsd = DeploymentServiceDriver(cs)
    dsd.execute()
 def setUp(self):
     self.csRetrieverService = CloudshellDataRetrieverService()
class test_cloudshellDataRetrieverService(unittest.TestCase):
    def setUp(self):
        self.csRetrieverService = CloudshellDataRetrieverService()

    def tearDown(self):
        pass

    def test_getVCenterTemplateAttributeData(self):
        # Arrange
        attributes = {"vCenter Template": "vCenter/Alex/test"}
        resource_attributes = Mock(attributes=attributes)

        # Act
        result = self.csRetrieverService.getVCenterTemplateAttributeData(resource_attributes)

        # Assert
        self.assertEquals(result.vCenter_resource_name, "vCenter")
        self.assertEqual(result.vm_folder, "Alex")
        self.assertEqual(result.template_name, "test")

    def test_getPowerStateAttributeData_Value_Is_True(self):
        # Arrange
        attributes = {"VM Power State": "True"}
        resource_attributes = Mock(attributes=attributes)

        # Act
        result = self.csRetrieverService.getPowerStateAttributeData(resource_attributes)

        # Assert
        self.assertEquals(result, True)

    def test_getPowerStateAttributeData_Value_Is_False(self):
        # Arrange
        attributes = {"VM Power State": "False"}
        resource_attributes = Mock(attributes=attributes)

        # Act
        result = self.csRetrieverService.getPowerStateAttributeData(resource_attributes)

        # Assert
        self.assertEquals(result, False)

    def test_getVMClusterAttributeData(self):
        # Arrange
        attributes = {"VM Cluster": "cluster1/resourcepool1"}
        resource_attributes = Mock(attributes=attributes)

        # Act
        result = self.csRetrieverService.getVMClusterAttributeData(resource_attributes)

        # Assert
        self.assertEquals(result.cluster_name, "cluster1")
        self.assertEquals(result.resource_pool, "resourcepool1")

    def test_getVMClusterAttributeData_Empty_Attribute(self):
        # Arrange
        attributes = {"VM Cluster": ""}
        resource_attributes = Mock(attributes=attributes)

        # Act
        result = self.csRetrieverService.getVMClusterAttributeData(resource_attributes)

        # Assert
        self.assertEquals(result.cluster_name, None)
        self.assertEquals(result.resource_pool, None)

    
    def test_getVMStorageAttributeData(self):
        # Arrange
        attributes = {"VM Storage": "my storage"}
        resource_attributes = Mock(attributes=attributes)

        # Act
        datastore_name = self.csRetrieverService.getVMStorageAttributeData(resource_attributes)

        # Assert
        self.assertEquals(datastore_name, "my storage")

    def test_getVMStorageAttributeData_Empty_Attribute(self):
        # Arrange
        attributes = {"VM Storage": ""}
        resource_attributes = Mock(attributes=attributes)

        # Act
        datastore_name = self.csRetrieverService.getVMStorageAttributeData(resource_attributes)

        # Arrange
        self.assertEquals(datastore_name, None)

    def test_getVCenterConnectionDetails(self):
        # Arrange
        password = "******"
        session = Mock()
        session.DecryptPassword = Mock(return_value=Mock(Value=password))

        attributes = {"User": "******", "Password": password}
        vCenter_resource_details = Mock()
        vCenter_resource_details.attributes = attributes
        vCenter_resource_details.address = 'vCenterIP'


         # Act
        conn_details = self.csRetrieverService.getVCenterConnectionDetails(session, vCenter_resource_details)

        # Assert
        self.assertEquals(conn_details.username, "user1")
        self.assertEquals(conn_details.password, "pass1")
        self.assertEquals(conn_details.host, "vCenterIP")

    def test_getVCenterInventoryPathAttributeData(self):
        # Arrange
        attributes = {"vCenter Inventory Path": "vCenter/Alex"}
        resource_attributes = Mock(attributes=attributes)

        # Act
        result = self.csRetrieverService.getVCenterInventoryPathAttributeData(resource_attributes)

        # Assert
        self.assertEquals(result.vCenter_resource_name, "vCenter")
        self.assertEqual(result.vm_folder, "Alex")
 def setUp(self):
     self.csRetrieverService = CloudshellDataRetrieverService()
class test_cloudshellDataRetrieverService(unittest.TestCase):
    def setUp(self):
        self.csRetrieverService = CloudshellDataRetrieverService()

    def tearDown(self):
        pass

    def test_getVCenterTemplateAttributeData(self):
        # Arrange
        attributes = {"vCenter Template": "vCenter/Alex/test"}
        resource_attributes = Mock(attributes=attributes)

        # Act
        result = self.csRetrieverService.getVCenterTemplateAttributeData(
            resource_attributes)

        # Assert
        self.assertEquals(result.vCenter_resource_name, "vCenter")
        self.assertEqual(result.vm_folder, "Alex")
        self.assertEqual(result.template_name, "test")

    def test_getPowerStateAttributeData_Value_Is_True(self):
        # Arrange
        attributes = {"VM Power State": "True"}
        resource_attributes = Mock(attributes=attributes)

        # Act
        result = self.csRetrieverService.getPowerStateAttributeData(
            resource_attributes)

        # Assert
        self.assertEquals(result, True)

    def test_getPowerStateAttributeData_Value_Is_False(self):
        # Arrange
        attributes = {"VM Power State": "False"}
        resource_attributes = Mock(attributes=attributes)

        # Act
        result = self.csRetrieverService.getPowerStateAttributeData(
            resource_attributes)

        # Assert
        self.assertEquals(result, False)

    def test_getVMClusterAttributeData(self):
        # Arrange
        attributes = {"VM Cluster": "cluster1/resourcepool1"}
        resource_attributes = Mock(attributes=attributes)

        # Act
        result = self.csRetrieverService.getVMClusterAttributeData(
            resource_attributes)

        # Assert
        self.assertEquals(result.cluster_name, "cluster1")
        self.assertEquals(result.resource_pool, "resourcepool1")

    def test_getVMClusterAttributeData_Empty_Attribute(self):
        # Arrange
        attributes = {"VM Cluster": ""}
        resource_attributes = Mock(attributes=attributes)

        # Act
        result = self.csRetrieverService.getVMClusterAttributeData(
            resource_attributes)

        # Assert
        self.assertEquals(result.cluster_name, None)
        self.assertEquals(result.resource_pool, None)

    def test_getVMStorageAttributeData(self):
        # Arrange
        attributes = {"VM Storage": "my storage"}
        resource_attributes = Mock(attributes=attributes)

        # Act
        datastore_name = self.csRetrieverService.getVMStorageAttributeData(
            resource_attributes)

        # Assert
        self.assertEquals(datastore_name, "my storage")

    def test_getVMStorageAttributeData_Empty_Attribute(self):
        # Arrange
        attributes = {"VM Storage": ""}
        resource_attributes = Mock(attributes=attributes)

        # Act
        datastore_name = self.csRetrieverService.getVMStorageAttributeData(
            resource_attributes)

        # Arrange
        self.assertEquals(datastore_name, None)

    def test_getVCenterConnectionDetails(self):
        # Arrange
        password = "******"
        session = Mock()
        session.DecryptPassword = Mock(return_value=Mock(Value=password))

        attributes = {"User": "******", "Password": password}
        vCenter_resource_details = Mock()
        vCenter_resource_details.attributes = attributes
        vCenter_resource_details.address = 'vCenterIP'

        # Act
        conn_details = self.csRetrieverService.getVCenterConnectionDetails(
            session, vCenter_resource_details)

        # Assert
        self.assertEquals(conn_details.username, "user1")
        self.assertEquals(conn_details.password, "pass1")
        self.assertEquals(conn_details.host, "vCenterIP")

    def test_getVCenterInventoryPathAttributeData(self):
        # Arrange
        attributes = {"vCenter Inventory Path": "vCenter/Alex"}
        resource_attributes = Mock(attributes=attributes)

        # Act
        result = self.csRetrieverService.getVCenterInventoryPathAttributeData(
            resource_attributes)

        # Assert
        self.assertEquals(result.vCenter_resource_name, "vCenter")
        self.assertEqual(result.vm_folder, "Alex")