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