]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
e017c204b06d6c0f0037f8a0b1c79a0825116792
[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     }
77   }
78
79   /**
80    * Set the 'editing' flag. If set to TRUE, the modal dialog is in
81    * 'Edit' mode, otherwise in 'Create' mode.
82    * @param {boolean} editing
83    */
84   setEditing(editing: boolean = true) {
85     this.editing = editing;
86     this.action = this.editing ? this.actionLabels.RENAME : this.actionLabels.CREATE;
87   }
88
89   editAction() {
90     const snapshotName = this.snapshotForm.getValue('snapshotName');
91     const imageSpec = new ImageSpec(this.poolName, this.namespace, this.imageName);
92     const finishedTask = new FinishedTask();
93     finishedTask.name = 'rbd/snap/edit';
94     finishedTask.metadata = {
95       image_spec: imageSpec.toString(),
96       snapshot_name: snapshotName
97     };
98     this.rbdService
99       .renameSnapshot(imageSpec, this.snapName, snapshotName)
100       .toPromise()
101       .then(() => {
102         this.taskManagerService.subscribe(
103           finishedTask.name,
104           finishedTask.metadata,
105           (asyncFinishedTask: FinishedTask) => {
106             this.notificationService.notifyTask(asyncFinishedTask);
107           }
108         );
109         this.activeModal.close();
110         this.onSubmit.next(this.snapName);
111       })
112       .catch(() => {
113         this.snapshotForm.setErrors({ cdSubmitButton: true });
114       });
115   }
116
117   createAction() {
118     const snapshotName = this.snapshotForm.getValue('snapshotName');
119     const mirrorImageSnapshot = this.snapshotForm.getValue('mirrorImageSnapshot');
120     const imageSpec = new ImageSpec(this.poolName, this.namespace, this.imageName);
121     const finishedTask = new FinishedTask();
122     finishedTask.name = 'rbd/snap/create';
123     finishedTask.metadata = {
124       image_spec: imageSpec.toString(),
125       snapshot_name: snapshotName
126     };
127     this.rbdService
128       .createSnapshot(imageSpec, snapshotName, mirrorImageSnapshot)
129       .toPromise()
130       .then(() => {
131         this.taskManagerService.subscribe(
132           finishedTask.name,
133           finishedTask.metadata,
134           (asyncFinishedTask: FinishedTask) => {
135             this.notificationService.notifyTask(asyncFinishedTask);
136           }
137         );
138         this.activeModal.close();
139         this.onSubmit.next(snapshotName);
140       })
141       .catch(() => {
142         this.snapshotForm.setErrors({ cdSubmitButton: true });
143       });
144   }
145
146   submit() {
147     if (this.editing) {
148       this.editAction();
149     } else {
150       this.createAction();
151     }
152   }
153 }