0 Replies Latest reply on Nov 10, 2015 1:30 AM by adri_costin

    How to delete "Virtual Machines to Hosts" DRS rules using pyvmomi?

    adri_costin Lurker

      Hi,

      I am using the following code to remove a DRS VmHostRule. The vm group and the host group used in the rule do not exist anymore when trying to delete the rule (they were deleted previously).

      spec = vim.cluster.ConfigSpecEx()
      spec.rulesSpec = []
      rules = cluster.configurationEx.rule
      if rules:
          for rule in rules:
              if rule.name == "my_rule":
                  rule_spec = vim.cluster.RuleSpec()
                  rule_spec.operation = "remove"
                  rule_spec.removeKey = rule.key
                  rule_info = vim.cluster.VmHostRuleInfo()
                  rule_info.enabled = rule.enabled
                  rule_info.name = rule.name
                  rule_info.key = rule.key
                  rule_info.mandatory = rule.mandatory
                  rule_info.vmGroupName = rule.vmGroupName
                  rule_info.affineHostGroupName = rule.affineHostGroupName
                  rule_info.userCreated = rule.userCreated
                  rule_spec.info = rule_info
                  spec.rulesSpec.append(rule_spec)
      if spec.rulesSpec:
          try:
              task = cluster.ReconfigureComputeResource_Task(spec, True)
              wait_for_reconfig_tasks(self.content, [task])
          except Exception:
              print("Failed to remove affinity rule")

      Sometimes, the code doesn't work and the following result is received:

      pyVmomi.VmomiSupport.vmodl.fault.InvalidArgument: (vmodl.fault.InvalidArgument) {
         dynamicType = <unset>,
         dynamicProperty = (vmodl.DynamicProperty) [],
         msg = 'A specified parameter was not correct: ',
         faultCause = <unset>,
         faultMessage = (vmodl.LocalizableMessage) [],
         invalidProperty = <unset>
      }

      The vCenter vpxd.log contains the following:

      2015-11-09T13:47:00.415Z info vpxd[7F35A15DC700] [Originator@6876 sub=Default opID=206261f7] [VpxLRO] -- ERROR task-30451 -- domain-c822 -- vim.ComputeResource.reconfigureEx: vmodl.fault.InvalidArgument:
       Result:
       (vmodl.fault.InvalidArgument) {
          faultCause = (vmodl.MethodFault) null,
          invalidProperty = <unset>,
          msg = ""
       }
       Args:

       Arg spec:
       (vim.cluster.ConfigSpecEx) {
          vmSwapPlacement = <unset>,
          spbmEnabled = <unset>,
          defaultHardwareVersionKey = <unset>,
          dasConfig = (vim.cluster.DasConfigInfo) null,
          drsConfig = (vim.cluster.DrsConfigInfo) null,
          rulesSpec = (vim.cluster.RuleSpec) [
             (vim.cluster.RuleSpec) {
                operation = "remove",
                removeKey = 103,
                info = (vim.cluster.VmHostRuleInfo) {
                   key = 103,
                   status = <unset>,
                   enabled = true,
                   name = "my_rule",
                   mandatory = true,
                   userCreated = <unset>,
                   inCompliance = <unset>,
                   ruleUuid = <unset>,
                   vmGroupName = "vm_group",
                   affineHostGroupName = "host_group",
                   antiAffineHostGroupName = <unset>
                }
             }
          ],
          dpmConfig = (vim.cluster.DpmConfigInfo) null,
          vsanConfig = (vim.vsan.cluster.ConfigInfo) null,
       }
       Arg modify:
       true

      The code sometimes works, sometimes returns the above error. Anyone knows what is wrong?

      Thanks,

      Adriana