# All files / src/app/ceph/cluster/configuration/configuration-form configuration-form.component.ts

50.57% Statements 44/87
78.26% Branches 18/23
31.25% Functions 5/16
51.85% Lines 42/81

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

    17x

    17x
    17x
    17x
    17x
    17x

    17x

    6x

    17x
    102x

    17x

    6x

    6x
    8x

    6x

    2x

    2x

    6x

    6x
    11x

    5x

    4x

    3x
    3x

    2x

    1x

    6x

    6x

    6x

    6x
     import { Component, OnInit } from '@angular/core';
    import { FormControl, FormGroup, ValidatorFn } from '@angular/forms';
    import { ActivatedRoute, Router } from '@angular/router';

    import { I18n } from '@ngx-translate/i18n-polyfill';
    import * as _ from 'lodash';

    import { ConfigurationService } from '../../../../shared/api/configuration.service';
    import { NotificationType } from '../../../../shared/enum/notification-type.enum';
    import { CdFormGroup } from '../../../../shared/forms/cd-form-group';
    import { NotificationService } from '../../../../shared/services/notification.service';
    import { ConfigFormCreateRequestModel } from './configuration-form-create-request.model';
    import { ConfigFormModel } from './configuration-form.model';
    import { ConfigOptionTypes } from './configuration-form.types';

    @Component({
      selector: 'cd-configuration-form',
      template: require('./configuration-form.component.html'),
      styles: []
    })
    export class ConfigurationFormComponent implements OnInit {
      configForm: CdFormGroup;
      response: ConfigFormModel;
      type: string;
      inputType: string;
      humanReadableType: string;
      minValue: number;
      maxValue: number;
      patternHelpText: string;
      availSections = ['global', 'mon', 'mgr', 'osd', 'mds', 'client'];

      constructor(
        private route: ActivatedRoute,
        private router: Router,
        private configService: ConfigurationService,
        private notificationService: NotificationService,
        private i18n: I18n
      ) {
        this.createForm();
      }

      createForm() {
        const formControls = {
          name: new FormControl({ value: null }),
          desc: new FormControl({ value: null }),
          long_desc: new FormControl({ value: null }),
          values: new FormGroup({}),
          default: new FormControl({ value: null }),
          daemon_default: new FormControl({ value: null }),
          services: new FormControl([])
        };

        this.availSections.forEach((section) => {
          formControls.values.addControl(section, new FormControl(null));
        });

        this.configForm = new CdFormGroup(formControls);
      }

      ngOnInit() {
        this.route.params.subscribe((params: { name: string }) => {
          const configName = params.name;
          this.configService.get(configName).subscribe((resp: ConfigFormModel) => {
            this.setResponse(resp);
          });
        });
      }

      getValidators(configOption: any): ValidatorFn[] {
        const typeValidators = ConfigOptionTypes.getTypeValidators(configOption);
        if (typeValidators) {
          this.patternHelpText = typeValidators.patternHelpText;

          if ('max' in typeValidators && typeValidators.max !== '') {
            this.maxValue = typeValidators.max;
          }

          if ('min' in typeValidators && typeValidators.min !== '') {
            this.minValue = typeValidators.min;
          }

          return typeValidators.validators;
        }
      }

      getStep(type: string, value: number): number | undefined {
        const numberTypes = ['uint', 'int', 'size', 'secs'];

        if (numberTypes.includes(type)) {
          return 1;
        }

        if (type === 'float') {
          if (value !== null) {
            const stringVal = value.toString();
            if (stringVal.indexOf('.') !== -1) {
              // Value type float and contains decimal characters
              const decimal = value.toString().split('.');
              return Math.pow(10, -decimal[1].length);
            }
          }

          return 0.1;
        }

        return undefined;
      }

      setResponse(response: ConfigFormModel) {
        this.response = response;
        const validators = this.getValidators(response);

        this.configForm.get('name').setValue(response.name);
        this.configForm.get('desc').setValue(response.desc);
        this.configForm.get('long_desc').setValue(response.long_desc);
        this.configForm.get('default').setValue(response.default);
        this.configForm.get('daemon_default').setValue(response.daemon_default);
        this.configForm.get('services').setValue(response.services);

        if (this.response.value) {
          this.response.value.forEach((value) => {
            // Check value type. If it's a boolean value we need to convert it because otherwise we
            // would use the string representation. That would cause issues for e.g. checkboxes.
            let sectionValue = null;
            if (value.value === 'true') {
              sectionValue = true;
            } else if (value.value === 'false') {
              sectionValue = false;
            } else {
              sectionValue = value.value;
            }
            this.configForm
              .get('values')
              .get(value.section)
              .setValue(sectionValue);
          });
        }

        this.availSections.forEach((section) => {
          this.configForm
            .get('values')
            .get(section)
            .setValidators(validators);
        });

        const currentType = ConfigOptionTypes.getType(response.type);
        this.type = currentType.name;
        this.inputType = currentType.inputType;
        this.humanReadableType = currentType.humanReadable;
      }

      createRequest(): ConfigFormCreateRequestModel | null {
        const values = [];

        this.availSections.forEach((section) => {
          const sectionValue = this.configForm.getValue(section);
          if (sectionValue) {
            values.push({ section: section, value: sectionValue });
          }
        });

        if (!_.isEqual(this.response.value, values)) {
          const request = new ConfigFormCreateRequestModel();
          request.name = this.configForm.getValue('name');
          request.value = values;
          return request;
        }

        return null;
      }

      submit() {
        const request = this.createRequest();

        if (request) {
          this.configService.create(request).subscribe(
            () => {
              this.notificationService.show(
                NotificationType.success,
                this.i18n('Updated config option {{name}}', { name: request.name })
              );
              this.router.navigate(['/configuration']);
            },
            () => {
              this.configForm.setErrors({ cdSubmitButton: true });
            }
          );
        }

        this.router.navigate(['/configuration']);
      }
    }