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

70.59% Statements 60/85
73.91% Branches 34/46
33.33% Functions 5/15
71.25% Lines 57/80

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 1476x 6x 6x   6x 6x 6x   6x 6x 6x   6x 6x             6x   10x 10x 10x 10x     10x 10x 10x 10x 10x 10x     10x 10x   10x 10x 10x 10x 10x 10x                                       6x 18x 18x   1x 1x         12x 12x 12x 8x   12x 4x   12x   3x 2x   3x 1x   3x   1x 1x 1x   1x 1x   18x     6x                     6x       6x                                                       6x  
import { Component, OnInit } from '@angular/core';
import { ValidatorFn, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
 
import { I18n } from '@ngx-translate/i18n-polyfill';
import * as _ from 'lodash';
import { forkJoin as observableForkJoin } from 'rxjs';
 
import { MgrModuleService } from '../../../../shared/api/mgr-module.service';
import { NotificationType } from '../../../../shared/enum/notification-type.enum';
import { CdFormBuilder } from '../../../../shared/forms/cd-form-builder';
import { CdFormGroup } from '../../../../shared/forms/cd-form-group';
import { CdValidators } from '../../../../shared/forms/cd-validators';
import { NotificationService } from '../../../../shared/services/notification.service';
 
@Component({
  selector: 'cd-mgr-module-form',
  template: require('./mgr-module-form.component.html'),
  styles: []
})
export class MgrModuleFormComponent implements OnInit {
  mgrModuleForm: CdFormGroup;
  error = false;
  loading = false;
  moduleName = '';
  moduleOptions = [];
 
  constructor(
    private route: ActivatedRoute,
    private router: Router,
    private formBuilder: CdFormBuilder,
    private mgrModuleService: MgrModuleService,
    private notificationService: NotificationService,
    private i18n: I18n
  ) {}
 
  ngOnInit() {
    this.route.params.subscribe(
      (params: { name: string }) => {
        this.moduleName = decodeURIComponent(params.name);
        this.loading = true;
        const observables = [];
        observables.push(this.mgrModuleService.getOptions(this.moduleName));
        observables.push(this.mgrModuleService.getConfig(this.moduleName));
        observableForkJoin(observables).subscribe(
          (resp: object) => {
            this.loading = false;
            this.moduleOptions = resp[0];
            // Create the form dynamically.
            this.createForm();
            // Set the form field values.
            this.mgrModuleForm.setValue(resp[1]);
          },
          (error) => {
            this.error = error;
          }
        );
      },
      (error) => {
        this.error = error;
      }
    );
  }
 
  getValidators(moduleOption): ValidatorFn[] {
    const result = [];
    switch (moduleOption.type) {
      case 'addr':
        result.push(CdValidators.ip());
        break;
      case 'uint':
      case 'int':
      case 'size':
      case 'secs':
        result.push(CdValidators.number());
        result.push(Validators.required);
        if (_.isNumber(moduleOption.min)) {
          result.push(Validators.min(moduleOption.min));
        }
        if (_.isNumber(moduleOption.max)) {
          result.push(Validators.max(moduleOption.max));
        }
        break;
      case 'str':
        if (_.isNumber(moduleOption.min)) {
          result.push(Validators.minLength(moduleOption.min));
        }
        if (_.isNumber(moduleOption.max)) {
          result.push(Validators.maxLength(moduleOption.max));
        }
        break;
      case 'float':
        result.push(Validators.required);
        result.push(CdValidators.decimalNumber());
        break;
      case 'uuid':
        result.push(CdValidators.uuid());
        break;
    }
    return result;
  }
 
  createForm() {
    const controlsConfig = {};
    _.forEach(this.moduleOptions, (moduleOption) => {
      controlsConfig[moduleOption.name] = [
        moduleOption.default_value,
        this.getValidators(moduleOption)
      ];
    });
    this.mgrModuleForm = this.formBuilder.group(controlsConfig);
  }
 
  goToListView() {
    this.router.navigate(['/mgr-modules']);
  }
 
  onSubmit() {
    // Exit immediately if the form isn't dirty.
    if (this.mgrModuleForm.pristine) {
      this.goToListView();
      return;
    }
    const config = {};
    _.forEach(this.moduleOptions, (moduleOption) => {
      const control = this.mgrModuleForm.get(moduleOption.name);
      // Append the option only if the value has been modified.
      if (control.dirty && control.valid) {
        config[moduleOption.name] = control.value;
      }
    });
    this.mgrModuleService.updateConfig(this.moduleName, config).subscribe(
      () => {
        this.notificationService.show(
          NotificationType.success,
          this.i18n('Updated options for module "{{name}}".', { name: this.moduleName })
        );
        this.goToListView();
      },
      () => {
        // Reset the 'Submit' button.
        this.mgrModuleForm.setErrors({ cdSubmitButton: true });
      }
    );
  }
}