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+
1839export 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