Exemplo n.º 1
0
        /// <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");
            }
        }
Exemplo n.º 2
0
        /// <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");
            }
        }