Skip to content

Commit 2bb4a18

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

2 files changed

Lines changed: 66 additions & 3 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 & 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,
@@ -45,3 +66,45 @@ export function validateLinks (router, isStatic, resource) {
4566

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

0 commit comments

Comments
 (0)