private static DVSConfigSpec GetConfigSpec(string dvsName, string dvsDesc) { DVSConfigSpec dvsConfigSpec = new DVSConfigSpec(); dvsConfigSpec.name = dvsName; if (dvsDesc != null) { dvsConfigSpec.description = dvsDesc; } DVSPolicy dvsPolicy = new DVSPolicy(); dvsPolicy.autoPreInstallAllowed = true; dvsPolicy.autoUpgradeAllowed = true; dvsPolicy.partialUpgradeAllowed = true; return(dvsConfigSpec); }
/// <summary> /// This method is used to create DVS or add port group according to user choice. /// </summary> private void DoCreate() { string dcname = cb.get_option("dcname"); string dvsname = cb.get_option("dvsname"); string dvsdesc = cb.get_option("dvsdesc"); string dvsversion = cb.get_option("dvsversion"); int numPorts = 0; if (cb.get_option("numports") != null) { numPorts = int.Parse(cb.get_option("numports")); } string portGroupName = cb.get_option("portgroupname"); try { if (GetItemType().Equals("createdvs")) { ManagedObjectReference dcmor = cb._svcUtil.getEntityByName("Datacenter", dcname); if (dcmor != null) { ManagedObjectReference networkmor = cb.getServiceUtil().GetMoRefProp(dcmor, "networkFolder"); DVSCreateSpec dvscreatespec = new DVSCreateSpec(); DistributedVirtualSwitchProductSpec dvsProdSpec = GetDVSProductSpec(dvsversion); dvscreatespec.productInfo = dvsProdSpec; DistributedVirtualSwitchHostProductSpec[] dvsHostProdSpec = cb.getConnection()._service.QueryDvsCompatibleHostSpec(cb.getConnection(). _sic.dvSwitchManager, dvsProdSpec); DVSCapability dvsCapability = new DVSCapability(); dvsCapability.compatibleHostComponentProductInfo = dvsHostProdSpec; dvscreatespec.capability = dvsCapability; DVSConfigSpec configSpec = GetConfigSpec(dvsname, dvsdesc); dvscreatespec.configSpec = configSpec; ManagedObjectReference taskmor = cb.getConnection()._service.CreateDVS_Task(networkmor, dvscreatespec); if (taskmor != null) { String status = cb.getServiceUtil().WaitForTask( taskmor); if (status.Equals("sucess")) { Console.WriteLine("Sucessfully created::" + dvsname); } else { Console.WriteLine("dvs switch" + dvsname + " not created::"); throw new Exception(status); } } } else { throw new Exception("Datacenter" + dcname + "not found"); } } else if (GetItemType().Equals("addportgroup")) { ManagedObjectReference dvsMor = cb._svcUtil.getEntityByName("VmwareDistributedVirtualSwitch", dvsname); if (dvsMor != null) { DVPortgroupConfigSpec portGroupConfigSpec = new DVPortgroupConfigSpec(); portGroupConfigSpec.name = portGroupName; portGroupConfigSpec.numPorts = numPorts; portGroupConfigSpec.type = "earlyBinding"; List <DVPortgroupConfigSpec> lst = new List <DVPortgroupConfigSpec>(); lst.Add(portGroupConfigSpec); ManagedObjectReference taskmor = cb.getConnection()._service.AddDVPortgroup_Task(dvsMor, lst.ToArray()); if (taskmor != null) { String status = cb.getServiceUtil().WaitForTask( taskmor); if (status.Equals("sucess")) { Console.WriteLine("Sucessfully added port group :" + portGroupName); } else { Console.WriteLine("port group" + portGroupName + " not added:"); throw new Exception(status); } } } else { throw new Exception("DvsSwitch " + dvsname + " not found"); } } else { Console.WriteLine("Unknown Type. Allowed types are:"); Console.WriteLine(" createdvs"); Console.WriteLine(" addportgroup"); } } catch (Exception e) { throw new Exception(e.Message); } }
private static DVSConfigSpec GetConfigSpec(string dvsName, string dvsDesc) { DVSConfigSpec dvsConfigSpec = new DVSConfigSpec(); dvsConfigSpec.name = dvsName; if (dvsDesc != null) { dvsConfigSpec.description = dvsDesc; } DVSPolicy dvsPolicy = new DVSPolicy(); dvsPolicy.autoPreInstallAllowed = true; dvsPolicy.autoUpgradeAllowed = true; dvsPolicy.partialUpgradeAllowed = true; return dvsConfigSpec; }