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