]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/hosts/hosts.component.ts
import 15.2.4
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / cluster / hosts / hosts.component.ts
1 import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
2 import { Router } from '@angular/router';
3
4 import { I18n } from '@ngx-translate/i18n-polyfill';
5 import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
6
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';
24
25 const BASE_URL = 'hosts';
26
27 @Component({
28 selector: 'cd-hosts',
29 templateUrl: './hosts.component.html',
30 styleUrls: ['./hosts.component.scss'],
31 providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }]
32 })
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();
41 modalRef: BsModalRef;
42
43 @ViewChild('servicesTpl', { static: true })
44 public servicesTpl: TemplateRef<any>;
45
46 constructor(
47 private authStorageService: AuthStorageService,
48 private hostService: HostService,
49 private cephShortVersionPipe: CephShortVersionPipe,
50 private joinPipe: JoinPipe,
51 private i18n: I18n,
52 private urlBuilder: URLBuilderService,
53 private actionLabels: ActionLabelsI18n,
54 private modalService: BsModalService,
55 private taskWrapper: TaskWrapperService,
56 private router: Router,
57 private depCheckerService: DepCheckerService
58 ) {
59 super();
60 this.permissions = this.authStorageService.getPermissions();
61 this.tableActions = [
62 {
63 name: this.actionLabels.CREATE,
64 permission: 'create',
65 icon: Icons.add,
66 click: () => {
67 this.depCheckerService.checkOrchestratorOrModal(
68 this.actionLabels.CREATE,
69 this.i18n('Host'),
70 () => {
71 this.router.navigate([this.urlBuilder.getCreate()]);
72 }
73 );
74 }
75 },
76 {
77 name: this.actionLabels.DELETE,
78 permission: 'delete',
79 icon: Icons.destroy,
80 click: () => {
81 this.depCheckerService.checkOrchestratorOrModal(
82 this.actionLabels.DELETE,
83 this.i18n('Host'),
84 () => this.deleteHostModal()
85 );
86 },
87 disable: () => !this.selection.hasSelection
88 }
89 ];
90 }
91
92 ngOnInit() {
93 this.columns = [
94 {
95 name: this.i18n('Hostname'),
96 prop: 'hostname',
97 flexGrow: 1
98 },
99 {
100 name: this.i18n('Services'),
101 prop: 'services',
102 flexGrow: 3,
103 cellTemplate: this.servicesTpl
104 },
105 {
106 name: this.i18n('Labels'),
107 prop: 'labels',
108 flexGrow: 1,
109 pipe: this.joinPipe
110 },
111 {
112 name: this.i18n('Version'),
113 prop: 'ceph_version',
114 flexGrow: 1,
115 pipe: this.cephShortVersionPipe
116 }
117 ];
118 }
119
120 updateSelection(selection: CdTableSelection) {
121 this.selection = selection;
122 }
123
124 deleteHostModal() {
125 const hostname = this.selection.first().hostname;
126 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
127 initialState: {
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)
135 })
136 }
137 });
138 }
139
140 getHosts(context: CdTableFetchDataContext) {
141 if (this.isLoadingHosts) {
142 return;
143 }
144 const typeToPermissionKey = {
145 mds: 'cephfs',
146 mon: 'monitor',
147 osd: 'osd',
148 rgw: 'rgw',
149 'rbd-mirror': 'rbdMirroring',
150 mgr: 'manager',
151 'tcmu-runner': 'iscsi'
152 };
153 this.isLoadingHosts = true;
154 this.hostService.list().subscribe(
155 (resp: any[]) => {
156 resp.map((host) => {
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;
161 return service;
162 });
163 return host;
164 });
165 this.hosts = resp;
166 this.isLoadingHosts = false;
167 },
168 () => {
169 this.isLoadingHosts = false;
170 context.error();
171 }
172 );
173 }
174 }