]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/module-status-guard.service.ts
import ceph quincy 17.2.4
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / services / module-status-guard.service.ts
index 98c4b490a7039a42d3c66f6b0f0d0e005a1b3e87..df6f4854e1967d45090387c48ffdd0fb65521016 100644 (file)
@@ -5,11 +5,12 @@ import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, Router } from '@
 import { of as observableOf } from 'rxjs';
 import { catchError, map } from 'rxjs/operators';
 
-import { ServicesModule } from './services.module';
+import { MgrModuleService } from '~/app/shared/api/mgr-module.service';
+import { Icons } from '~/app/shared/enum/icons.enum';
 
 /**
  * This service checks if a route can be activated by executing a
- * REST API call to '/api/<apiPath>/status'. If the returned response
+ * REST API call to '/ui-api/<uiApiPath>/status'. If the returned response
  * states that the module is not available, then the user is redirected
  * to the specified <redirectTo> URL path.
  *
@@ -25,7 +26,7 @@ import { ServicesModule } from './services.module';
  *   canActivate: [AuthGuardService, ModuleStatusGuardService],
  *   data: {
  *     moduleStatusGuardConfig: {
- *       apiPath: 'rgw',
+ *       uiApiPath: 'rgw',
  *       redirectTo: 'rgw/501'
  *     }
  *   }
@@ -33,13 +34,17 @@ import { ServicesModule } from './services.module';
  * ...
  */
 @Injectable({
-  providedIn: ServicesModule
+  providedIn: 'root'
 })
 export class ModuleStatusGuardService implements CanActivate, CanActivateChild {
-  // TODO: Hotfix - remove WHITELIST'ing when a generic ErrorComponent is implemented
-  static readonly WHITELIST: string[] = ['501'];
+  // TODO: Hotfix - remove ALLOWLIST'ing when a generic ErrorComponent is implemented
+  static readonly ALLOWLIST: string[] = ['501'];
 
-  constructor(private http: HttpClient, private router: Router) {}
+  constructor(
+    private http: HttpClient,
+    private router: Router,
+    private mgrModuleService: MgrModuleService
+  ) {}
 
   canActivate(route: ActivatedRouteSnapshot) {
     return this.doCheck(route);
@@ -50,14 +55,40 @@ export class ModuleStatusGuardService implements CanActivate, CanActivateChild {
   }
 
   private doCheck(route: ActivatedRouteSnapshot) {
-    if (route.url.length > 0 && ModuleStatusGuardService.WHITELIST.includes(route.url[0].path)) {
+    if (route.url.length > 0 && ModuleStatusGuardService.ALLOWLIST.includes(route.url[0].path)) {
       return observableOf(true);
     }
     const config = route.data['moduleStatusGuardConfig'];
-    return this.http.get(`/api/${config.apiPath}/status`).pipe(
+    let backendCheck = false;
+    if (config.backend) {
+      this.mgrModuleService.getConfig('orchestrator').subscribe(
+        (resp) => {
+          backendCheck = config.backend === resp['orchestrator'];
+        },
+        () => {
+          this.router.navigate([config.redirectTo]);
+          return observableOf(false);
+        }
+      );
+    }
+    return this.http.get(`ui-api/${config.uiApiPath}/status`).pipe(
       map((resp: any) => {
-        if (!resp.available) {
-          this.router.navigate([config.redirectTo, resp.message || '']);
+        if (!resp.available && !backendCheck) {
+          this.router.navigate([config.redirectTo || ''], {
+            state: {
+              header: config.header,
+              message: resp.message,
+              section: config.section,
+              section_info: config.section_info,
+              button_name: config.button_name,
+              button_route: config.button_route,
+              button_title: config.button_title,
+              uiConfig: config.uiConfig,
+              uiApiPath: config.uiApiPath,
+              icon: Icons.wrench,
+              component: config.component
+            }
+          });
         }
         return resp.available;
       }),