]>
Commit | Line | Data |
---|---|---|
11fdf7f2 | 1 | import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core'; |
9f95a23c | 2 | import { Router } from '@angular/router'; |
11fdf7f2 TL |
3 | |
4 | import { I18n } from '@ngx-translate/i18n-polyfill'; | |
9f95a23c | 5 | import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal'; |
11fdf7f2 TL |
6 | |
7 | import { HostService } from '../../../shared/api/host.service'; | |
e306af50 | 8 | import { ListWithDetails } from '../../../shared/classes/list-with-details.class'; |
9f95a23c TL |
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'; | |
11fdf7f2 TL |
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'; | |
9f95a23c | 16 | import { FinishedTask } from '../../../shared/models/finished-task'; |
11fdf7f2 TL |
17 | import { Permissions } from '../../../shared/models/permissions'; |
18 | import { CephShortVersionPipe } from '../../../shared/pipes/ceph-short-version.pipe'; | |
e306af50 | 19 | import { JoinPipe } from '../../../shared/pipes/join.pipe'; |
11fdf7f2 | 20 | import { AuthStorageService } from '../../../shared/services/auth-storage.service'; |
9f95a23c TL |
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'; | |
11fdf7f2 TL |
26 | |
27 | @Component({ | |
28 | selector: 'cd-hosts', | |
29 | templateUrl: './hosts.component.html', | |
9f95a23c TL |
30 | styleUrls: ['./hosts.component.scss'], |
31 | providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }] | |
11fdf7f2 | 32 | }) |
e306af50 | 33 | export class HostsComponent extends ListWithDetails implements OnInit { |
11fdf7f2 TL |
34 | permissions: Permissions; |
35 | columns: Array<CdTableColumn> = []; | |
36 | hosts: Array<object> = []; | |
37 | isLoadingHosts = false; | |
38 | cdParams = { fromLink: '/hosts' }; | |
9f95a23c | 39 | tableActions: CdTableAction[]; |
11fdf7f2 | 40 | selection = new CdTableSelection(); |
9f95a23c | 41 | modalRef: BsModalRef; |
11fdf7f2 | 42 | |
9f95a23c | 43 | @ViewChild('servicesTpl', { static: true }) |
11fdf7f2 TL |
44 | public servicesTpl: TemplateRef<any>; |
45 | ||
46 | constructor( | |
47 | private authStorageService: AuthStorageService, | |
48 | private hostService: HostService, | |
49 | private cephShortVersionPipe: CephShortVersionPipe, | |
e306af50 | 50 | private joinPipe: JoinPipe, |
9f95a23c TL |
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 | |
11fdf7f2 | 58 | ) { |
e306af50 | 59 | super(); |
11fdf7f2 | 60 | this.permissions = this.authStorageService.getPermissions(); |
9f95a23c TL |
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 | ]; | |
11fdf7f2 TL |
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 | }, | |
e306af50 TL |
105 | { |
106 | name: this.i18n('Labels'), | |
107 | prop: 'labels', | |
108 | flexGrow: 1, | |
109 | pipe: this.joinPipe | |
110 | }, | |
11fdf7f2 TL |
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 | ||
9f95a23c TL |
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 | ||
11fdf7f2 TL |
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; | |
9f95a23c TL |
154 | this.hostService.list().subscribe( |
155 | (resp: any[]) => { | |
11fdf7f2 | 156 | resp.map((host) => { |
9f95a23c | 157 | host.services.map((service: any) => { |
81eedcae | 158 | service.cdLink = `/perf_counters/${service.type}/${encodeURIComponent(service.id)}`; |
11fdf7f2 TL |
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; | |
9f95a23c TL |
167 | }, |
168 | () => { | |
11fdf7f2 TL |
169 | this.isLoadingHosts = false; |
170 | context.error(); | |
9f95a23c TL |
171 | } |
172 | ); | |
11fdf7f2 TL |
173 | } |
174 | } |