Skip to content

Commit 86ad3a2

Browse files
committed
ui: fix info card showing invalid template, iso link
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent a7c2a05 commit 86ad3a2

2 files changed

Lines changed: 79 additions & 5 deletions

File tree

ui/src/components/view/InfoCard.vue

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -560,15 +560,17 @@
560560
<div class="resource-detail-item__details">
561561
<resource-icon v-if="resource.icon" :image="getImage(resource.icon.base64image)" size="1x" style="margin-right: 5px"/>
562562
<SaveOutlined v-else />
563-
<router-link :to="{ path: (resource.templateformat === 'ISO' ? '/iso/' : '/template/') + resource.templateid }">{{ resource.templatedisplaytext || resource.templatename || resource.templateid }} </router-link>
563+
<router-link v-if="validLinks.template" :to="{ path: (resource.templateformat === 'ISO' ? '/iso/' : '/template/') + resource.templateid }">{{ resource.templatedisplaytext || resource.templatename || resource.templateid }} </router-link>
564+
<span v-else>{{ resource.templatedisplaytext || resource.templatename || resource.templateid }}</span>
564565
</div>
565566
</div>
566567
<div class="resource-detail-item" v-if="resource.isoid">
567568
<div class="resource-detail-item__label">{{ $t('label.isoname') }}</div>
568569
<div class="resource-detail-item__details">
569570
<resource-icon v-if="resource.icon" :image="getImage(resource.icon.base64image)" size="1x" style="margin-right: 5px"/>
570571
<UsbOutlined v-else />
571-
<router-link :to="{ path: '/iso/' + resource.isoid }">{{ resource.isodisplaytext || resource.isoname || resource.isoid }} </router-link>
572+
<router-link v-if="validLinks.iso" :to="{ path: '/iso/' + resource.isoid }">{{ resource.isodisplaytext || resource.isoname || resource.isoid }} </router-link>
573+
<span v-else>{{ resource.isodisplaytext || resource.isoname || resource.isoid }}</span>
572574
</div>
573575
</div>
574576
<div class="resource-detail-item" v-if="resource.serviceofferingname && resource.serviceofferingid">
@@ -881,7 +883,7 @@
881883
<script>
882884
import { api } from '@/api'
883885
import { createPathBasedOnVmType } from '@/utils/plugins'
884-
import { validateLinks } from '@/utils/links'
886+
import { validateLinksAsync } from '@/utils/links'
885887
import Console from '@/components/widgets/Console'
886888
import OsLogo from '@/components/widgets/OsLogo'
887889
import Status from '@/components/widgets/Status'
@@ -966,12 +968,12 @@ export default {
966968
},
967969
resource: {
968970
deep: true,
969-
handler (newData, oldData) {
971+
async handler (newData, oldData) {
970972
if (newData === oldData) return
971973
this.newResource = newData
972974
this.showKeys = false
973975
this.setData()
974-
this.validLinks = validateLinks(this.$router, this.isStatic, this.resource)
976+
this.validLinks = await validateLinksAsync(this.$router, this.isStatic, this.resource)
975977
976978
if ('apikey' in this.resource) {
977979
this.getUserKeys()

ui/src/utils/links.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,27 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
import { api } from '@/api'
19+
20+
async function isValidObject (apiName, id, params) {
21+
try {
22+
const allParams = { ...params, listAll: true, id }
23+
const json = await api(apiName, allParams)
24+
const responseName = Object.keys(json).find(key => key.endsWith('response')) || apiName.toLowerCase() + 'response'
25+
const response = json?.[responseName]
26+
if (!response) {
27+
return false
28+
}
29+
const objectName = Object.keys(response).find(key => key !== 'count')
30+
if (!objectName || !Array.isArray(response[objectName])) {
31+
return false
32+
}
33+
return response[objectName].some(item => item.id === id)
34+
} catch (e) {
35+
return false
36+
}
37+
}
38+
1839
export function validateLinks (router, isStatic, resource) {
1940
const validLinks = {
2041
volume: false
@@ -34,3 +55,54 @@ export function validateLinks (router, isStatic, resource) {
3455

3556
return validLinks
3657
}
58+
59+
export async function validateLinksAsync (router, isStatic, resource) {
60+
const validLinks = {
61+
volume: false,
62+
template: false,
63+
iso: false
64+
}
65+
const pendingChecks = []
66+
67+
if (isStatic) {
68+
return validLinks
69+
}
70+
71+
if (resource.volumeid && router.resolve('/volume/' + resource.volumeid).matched[0].redirect !== '/exception/404') {
72+
if (resource.volumestate) {
73+
validLinks.volume = resource.volumestate !== 'Expunged'
74+
} else {
75+
validLinks.volume = true
76+
}
77+
}
78+
79+
if (resource.templateid) {
80+
const templatePath = (resource.templateformat === 'ISO' ? '/iso/' : '/template/') + resource.templateid
81+
if (router.resolve(templatePath).matched[0].redirect !== '/exception/404') {
82+
pendingChecks.push(
83+
isValidObject('listTemplates', resource.templateid, { templatefilter: 'executable' }).then(result => {
84+
validLinks.template = result
85+
})
86+
)
87+
}
88+
}
89+
90+
if (resource.isoid) {
91+
const isoPath = '/iso/' + resource.isoid
92+
if (router.resolve(isoPath).matched[0].redirect !== '/exception/404') {
93+
pendingChecks.push(
94+
isValidObject('listIsos', resource.isoid, { isofilter: 'executable' }).then(result => {
95+
validLinks.iso = result
96+
})
97+
)
98+
}
99+
}
100+
101+
if (pendingChecks.length) {
102+
await Promise.all(pendingChecks).catch(error => {
103+
console.error('Error validating links:', error)
104+
})
105+
}
106+
107+
return validLinks
108+
}

0 commit comments

Comments
 (0)