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]);
76 this.snapshotForm.get('snapshotName').updateValueAndValidity();
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
85 setEditing(editing: boolean = true) {
86 this.editing = editing;
87 this.action = this.editing ? this.actionLabels.RENAME : this.actionLabels.CREATE;
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
100 .renameSnapshot(imageSpec, this.snapName, snapshotName)
103 this.taskManagerService.subscribe(
105 finishedTask.metadata,
106 (asyncFinishedTask: FinishedTask) => {
107 this.notificationService.notifyTask(asyncFinishedTask);
110 this.activeModal.close();
111 this.onSubmit.next(this.snapName);
114 this.snapshotForm.setErrors({ cdSubmitButton: true });
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
129 .createSnapshot(imageSpec, snapshotName, mirrorImageSnapshot)
132 this.taskManagerService.subscribe(
134 finishedTask.metadata,
135 (asyncFinishedTask: FinishedTask) => {
136 this.notificationService.notifyTask(asyncFinishedTask);
139 this.activeModal.close();
140 this.onSubmit.next(snapshotName);
143 this.snapshotForm.setErrors({ cdSubmitButton: true });