]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
2805645027c0b4c774ff42c1a1b713a04d1a938a
[ceph.git] /
1 import { Component, OnInit } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
3
4 import { BsModalRef } from 'ngx-bootstrap/modal';
5 import { Subject } from 'rxjs';
6
7 import { RbdService } from '../../../shared/api/rbd.service';
8 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
9 import { FinishedTask } from '../../../shared/models/finished-task';
10 import { NotificationService } from '../../../shared/services/notification.service';
11 import { TaskManagerService } from '../../../shared/services/task-manager.service';
12
13 @Component({
14   selector: 'cd-rbd-snapshot-form',
15   templateUrl: './rbd-snapshot-form.component.html',
16   styleUrls: ['./rbd-snapshot-form.component.scss']
17 })
18 export class RbdSnapshotFormComponent implements OnInit {
19   poolName: string;
20   imageName: string;
21   snapName: string;
22
23   snapshotForm: CdFormGroup;
24
25   editing = false;
26
27   public onSubmit: Subject<string>;
28
29   constructor(
30     public modalRef: BsModalRef,
31     private rbdService: RbdService,
32     private taskManagerService: TaskManagerService,
33     private notificationService: NotificationService
34   ) {
35     this.createForm();
36   }
37
38   createForm() {
39     this.snapshotForm = new CdFormGroup({
40       snapshotName: new FormControl('', {
41         validators: [Validators.required]
42       })
43     });
44   }
45
46   ngOnInit() {
47     this.onSubmit = new Subject();
48   }
49
50   setSnapName(snapName) {
51     this.snapName = snapName;
52     this.snapshotForm.get('snapshotName').setValue(snapName);
53   }
54
55   /**
56    * Set the 'editing' flag. If set to TRUE, the modal dialog is in
57    * 'Edit' mode, otherwise in 'Create' mode.
58    * @param {boolean} editing
59    */
60   setEditing(editing: boolean = true) {
61     this.editing = editing;
62   }
63
64   editAction() {
65     const snapshotName = this.snapshotForm.getValue('snapshotName');
66     const finishedTask = new FinishedTask();
67     finishedTask.name = 'rbd/snap/edit';
68     finishedTask.metadata = {
69       pool_name: this.poolName,
70       image_name: this.imageName,
71       snapshot_name: snapshotName
72     };
73     this.rbdService
74       .renameSnapshot(this.poolName, this.imageName, this.snapName, snapshotName)
75       .toPromise()
76       .then((resp) => {
77         this.taskManagerService.subscribe(
78           finishedTask.name,
79           finishedTask.metadata,
80           (asyncFinishedTask: FinishedTask) => {
81             this.notificationService.notifyTask(asyncFinishedTask);
82           }
83         );
84         this.modalRef.hide();
85         this.onSubmit.next(this.snapName);
86       })
87       .catch((resp) => {
88         this.snapshotForm.setErrors({ cdSubmitButton: true });
89       });
90   }
91
92   createAction() {
93     const snapshotName = this.snapshotForm.getValue('snapshotName');
94     const finishedTask = new FinishedTask();
95     finishedTask.name = 'rbd/snap/create';
96     finishedTask.metadata = {
97       pool_name: this.poolName,
98       image_name: this.imageName,
99       snapshot_name: snapshotName
100     };
101     this.rbdService
102       .createSnapshot(this.poolName, this.imageName, snapshotName)
103       .toPromise()
104       .then((resp) => {
105         this.taskManagerService.subscribe(
106           finishedTask.name,
107           finishedTask.metadata,
108           (asyncFinishedTask: FinishedTask) => {
109             this.notificationService.notifyTask(asyncFinishedTask);
110           }
111         );
112         this.modalRef.hide();
113         this.onSubmit.next(snapshotName);
114       })
115       .catch((resp) => {
116         this.snapshotForm.setErrors({ cdSubmitButton: true });
117       });
118   }
119
120   submit() {
121     if (this.editing) {
122       this.editAction();
123     } else {
124       this.createAction();
125     }
126   }
127 }