1 import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
2 import { Router } from '@angular/router';
4 import { I18n } from '@ngx-translate/i18n-polyfill';
5 import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
7 import { HostService } from '../../../shared/api/host.service';
8 import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
9 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
10 import { Icons } from '../../../shared/enum/icons.enum';
11 import { CdTableAction } from '../../../shared/models/cd-table-action';
12 import { CdTableColumn } from '../../../shared/models/cd-table-column';
13 import { CdTableFetchDataContext } from '../../../shared/models/cd-table-fetch-data-context';
14 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
15 import { FinishedTask } from '../../../shared/models/finished-task';
16 import { Permissions } from '../../../shared/models/permissions';
17 import { CephShortVersionPipe } from '../../../shared/pipes/ceph-short-version.pipe';
18 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
19 import { DepCheckerService } from '../../../shared/services/dep-checker.service';
20 import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
21 import { URLBuilderService } from '../../../shared/services/url-builder.service';
23 const BASE_URL = 'hosts';
27 templateUrl: './hosts.component.html',
28 styleUrls: ['./hosts.component.scss'],
29 providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }]
31 export class HostsComponent implements OnInit {
32 permissions: Permissions;
33 columns: Array<CdTableColumn> = [];
34 hosts: Array<object> = [];
35 isLoadingHosts = false;
36 cdParams = { fromLink: '/hosts' };
37 tableActions: CdTableAction[];
38 selection = new CdTableSelection();
41 @ViewChild('servicesTpl', { static: true })
42 public servicesTpl: TemplateRef<any>;
45 private authStorageService: AuthStorageService,
46 private hostService: HostService,
47 private cephShortVersionPipe: CephShortVersionPipe,
49 private urlBuilder: URLBuilderService,
50 private actionLabels: ActionLabelsI18n,
51 private modalService: BsModalService,
52 private taskWrapper: TaskWrapperService,
53 private router: Router,
54 private depCheckerService: DepCheckerService
56 this.permissions = this.authStorageService.getPermissions();
59 name: this.actionLabels.CREATE,
63 this.depCheckerService.checkOrchestratorOrModal(
64 this.actionLabels.CREATE,
67 this.router.navigate([this.urlBuilder.getCreate()]);
73 name: this.actionLabels.DELETE,
77 this.depCheckerService.checkOrchestratorOrModal(
78 this.actionLabels.DELETE,
80 () => this.deleteHostModal()
83 disable: () => !this.selection.hasSelection
91 name: this.i18n('Hostname'),
96 name: this.i18n('Services'),
99 cellTemplate: this.servicesTpl
102 name: this.i18n('Version'),
103 prop: 'ceph_version',
105 pipe: this.cephShortVersionPipe
110 updateSelection(selection: CdTableSelection) {
111 this.selection = selection;
115 const hostname = this.selection.first().hostname;
116 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
118 itemDescription: 'Host',
119 itemNames: [hostname],
120 actionDescription: 'delete',
121 submitActionObservable: () =>
122 this.taskWrapper.wrapTaskAroundCall({
123 task: new FinishedTask('host/delete', { hostname: hostname }),
124 call: this.hostService.delete(hostname)
130 getHosts(context: CdTableFetchDataContext) {
131 if (this.isLoadingHosts) {
134 const typeToPermissionKey = {
139 'rbd-mirror': 'rbdMirroring',
141 'tcmu-runner': 'iscsi'
143 this.isLoadingHosts = true;
144 this.hostService.list().subscribe(
147 host.services.map((service: any) => {
148 service.cdLink = `/perf_counters/${service.type}/${encodeURIComponent(service.id)}`;
149 const permission = this.permissions[typeToPermissionKey[service.type]];
150 service.canRead = permission ? permission.read : false;
156 this.isLoadingHosts = false;
159 this.isLoadingHosts = false;