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