Skip to content

Commit fadc6c1

Browse files
committed
fix
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 25a21ea commit fadc6c1

2 files changed

Lines changed: 66 additions & 5 deletions

File tree

ui/src/components/view/InfoCard.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@
883883
<script>
884884
import { api } from '@/api'
885885
import { createPathBasedOnVmType } from '@/utils/plugins'
886-
import { validateLinks } from '@/utils/links'
886+
import { validateLinksAsync } from '@/utils/links'
887887
import Console from '@/components/widgets/Console'
888888
import OsLogo from '@/components/widgets/OsLogo'
889889
import Status from '@/components/widgets/Status'
@@ -968,12 +968,12 @@ export default {
968968
},
969969
resource: {
970970
deep: true,
971-
handler (newData, oldData) {
971+
async handler (newData, oldData) {
972972
if (newData === oldData) return
973973
this.newResource = newData
974974
this.showKeys = false
975975
this.setData()
976-
this.validLinks = validateLinks(this.$router, this.isStatic, this.resource)
976+
this.validLinks = await validateLinksAsync(this.$router, this.isStatic, this.resource)
977977
978978
if ('apikey' in this.resource) {
979979
this.getUserKeys()

ui/src/utils/links.js

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,54 @@
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) {
40+
const validLinks = {
41+
volume: false
42+
}
43+
44+
if (isStatic) {
45+
return validLinks
46+
}
47+
48+
if (resource.volumeid && router.resolve('/volume/' + resource.volumeid).matched[0].redirect !== '/exception/404') {
49+
if (resource.volumestate) {
50+
validLinks.volume = resource.volumestate !== 'Expunged'
51+
} else {
52+
validLinks.volume = true
53+
}
54+
}
55+
56+
return validLinks
57+
}
58+
59+
export async function validateLinksAsync (router, isStatic, resource) {
1960
const validLinks = {
2061
volume: false,
2162
template: false,
2263
iso: false
2364
}
65+
const pendingChecks = []
2466

2567
if (isStatic) {
2668
return validLinks
@@ -36,11 +78,30 @@ export function validateLinks (router, isStatic, resource) {
3678

3779
if (resource.templateid) {
3880
const templatePath = (resource.templateformat === 'ISO' ? '/iso/' : '/template/') + resource.templateid
39-
validLinks.template = router.resolve(templatePath).matched[0].redirect !== '/exception/404'
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+
}
4088
}
4189

4290
if (resource.isoid) {
43-
validLinks.iso = router.resolve('/iso/' + resource.isoid).matched[0].redirect !== '/exception/404'
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+
})
44105
}
45106

46107
return validLinks

0 commit comments

Comments
 (0)