1 import { Component, OnInit } 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 '../../../shared/api/rbd.service';
8 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
9 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
10 import { FinishedTask } from '../../../shared/models/finished-task';
11 import { ImageSpec } from '../../../shared/models/image-spec';
12 import { NotificationService } from '../../../shared/services/notification.service';
13 import { TaskManagerService } from '../../../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 implements OnInit {
26 snapshotForm: CdFormGroup;
32 public onSubmit: Subject<string>;
35 public activeModal: NgbActiveModal,
36 private rbdService: RbdService,
37 private taskManagerService: TaskManagerService,
38 private notificationService: NotificationService,
39 private actionLabels: ActionLabelsI18n
41 this.action = this.actionLabels.CREATE;
42 this.resource = $localize`RBD Snapshot`;
47 this.snapshotForm = new CdFormGroup({
48 snapshotName: new FormControl('', {
49 validators: [Validators.required]
55 this.onSubmit = new Subject();
58 setSnapName(snapName: string) {
59 this.snapName = snapName;
60 this.snapshotForm.get('snapshotName').setValue(snapName);
64 * Set the 'editing' flag. If set to TRUE, the modal dialog is in
65 * 'Edit' mode, otherwise in 'Create' mode.
66 * @param {boolean} editing
68 setEditing(editing: boolean = true) {
69 this.editing = editing;
70 this.action = this.editing ? this.actionLabels.RENAME : this.actionLabels.CREATE;
74 const snapshotName = this.snapshotForm.getValue('snapshotName');
75 const imageSpec = new ImageSpec(this.poolName, this.namespace, this.imageName);
76 const finishedTask = new FinishedTask();
77 finishedTask.name = 'rbd/snap/edit';
78 finishedTask.metadata = {
79 image_spec: imageSpec.toString(),
80 snapshot_name: snapshotName
83 .renameSnapshot(imageSpec, this.snapName, snapshotName)
86 this.taskManagerService.subscribe(
88 finishedTask.metadata,
89 (asyncFinishedTask: FinishedTask) => {
90 this.notificationService.notifyTask(asyncFinishedTask);
93 this.activeModal.close();
94 this.onSubmit.next(this.snapName);
97 this.snapshotForm.setErrors({ cdSubmitButton: true });
102 const snapshotName = this.snapshotForm.getValue('snapshotName');
103 const imageSpec = new ImageSpec(this.poolName, this.namespace, this.imageName);
104 const finishedTask = new FinishedTask();
105 finishedTask.name = 'rbd/snap/create';
106 finishedTask.metadata = {
107 image_spec: imageSpec.toString(),
108 snapshot_name: snapshotName
111 .createSnapshot(imageSpec, snapshotName)
114 this.taskManagerService.subscribe(
116 finishedTask.metadata,
117 (asyncFinishedTask: FinishedTask) => {
118 this.notificationService.notifyTask(asyncFinishedTask);
121 this.activeModal.close();
122 this.onSubmit.next(snapshotName);
125 this.snapshotForm.setErrors({ cdSubmitButton: true });