// Find a name to use of a VM within an envelope that could have come from any hypervisor. // TODO: Consider refactoring this method because it is very similar to OVF.FindSystemName(). private string FindVMName(EnvelopeType ovfEnv, string systemid) { VirtualSystem_Type vSystem = OVF.FindVirtualSystemById(ovfEnv, systemid); // Use the given name if present and valid. // The given name is Envelope.VirtualSystem.Name specified in the OVF Specification 1.1 clause 7.2. // XenServer sets the name property. // vSphere 4.1 and Virtual Box 4.0.6 do not. if ((Tools.ValidateProperty("Name", vSystem)) && !String.IsNullOrEmpty(vSystem.Name[0].Value)) { return(vSystem.Name[0].Value); } // The VM wasn't given a name. // Build a list of choices from various properties. var choices = new List <string>(); // VirtualSystem.id is next preference because vSphere and Virtual Box typically set this property to the VM name. if (!string.IsNullOrEmpty(vSystem.id)) { choices.Add(vSystem.id); } // VirtualHardwareSection_Type.VirtualSystemIdentifier is next preference because Virtual Box will also set this property to the VM name. VirtualHardwareSection_Type[] vhsList = OVF.FindVirtualHardwareSection(ovfEnv, systemid); foreach (VirtualHardwareSection_Type vhs in vhsList) { if (vhs == null || vhs.System == null) { continue; } if (Tools.ValidateProperty("VirtualSystemIdentifier", vhs.System)) { choices.Add(vhs.System.VirtualSystemIdentifier.Value); } } // Operating system description is next preference. OperatingSystemSection_Type[] ossList = OVF.FindSections <OperatingSystemSection_Type>(vSystem.Items); foreach (OperatingSystemSection_Type oss in ossList) { if (Tools.ValidateProperty("Description", oss)) { choices.Add(oss.Description.Value); } } // Envelope name is the last preference for XenServer that can could be a path in some cases. // vSphere and Virtual Box usually don't set this property. choices.Add(Path.GetFileNameWithoutExtension(ovfEnv.Name)); // Last preference is file name. choices.Add(Path.GetFileNameWithoutExtension(m_pageImportSource.SelectedOvfPackage.PackageSourceFile)); // First choice is one that is not a GUID. foreach (var choice in choices) { if (!String.IsNullOrEmpty(choice) && !IsGUID(choice)) { return(choice); } } // Second choice is the first GUID. foreach (var choice in choices) { if (!String.IsNullOrEmpty(choice)) { return(choice); } } // Last resort is a new GUID. return(Guid.NewGuid().ToString()); }