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