]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
814dc44b9f58e7f8ae3b83a5e148b1dea24e76de
[ceph-ci.git] /
1 import { Component, Input, OnChanges, OnInit, TemplateRef, ViewChild } from '@angular/core';
2
3 import { I18n } from '@ngx-translate/i18n-polyfill';
4 import * as _ from 'lodash';
5 import { NodeEvent, TreeModel } from 'ng2-tree';
6
7 import { TableComponent } from '../../../shared/datatable/table/table.component';
8 import { CdTableColumn } from '../../../shared/models/cd-table-column';
9 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
10
11 @Component({
12   selector: 'cd-iscsi-target-details',
13   templateUrl: './iscsi-target-details.component.html',
14   styleUrls: ['./iscsi-target-details.component.scss']
15 })
16 export class IscsiTargetDetailsComponent implements OnChanges, OnInit {
17   @Input()
18   selection: CdTableSelection;
19   @Input()
20   settings: any;
21
22   @ViewChild('highlightTpl')
23   highlightTpl: TemplateRef<any>;
24
25   private detailTable: TableComponent;
26   @ViewChild('detailTable')
27   set content(content: TableComponent) {
28     this.detailTable = content;
29     if (content) {
30       content.updateColumns();
31     }
32   }
33
34   columns: CdTableColumn[];
35   data: any;
36   metadata: any = {};
37   selectedItem: any;
38   title: string;
39   tree: TreeModel;
40
41   constructor(private i18n: I18n) {}
42
43   ngOnInit() {
44     this.columns = [
45       {
46         prop: 'displayName',
47         name: this.i18n('Name'),
48         flexGrow: 2,
49         cellTemplate: this.highlightTpl
50       },
51       {
52         prop: 'current',
53         name: this.i18n('Current'),
54         flexGrow: 1,
55         cellTemplate: this.highlightTpl
56       },
57       {
58         prop: 'default',
59         name: this.i18n('Default'),
60         flexGrow: 1,
61         cellTemplate: this.highlightTpl
62       }
63     ];
64   }
65
66   ngOnChanges() {
67     if (this.selection.hasSelection) {
68       this.selectedItem = this.selection.first();
69       this.generateTree();
70     }
71
72     this.data = undefined;
73   }
74
75   private generateTree() {
76     this.metadata = { root: this.selectedItem.target_controls };
77
78     const cssClasses = {
79       target: {
80         expanded: 'fa fa-fw fa-bullseye fa-lg'
81       },
82       initiators: {
83         expanded: 'fa fa-fw fa-user fa-lg',
84         leaf: 'fa fa-fw fa-user'
85       },
86       groups: {
87         expanded: 'fa fa-fw fa-users fa-lg',
88         leaf: 'fa fa-fw fa-users'
89       },
90       disks: {
91         expanded: 'fa fa-fw fa-hdd-o fa-lg',
92         leaf: 'fa fa-fw fa-hdd-o'
93       },
94       portals: {
95         expanded: 'fa fa-fw fa-server fa-lg',
96         leaf: 'fa fa-fw fa-server fa-lg'
97       }
98     };
99
100     const disks = [];
101     _.forEach(this.selectedItem.disks, (disk) => {
102       const id = 'disk_' + disk.pool + '_' + disk.image;
103       this.metadata[id] = {
104         controls: disk.controls,
105         backstore: disk.backstore
106       };
107
108       disks.push({
109         value: `${disk.pool}/${disk.image}`,
110         id: id
111       });
112     });
113
114     const portals = [];
115     _.forEach(this.selectedItem.portals, (portal) => {
116       portals.push({ value: `${portal.host}:${portal.ip}` });
117     });
118
119     const clients = [];
120     _.forEach(this.selectedItem.clients, (client) => {
121       this.metadata['client_' + client.client_iqn] = client.auth;
122
123       const luns = [];
124       client.luns.forEach((lun) => {
125         luns.push({
126           value: `${lun.pool}/${lun.image}`,
127           id: 'disk_' + lun.pool + '_' + lun.image,
128           settings: {
129             cssClasses: cssClasses.disks
130           }
131         });
132       });
133
134       clients.push({
135         value: client.client_iqn,
136         id: 'client_' + client.client_iqn,
137         children: luns
138       });
139     });
140
141     const groups = [];
142     _.forEach(this.selectedItem.groups, (group) => {
143       const luns = [];
144       group.disks.forEach((disk) => {
145         luns.push({
146           value: `${disk.pool}/${disk.image}`,
147           id: 'disk_' + disk.pool + '_' + disk.image
148         });
149       });
150
151       const initiators = [];
152       group.members.forEach((member) => {
153         initiators.push({
154           value: member,
155           id: 'client_' + member
156         });
157       });
158
159       groups.push({
160         value: group.group_id,
161         children: [
162           {
163             value: 'Disks',
164             children: luns,
165             settings: {
166               selectionAllowed: false,
167               cssClasses: cssClasses.disks
168             }
169           },
170           {
171             value: 'Initiators',
172             children: initiators,
173             settings: {
174               selectionAllowed: false,
175               cssClasses: cssClasses.initiators
176             }
177           }
178         ]
179       });
180     });
181
182     this.tree = {
183       value: this.selectedItem.target_iqn,
184       id: 'root',
185       settings: {
186         static: true,
187         cssClasses: cssClasses.target
188       },
189       children: [
190         {
191           value: 'Disks',
192           children: disks,
193           settings: {
194             selectionAllowed: false,
195             cssClasses: cssClasses.disks
196           }
197         },
198         {
199           value: 'Portals',
200           children: portals,
201           settings: {
202             selectionAllowed: false,
203             cssClasses: cssClasses.portals
204           }
205         },
206         {
207           value: 'Initiators',
208           children: clients,
209           settings: {
210             selectionAllowed: false,
211             cssClasses: cssClasses.initiators
212           }
213         },
214         {
215           value: 'Groups',
216           children: groups,
217           settings: {
218             selectionAllowed: false,
219             cssClasses: cssClasses.groups
220           }
221         }
222       ]
223     };
224   }
225
226   onNodeSelected(e: NodeEvent) {
227     if (e.node.id) {
228       this.title = e.node.value;
229       const tempData = this.metadata[e.node.id] || {};
230
231       if (e.node.id === 'root') {
232         this.columns[2].isHidden = false;
233         this.data = _.map(this.settings.target_default_controls, (value, key) => {
234           return {
235             displayName: key,
236             default: value,
237             current: tempData[key] || value
238           };
239         });
240       } else if (e.node.id.toString().startsWith('disk_')) {
241         this.columns[2].isHidden = false;
242         this.data = _.map(this.settings.disk_default_controls[tempData.backstore], (value, key) => {
243           return {
244             displayName: key,
245             default: value,
246             current: !_.isUndefined(tempData.controls[key]) ? tempData.controls[key] : value
247           };
248         });
249         this.data.push({
250           displayName: 'backstore',
251           default: this.settings.default_backstore,
252           current: tempData.backstore
253         });
254       } else {
255         this.columns[2].isHidden = true;
256         this.data = _.map(tempData, (value, key) => {
257           return {
258             displayName: key,
259             default: undefined,
260             current: value
261           };
262         });
263       }
264     } else {
265       this.data = undefined;
266     }
267
268     if (this.detailTable) {
269       this.detailTable.updateColumns();
270     }
271   }
272 }