]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { Component, OnDestroy, OnInit, TemplateRef, ViewChild } from '@angular/core'; |
2 | ||
3 | import { I18n } from '@ngx-translate/i18n-polyfill'; | |
4 | import * as _ from 'lodash'; | |
5 | import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal'; | |
6 | import { Subscription } from 'rxjs'; | |
7 | ||
8 | import { NfsService } from '../../../shared/api/nfs.service'; | |
9 | import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component'; | |
eafe8130 | 10 | import { ActionLabelsI18n } from '../../../shared/constants/app.constants'; |
11fdf7f2 TL |
11 | import { TableComponent } from '../../../shared/datatable/table/table.component'; |
12 | import { CellTemplate } from '../../../shared/enum/cell-template.enum'; | |
13 | import { ViewCacheStatus } from '../../../shared/enum/view-cache-status.enum'; | |
14 | import { CdTableAction } from '../../../shared/models/cd-table-action'; | |
15 | import { CdTableColumn } from '../../../shared/models/cd-table-column'; | |
16 | import { CdTableSelection } from '../../../shared/models/cd-table-selection'; | |
17 | import { FinishedTask } from '../../../shared/models/finished-task'; | |
18 | import { Permission } from '../../../shared/models/permissions'; | |
19 | import { AuthStorageService } from '../../../shared/services/auth-storage.service'; | |
20 | import { TaskListService } from '../../../shared/services/task-list.service'; | |
21 | import { TaskWrapperService } from '../../../shared/services/task-wrapper.service'; | |
22 | ||
23 | @Component({ | |
24 | selector: 'cd-nfs-list', | |
25 | templateUrl: './nfs-list.component.html', | |
26 | styleUrls: ['./nfs-list.component.scss'], | |
27 | providers: [TaskListService] | |
28 | }) | |
29 | export class NfsListComponent implements OnInit, OnDestroy { | |
30 | @ViewChild('nfsState') | |
31 | nfsState: TemplateRef<any>; | |
32 | @ViewChild('nfsFsal') | |
33 | nfsFsal: TemplateRef<any>; | |
34 | ||
35 | @ViewChild('table') | |
36 | table: TableComponent; | |
37 | ||
38 | columns: CdTableColumn[]; | |
39 | permission: Permission; | |
40 | selection = new CdTableSelection(); | |
41 | summaryDataSubscription: Subscription; | |
42 | viewCacheStatus: any; | |
43 | exports: any[]; | |
44 | tableActions: CdTableAction[]; | |
45 | isDefaultCluster = false; | |
46 | ||
47 | modalRef: BsModalRef; | |
48 | ||
49 | builders = { | |
50 | 'nfs/create': (metadata) => { | |
51 | return { | |
52 | path: metadata['path'], | |
53 | cluster_id: metadata['cluster_id'], | |
54 | fsal: metadata['fsal'] | |
55 | }; | |
56 | } | |
57 | }; | |
58 | ||
59 | constructor( | |
60 | private authStorageService: AuthStorageService, | |
61 | private i18n: I18n, | |
62 | private modalService: BsModalService, | |
63 | private nfsService: NfsService, | |
64 | private taskListService: TaskListService, | |
eafe8130 TL |
65 | private taskWrapper: TaskWrapperService, |
66 | public actionLabels: ActionLabelsI18n | |
11fdf7f2 TL |
67 | ) { |
68 | this.permission = this.authStorageService.getPermissions().nfs; | |
69 | const getNfsUri = () => | |
70 | this.selection.first() && | |
71 | `${encodeURI(this.selection.first().cluster_id)}/${encodeURI( | |
72 | this.selection.first().export_id | |
73 | )}`; | |
74 | ||
eafe8130 | 75 | const createAction: CdTableAction = { |
11fdf7f2 TL |
76 | permission: 'create', |
77 | icon: 'fa-plus', | |
eafe8130 | 78 | routerLink: () => '/nfs/create', |
11fdf7f2 | 79 | canBePrimary: (selection: CdTableSelection) => !selection.hasSingleSelection, |
eafe8130 | 80 | name: this.actionLabels.CREATE |
11fdf7f2 TL |
81 | }; |
82 | ||
83 | const editAction: CdTableAction = { | |
84 | permission: 'update', | |
85 | icon: 'fa-pencil', | |
86 | routerLink: () => `/nfs/edit/${getNfsUri()}`, | |
eafe8130 | 87 | name: this.actionLabels.EDIT |
11fdf7f2 TL |
88 | }; |
89 | ||
90 | const deleteAction: CdTableAction = { | |
91 | permission: 'delete', | |
92 | icon: 'fa-times', | |
93 | click: () => this.deleteNfsModal(), | |
eafe8130 | 94 | name: this.actionLabels.DELETE |
11fdf7f2 TL |
95 | }; |
96 | ||
eafe8130 | 97 | this.tableActions = [createAction, editAction, deleteAction]; |
11fdf7f2 TL |
98 | } |
99 | ||
100 | ngOnInit() { | |
101 | this.columns = [ | |
102 | { | |
eafe8130 | 103 | name: this.i18n('Path'), |
11fdf7f2 TL |
104 | prop: 'path', |
105 | flexGrow: 2, | |
106 | cellTransformation: CellTemplate.executing | |
107 | }, | |
eafe8130 TL |
108 | { |
109 | name: this.i18n('Pseudo'), | |
110 | prop: 'pseudo', | |
111 | flexGrow: 2 | |
112 | }, | |
11fdf7f2 TL |
113 | { |
114 | name: this.i18n('Cluster'), | |
115 | prop: 'cluster_id', | |
116 | flexGrow: 2 | |
117 | }, | |
118 | { | |
119 | name: this.i18n('Daemons'), | |
120 | prop: 'daemons', | |
121 | flexGrow: 2 | |
122 | }, | |
123 | { | |
124 | name: this.i18n('Storage Backend'), | |
125 | prop: 'fsal', | |
126 | flexGrow: 2, | |
127 | cellTemplate: this.nfsFsal | |
128 | }, | |
129 | { | |
130 | name: this.i18n('Access Type'), | |
131 | prop: 'access_type', | |
132 | flexGrow: 2 | |
133 | } | |
134 | ]; | |
135 | ||
136 | this.nfsService.daemon().subscribe( | |
137 | (daemons: any) => { | |
138 | const clusters = _(daemons) | |
139 | .map((daemon) => daemon.cluster_id) | |
140 | .uniq() | |
141 | .value(); | |
142 | ||
143 | this.isDefaultCluster = clusters.length === 1 && clusters[0] === '_default_'; | |
eafe8130 | 144 | this.columns[2].isHidden = this.isDefaultCluster; |
11fdf7f2 TL |
145 | if (this.table) { |
146 | this.table.updateColumns(); | |
147 | } | |
148 | ||
149 | this.taskListService.init( | |
150 | () => this.nfsService.list(), | |
151 | (resp) => this.prepareResponse(resp), | |
152 | (exports) => (this.exports = exports), | |
153 | () => this.onFetchError(), | |
154 | this.taskFilter, | |
155 | this.itemFilter, | |
156 | this.builders | |
157 | ); | |
158 | }, | |
159 | () => { | |
160 | this.onFetchError(); | |
161 | } | |
162 | ); | |
163 | } | |
164 | ||
165 | ngOnDestroy() { | |
166 | if (this.summaryDataSubscription) { | |
167 | this.summaryDataSubscription.unsubscribe(); | |
168 | } | |
169 | } | |
170 | ||
171 | prepareResponse(resp: any): any[] { | |
172 | let result = []; | |
173 | resp.forEach((nfs) => { | |
174 | nfs.id = `${nfs.cluster_id}:${nfs.export_id}`; | |
175 | nfs.state = 'LOADING'; | |
176 | result = result.concat(nfs); | |
177 | }); | |
178 | ||
179 | return result; | |
180 | } | |
181 | ||
182 | onFetchError() { | |
183 | this.table.reset(); // Disable loading indicator. | |
184 | this.viewCacheStatus = { status: ViewCacheStatus.ValueException }; | |
185 | } | |
186 | ||
187 | itemFilter(entry, task) { | |
188 | return ( | |
189 | entry.cluster_id === task.metadata['cluster_id'] && | |
190 | entry.export_id === task.metadata['export_id'] | |
191 | ); | |
192 | } | |
193 | ||
194 | taskFilter(task) { | |
195 | return ['nfs/create', 'nfs/delete', 'nfs/edit'].includes(task.name); | |
196 | } | |
197 | ||
198 | updateSelection(selection: CdTableSelection) { | |
199 | this.selection = selection; | |
200 | } | |
201 | ||
202 | deleteNfsModal() { | |
203 | const cluster_id = this.selection.first().cluster_id; | |
204 | const export_id = this.selection.first().export_id; | |
205 | ||
206 | this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, { | |
207 | initialState: { | |
eafe8130 TL |
208 | itemDescription: this.i18n('NFS export'), |
209 | itemNames: [`${cluster_id}:${export_id}`], | |
11fdf7f2 TL |
210 | submitActionObservable: () => |
211 | this.taskWrapper.wrapTaskAroundCall({ | |
212 | task: new FinishedTask('nfs/delete', { | |
213 | cluster_id: cluster_id, | |
214 | export_id: export_id | |
215 | }), | |
216 | call: this.nfsService.delete(cluster_id, export_id) | |
217 | }) | |
218 | } | |
219 | }); | |
220 | } | |
221 | } |