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]
55 setSnapName(snapName: string) {
56 this.snapName = snapName;
57 if (this.mirroring !== 'snapshot') {
58 this.snapshotForm.get('snapshotName').setValue(snapName);
60 this.snapshotForm.get('snapshotName').clearValidators();
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
69 setEditing(editing: boolean = true) {
70 this.editing = editing;
71 this.action = this.editing ? this.actionLabels.RENAME : this.actionLabels.CREATE;
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
84 .renameSnapshot(imageSpec, this.snapName, snapshotName)
87 this.taskManagerService.subscribe(
89 finishedTask.metadata,
90 (asyncFinishedTask: FinishedTask) => {
91 this.notificationService.notifyTask(asyncFinishedTask);
94 this.activeModal.close();
95 this.onSubmit.next(this.snapName);
98 this.snapshotForm.setErrors({ cdSubmitButton: true });
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
112 .createSnapshot(imageSpec, snapshotName)
115 this.taskManagerService.subscribe(
117 finishedTask.metadata,
118 (asyncFinishedTask: FinishedTask) => {
119 this.notificationService.notifyTask(asyncFinishedTask);
122 this.activeModal.close();
123 this.onSubmit.next(snapshotName);
126 this.snapshotForm.setErrors({ cdSubmitButton: true });