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 { ListWithDetails } from '../../../shared/classes/list-with-details.class';
9 import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
10 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
11 import { Icons } from '../../../shared/enum/icons.enum';
12 import { CdTableAction } from '../../../shared/models/cd-table-action';
13 import { CdTableColumn } from '../../../shared/models/cd-table-column';
14 import { CdTableFetchDataContext } from '../../../shared/models/cd-table-fetch-data-context';
15 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
16 import { FinishedTask } from '../../../shared/models/finished-task';
17 import { Permissions } from '../../../shared/models/permissions';
18 import { CephShortVersionPipe } from '../../../shared/pipes/ceph-short-version.pipe';
19 import { JoinPipe } from '../../../shared/pipes/join.pipe';
20 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
21 import { DepCheckerService } from '../../../shared/services/dep-checker.service';
22 import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
23 import { URLBuilderService } from '../../../shared/services/url-builder.service';
25 const BASE_URL = 'hosts';
29 templateUrl: './hosts.component.html',
30 styleUrls: ['./hosts.component.scss'],
31 providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }]
33 export class HostsComponent extends ListWithDetails implements OnInit {
34 permissions: Permissions;
35 columns: Array<CdTableColumn> = [];
36 hosts: Array<object> = [];
37 isLoadingHosts = false;
38 cdParams = { fromLink: '/hosts' };
39 tableActions: CdTableAction[];
40 selection = new CdTableSelection();
43 @ViewChild('servicesTpl', { static: true })
44 public servicesTpl: TemplateRef<any>;
47 private authStorageService: AuthStorageService,
48 private hostService: HostService,
49 private cephShortVersionPipe: CephShortVersionPipe,
50 private joinPipe: JoinPipe,
52 private urlBuilder: URLBuilderService,
53 private actionLabels: ActionLabelsI18n,
54 private modalService: BsModalService,
55 private taskWrapper: TaskWrapperService,
56 private router: Router,
57 private depCheckerService: DepCheckerService
60 this.permissions = this.authStorageService.getPermissions();
63 name: this.actionLabels.CREATE,
67 this.depCheckerService.checkOrchestratorOrModal(
68 this.actionLabels.CREATE,
71 this.router.navigate([this.urlBuilder.getCreate()]);
77 name: this.actionLabels.DELETE,
81 this.depCheckerService.checkOrchestratorOrModal(
82 this.actionLabels.DELETE,
84 () => this.deleteHostModal()
87 disable: () => !this.selection.hasSelection
95 name: this.i18n('Hostname'),
100 name: this.i18n('Services'),
103 cellTemplate: this.servicesTpl
106 name: this.i18n('Labels'),
112 name: this.i18n('Version'),
113 prop: 'ceph_version',
115 pipe: this.cephShortVersionPipe
120 updateSelection(selection: CdTableSelection) {
121 this.selection = selection;
125 const hostname = this.selection.first().hostname;
126 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
128 itemDescription: 'Host',
129 itemNames: [hostname],
130 actionDescription: 'delete',
131 submitActionObservable: () =>
132 this.taskWrapper.wrapTaskAroundCall({
133 task: new FinishedTask('host/delete', { hostname: hostname }),
134 call: this.hostService.delete(hostname)
140 getHosts(context: CdTableFetchDataContext) {
141 if (this.isLoadingHosts) {
144 const typeToPermissionKey = {
149 'rbd-mirror': 'rbdMirroring',
151 'tcmu-runner': 'iscsi'
153 this.isLoadingHosts = true;
154 this.hostService.list().subscribe(
157 host.services.map((service: any) => {
158 service.cdLink = `/perf_counters/${service.type}/${encodeURIComponent(service.id)}`;
159 const permission = this.permissions[typeToPermissionKey[service.type]];
160 service.canRead = permission ? permission.read : false;
166 this.isLoadingHosts = false;
169 this.isLoadingHosts = false;