sureshanaparti commented on code in PR #9520:
URL: https://github.com/apache/cloudstack/pull/9520#discussion_r1719544437


##########
ui/src/components/view/SearchFilter.vue:
##########
@@ -0,0 +1,619 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+<template>
+  <a-tag
+    v-if="!isTag"
+    closable
+    @close="() => $emit('close', searchFilter.key)"
+  >
+    {{ retrieveFieldLabel(searchFilter.key) }}: {{ searchFilter.value }}
+  </a-tag>
+  <a-tag
+    v-else
+    closable
+    @close="() => $emit('close', searchFilter.key)"
+  >
+    {{ $t('label.tag') }}: {{ searchFilter.key }}={{ searchFilter.value }}
+  </a-tag>
+</template>
+
+<script>
+import { api } from '@/api'
+
+export default {
+  name: 'SearchFilter',
+  props: {
+    apiName: {
+      type: String,
+      default: () => ''
+    },
+    filterKey: {
+      type: String,
+      default: () => ''
+    },
+    filterValue: {
+      type: String,
+      default: () => ''
+    },
+    isTag: {
+      type: Boolean,
+      default: () => false
+    }
+  },
+  emits: ['close'],
+  data () {
+    return {
+      alertTypes: {},
+      searchFilter: {
+        key: this.filterKey,
+        value: this.filterKey
+      }
+    }
+  },
+  created () {
+    this.getSearchFilters()
+  },
+  methods: {
+    retrieveFieldLabel (fieldName) {
+      if (fieldName === 'groupid') {
+        fieldName = 'group'
+      }
+      if (fieldName === 'keyword') {
+        if ('listAnnotations' in this.$store.getters.apis) {
+          return this.$t('label.annotation')
+        } else {
+          return this.$t('label.name')
+        }
+      }
+      return this.$t('label.' + fieldName)
+    },
+    getSearchFilters () {
+      if (this.filterKey === 'domainid' && !('listDomains' in 
this.$store.getters.apis)) {
+        return true
+      }
+      if (this.filterKey === 'account' && !('listAccounts' in 
this.$store.getters.apis)) {
+        return true
+      }
+      if (this.filterKey === 'account' && !('addAccountToProject' in 
this.$store.getters.apis || 'createAccount' in this.$store.getters.apis)) {
+        return true
+      }
+      if (this.filterKey === 'podid' && !('listPods' in 
this.$store.getters.apis)) {
+        return true
+      }
+      if (this.filterKey === 'clusterid' && !('listClusters' in 
this.$store.getters.apis)) {
+        return true
+      }
+      if (this.filterKey === 'groupid' && !('listInstanceGroups' in 
this.$store.getters.apis)) {
+        return true
+      }
+      this.searchFilter = {
+        key: this.filterKey,
+        value: this.filterValue
+      }
+      let value = this.getStaticFieldValue(this.filterKey, this.filterValue)
+      if (value !== '') {
+        this.searchFilter = {
+          key: this.filterKey,
+          value: value
+        }
+      } else {
+        value = this.getDynamicFieldValue(this.filterKey, this.filterValue)
+        value.then((result) => {
+          if (result) {
+            this.searchFilter = {
+              key: this.filterKey,
+              value: result
+            }
+          } else {
+            this.searchFilter = {
+              key: this.filterKey,
+              value: this.filterValue
+            }
+          }
+        })
+      }
+    },
+    getStaticFieldValue (key, value) {
+      let formattedValue = ''
+      if (key.includes('type')) {
+        if (this.$route.path === '/guestnetwork' || 
this.$route.path.includes('/guestnetwork/')) {
+          formattedValue = this.getGuestNetworkType(value)
+        } else if (this.$route.path === '/role' || 
this.$route.path.includes('/role/')) {
+          formattedValue = this.getRoleType(value)
+        }
+      }
+
+      if (key.includes('scope')) {
+        formattedValue = this.getScope(value)
+      }
+
+      if (key.includes('state')) {
+        formattedValue = this.getState(value)
+      }
+
+      if (key.includes('level')) {
+        formattedValue = this.getLevel(value)
+      }
+
+      if (key.includes('entitytype')) {
+        formattedValue = this.getEntityType(value)
+      }
+
+      if (key.includes('accounttype')) {
+        formattedValue = this.getAccountType(value)
+      }
+
+      if (key.includes('systemvmtype')) {
+        formattedValue = this.getSystemVmType(value)
+      }
+
+      if (key.includes('scope')) {
+        formattedValue = this.getStoragePoolScope(value)
+      }
+
+      if (key.includes('provider')) {
+        formattedValue = this.getImageStoreProvider(value)
+      }
+
+      if (key.includes('resourcetype')) {
+        formattedValue = value
+      }
+
+      this.searchFilter = {
+        key: this.filterKey,
+        value: formattedValue
+      }
+      return formattedValue
+    },
+    async getDynamicFieldValue (key, value) {
+      let formattedValue = ''
+
+      if (key.includes('type')) {
+        if (this.$route.path === '/alert') {
+          formattedValue = await this.getAlertType(value)
+        } else if (this.$route.path === '/affinitygroup') {
+          formattedValue = await this.getAffinityGroupType(value)
+        }
+      }
+
+      if (key.includes('zoneid')) {
+        formattedValue = await this.getZone(value)
+      }
+
+      if (key.includes('domainid')) {
+        formattedValue = await this.getDomain(value)
+      }
+
+      if (key.includes('account')) {
+        formattedValue = await this.getAccount(value)
+      }
+
+      if (key.includes('hypervisor')) {
+        formattedValue = await this.getHypervisor(value)
+      }
+
+      if (key.includes('imagestoreid')) {
+        formattedValue = await this.getImageStore(value)
+      }
+
+      if (key.includes('storageid')) {
+        formattedValue = await this.getStoragePool(value)
+      }
+
+      if (key.includes('podid')) {
+        formattedValue = await this.getPod(value)
+      }
+
+      if (key.includes('clusterid')) {
+        formattedValue = await this.getCluster(value)
+      }
+
+      if (key.includes('groupid')) {
+        formattedValue = await this.getInstanceGroup(value)
+      }
+
+      if (key.includes('managementserverid')) {
+        formattedValue = await this.getManagementServer(value)
+      }
+
+      if (key.includes('serviceofferingid')) {
+        formattedValue = await this.getServiceOffering(value)
+      }
+
+      if (key.includes('diskofferingid')) {
+        formattedValue = await this.getDiskOffering(value)
+      }
+
+      return formattedValue
+    },
+    getZone (zoneId) {
+      return new Promise((resolve) => {
+        api('listZones', { showicon: true, id: zoneId }).then(json => {
+          if (json?.listzonesresponse?.zone) {
+            resolve(json.listzonesresponse.zone[0].name)
+          }
+        }).catch(() => {
+          resolve(null)
+        })
+      })
+    },
+    getDomain (domainId) {
+      return new Promise((resolve) => {
+        api('listDomains', { listAll: true, showicon: true, id: domainId 
}).then(json => {
+          if (json?.listdomainsresponse?.domain) {
+            resolve(json.listdomainsresponse.domain[0].path)
+          }
+        }).catch(() => {
+          resolve(null)
+        })
+      })
+    },
+    getAccount (accountId) {
+      return new Promise((resolve) => {
+        if (!this.$isValidUuid(accountId)) {
+          resolve(accountId)
+        }
+        const params = { listAll: true, isrecursive: false, showicon: true, 
id: accountId }
+        api('listAccounts', params).then(json => {
+          if (json?.listaccountsresponse?.account) {
+            resolve(json.listaccountsresponse.account[0].name)
+          }
+        }).catch(() => {
+          resolve(null)
+        })
+      })
+    },
+    getHypervisor (value) {
+      return new Promise((resolve) => {
+        api('listHypervisors').then(json => {
+          if (json?.listhypervisorsresponse?.hypervisor) {
+            for (const key in json.listhypervisorsresponse.hypervisor) {
+              const hypervisor = json.listhypervisorsresponse.hypervisor[key]
+              if (hypervisor.name === value) {
+                resolve(hypervisor.name)
+              }
+            }
+          }
+        }).catch(() => {
+          resolve(null)
+        })
+      })
+    },
+    getImageStore (storeId) {
+      return new Promise((resolve) => {
+        api('listImageStores', { listAll: true, showicon: true, id: storeId 
}).then(json => {
+          if (json?.listimagestoresresponse?.imagestore) {
+            resolve(json.listimagestoresresponse.imagestore[0].name)
+          }
+        }).catch(() => {
+          resolve(null)
+        })
+      })
+    },
+    getStoragePool (poolId) {
+      return new Promise((resolve) => {
+        api('listStoragePools', { listAll: true, showicon: true, id: poolId 
}).then(json => {
+          if (json?.liststoragepoolsresponse?.storagepool) {
+            resolve(json.liststoragepoolsresponse.storagepool[0].name)
+          }
+        }).catch(() => {
+          resolve(null)
+        })
+      })
+    },
+    getPod (podId) {
+      return new Promise((resolve) => {
+        api('listPods', { id: podId }).then(json => {
+          if (json?.listpodsresponse?.pod) {
+            resolve(json.listpodsresponse.pod[0].name)
+          }
+        }).catch(() => {
+          resolve(null)
+        })
+      })
+    },
+    getCluster (clusterId) {
+      return new Promise((resolve) => {
+        api('listClusters', { id: clusterId }).then(json => {
+          if (json?.listclustersresponse?.cluster) {
+            resolve(json.listclustersresponse.cluster[0].name)
+          }
+        }).catch(() => {
+          resolve(null)
+        })
+      })
+    },
+    getInstanceGroup (groupId) {
+      return new Promise((resolve) => {
+        api('listInstanceGroups', { listAll: true, id: groupId }).then(json => 
{
+          if (json?.listinstancegroupsresponse?.instancegroup) {
+            resolve(json.listinstancegroupsresponse.instancegroup[0].name)
+          }
+        }).catch(() => {
+          resolve(null)
+        })
+      })
+    },
+    getServiceOffering (offeringId) {
+      return new Promise((resolve) => {
+        api('listServiceOfferings', { listAll: true, id: offeringId 
}).then(json => {
+          if (json?.listserviceofferingsresponse?.serviceoffering) {
+            resolve(json.listserviceofferingsresponse.serviceoffering[0].name)
+          }
+        }).catch(() => {
+          resolve(null)
+        })
+      })
+    },
+    getDiskOffering (offeringId) {
+      return new Promise((resolve) => {
+        api('listDiskOfferings', { listAll: true, id: offeringId }).then(json 
=> {
+          if (json?.listdiskofferingsresponse?.diskoffering) {
+            resolve(json.listdiskofferingsresponse.diskoffering[0].name)
+          }
+        }).catch(() => {

Review Comment:
   can have some generic method using resource type for all this listing 
methods? (maybe, keep/map resource & list api details, and use api for that 
resource type - will be easy to add in case any other resources are added later)



-- 
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