1515// specific language governing permissions and limitations
1616// under the License.
1717
18+ import { getAPI } from '@/api'
19+
20+ async function isValidObject ( apiName , id , params ) {
21+ try {
22+ const allParams = { ...params , listAll : true , id }
23+ const json = await getAPI ( 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+
1839export 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