]> git.proxmox.com Git - ceph.git/blob - 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
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 { 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';
22
23 const BASE_URL = 'hosts';
24
25 @Component({
26 selector: 'cd-hosts',
27 templateUrl: './hosts.component.html',
28 styleUrls: ['./hosts.component.scss'],
29 providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }]
30 })
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();
39 modalRef: BsModalRef;
40
41 @ViewChild('servicesTpl', { static: true })
42 public servicesTpl: TemplateRef<any>;
43
44 constructor(
45 private authStorageService: AuthStorageService,
46 private hostService: HostService,
47 private cephShortVersionPipe: CephShortVersionPipe,
48 private i18n: I18n,
49 private urlBuilder: URLBuilderService,
50 private actionLabels: ActionLabelsI18n,
51 private modalService: BsModalService,
52 private taskWrapper: TaskWrapperService,
53 private router: Router,
54 private depCheckerService: DepCheckerService
55 ) {
56 this.permissions = this.authStorageService.getPermissions();
57 this.tableActions = [
58 {
59 name: this.actionLabels.CREATE,
60 permission: 'create',
61 icon: Icons.add,
62 click: () => {
63 this.depCheckerService.checkOrchestratorOrModal(
64 this.actionLabels.CREATE,
65 this.i18n('Host'),
66 () => {
67 this.router.navigate([this.urlBuilder.getCreate()]);
68 }
69 );
70 }
71 },
72 {
73 name: this.actionLabels.DELETE,
74 permission: 'delete',
75 icon: Icons.destroy,
76 click: () => {
77 this.depCheckerService.checkOrchestratorOrModal(
78 this.actionLabels.DELETE,
79 this.i18n('Host'),
80 () => this.deleteHostModal()
81 );
82 },
83 disable: () => !this.selection.hasSelection
84 }
85 ];
86 }
87
88 ngOnInit() {
89 this.columns = [
90 {
91 name: this.i18n('Hostname'),
92 prop: 'hostname',
93 flexGrow: 1
94 },
95 {
96 name: this.i18n('Services'),
97 prop: 'services',
98 flexGrow: 3,
99 cellTemplate: this.servicesTpl
100 },
101 {
102 name: this.i18n('Version'),
103 prop: 'ceph_version',
104 flexGrow: 1,
105 pipe: this.cephShortVersionPipe
106 }
107 ];
108 }
109
110 updateSelection(selection: CdTableSelection) {
111 this.selection = selection;
112 }
113
114 deleteHostModal() {
115 const hostname = this.selection.first().hostname;
116 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
117 initialState: {
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)
125 })
126 }
127 });
128 }
129
130 getHosts(context: CdTableFetchDataContext) {
131 if (this.isLoadingHosts) {
132 return;
133 }
134 const typeToPermissionKey = {
135 mds: 'cephfs',
136 mon: 'monitor',
137 osd: 'osd',
138 rgw: 'rgw',
139 'rbd-mirror': 'rbdMirroring',
140 mgr: 'manager',
141 'tcmu-runner': 'iscsi'
142 };
143 this.isLoadingHosts = true;
144 this.hostService.list().subscribe(
145 (resp: any[]) => {
146 resp.map((host) => {
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;
151 return service;
152 });
153 return host;
154 });
155 this.hosts = resp;
156 this.isLoadingHosts = false;
157 },
158 () => {
159 this.isLoadingHosts = false;
160 context.error();
161 }
162 );
163 }
164 }