python vsphere DRS 组

python vsphere DRS group

我正在编写 python 脚本以将 VM 添加到 DRS 组。 这是一个正在完成这项工作的 powershell 脚本。如何翻译成py?

$spec = New-Object VMware.Vim.ClusterConfigSpecEx
$spec.groupSpec = New-Object VMware.Vim.ClusterGroupSpec[] (1)
$spec.groupSpec[0] = New-Object VMware.Vim.ClusterGroupSpec
$spec.groupSpec[0].operation = "edit"
$spec.groupSpec[0].info = $DrsGroup
$spec.groupSpec[0].info.vm += $VM.ExtensionData.MoRef

$Cluster.ExtensionData.ReconfigureComputeResource_Task($spec, $true) 

实际上我在最后一行阻塞了,因为我没有找到 ReconfigureComputeResource 方法。以下是我的代码:

def get_drsgroup(self, cluster_obj=None, group_name=None):
    if cluster_obj is None:
        cluster_obj = self.cluster_obj
    if group_name:
        group_list = [group for group in cluster_obj.configurationEx.group if group.name == group_name]
        if group_list:
            return group_list[0]
    return None

def assign_vm_to_group(self):
    drs_group = self.get_drsgroup(group_name="DC_VM")
    my_vm = self.get_obj(
        [vim.VirtualMachine],
        "Test_vm",
        return_all=True)

    spec = vim.cluster.ConfigSpecEx()
    groupspec = vim.cluster.GroupSpec()
    groupspec.operation = "edit"
    groupspec.info = drs_group
    groupspec.info.vm += my_vm

    self.cluster_obj.xxxxxxxxxxxxxx

感谢您的帮助

     def manage_vm_to_DRS_group(self, action=None):
        #--- requiered objects already exist
        drs_group = self.get_drsgroup(group_name=self.drs_rule_name)
        my_vm = self.get_obj(
            [vim.VirtualMachine],
            self.vm,
            return_all=True
        )
        #---
        try:
            spec = vim.cluster.ConfigSpecEx()
            my_groupSpec = vim.cluster.GroupSpec()

            my_groupSpec.operation = "edit"
            my_groupSpec.info = drs_group
            if self.state == 'present':
                my_groupSpec.info.vm += my_vm
            elif self.state == 'removed':
                #my_groupSpec.info.vm = my_groupSpec.info.vm.remove(my_vm[0])
                my_groupSpec.info.vm.remove(my_vm[0])
            else:
                self.module.fail_json(msg="Module failure: check module, fuction: assign_vm_to_group, indice: 1")
            spec.groupSpec.append(my_groupSpec)

            if not self.check_mode:
                self.cluster_obj.ReconfigureComputeResource_Task(spec, modify=True)

        except:
            self.module.fail_json(msg="Module failure:Failed to assign vm '%s' to DRS group '%s'" % (
                self.vm, self.drs_rule_name))

        self.module.exit_json(changed=True, result="SUCCESS vm '%s' is '%s' on  DRS group '%s'" % (
                self.vm, self.state, self.drs_rule_name))