All files / src/app/ceph/cluster/mgr-modules/mgr-module-list mgr-module-list.component.ts

75% Statements 60/80
56.82% Branches 25/44
47.83% Functions 11/23
73.68% Lines 56/76

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 1936x   6x 6x 6x   6x 6x 6x       6x   6x 6x             6x   6x   6x       9x 9x 9x   9x 9x 9x 9x 9x   9x 9x                           9x   9x         8x 8x                                                   6x 5x                   6x                   6x 2x     2x       2x 1x     1x 1x                   6x                       6x 2x         2x 3x     3x     2x   2x   2x     1x               2x 2x 1x   1x   2x       2x     2x 2x       6x  
import { Component, ViewChild } from '@angular/core';
 
import { I18n } from '@ngx-translate/i18n-polyfill';
import { BlockUI, NgBlockUI } from 'ng-block-ui';
import { timer as observableTimer } from 'rxjs';
 
import { MgrModuleService } from '../../../../shared/api/mgr-module.service';
import { TableComponent } from '../../../../shared/datatable/table/table.component';
import { CellTemplate } from '../../../../shared/enum/cell-template.enum';
import { CdTableAction } from '../../../../shared/models/cd-table-action';
import { CdTableColumn } from '../../../../shared/models/cd-table-column';
import { CdTableFetchDataContext } from '../../../../shared/models/cd-table-fetch-data-context';
import { CdTableSelection } from '../../../../shared/models/cd-table-selection';
import { Permission } from '../../../../shared/models/permissions';
import { AuthStorageService } from '../../../../shared/services/auth-storage.service';
import { NotificationService } from '../../../../shared/services/notification.service';
 
@Component({
  selector: 'cd-mgr-module-list',
  template: require('./mgr-module-list.component.html'),
  styles: []
})
export class MgrModuleListComponent {
  @ViewChild(TableComponent)
  table: TableComponent;
  @BlockUI()
  blockUI: NgBlockUI;
 
  permission: Permission;
  tableActions: CdTableAction[];
  columns: CdTableColumn[] = [];
  modules: object[] = [];
  selection: CdTableSelection = new CdTableSelection();
 
  constructor(
    private authStorageService: AuthStorageService,
    private mgrModuleService: MgrModuleService,
    private notificationService: NotificationService,
    private i18n: I18n
  ) {
    this.permission = this.authStorageService.getPermissions().configOpt;
    this.columns = [
      {
        name: this.i18n('Name'),
        prop: 'name',
        flexGrow: 1
      },
      {
        name: this.i18n('Enabled'),
        prop: 'enabled',
        flexGrow: 1,
        cellClass: 'text-center',
        cellTransformation: CellTemplate.checkIcon
      }
    ];
    const getModuleUri = () =>
      this.selection.first() && encodeURIComponent(this.selection.first().name);
    this.tableActions = [
      {
        name: this.i18n('Edit'),
        permission: 'update',
        disable: () => {
          Eif (!this.selection.hasSelection) {
            return true;
          }
          // Disable the 'edit' button when the module has no options.
          return Object.values(this.selection.first().options).length === 0;
        },
        routerLink: () => `/mgr-modules/edit/${getModuleUri()}`,
        icon: 'fa-pencil'
      },
      {
        name: this.i18n('Enable'),
        permission: 'update',
        click: () => this.updateModuleState(),
        disable: () => this.isTableActionDisabled('enabled'),
        icon: 'fa-play'
      },
      {
        name: this.i18n('Disable'),
        permission: 'update',
        click: () => this.updateModuleState(),
        disable: () => this.isTableActionDisabled('disabled'),
        disableDesc: () => this.getTableActionDisabledDesc(),
        icon: 'fa-stop'
      }
    ];
  }
 
  getModuleList(context: CdTableFetchDataContext) {
    this.mgrModuleService.list().subscribe(
      (resp: object[]) => {
        this.modules = resp;
      },
      () => {
        context.error();
      }
    );
  }
 
  updateSelection(selection: CdTableSelection) {
    this.selection = selection;
  }
 
  /**
   * Check if the table action is disabled.
   * @param state The expected module state, e.g. ``enabled`` or ``disabled``.
   * @returns If the specified state is validated to true or no selection is
   *   done, then ``true`` is returned, otherwise ``false``.
   */
  isTableActionDisabled(state: 'enabled' | 'disabled') {
    Iif (!this.selection.hasSelection) {
      return true;
    }
    const selected = this.selection.first();
    // Make sure the user can't modify the run state of the 'Dashboard' module.
    // This check is only done in the UI because the REST API should still be
    // able to do so.
    if (selected.name === 'dashboard') {
      return true;
    }
    // Always-on modules can't be disabled.
    Eif (selected.always_on) {
      return true;
    }
    switch (state) {
      case 'enabled':
        return selected.enabled;
      case 'disabled':
        return !selected.enabled;
    }
  }
 
  getTableActionDisabledDesc(): string | undefined {
    if (this.selection.hasSelection) {
      const selected = this.selection.first();
      if (selected.always_on) {
        return this.i18n('This Manager module is always on.');
      }
    }
  }
 
  /**
   * Update the Ceph Mgr module state to enabled or disabled.
   */
  updateModuleState() {
    Iif (!this.selection.hasSelection) {
      return;
    }
 
    let $obs;
    const fnWaitUntilReconnected = () => {
      observableTimer(2000).subscribe(() => {
        // Trigger an API request to check if the connection is
        // re-established.
        this.mgrModuleService.list().subscribe(
          () => {
            // Resume showing the notification toasties.
            this.notificationService.suspendToasties(false);
            // Unblock the whole UI.
            this.blockUI.stop();
            // Reload the data table content.
            this.table.refreshBtn();
          },
          () => {
            fnWaitUntilReconnected();
          }
        );
      });
    };
 
    // Note, the Ceph Mgr is always restarted when a module
    // is enabled/disabled.
    const module = this.selection.first();
    if (module.enabled) {
      $obs = this.mgrModuleService.disable(module.name);
    } else {
      $obs = this.mgrModuleService.enable(module.name);
    }
    $obs.subscribe(
      () => {},
      () => {
        // Suspend showing the notification toasties.
        this.notificationService.suspendToasties(true);
        // Block the whole UI to prevent user interactions until
        // the connection to the backend is reestablished
        this.blockUI.start(this.i18n('Reconnecting, please wait ...'));
        fnWaitUntilReconnected();
      }
    );
  }
}