def connect(self, virtual_switch: 'VirtualSwitch'): """ Connect adapter to given virtual switch. :param virtual_switch: virtual switch to connect """ management_service = VirtualSystemManagementService( self.Scope.query_one( 'SELECT * FROM Msvm_VirtualSystemManagementService')) Msvm_VirtualSystemSettingData = self.traverse((RelatedNode( ("Msvm_VirtualSystemSettingData", )), ))[-1][-1] Msvm_ResourcePool = self.Scope.query_one( "SELECT * FROM Msvm_ResourcePool WHERE ResourceSubType = 'Microsoft:Hyper-V:Ethernet Connection' AND Primordial = True" ) Msvm_EthernetPortAllocationSettingData_Path = ( RelatedNode( ("Msvm_AllocationCapabilities", "Msvm_ElementCapabilities", None, None, None, None, False, None)), RelationshipNode(("Msvm_SettingsDefineCapabilities", ), selector=PropertiesSelector(ValueRole=0)), PropertyNode("PartComponent", transformer=ReferenceTransformer())) Msvm_EthernetPortAllocationSettingData = \ Msvm_ResourcePool.traverse(Msvm_EthernetPortAllocationSettingData_Path)[-1][-1] Msvm_EthernetPortAllocationSettingData.properties.Parent = self Msvm_EthernetPortAllocationSettingData.properties.HostResource = [ virtual_switch ] management_service.AddResourceSettings( Msvm_VirtualSystemSettingData, Msvm_EthernetPortAllocationSettingData)
def add_vhd_disk(self, vhd_disk: VHDDisk): """ Adds given ``VHDDisk`` to virtual machine. :param vhd_disk: ``VHDDisk`` to add to machine """ # TODO ability to select controller, disk port, error checking. Make disk bootable by default, etc management_service = VirtualSystemManagementService( self.Scope.query_one( 'SELECT * FROM Msvm_VirtualSystemManagementService')) Msvm_VirtualSystemSettingData = self.get_child( (VirtualSystemSettingDataNode, )) Msvm_ResourcePool_SyntheticDiskDrive = self.Scope.query_one( "SELECT * FROM Msvm_ResourcePool WHERE ResourceSubType = 'Microsoft:Hyper-V:Synthetic Disk Drive' AND Primordial = True" ) Msvm_StorageAllocationSettingData_Path = ( RelatedNode( ("Msvm_AllocationCapabilities", "Msvm_ElementCapabilities", None, None, None, None, False, None)), RelationshipNode(("Msvm_SettingsDefineCapabilities", ), selector=PropertiesSelector(ValueRole=0)), PropertyNode("PartComponent", transformer=ReferenceTransformer())) IdeController_Path = (RelatedNode( ("Msvm_ResourceAllocationSettingData", ), selector=PropertiesSelector( ResourceType=5, ResourceSubType='Microsoft:Hyper-V:Emulated IDE Controller', Address=0)), ) IdeController = Msvm_VirtualSystemSettingData.get_child( IdeController_Path) Msvm_StorageAllocationSettingData = Msvm_ResourcePool_SyntheticDiskDrive.get_child( Msvm_StorageAllocationSettingData_Path).clone() Msvm_StorageAllocationSettingData.properties.Parent = IdeController Msvm_StorageAllocationSettingData.properties.AddressOnParent = 0 synthetic_disk_drive = \ management_service.AddResourceSettings(Msvm_VirtualSystemSettingData, Msvm_StorageAllocationSettingData)[ 'ResultingResourceSettings'][-1] Msvm_ResourcePool_VirtualHardDisk = self.Scope.query_one( "SELECT * FROM Msvm_ResourcePool WHERE ResourceSubType = 'Microsoft:Hyper-V:Virtual Hard Disk' AND Primordial = True" ) virtual_hard_disk_path = (RelatedNode( ("Msvm_AllocationCapabilities", "Msvm_ElementCapabilities", None, None, None, None, False, None)), RelationshipNode( ("Msvm_SettingsDefineCapabilities", ), selector=PropertiesSelector( ValueRole=0)), PropertyNode( "PartComponent", transformer=ReferenceTransformer())) virtual_hard_disk_data = Msvm_ResourcePool_VirtualHardDisk.get_child( virtual_hard_disk_path).clone() virtual_hard_disk_data.properties.Parent = synthetic_disk_drive virtual_hard_disk_data.properties.HostResource = [vhd_disk.Path] management_service.AddResourceSettings(Msvm_VirtualSystemSettingData, virtual_hard_disk_data)
def set_ip_settings(self, dhcp=True, ip=[], sub_nets=[], gateways=[], dns=[]): management_service = VirtualSystemManagementService( self.Scope.query_one( 'SELECT * FROM Msvm_VirtualSystemManagementService')) self.properties.DHCPEnabled = dhcp self.properties.IPAddresses = ip self.properties.Subnets = sub_nets self.properties.DefaultGateways = gateways self.properties.DNSServers = dns self.properties.ProtocolIFType = 4096 computer_system = self.parent.parent management_service.SetGuestNetworkAdapterConfiguration( computer_system, self)
def apply_properties(self, class_name: str, properties: Dict[str, Any]): """ Apply ``properties`` for ``class_name`` that associated with virtual machine. :param class_name: class name that will be used for modification :param properties: properties to apply """ management_service = VirtualSystemManagementService( self.Scope.query_one( 'SELECT * FROM Msvm_VirtualSystemManagementService')) class_instance = self.traverse(self.PATH_MAP[class_name])[0][-1] for property_name, property_value in properties.items(): setattr(class_instance.properties, property_name, property_value) if class_name in self.RESOURCE_CLASSES: management_service.ModifyResourceSettings(class_instance) if class_name in self.SYSTEM_CLASSES: management_service.ModifySystemSettings( SystemSettings=class_instance)
def create_machine( self, name, properties_group: Dict[str, Dict[str, Any]] = None, machine_generation: VirtualMachineGeneration = VirtualMachineGeneration .GEN1 ) -> VirtualMachine: management_service = VirtualSystemManagementService( self.scope.query_one( 'SELECT * FROM Msvm_VirtualSystemManagementService')) Msvm_VirtualSystemSettingData = self.scope.cls_instance( "Msvm_VirtualSystemSettingData") Msvm_VirtualSystemSettingData.properties.ElementName = name Msvm_VirtualSystemSettingData.properties.VirtualSystemSubType = machine_generation.value result = management_service.DefineSystem( SystemSettings=Msvm_VirtualSystemSettingData) vm = VirtualMachine(result['ResultingSystem']) vm.apply_properties_group(properties_group) return vm
def add_adapter(self, static_mac=False, mac=None, adapter_name="Network Adapter") -> 'VirtualNetworkAdapter': """ Add adapter to virtual machine. :param static_mac: make adapter with static mac :param mac: mac address t assign :param adapter_name: adapter name :return: created adapter """ management_service = VirtualSystemManagementService( self.Scope.query_one( 'SELECT * FROM Msvm_VirtualSystemManagementService')) Msvm_ResourcePool = self.Scope.query_one( "SELECT * FROM Msvm_ResourcePool WHERE ResourceSubType = 'Microsoft:Hyper-V:Synthetic Ethernet Port' " "AND Primordial = True") Msvm_SyntheticEthernetPortSettingData_Path = ( RelatedNode( ("Msvm_AllocationCapabilities", "Msvm_ElementCapabilities", None, None, None, None, False, None)), RelationshipNode(("Msvm_SettingsDefineCapabilities", ), selector=PropertiesSelector(ValueRole=0)), PropertyNode("PartComponent", transformer=ReferenceTransformer())) Msvm_VirtualSystemSettingData = self.traverse( (VirtualSystemSettingDataNode, ))[-1][-1] Msvm_SyntheticEthernetPortSettingData = Msvm_ResourcePool.traverse( Msvm_SyntheticEthernetPortSettingData_Path)[-1][-1] Msvm_SyntheticEthernetPortSettingData.properties.VirtualSystemIdentifiers = clr_Array[ clr_String]([generate_guid()]) Msvm_SyntheticEthernetPortSettingData.properties.ElementName = adapter_name Msvm_SyntheticEthernetPortSettingData.properties.StaticMacAddress = static_mac if mac: Msvm_SyntheticEthernetPortSettingData.properties.Address = mac result = management_service.AddResourceSettings( Msvm_VirtualSystemSettingData, Msvm_SyntheticEthernetPortSettingData) return VirtualNetworkAdapter(result['ResultingResourceSettings'][-1], self)
def path(self, value): management_service = VirtualSystemManagementService( self.Scope.query_one( 'SELECT * FROM Msvm_VirtualSystemManagementService')) self.properties.Connection = [value] management_service.ModifyResourceSettings(self)