/// <summary> /// method to print HttpNfcLease Info /// </summary> /// <param name="info">HttpNfcLeaseInfo</param> private void PrintHttpNfcLeaseInfo(HttpNfcLeaseInfo info) { Console.WriteLine("########################################################"); Console.WriteLine("HttpNfcLeaseInfo"); Console.WriteLine("Lease Timeout: " + info.leaseTimeout); Console.WriteLine("Total Disk capacity: " + info.totalDiskCapacityInKB); HttpNfcLeaseDeviceUrl[] deviceUrlArr = info.deviceUrl; if (deviceUrlArr != null) { int deviceUrlCount = 1; foreach (HttpNfcLeaseDeviceUrl durl in deviceUrlArr) { Console.WriteLine("HttpNfcLeaseDeviceUrl : " + deviceUrlCount++); Console.WriteLine(" Device URL Import Key: " + durl.importKey); Console.WriteLine(" Device URL Key: " + durl.key); Console.WriteLine(" Device URL : " + durl.url); Console.WriteLine(" SSL Thumbprint : " + durl.sslThumbprint); } } else { Console.WriteLine("No Device URLS Found"); Console.WriteLine("########################################################"); } }
/// <summary> /// method to print HttpNfcLease Info /// </summary> /// <param name="info">HttpNfcLeaseInfo</param> private void PrintHttpNfcLeaseInfo(HttpNfcLeaseInfo info) { Console.WriteLine("########################################################"); Console.WriteLine("HttpNfcLeaseInfo"); Console.WriteLine("Lease Timeout: " + info.leaseTimeout); Console.WriteLine("Total Disk capacity: " + info.totalDiskCapacityInKB); HttpNfcLeaseDeviceUrl[] deviceUrlArr = info.deviceUrl; if (deviceUrlArr != null) { int deviceUrlCount = 1; foreach (HttpNfcLeaseDeviceUrl durl in deviceUrlArr) { Console.WriteLine("HttpNfcLeaseDeviceUrl : " + deviceUrlCount++); Console.WriteLine(" Device URL Import Key: " + durl.importKey); Console.WriteLine(" Device URL Key: " + durl.key); Console.WriteLine(" Device URL : " + durl.url.Replace("*", cb.getHostName())); Console.WriteLine(" SSL Thumbprint : " + durl.sslThumbprint); } } else { Console.WriteLine("No Device URLS Found"); Console.WriteLine("########################################################"); } }
private void ExportVM() { string vmName = cb.get_option("vmname"); localPath = cb.get_option("localpath"); ManagedObjectReference vmMoRef = cb._svcUtil.getEntityByName("VirtualMachine", vmName); if (vmMoRef != null) { Console.WriteLine("Getting the HTTP NFCLEASE for the VM: " + vmName); ManagedObjectReference httpNfcLease = cb._connection._service.ExportVm(vmMoRef); HttpNfcLeaseInfo httpNfcLeaseInfo = null; Object[] result = cb._svcUtil.WaitForValues(httpNfcLease, new String[] { "state" }, new String[] { "state" }, new Object[][] { new Object[] { "ready", "error" } }); if (result[0].Equals("ready")) { httpNfcLeaseInfo = cb._svcUtil.GetDynamicProperty(httpNfcLease, "info") as HttpNfcLeaseInfo; httpNfcLeaseInfo.leaseTimeout = 300000000; PrintHttpNfcLeaseInfo(httpNfcLeaseInfo); long diskCapacity = (httpNfcLeaseInfo.totalDiskCapacityInKB) * 1024; Console.WriteLine("************ " + diskCapacity); HttpNfcLeaseDeviceUrl[] deviceUrlArr = httpNfcLeaseInfo.deviceUrl; if (deviceUrlArr != null) { Console.WriteLine("Downloading Files:"); foreach (HttpNfcLeaseDeviceUrl urlArray in deviceUrlArr) { string deviceUrlStr = urlArray.url; string absoluteFile = deviceUrlStr.Substring(deviceUrlStr.LastIndexOf("/") + 1); Console.WriteLine(" Absolute File Name: " + absoluteFile); Console.WriteLine(" VMDK URL: " + deviceUrlStr.Replace("*", cb.getHostName())); WriteVmdkFile(absoluteFile, deviceUrlStr.Replace("*", cb.getHostName()), vmName); } Console.WriteLine("Completed Downloading the files"); cb._connection._service.HttpNfcLeaseComplete(httpNfcLease); } else { throw new Exception("No url found for the specified VM"); } } else { throw new Exception("Error happened while acquiring HttpNfcLease"); } } else { throw new Exception("Virtual machine " + vmName + "not found"); } }
/// <summary> /// method to print HttpNfcLease Info /// </summary> /// <param name="info">HttpNfcLeaseInfo</param> /// <param name="hostName">string</param> private void printHttpNfcLeaseInfo(HttpNfcLeaseInfo info) { Console.WriteLine("########################################################"); Console.WriteLine("HttpNfcLeaseInfo"); // System.out.println("cookie: " + info.getCookie()); HttpNfcLeaseDeviceUrl[] deviceUrlArr = info.deviceUrl; foreach (HttpNfcLeaseDeviceUrl durl in deviceUrlArr) { Console.WriteLine("Device URL Import Key: " + durl.importKey); Console.WriteLine("Device URL Key: " + durl.key); Console.WriteLine("Device URL : " + durl.url); } Console.WriteLine("Lease Timeout: " + info.leaseTimeout); Console.WriteLine("Total Disk capacity: " + info.totalDiskCapacityInKB); Console.WriteLine("########################################################"); }
/// <summary> /// method to print HttpNfcLease Info /// </summary> /// <param name="info">HttpNfcLeaseInfo</param> /// <param name="hostName">string</param> private void printHttpNfcLeaseInfo(HttpNfcLeaseInfo info) { Console.WriteLine("########################################################"); Console.WriteLine("HttpNfcLeaseInfo"); // System.out.println("cookie: " + info.getCookie()); HttpNfcLeaseDeviceUrl[] deviceUrlArr = info.deviceUrl; foreach (HttpNfcLeaseDeviceUrl durl in deviceUrlArr) { Console.WriteLine("Device URL Import Key: " + durl.importKey); Console.WriteLine("Device URL Key: " + durl.key); Console.WriteLine("Device URL : " + durl.url); } Console.WriteLine("Lease Timeout: " + info.leaseTimeout); Console.WriteLine("Total Disk capacity: " + info.totalDiskCapacityInKB); Console.WriteLine("########################################################"); }
private void ExportVApp() { string vApp = cb.get_option("vapp"); localPath = cb.get_option("localpath"); ManagedObjectReference vAppMoRef = cb._svcUtil.getEntityByName("VirtualApp", vApp); if (vAppMoRef != null) { OvfCreateDescriptorParams ovfCreateDescriptorParams = new OvfCreateDescriptorParams(); Console.WriteLine("Getting the HTTP NFCLEASE for the vApp: " + vApp); ManagedObjectReference httpNfcLease = cb._connection._service.ExportVApp(vAppMoRef); HttpNfcLeaseInfo httpNfcLeaseInfo = null; Object[] result = cb._svcUtil.WaitForValues(httpNfcLease, new String[] { "state" }, new String[] { "state" }, new Object[][] { new Object[] { "ready", "error" } }); if (result[0].Equals("ready")) { httpNfcLeaseInfo = cb._svcUtil.GetDynamicProperty(httpNfcLease, "info") as HttpNfcLeaseInfo; httpNfcLeaseInfo.leaseTimeout = 300000000; PrintHttpNfcLeaseInfo(httpNfcLeaseInfo); long diskCapacity = (httpNfcLeaseInfo.totalDiskCapacityInKB) * 1024; Console.WriteLine("************ " + diskCapacity); HttpNfcLeaseDeviceUrl[] deviceUrlArr = httpNfcLeaseInfo.deviceUrl; if (deviceUrlArr != null) { Console.WriteLine("Downloading Files:"); List <OvfFile> ovfFiles = new List <OvfFile>(); foreach (HttpNfcLeaseDeviceUrl urlArray in deviceUrlArr) { string deviceId = urlArray.key; string deviceUrlStr = urlArray.url; string absoluteFile = deviceUrlStr.Substring(deviceUrlStr.LastIndexOf("/") + 1); Console.WriteLine(" Absolute File Name: " + absoluteFile); Console.WriteLine(" VMDK URL: " + deviceUrlStr); long writtenSize = WriteVMDKFile(absoluteFile, deviceUrlStr); OvfFile ovfFile = new OvfFile(); ovfFile.path = absoluteFile; ovfFile.deviceId = deviceId; ovfFile.size = writtenSize; ovfFiles.Add(ovfFile); } ovfCreateDescriptorParams.ovfFiles = ovfFiles.ToArray(); OvfCreateDescriptorResult ovfCreateDescriptorResult = cb._connection._service.CreateDescriptor( cb._connection._sic.ovfManager, vAppMoRef, ovfCreateDescriptorParams); String outOvf = localPath + "/" + vApp + ".ovf"; File.WriteAllText(outOvf, ovfCreateDescriptorResult.ovfDescriptor); Console.WriteLine("OVF Desriptor Written to file " + vApp + ".ovf"); Console.WriteLine("DONE"); if (ovfCreateDescriptorResult.error != null) { Console.WriteLine("Following Errors occured:"); foreach (LocalizedMethodFault lf in ovfCreateDescriptorResult.error) { Console.WriteLine(lf.localizedMessage); } } if (ovfCreateDescriptorResult.warning != null) { Console.WriteLine("Following Warnings occured:"); foreach (LocalizedMethodFault lf in ovfCreateDescriptorResult.warning) { Console.WriteLine(lf.localizedMessage); } } } else { Console.WriteLine("No url found"); } Console.WriteLine("Completed Downloading the files"); cb._connection._service.HttpNfcLeaseProgress(httpNfcLease, 100); cb._connection._service.HttpNfcLeaseComplete(httpNfcLease); } else { throw new Exception("Error happened while acquiring HttpNfcLease"); } } else { throw new Exception("vApp not found"); } }
/// <summary> /// Method to import vapp from the local path /// </summary> private void ImportVApp() { string hostName = cb.get_option("host"); string vappName = cb.get_option("vappname"); string localPath = cb.get_option("localpath"); string datastore = cb.get_option("datastore"); ManagedObjectReference rpMor = null; ManagedObjectReference parentMor = null; ManagedObjectReference dsMor = null; long totalBytes; ManagedObjectReference hostRef = cb._svcUtil.getEntityByName("HostSystem", hostName); if (hostRef == null) { throw new Exception("Host Not Found"); } else { ManagedObjectReference[] dsList = null; ObjectContent[] objContent = cb.getServiceUtil().GetObjectProperties(null, hostRef, new string[] { "datastore", "parent" }); if (objContent != null) { foreach (ObjectContent oc in objContent) { DynamicProperty[] dps = oc.propSet; if (dps != null) { foreach (DynamicProperty dp in dps) { if (dp.name.Equals("datastore")) { dsList = (ManagedObjectReference[])dp.val; } else if (dp.name.Equals("parent")) { parentMor = (ManagedObjectReference)dp.val; } } } } } if (dsList.Length == 0) { throw new Exception("No Datastores accesible from host " + hostName); } if (datastore == null) { dsMor = dsList.First(); } else { foreach (ManagedObjectReference ds in dsList) { if (datastore.Equals((string)cb._svcUtil.GetDynamicProperty(ds, "name"))) { dsMor = ds; break; } } } if (dsMor == null) { if (datastore != null) { throw new Exception("No Datastore by name " + datastore + " is accessible from host " + hostName); } } rpMor = (ManagedObjectReference)cb._svcUtil.GetDynamicProperty(parentMor, "resourcePool"); ManagedObjectReference dcMor = getDatacenterOfDatastore(dsMor); ManagedObjectReference vmFolder = (ManagedObjectReference)cb._svcUtil.GetDynamicProperty(dcMor, "vmFolder"); OvfCreateImportSpecParams importSpecParams = createImportSpecParams(hostRef, vappName); string ovfDescriptor = getOvfDescriptorFromLocal(localPath); if (ovfDescriptor == null) { throw new Exception("Could not load the OVF descriptor from the file " + localPath); } OvfCreateImportSpecResult ovfImportResult = cb._connection._service.CreateImportSpec(cb._connection._sic.ovfManager, ovfDescriptor, rpMor, dsMor, importSpecParams); OvfFileItem[] fileItemAttr = ovfImportResult.fileItem; if (fileItemAttr != null) { foreach (OvfFileItem fi in fileItemAttr) { printOvfFileItem(fi); totalBytes = fi.size; Console.WriteLine(totalBytes); } ManagedObjectReference httpNfcLease = cb._connection._service.ImportVApp(rpMor, ovfImportResult.importSpec, vmFolder, hostRef); HttpNfcLeaseInfo httpNfcLeaseInfo = null; Object[] result = cb._svcUtil.WaitForValues(httpNfcLease, new String[] { "state" }, new String[] { "state" }, new Object[][] { new Object[] { "ready", "error" } }); if (result[0].Equals("ready")) { httpNfcLeaseInfo = cb._svcUtil.GetDynamicProperty(httpNfcLease, "info") as HttpNfcLeaseInfo; printHttpNfcLeaseInfo(httpNfcLeaseInfo); HttpNfcLeaseDeviceUrl[] deviceUrlArr = httpNfcLeaseInfo.deviceUrl; if (deviceUrlArr != null) { List <OvfFile> ovfFiles = new List <OvfFile>(); int step = 100 / fileItemAttr.Length; int progress = 0; foreach (HttpNfcLeaseDeviceUrl deviceUrl in deviceUrlArr) { string deviceKey = deviceUrl.importKey; foreach (OvfFileItem ovfFileItem in fileItemAttr) { if (deviceKey.Equals(ovfFileItem.deviceId)) { Console.WriteLine("Import key: " + deviceKey); Console.WriteLine("OvfFileItem device id: " + ovfFileItem.deviceId); Console.WriteLine("HTTP Post file: " + ovfFileItem.path); String absoluteFile = localPath.Substring(0, localPath.LastIndexOf("\\")); absoluteFile = absoluteFile + "/" + ovfFileItem.path; Console.WriteLine("Absolute path: " + absoluteFile); SendVMDKFile(ovfFileItem.create, absoluteFile, deviceUrl.url.Replace("*", hostName), ovfFileItem.size); Console.WriteLine("Completed uploading the VMDK file"); progress += step; cb._connection._service.HttpNfcLeaseProgress(httpNfcLease, progress); break; } } } cb._connection._service.HttpNfcLeaseProgress(httpNfcLease, 100); cb._connection._service.HttpNfcLeaseComplete(httpNfcLease); } } else { throw new Exception("Error happened while acquiring HttpNfcLease"); } } } }