1 import { Component } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
4 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
5 import { Subject } from 'rxjs';
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';
16 selector: 'cd-rbd-snapshot-form-modal',
17 templateUrl: './rbd-snapshot-form-modal.component.html',
18 styleUrls: ['./rbd-snapshot-form-modal.component.scss']
20 export class RbdSnapshotFormModalComponent {
27 snapshotForm: CdFormGroup;
33 public onSubmit: Subject<string> = new Subject();
36 public activeModal: NgbActiveModal,
37 private rbdService: RbdService,
38 private taskManagerService: TaskManagerService,
39 private notificationService: NotificationService,
40 private actionLabels: ActionLabelsI18n
42 this.action = this.actionLabels.CREATE;
43 this.resource = $localize`RBD Snapshot`;
48 this.snapshotForm = new CdFormGroup({
49 snapshotName: new FormControl('', {
50 validators: [Validators.required]
52 mirrorImageSnapshot: new FormControl(false, {})
56 setSnapName(snapName: string) {
57 this.snapName = snapName;
58 if (this.mirroring !== 'snapshot') {
59 this.snapshotForm.get('snapshotName').setValue(snapName);
61 this.snapshotForm.get('snapshotName').clearValidators();
65 onMirrorCheckBoxChange() {
66 if (this.snapshotForm.getValue('mirrorImageSnapshot') === true) {
67 this.snapshotForm.get('snapshotName').setValue('');
72 * Set the 'editing' flag. If set to TRUE, the modal dialog is in
73 * 'Edit' mode, otherwise in 'Create' mode.
74 * @param {boolean} editing
76 setEditing(editing: boolean = true) {
77 this.editing = editing;
78 this.action = this.editing ? this.actionLabels.RENAME : this.actionLabels.CREATE;
82 const snapshotName = this.snapshotForm.getValue('snapshotName');
83 const imageSpec = new ImageSpec(this.poolName, this.namespace, this.imageName);
84 const finishedTask = new FinishedTask();
85 finishedTask.name = 'rbd/snap/edit';
86 finishedTask.metadata = {
87 image_spec: imageSpec.toString(),
88 snapshot_name: snapshotName
91 .renameSnapshot(imageSpec, this.snapName, snapshotName)
94 this.taskManagerService.subscribe(
96 finishedTask.metadata,
97 (asyncFinishedTask: FinishedTask) => {
98 this.notificationService.notifyTask(asyncFinishedTask);
101 this.activeModal.close();
102 this.onSubmit.next(this.snapName);
105 this.snapshotForm.setErrors({ cdSubmitButton: true });
110 const snapshotName = this.snapshotForm.getValue('snapshotName');
111 const mirrorImageSnapshot = this.snapshotForm.getValue('mirrorImageSnapshot');
112 const imageSpec = new ImageSpec(this.poolName, this.namespace, this.imageName);
113 const finishedTask = new FinishedTask();
114 finishedTask.name = 'rbd/snap/create';
115 finishedTask.metadata = {
116 image_spec: imageSpec.toString(),
117 snapshot_name: snapshotName
120 .createSnapshot(imageSpec, snapshotName, mirrorImageSnapshot)
123 this.taskManagerService.subscribe(
125 finishedTask.metadata,
126 (asyncFinishedTask: FinishedTask) => {
127 this.notificationService.notifyTask(asyncFinishedTask);
130 this.activeModal.close();
131 this.onSubmit.next(snapshotName);
134 this.snapshotForm.setErrors({ cdSubmitButton: true });