Example #1
0
        /// <summary>
        /// Create object of OvfCreateImportSpecParams
        /// </summary>
        /// <param name="host">ManagedObjectReference</param>
        /// <param name="newVmName">string</param>
        /// <returns>OvfCreateImportSpecParams</returns>
        private OvfCreateImportSpecParams createImportSpecParams(
            ManagedObjectReference host, string newVmName)
        {
            OvfCreateImportSpecParams importSpecParams =
                new OvfCreateImportSpecParams();

            importSpecParams.hostSystem       = host;
            importSpecParams.locale           = "";
            importSpecParams.entityName       = newVmName;
            importSpecParams.deploymentOption = "";
            return(importSpecParams);
        }
 /// <summary>
 /// Create object of OvfCreateImportSpecParams
 /// </summary>
 /// <param name="host">ManagedObjectReference</param>
 /// <param name="newVmName">string</param>
 /// <returns>OvfCreateImportSpecParams</returns>
 private OvfCreateImportSpecParams createImportSpecParams(
  ManagedObjectReference host, string newVmName)
 {
     OvfCreateImportSpecParams importSpecParams =
           new OvfCreateImportSpecParams();
     importSpecParams.hostSystem = host;
     importSpecParams.locale = "";
     importSpecParams.entityName = newVmName;
     importSpecParams.deploymentOption = "";
     return importSpecParams;
 }
Example #3
0
        /// <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");
                    }
                }
            }
        }