]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-snapshot-form/rbd-snapshot-form-modal.component.ts
import ceph quincy 17.2.4
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / block / rbd-snapshot-form / rbd-snapshot-form-modal.component.ts
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 });
53 }
54
55 setSnapName(snapName: string) {
56 this.snapName = snapName;
57 if (this.mirroring !== 'snapshot') {
58 this.snapshotForm.get('snapshotName').setValue(snapName);
59 } else {
60 this.snapshotForm.get('snapshotName').clearValidators();
61 }
62 }
63
64 /**
65 * Set the 'editing' flag. If set to TRUE, the modal dialog is in
66 * 'Edit' mode, otherwise in 'Create' mode.
67 * @param {boolean} editing
68 */
69 setEditing(editing: boolean = true) {
70 this.editing = editing;
71 this.action = this.editing ? this.actionLabels.RENAME : this.actionLabels.CREATE;
72 }
73
74 editAction() {
75 const snapshotName = this.snapshotForm.getValue('snapshotName');
76 const imageSpec = new ImageSpec(this.poolName, this.namespace, this.imageName);
77 const finishedTask = new FinishedTask();
78 finishedTask.name = 'rbd/snap/edit';
79 finishedTask.metadata = {
80 image_spec: imageSpec.toString(),
81 snapshot_name: snapshotName
82 };
83 this.rbdService
84 .renameSnapshot(imageSpec, this.snapName, snapshotName)
85 .toPromise()
86 .then(() => {
87 this.taskManagerService.subscribe(
88 finishedTask.name,
89 finishedTask.metadata,
90 (asyncFinishedTask: FinishedTask) => {
91 this.notificationService.notifyTask(asyncFinishedTask);
92 }
93 );
94 this.activeModal.close();
95 this.onSubmit.next(this.snapName);
96 })
97 .catch(() => {
98 this.snapshotForm.setErrors({ cdSubmitButton: true });
99 });
100 }
101
102 createAction() {
103 const snapshotName = this.snapshotForm.getValue('snapshotName');
104 const imageSpec = new ImageSpec(this.poolName, this.namespace, this.imageName);
105 const finishedTask = new FinishedTask();
106 finishedTask.name = 'rbd/snap/create';
107 finishedTask.metadata = {
108 image_spec: imageSpec.toString(),
109 snapshot_name: snapshotName
110 };
111 this.rbdService
112 .createSnapshot(imageSpec, snapshotName)
113 .toPromise()
114 .then(() => {
115 this.taskManagerService.subscribe(
116 finishedTask.name,
117 finishedTask.metadata,
118 (asyncFinishedTask: FinishedTask) => {
119 this.notificationService.notifyTask(asyncFinishedTask);
120 }
121 );
122 this.activeModal.close();
123 this.onSubmit.next(snapshotName);
124 })
125 .catch(() => {
126 this.snapshotForm.setErrors({ cdSubmitButton: true });
127 });
128 }
129
130 submit() {
131 if (this.editing) {
132 this.editAction();
133 } else {
134 this.createAction();
135 }
136 }
137 }