# All files / src/app/ceph/block/iscsi-target-details iscsi-target-details.component.ts

88.24% Statements 60/68
72.73% Branches 16/22
81.25% Functions 13/16
87.5% Lines 56/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    2744x

    4x
    4x

    4x

    4x
    4x

    4x

    4x

    4x

    4x

    4x
    7x

    1x

    6x

    6x

    4x
    6x

    4x

    5x
    5x

    5x

    5x
    5x

    5x
    5x
    5x
    5x

    5x

    5x
    5x
    5x

    5x
    5x
    5x

    5x
    5x
    5x

    5x

    5x
    5x

    5x

    4x

    3x
    3x

    1x
    1x
    2x

    1x
    1x
    2x

    1x

    1x
    1x
    1x

    1x

    4x
     import { Component, Input, OnChanges, OnInit, TemplateRef, ViewChild } from '@angular/core';

    import { I18n } from '@ngx-translate/i18n-polyfill';
    import * as _ from 'lodash';
    import { NodeEvent, TreeModel } from 'ng2-tree';

    import { TableComponent } from '../../../shared/datatable/table/table.component';
    import { CdTableColumn } from '../../../shared/models/cd-table-column';
    import { CdTableSelection } from '../../../shared/models/cd-table-selection';
    import { IscsiBackstorePipe } from '../../../shared/pipes/iscsi-backstore.pipe';

    @Component({
      selector: 'cd-iscsi-target-details',
      template: require('./iscsi-target-details.component.html'),
      styles: []
    })
    export class IscsiTargetDetailsComponent implements OnChanges, OnInit {
      @Input()
      selection: CdTableSelection;
      @Input()
      settings: any;

      @ViewChild('highlightTpl')
      highlightTpl: TemplateRef<any>;

      private detailTable: TableComponent;
      @ViewChild('detailTable')
      set content(content: TableComponent) {
        this.detailTable = content;
        if (content) {
          content.updateColumns();
        }
      }

      columns: CdTableColumn[];
      data: any;
      metadata: any = {};
      selectedItem: any;
      title: string;
      tree: TreeModel;

      constructor(private i18n: I18n, private iscsiBackstorePipe: IscsiBackstorePipe) {}

      ngOnInit() {
        this.columns = [
          {
            prop: 'displayName',
            name: this.i18n('Name'),
            flexGrow: 2,
            cellTemplate: this.highlightTpl
          },
          {
            prop: 'current',
            name: this.i18n('Current'),
            flexGrow: 1,
            cellTemplate: this.highlightTpl
          },
          {
            prop: 'default',
            name: this.i18n('Default'),
            flexGrow: 1,
            cellTemplate: this.highlightTpl
          }
        ];
      }

      ngOnChanges() {
        if (this.selection.hasSelection) {
          this.selectedItem = this.selection.first();
          this.generateTree();
        }

        this.data = undefined;
      }

      private generateTree() {
        this.metadata = { root: this.selectedItem.target_controls };

        const cssClasses = {
          target: {
            expanded: 'fa fa-fw fa-bullseye fa-lg'
          },
          initiators: {
            expanded: 'fa fa-fw fa-user fa-lg',
            leaf: 'fa fa-fw fa-user'
          },
          groups: {
            expanded: 'fa fa-fw fa-users fa-lg',
            leaf: 'fa fa-fw fa-users'
          },
          disks: {
            expanded: 'fa fa-fw fa-hdd-o fa-lg',
            leaf: 'fa fa-fw fa-hdd-o'
          },
          portals: {
            expanded: 'fa fa-fw fa-server fa-lg',
            leaf: 'fa fa-fw fa-server fa-lg'
          }
        };

        const disks = [];
        _.forEach(this.selectedItem.disks, (disk) => {
          const id = 'disk_' + disk.pool + '_' + disk.image;
          this.metadata[id] = {
            controls: disk.controls,
            backstore: disk.backstore
          };

          disks.push({
            value: `${disk.pool}/${disk.image}`,
            id: id
          });
        });

        const portals = [];
        _.forEach(this.selectedItem.portals, (portal) => {
          portals.push({ value: `${portal.host}:${portal.ip}` });
        });

        const clients = [];
        _.forEach(this.selectedItem.clients, (client) => {
          this.metadata['client_' + client.client_iqn] = client.auth;

          const luns = [];
          client.luns.forEach((lun) => {
            luns.push({
              value: `${lun.pool}/${lun.image}`,
              id: 'disk_' + lun.pool + '_' + lun.image,
              settings: {
                cssClasses: cssClasses.disks
              }
            });
          });

          clients.push({
            value: client.client_iqn,
            id: 'client_' + client.client_iqn,
            children: luns
          });
        });

        const groups = [];
        _.forEach(this.selectedItem.groups, (group) => {
          const luns = [];
          group.disks.forEach((disk) => {
            luns.push({
              value: `${disk.pool}/${disk.image}`,
              id: 'disk_' + disk.pool + '_' + disk.image
            });
          });

          const initiators = [];
          group.members.forEach((member) => {
            initiators.push({
              value: member,
              id: 'client_' + member
            });
          });

          groups.push({
            value: group.group_id,
            children: [
              {
                value: 'Disks',
                children: luns,
                settings: {
                  selectionAllowed: false,
                  cssClasses: cssClasses.disks
                }
              },
              {
                value: 'Initiators',
                children: initiators,
                settings: {
                  selectionAllowed: false,
                  cssClasses: cssClasses.initiators
                }
              }
            ]
          });
        });

        this.tree = {
          value: this.selectedItem.target_iqn,
          id: 'root',
          settings: {
            static: true,
            cssClasses: cssClasses.target
          },
          children: [
            {
              value: 'Disks',
              children: disks,
              settings: {
                selectionAllowed: false,
                cssClasses: cssClasses.disks
              }
            },
            {
              value: 'Portals',
              children: portals,
              settings: {
                selectionAllowed: false,
                cssClasses: cssClasses.portals
              }
            },
            {
              value: 'Initiators',
              children: clients,
              settings: {
                selectionAllowed: false,
                cssClasses: cssClasses.initiators
              }
            },
            {
              value: 'Groups',
              children: groups,
              settings: {
                selectionAllowed: false,
                cssClasses: cssClasses.groups
              }
            }
          ]
        };
      }

      onNodeSelected(e: NodeEvent) {
        if (e.node.id) {
          this.title = e.node.value;
          const tempData = this.metadata[e.node.id] || {};

          if (e.node.id === 'root') {
            this.columns[2].isHidden = false;
            this.data = _.map(this.settings.target_default_controls, (value, key) => {
              return {
                displayName: key,
                default: value,
                current: tempData[key] || value
              };
            });
          } else if (e.node.id.toString().startsWith('disk_')) {
            this.columns[2].isHidden = false;
            this.data = _.map(this.settings.disk_default_controls[tempData.backstore], (value, key) => {
              return {
                displayName: key,
                default: value,
                current: !_.isUndefined(tempData.controls[key]) ? tempData.controls[key] : value
              };
            });
            this.data.push({
              displayName: 'backstore',
              default: this.iscsiBackstorePipe.transform(this.settings.default_backstore),
              current: this.iscsiBackstorePipe.transform(tempData.backstore)
            });
          } else {
            this.columns[2].isHidden = true;
            this.data = _.map(tempData, (value, key) => {
              return {
                displayName: key,
                default: undefined,
                current: value
              };
            });
          }
        } else {
          this.data = undefined;
        }

        if (this.detailTable) {
          this.detailTable.updateColumns();
        }
      }
    }