/// <summary> /// Delete VMdk AntiAffinity Rule Spec /// </summary> /// <param name="podName">string</param> /// <param name="ruleName">string</param> private void DeleteVmdkAntiAffinityRule(string podName, string ruleName) { ManagedObjectReference srmRef = cb._connection._sic.storageResourceManager; ManagedObjectReference sdrsMor = cb._svcUtil.getEntityByName("StoragePod", podName); if (sdrsMor != null) { PodStorageDrsEntry podStorageDrsEntry = (PodStorageDrsEntry)cb._svcUtil.GetDynamicProperty(sdrsMor, "podStorageDrsEntry"); StorageDrsVmConfigInfo drsVmConfigInfo = null; StorageDrsConfigSpec sdrsConfigSpec = new StorageDrsConfigSpec(); StorageDrsVmConfigSpec drsVmConfigSpec = new StorageDrsVmConfigSpec(); StorageDrsVmConfigInfo[] sdrsVmConfig = podStorageDrsEntry.storageDrsConfig.vmConfig; foreach (StorageDrsVmConfigInfo vmConfig in sdrsVmConfig) { if (vmConfig.intraVmAntiAffinity != null) { if (vmConfig.intraVmAntiAffinity.name.Equals(ruleName)) { drsVmConfigInfo = vmConfig; } } } if (drsVmConfigInfo != null) { drsVmConfigInfo.intraVmAntiAffinity = null; drsVmConfigSpec.info = drsVmConfigInfo; drsVmConfigSpec.operation = ArrayUpdateOperation.edit; sdrsConfigSpec.vmConfigSpec = new StorageDrsVmConfigSpec[] { drsVmConfigSpec }; } else { string msg = "\nFailure: Rule " + ruleName + " not found."; throw new Exception(msg); } ManagedObjectReference taskmor = cb._connection._service.ConfigureStorageDrsForPod_Task(srmRef, sdrsMor, sdrsConfigSpec, true); if (taskmor != null) { String status = cb.getServiceUtil().WaitForTask( taskmor); if (status.Equals("sucess")) { Console.WriteLine("Success: Delete VmdkAntiAffinity Rule."); } else { Console.WriteLine("Failure: Delete VmdkAntiAffinity Rule."); throw new Exception(status); } } } else { throw new Exception("Storage Pod " + podName + "not found"); } }
/// <summary> /// Method to add VmdkAntiAffinityRule /// </summary> /// <param name="podName">string</param> /// <param name="ruleName">string</param> /// <param name="enabled">string</param> /// <param name="vm">string</param> private void AddVmdkAntiAffinityRule(string podName, string ruleName, string enabled, string vm) { ManagedObjectReference srmRef = cb._connection._sic.storageResourceManager; ManagedObjectReference sdrsMor = cb._svcUtil.getEntityByName("StoragePod", podName); if (sdrsMor != null) { ManagedObjectReference vmMoref = null; StorageDrsConfigSpec sdrsConfigSpec = new StorageDrsConfigSpec(); StorageDrsVmConfigSpec drsVmConfigSpec = new StorageDrsVmConfigSpec(); StorageDrsVmConfigInfo drsVmConfigInfo = new StorageDrsVmConfigInfo(); VirtualDiskAntiAffinityRuleSpec vmdkAntiAffinityRuleSpec = new VirtualDiskAntiAffinityRuleSpec(); vmdkAntiAffinityRuleSpec.name = ruleName; if (enabled.Equals("true")) { vmdkAntiAffinityRuleSpec.enabled = true; vmdkAntiAffinityRuleSpec.enabledSpecified = true; } else { vmdkAntiAffinityRuleSpec.enabled = false; vmdkAntiAffinityRuleSpec.enabledSpecified = false; } vmMoref = cb._svcUtil.getEntityByName("VirtualMachine", vm); if (vmMoref != null) { VirtualMachineConfigInfo vmConfigInfo = (VirtualMachineConfigInfo)cb._svcUtil.GetDynamicProperty(vmMoref, "config"); VirtualDevice[] vDevice = vmConfigInfo.hardware.device; List <VirtualDevice> vDisk = new List <VirtualDevice>(); VirtualDevice[] virtualDisk = null; List <int> diskIdList = new List <int>(); foreach (VirtualDevice device in vDevice) { if (device.GetType().Name.Equals("VirtualDisk")) { vDisk.Add(device); diskIdList.Add(device.key); } } virtualDisk = vDisk.ToArray(); vmdkAntiAffinityRuleSpec.diskId = diskIdList.ToArray(); if (virtualDisk.Length < 2) { throw new Exception( "VM should have minimum of 2 virtual disks" + " while adding VMDK AntiAffinity Rule."); } Console.WriteLine("Adding below list of virtual disk to rule " + ruleName + " :"); foreach (VirtualDevice device in virtualDisk) { Console.WriteLine("Virtual Disk : " + device.deviceInfo.label + ", Key : " + device.key); } vmdkAntiAffinityRuleSpec.userCreated = true; vmdkAntiAffinityRuleSpec.userCreatedSpecified = true; drsVmConfigInfo.intraVmAntiAffinity = vmdkAntiAffinityRuleSpec; drsVmConfigInfo.intraVmAffinitySpecified = true; drsVmConfigInfo.vm = vmMoref; } else { string message = "Failure: " + vm + "VM not found"; throw new Exception(message); } drsVmConfigSpec.info = drsVmConfigInfo; drsVmConfigSpec.operation = ArrayUpdateOperation.add; sdrsConfigSpec.vmConfigSpec = new StorageDrsVmConfigSpec[] { drsVmConfigSpec }; ManagedObjectReference taskmor = cb._connection._service.ConfigureStorageDrsForPod_Task(srmRef, sdrsMor, sdrsConfigSpec, true); if (taskmor != null) { String status = cb.getServiceUtil().WaitForTask( taskmor); if (status.Equals("sucess")) { Console.WriteLine("Success: Adding VmdkAntiAffinity Rule."); } else { Console.WriteLine("Failure: Adding VmdkAntiAffinity Rule."); throw new Exception(status); } } } else { throw new Exception("Storage Pod " + podName + "not found"); } }
/// <summary> /// Method to add VmdkAntiAffinityRule /// </summary> /// <param name="podName">string</param> /// <param name="ruleName">string</param> /// <param name="enabled">string</param> /// <param name="vm">string</param> private void AddVmdkAntiAffinityRule(string podName, string ruleName, string enabled, string vm) { ManagedObjectReference srmRef = cb._connection._sic.storageResourceManager; ManagedObjectReference sdrsMor = cb._svcUtil.getEntityByName("StoragePod", podName); if (sdrsMor != null) { ManagedObjectReference vmMoref = null; StorageDrsConfigSpec sdrsConfigSpec = new StorageDrsConfigSpec(); StorageDrsVmConfigSpec drsVmConfigSpec = new StorageDrsVmConfigSpec(); StorageDrsVmConfigInfo drsVmConfigInfo = new StorageDrsVmConfigInfo(); VirtualDiskAntiAffinityRuleSpec vmdkAntiAffinityRuleSpec = new VirtualDiskAntiAffinityRuleSpec(); vmdkAntiAffinityRuleSpec.name = ruleName; if (enabled.Equals("true")) { vmdkAntiAffinityRuleSpec.enabled = true; vmdkAntiAffinityRuleSpec.enabledSpecified = true; } else { vmdkAntiAffinityRuleSpec.enabled = false; vmdkAntiAffinityRuleSpec.enabledSpecified = false; } vmMoref = cb._svcUtil.getEntityByName("VirtualMachine", vm); if (vmMoref != null) { VirtualMachineConfigInfo vmConfigInfo = (VirtualMachineConfigInfo)cb._svcUtil.GetDynamicProperty(vmMoref, "config"); VirtualDevice[] vDevice = vmConfigInfo.hardware.device; List<VirtualDevice> vDisk = new List<VirtualDevice>(); VirtualDevice[] virtualDisk = null; List<int> diskIdList = new List<int>(); foreach (VirtualDevice device in vDevice) { if (device.GetType().Name.Equals("VirtualDisk")) { vDisk.Add(device); diskIdList.Add(device.key); } } virtualDisk = vDisk.ToArray(); vmdkAntiAffinityRuleSpec.diskId = diskIdList.ToArray(); if (virtualDisk.Length < 2) { throw new Exception( "VM should have minimum of 2 virtual disks" + " while adding VMDK AntiAffinity Rule."); } Console.WriteLine("Adding below list of virtual disk to rule " + ruleName + " :"); foreach (VirtualDevice device in virtualDisk) { Console.WriteLine("Virtual Disk : " + device.deviceInfo.label + ", Key : " + device.key); } vmdkAntiAffinityRuleSpec.userCreated = true; vmdkAntiAffinityRuleSpec.userCreatedSpecified = true; drsVmConfigInfo.intraVmAntiAffinity = vmdkAntiAffinityRuleSpec; drsVmConfigInfo.intraVmAffinitySpecified = true; drsVmConfigInfo.vm = vmMoref; } else { string message = "Failure: " + vm + "VM not found"; throw new Exception(message); } drsVmConfigSpec.info = drsVmConfigInfo; drsVmConfigSpec.operation = ArrayUpdateOperation.add; sdrsConfigSpec.vmConfigSpec = new StorageDrsVmConfigSpec[] { drsVmConfigSpec }; ManagedObjectReference taskmor = cb._connection._service.ConfigureStorageDrsForPod_Task(srmRef, sdrsMor, sdrsConfigSpec, true); if (taskmor != null) { String status = cb.getServiceUtil().WaitForTask( taskmor); if (status.Equals("sucess")) { Console.WriteLine("Success: Adding VmdkAntiAffinity Rule."); } else { Console.WriteLine("Failure: Adding VmdkAntiAffinity Rule."); throw new Exception(status); } } } else { throw new Exception("Storage Pod " + podName + "not found"); } }