shwstppr commented on code in PR #13269:
URL: https://github.com/apache/cloudstack/pull/13269#discussion_r3812500890


##########
ui/src/views/tools/ManageInstances.vue:
##########
@@ -1379,18 +1379,121 @@ export default {
         this.fetchInstances()
       }
     },
-    async fetchGuestOsMappings (osIdentifier, hypervisorVersion) {
-      const params = {}
-      params.hypervisor = 'VMware'
-      params.hypervisorversion = hypervisorVersion
-      params.osnameforhypervisor = osIdentifier
+    async fetchGuestOsMappingsByParams (params) {
       return await getAPI('listGuestOsMapping', params).then(json => {
         return json.listguestosmappingresponse?.guestosmapping || []
       }).catch(error => {
         this.$notifyError(error)
         return []
       })
     },
+    normalizeGuestOsName (name) {
+      return (name || '')
+        .toLowerCase()
+        .replace(/^microsoft\s+/, '')
+        .replace(/[^a-z0-9]+/g, ' ')
+        .replace(/\s+/g, ' ')
+        .trim()
+    },
+    isStrongGuestOsNameMatch (osType, osDisplayName) {
+      const candidate = this.normalizeGuestOsName(osType.description || 
osType.osdisplayname)
+      const source = this.normalizeGuestOsName(osDisplayName)
+      if (!candidate || !source) {
+        return false
+      }
+      if (candidate === source) {
+        return true
+      }
+      if (Math.min(candidate.length, source.length) < 8) {
+        return false
+      }
+      return candidate.includes(source) || source.includes(candidate)
+    },
+    async fetchGuestOsTypeFallbackMappings (osDisplayName) {
+      if (!osDisplayName || !('listOsTypes' in this.$store.getters.apis)) {
+        return []
+      }
+      return await getAPI('listOsTypes', {
+        description: osDisplayName
+      }).then(json => {
+        const osTypes = json.listostypesresponse?.ostype || []
+        return osTypes
+          .filter(osType => this.isStrongGuestOsNameMatch(osType, 
osDisplayName))
+          .map(osType => {
+            return {
+              ostypeid: osType.id,
+              osdisplayname: osType.description || osType.osdisplayname
+            }
+          })
+      }).catch(error => {
+        this.$notifyError(error)
+        return []
+      })
+    },
+    filterGuestOsMappings (mappings, params, osDisplayName) {
+      if (!mappings || mappings.length === 0) {
+        return []
+      }
+
+      const osNameForHypervisor = (params.osnameforhypervisor || 
'').toLowerCase()
+      let filteredMappings = mappings
+      if (osNameForHypervisor) {
+        filteredMappings = mappings.filter(mapping => 
(mapping.osnameforhypervisor || '').toLowerCase() === osNameForHypervisor)
+      }
+
+      if (osDisplayName) {
+        const displayNameMatches = filteredMappings.filter(mapping => 
this.isStrongGuestOsNameMatch(mapping, osDisplayName))
+        if (displayNameMatches.length > 0) {
+          return displayNameMatches
+        }
+        if (params.osdisplayname) {
+          return []
+        }
+      }
+
+      return filteredMappings
+    },
+    async fetchGuestOsMappings (osIdentifier, osDisplayName, 
hypervisorVersion) {
+      const lookups = []
+      if (osIdentifier) {
+        lookups.push({
+          hypervisor: 'VMware',
+          hypervisorversion: hypervisorVersion,
+          osnameforhypervisor: osIdentifier
+        })
+      }
+      if (osIdentifier) {
+        lookups.push({
+          hypervisor: 'VMware',
+          osnameforhypervisor: osIdentifier
+        })
+      }
+      if (osDisplayName) {
+        lookups.push({
+          hypervisor: 'VMware',
+          hypervisorversion: hypervisorVersion,
+          osdisplayname: osDisplayName
+        })
+      }
+      if (osDisplayName) {
+        lookups.push({
+          hypervisor: 'VMware',
+          osdisplayname: osDisplayName
+        })
+      }

Review Comment:
   maybe this can be refactored. As I understand we want 2 looksups for each 
type - one with version and one without



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to