# All files / src/app/ceph/block/rbd-snapshot-form rbd-snapshot-form.component.ts

57.14% Statements 32/56
70.59% Branches 12/17
35.71% Functions 5/14
56.86% Lines 29/51

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
    1287x
    7x

    7x
    7x

    7x
    7x
    7x
    7x
    7x

    7x

    3x

    3x
    3x
    3x
    3x

    3x

    7x
    3x

    7x
    1x

    7x
    2x
    2x

    7x
    1x

    7x

    7x

    7x

    7x
     import { Component, OnInit } from '@angular/core';
    import { FormControl, Validators } from '@angular/forms';

    import { BsModalRef } from 'ngx-bootstrap/modal';
    import { Subject } from 'rxjs';

    import { RbdService } from '../../../shared/api/rbd.service';
    import { CdFormGroup } from '../../../shared/forms/cd-form-group';
    import { FinishedTask } from '../../../shared/models/finished-task';
    import { NotificationService } from '../../../shared/services/notification.service';
    import { TaskManagerService } from '../../../shared/services/task-manager.service';

    @Component({
      selector: 'cd-rbd-snapshot-form',
      template: require('./rbd-snapshot-form.component.html'),
      styles: []
    })
    export class RbdSnapshotFormComponent implements OnInit {
      poolName: string;
      imageName: string;
      snapName: string;

      snapshotForm: CdFormGroup;

      editing = false;

      public onSubmit: Subject<string>;

      constructor(
        public modalRef: BsModalRef,
        private rbdService: RbdService,
        private taskManagerService: TaskManagerService,
        private notificationService: NotificationService
      ) {
        this.createForm();
      }

      createForm() {
        this.snapshotForm = new CdFormGroup({
          snapshotName: new FormControl('', {
            validators: [Validators.required]
          })
        });
      }

      ngOnInit() {
        this.onSubmit = new Subject();
      }

      setSnapName(snapName) {
        this.snapName = snapName;
        this.snapshotForm.get('snapshotName').setValue(snapName);
      }

      /**
       * Set the 'editing' flag. If set to TRUE, the modal dialog is in
       * 'Edit' mode, otherwise in 'Create' mode.
       * @param {boolean} editing
       */
      setEditing(Eediting: boolean = true) {
        this.editing = editing;
      }

      editAction() {
        const snapshotName = this.snapshotForm.getValue('snapshotName');
        const finishedTask = new FinishedTask();
        finishedTask.name = 'rbd/snap/edit';
        finishedTask.metadata = {
          pool_name: this.poolName,
          image_name: this.imageName,
          snapshot_name: snapshotName
        };
        this.rbdService
          .renameSnapshot(this.poolName, this.imageName, this.snapName, snapshotName)
          .toPromise()
          .then(() => {
            this.taskManagerService.subscribe(
              finishedTask.name,
              finishedTask.metadata,
              (asyncFinishedTask: FinishedTask) => {
                this.notificationService.notifyTask(asyncFinishedTask);
              }
            );
            this.modalRef.hide();
            this.onSubmit.next(this.snapName);
          })
          .catch(() => {
            this.snapshotForm.setErrors({ cdSubmitButton: true });
          });
      }

      createAction() {
        const snapshotName = this.snapshotForm.getValue('snapshotName');
        const finishedTask = new FinishedTask();
        finishedTask.name = 'rbd/snap/create';
        finishedTask.metadata = {
          pool_name: this.poolName,
          image_name: this.imageName,
          snapshot_name: snapshotName
        };
        this.rbdService
          .createSnapshot(this.poolName, this.imageName, snapshotName)
          .toPromise()
          .then(() => {
            this.taskManagerService.subscribe(
              finishedTask.name,
              finishedTask.metadata,
              (asyncFinishedTask: FinishedTask) => {
                this.notificationService.notifyTask(asyncFinishedTask);
              }
            );
            this.modalRef.hide();
            this.onSubmit.next(snapshotName);
          })
          .catch(() => {
            this.snapshotForm.setErrors({ cdSubmitButton: true });
          });
      }

      submit() {
        if (this.editing) {
          this.editAction();
        } else {
          this.createAction();
        }
      }
    }