# All files / src/app/ceph/rgw/rgw-bucket-form rgw-bucket-form.component.ts

80.52% Statements 62/77
74.29% Branches 26/35
70.59% Functions 12/17
79.45% Lines 58/73

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
    1611x
    1x
    1x

    1x
    1x

    1x
    1x
    1x
    1x
    1x

    1x

    1x

    7x
    7x
    7x
    7x

    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x

    7x
    7x
    7x
    7x

    1x
    7x

    7x

    7x

    7x

    7x

    1x
    2x

    2x

    2x
    2x

    1x
    1x

    1x

    1x

    1x

    1x

    1x

    1x
    11x
    11x
    4x

    1x
    1x

    3x

    1x
    1x

    2x

    1x

    1x
     import { Component, OnInit } from '@angular/core';
    import { AbstractControl, AsyncValidatorFn, ValidationErrors, Validators } from '@angular/forms';
    import { ActivatedRoute, Router } from '@angular/router';

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

    import { RgwBucketService } from '../../../shared/api/rgw-bucket.service';
    import { RgwUserService } from '../../../shared/api/rgw-user.service';
    import { ActionLabelsI18n, URLVerbs } from '../../../shared/constants/app.constants';
    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 { NotificationService } from '../../../shared/services/notification.service';

    @Component({
      selector: 'cd-rgw-bucket-form',
      template: require('./rgw-bucket-form.component.html'),
      styles: []
    })
    export class RgwBucketFormComponent implements OnInit {
      bucketForm: CdFormGroup;
      editing = false;
      error = false;
      loading = false;
      owners = null;
      action: string;
      resource: string;

      constructor(
        private route: ActivatedRoute,
        private router: Router,
        private formBuilder: CdFormBuilder,
        private rgwBucketService: RgwBucketService,
        private rgwUserService: RgwUserService,
        private notificationService: NotificationService,
        private i18n: I18n,
        public actionLabels: ActionLabelsI18n
      ) {
        this.editing = this.router.url.startsWith(`/rgw/bucket/${URLVerbs.EDIT}`);
        this.action = this.editing ? this.actionLabels.EDIT : this.actionLabels.CREATE;
        this.resource = this.i18n('bucket');
        this.createForm();
      }

      createForm() {
        this.bucketForm = this.formBuilder.group({
          id: [null],
          bid: [null, [Validators.required], [this.bucketNameValidator()]],
          owner: [null, [Validators.required]]
        });
      }

      ngOnInit() {
        // Get the list of possible owners.
        this.rgwUserService.enumerate().subscribe((resp: string[]) => {
          this.owners = resp.sort();
        });

        // Process route parameters.
        this.route.params.subscribe(
          (params: { bid: string }) => {
            if (!params.hasOwnProperty('bid')) {
              return;
            }
            const bid = decodeURIComponent(params.bid);
            this.loading = true;

            this.rgwBucketService.get(bid).subscribe((resp: object) => {
              this.loading = false;
              // Get the default values.
              const defaults = _.clone(this.bucketForm.value);
              // Extract the values displayed in the form.
              let value = _.pick(resp, _.keys(this.bucketForm.value));
              // Append default values.
              value = _.merge(defaults, value);
              // Update the form.
              this.bucketForm.setValue(value);
            });
          },
          (error) => {
            this.error = error;
          }
        );
      }

      goToListView() {
        this.router.navigate(['/rgw/bucket']);
      }

      submit() {
        // Exit immediately if the form isn't dirty.
        if (this.bucketForm.pristine) {
          this.goToListView();
          return;
        }
        const bidCtl = this.bucketForm.get('bid');
        const ownerCtl = this.bucketForm.get('owner');
        if (this.editing) {
          // Edit
          const idCtl = this.bucketForm.get('id');
          this.rgwBucketService.update(bidCtl.value, idCtl.value, ownerCtl.value).subscribe(
            () => {
              this.notificationService.show(
                NotificationType.success,
                this.i18n('Updated Object Gateway bucket "{{bid}}"', { bid: bidCtl.value })
              );
              this.goToListView();
            },
            () => {
              // Reset the 'Submit' button.
              this.bucketForm.setErrors({ cdSubmitButton: true });
            }
          );
        } else {
          // Add
          this.rgwBucketService.create(bidCtl.value, ownerCtl.value).subscribe(
            () => {
              this.notificationService.show(
                NotificationType.success,
                this.i18n('Created Object Gateway bucket "{{bid}}"', { bid: bidCtl.value })
              );
              this.goToListView();
            },
            () => {
              // Reset the 'Submit' button.
              this.bucketForm.setErrors({ cdSubmitButton: true });
            }
          );
        }
      }

      bucketNameValidator(): AsyncValidatorFn {
        const rgwBucketService = this.rgwBucketService;
        return (control: AbstractControl): Promise<ValidationErrors | null> => {
          return new Promise((resolve) => {
            // Exit immediately if user has not interacted with the control yet
            // or the control value is empty.
            if (control.pristine || control.value === '') {
              resolve(null);
              return;
            }
            // Validate the bucket name.
            const nameRe = /^[0-9A-Za-z][\w-\.]{2,254}$/;
            if (!nameRe.test(control.value)) {
              resolve({ bucketNameInvalid: true });
              return;
            }
            // Does any bucket with the given name already exist?
            rgwBucketService.exists(control.value).subscribe((resp: boolean) => {
              if (!resp) {
                resolve(null);
              } else {
                resolve({ bucketNameExists: true });
              }
            });
          });
        };
      }
    }