All files / src/app/ceph/nfs/nfs-list nfs-list.component.ts

86.05% Statements 74/86
70% Branches 35/50
68% Functions 17/25
86.59% Lines 71/82

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 2226x   6x 6x 6x     6x 6x 6x 6x 6x 6x     6x 6x   6x 6x 6x               6x   6x   6x     6x       23x         23x       23x   1x               23x 23x 23x 23x 23x 23x 23x 23x   23x 23x           23x     16x 32x       23x             23x             23x     22x 22x                                                                     22x   6x         6x 6x 6x 6x     6x 7x 6x 6x 1x                       6x 22x         6x 6x 6x 18x 18x 18x     6x     6x 1x 1x     6x 20x           6x 4x     6x       6x                                     6x  
import { Component, OnDestroy, OnInit, TemplateRef, ViewChild } from '@angular/core';
 
import { I18n } from '@ngx-translate/i18n-polyfill';
import * as _ from 'lodash';
import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
import { Subscription } from 'rxjs';
 
import { NfsService } from '../../../shared/api/nfs.service';
import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
import { TableComponent } from '../../../shared/datatable/table/table.component';
import { CellTemplate } from '../../../shared/enum/cell-template.enum';
import { ViewCacheStatus } from '../../../shared/enum/view-cache-status.enum';
import { CdTableAction } from '../../../shared/models/cd-table-action';
import { CdTableColumn } from '../../../shared/models/cd-table-column';
import { CdTableSelection } from '../../../shared/models/cd-table-selection';
import { FinishedTask } from '../../../shared/models/finished-task';
import { Permission } from '../../../shared/models/permissions';
import { AuthStorageService } from '../../../shared/services/auth-storage.service';
import { TaskListService } from '../../../shared/services/task-list.service';
import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
 
@Component({
  selector: 'cd-nfs-list',
  template: require('./nfs-list.component.html'),
  styles: [],
  providers: [TaskListService]
})
export class NfsListComponent implements OnInit, OnDestroy {
  @ViewChild('nfsState')
  nfsState: TemplateRef<any>;
  @ViewChild('nfsFsal')
  nfsFsal: TemplateRef<any>;
 
  @ViewChild('table')
  table: TableComponent;
 
  columns: CdTableColumn[];
  permission: Permission;
  selection = new CdTableSelection();
  summaryDataSubscription: Subscription;
  viewCacheStatus: any;
  exports: any[];
  tableActions: CdTableAction[];
  isDefaultCluster = false;
 
  modalRef: BsModalRef;
 
  builders = {
    'nfs/create': (metadata) => {
      return {
        path: metadata['path'],
        cluster_id: metadata['cluster_id'],
        fsal: metadata['fsal']
      };
    }
  };
 
  constructor(
    private authStorageService: AuthStorageService,
    private i18n: I18n,
    private modalService: BsModalService,
    private nfsService: NfsService,
    private taskListService: TaskListService,
    private taskWrapper: TaskWrapperService,
    public actionLabels: ActionLabelsI18n
  ) {
    this.permission = this.authStorageService.getPermissions().nfs;
    const getNfsUri = () =>
      this.selection.first() &&
      `${encodeURI(this.selection.first().cluster_id)}/${encodeURI(
        this.selection.first().export_id
      )}`;
 
    const createAction: CdTableAction = {
      permission: 'create',
      icon: 'fa-plus',
      routerLink: () => '/nfs/create',
      canBePrimary: (selection: CdTableSelection) => !selection.hasSingleSelection,
      name: this.actionLabels.CREATE
    };
 
    const editAction: CdTableAction = {
      permission: 'update',
      icon: 'fa-pencil',
      routerLink: () => `/nfs/edit/${getNfsUri()}`,
      name: this.actionLabels.EDIT
    };
 
    const deleteAction: CdTableAction = {
      permission: 'delete',
      icon: 'fa-times',
      click: () => this.deleteNfsModal(),
      name: this.actionLabels.DELETE
    };
 
    this.tableActions = [createAction, editAction, deleteAction];
  }
 
  ngOnInit() {
    this.columns = [
      {
        name: this.i18n('Path'),
        prop: 'path',
        flexGrow: 2,
        cellTransformation: CellTemplate.executing
      },
      {
        name: this.i18n('Pseudo'),
        prop: 'pseudo',
        flexGrow: 2
      },
      {
        name: this.i18n('Cluster'),
        prop: 'cluster_id',
        flexGrow: 2
      },
      {
        name: this.i18n('Daemons'),
        prop: 'daemons',
        flexGrow: 2
      },
      {
        name: this.i18n('Storage Backend'),
        prop: 'fsal',
        flexGrow: 2,
        cellTemplate: this.nfsFsal
      },
      {
        name: this.i18n('Access Type'),
        prop: 'access_type',
        flexGrow: 2
      }
    ];
 
    this.nfsService.daemon().subscribe(
      (daemons: any) => {
        const clusters = _(daemons)
          .map((daemon) => daemon.cluster_id)
          .uniq()
          .value();
 
        this.isDefaultCluster = clusters.length === 1 && clusters[0] === '_default_';
        this.columns[2].isHidden = this.isDefaultCluster;
        Eif (this.table) {
          this.table.updateColumns();
        }
 
        this.taskListService.init(
          () => this.nfsService.list(),
          (resp) => this.prepareResponse(resp),
          (exports) => (this.exports = exports),
          () => this.onFetchError(),
          this.taskFilter,
          this.itemFilter,
          this.builders
        );
      },
      () => {
        this.onFetchError();
      }
    );
  }
 
  ngOnDestroy() {
    Iif (this.summaryDataSubscription) {
      this.summaryDataSubscription.unsubscribe();
    }
  }
 
  prepareResponse(resp: any): any[] {
    let result = [];
    resp.forEach((nfs) => {
      nfs.id = `${nfs.cluster_id}:${nfs.export_id}`;
      nfs.state = 'LOADING';
      result = result.concat(nfs);
    });
 
    return result;
  }
 
  onFetchError() {
    this.table.reset(); // Disable loading indicator.
    this.viewCacheStatus = { status: ViewCacheStatus.ValueException };
  }
 
  itemFilter(entry, task) {
    return (
      entry.cluster_id === task.metadata['cluster_id'] &&
      entry.export_id === task.metadata['export_id']
    );
  }
 
  taskFilter(task) {
    return ['nfs/create', 'nfs/delete', 'nfs/edit'].includes(task.name);
  }
 
  updateSelection(selection: CdTableSelection) {
    this.selection = selection;
  }
 
  deleteNfsModal() {
    const cluster_id = this.selection.first().cluster_id;
    const export_id = this.selection.first().export_id;
 
    this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
      initialState: {
        itemDescription: this.i18n('NFS export'),
        itemNames: [`${cluster_id}:${export_id}`],
        submitActionObservable: () =>
          this.taskWrapper.wrapTaskAroundCall({
            task: new FinishedTask('nfs/delete', {
              cluster_id: cluster_id,
              export_id: export_id
            }),
            call: this.nfsService.delete(cluster_id, export_id)
          })
      }
    });
  }
}