1 import { Component, OnInit } from '@angular/core';
2 import { UntypedFormControl, Validators } from '@angular/forms';
4 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
5 import { Observable, Subject } from 'rxjs';
6 import { RbdMirroringService } from '~/app/shared/api/rbd-mirroring.service';
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';
17 selector: 'cd-rbd-snapshot-form-modal',
18 templateUrl: './rbd-snapshot-form-modal.component.html',
19 styleUrls: ['./rbd-snapshot-form-modal.component.scss']
21 export class RbdSnapshotFormModalComponent implements OnInit {
28 snapshotForm: CdFormGroup;
34 public onSubmit: Subject<string> = new Subject();
36 peerConfigured$: Observable<any>;
39 public activeModal: NgbActiveModal,
40 private rbdService: RbdService,
41 private taskManagerService: TaskManagerService,
42 private notificationService: NotificationService,
43 private actionLabels: ActionLabelsI18n,
44 private rbdMirrorService: RbdMirroringService
46 this.action = this.actionLabels.CREATE;
47 this.resource = $localize`RBD Snapshot`;
52 this.snapshotForm = new CdFormGroup({
53 snapshotName: new UntypedFormControl('', {
54 validators: [Validators.required]
56 mirrorImageSnapshot: new UntypedFormControl(false, {})
61 this.peerConfigured$ = this.rbdMirrorService.getPeerForPool(this.poolName);
64 setSnapName(snapName: string) {
65 this.snapName = snapName;
66 this.snapshotForm.get('snapshotName').setValue(snapName);
69 onMirrorCheckBoxChange() {
70 if (this.snapshotForm.getValue('mirrorImageSnapshot') === true) {
71 this.snapshotForm.get('snapshotName').setValue('');
72 this.snapshotForm.get('snapshotName').clearValidators();
74 this.snapshotForm.get('snapshotName').setValue(this.snapName);
75 this.snapshotForm.get('snapshotName').setValidators([Validators.required]);
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
84 setEditing(editing: boolean = true) {
85 this.editing = editing;
86 this.action = this.editing ? this.actionLabels.RENAME : this.actionLabels.CREATE;
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
99 .renameSnapshot(imageSpec, this.snapName, snapshotName)
102 this.taskManagerService.subscribe(
104 finishedTask.metadata,
105 (asyncFinishedTask: FinishedTask) => {
106 this.notificationService.notifyTask(asyncFinishedTask);
109 this.activeModal.close();
110 this.onSubmit.next(this.snapName);
113 this.snapshotForm.setErrors({ cdSubmitButton: true });
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
128 .createSnapshot(imageSpec, snapshotName, mirrorImageSnapshot)
131 this.taskManagerService.subscribe(
133 finishedTask.metadata,
134 (asyncFinishedTask: FinishedTask) => {
135 this.notificationService.notifyTask(asyncFinishedTask);
138 this.activeModal.close();
139 this.onSubmit.next(snapshotName);
142 this.snapshotForm.setErrors({ cdSubmitButton: true });