]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/hosts/hosts.component.ts
import 15.2.0 Octopus source
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / cluster / hosts / hosts.component.ts
index fa107253fd7a14e27bfccea59b623c7b4660e88a..d6812be0d69d80809195846cc7aeeb5a75ff28e6 100644 (file)
@@ -1,19 +1,32 @@
 import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
+import { Router } from '@angular/router';
 
 import { I18n } from '@ngx-translate/i18n-polyfill';
+import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
 
 import { HostService } from '../../../shared/api/host.service';
+import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
+import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
+import { Icons } from '../../../shared/enum/icons.enum';
+import { CdTableAction } from '../../../shared/models/cd-table-action';
 import { CdTableColumn } from '../../../shared/models/cd-table-column';
 import { CdTableFetchDataContext } from '../../../shared/models/cd-table-fetch-data-context';
 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
+import { FinishedTask } from '../../../shared/models/finished-task';
 import { Permissions } from '../../../shared/models/permissions';
 import { CephShortVersionPipe } from '../../../shared/pipes/ceph-short-version.pipe';
 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
+import { DepCheckerService } from '../../../shared/services/dep-checker.service';
+import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
+import { URLBuilderService } from '../../../shared/services/url-builder.service';
+
+const BASE_URL = 'hosts';
 
 @Component({
   selector: 'cd-hosts',
   templateUrl: './hosts.component.html',
-  styleUrls: ['./hosts.component.scss']
+  styleUrls: ['./hosts.component.scss'],
+  providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }]
 })
 export class HostsComponent implements OnInit {
   permissions: Permissions;
@@ -21,18 +34,55 @@ export class HostsComponent implements OnInit {
   hosts: Array<object> = [];
   isLoadingHosts = false;
   cdParams = { fromLink: '/hosts' };
+  tableActions: CdTableAction[];
   selection = new CdTableSelection();
+  modalRef: BsModalRef;
 
-  @ViewChild('servicesTpl')
+  @ViewChild('servicesTpl', { static: true })
   public servicesTpl: TemplateRef<any>;
 
   constructor(
     private authStorageService: AuthStorageService,
     private hostService: HostService,
     private cephShortVersionPipe: CephShortVersionPipe,
-    private i18n: I18n
+    private i18n: I18n,
+    private urlBuilder: URLBuilderService,
+    private actionLabels: ActionLabelsI18n,
+    private modalService: BsModalService,
+    private taskWrapper: TaskWrapperService,
+    private router: Router,
+    private depCheckerService: DepCheckerService
   ) {
     this.permissions = this.authStorageService.getPermissions();
+    this.tableActions = [
+      {
+        name: this.actionLabels.CREATE,
+        permission: 'create',
+        icon: Icons.add,
+        click: () => {
+          this.depCheckerService.checkOrchestratorOrModal(
+            this.actionLabels.CREATE,
+            this.i18n('Host'),
+            () => {
+              this.router.navigate([this.urlBuilder.getCreate()]);
+            }
+          );
+        }
+      },
+      {
+        name: this.actionLabels.DELETE,
+        permission: 'delete',
+        icon: Icons.destroy,
+        click: () => {
+          this.depCheckerService.checkOrchestratorOrModal(
+            this.actionLabels.DELETE,
+            this.i18n('Host'),
+            () => this.deleteHostModal()
+          );
+        },
+        disable: () => !this.selection.hasSelection
+      }
+    ];
   }
 
   ngOnInit() {
@@ -61,6 +111,22 @@ export class HostsComponent implements OnInit {
     this.selection = selection;
   }
 
+  deleteHostModal() {
+    const hostname = this.selection.first().hostname;
+    this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
+      initialState: {
+        itemDescription: 'Host',
+        itemNames: [hostname],
+        actionDescription: 'delete',
+        submitActionObservable: () =>
+          this.taskWrapper.wrapTaskAroundCall({
+            task: new FinishedTask('host/delete', { hostname: hostname }),
+            call: this.hostService.delete(hostname)
+          })
+      }
+    });
+  }
+
   getHosts(context: CdTableFetchDataContext) {
     if (this.isLoadingHosts) {
       return;
@@ -75,11 +141,10 @@ export class HostsComponent implements OnInit {
       'tcmu-runner': 'iscsi'
     };
     this.isLoadingHosts = true;
-    this.hostService
-      .list()
-      .then((resp) => {
+    this.hostService.list().subscribe(
+      (resp: any[]) => {
         resp.map((host) => {
-          host.services.map((service) => {
+          host.services.map((service: any) => {
             service.cdLink = `/perf_counters/${service.type}/${encodeURIComponent(service.id)}`;
             const permission = this.permissions[typeToPermissionKey[service.type]];
             service.canRead = permission ? permission.read : false;
@@ -89,10 +154,11 @@ export class HostsComponent implements OnInit {
         });
         this.hosts = resp;
         this.isLoadingHosts = false;
-      })
-      .catch(() => {
+      },
+      () => {
         this.isLoadingHosts = false;
         context.error();
-      });
+      }
+    );
   }
 }