1 import { Component, OnInit } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
4 import { I18n } from '@ngx-translate/i18n-polyfill';
5 import { BsModalRef } from 'ngx-bootstrap/modal';
6 import { Subject } from 'rxjs';
8 import { RbdService } from '../../../shared/api/rbd.service';
9 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
10 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
11 import { FinishedTask } from '../../../shared/models/finished-task';
12 import { ImageSpec } from '../../../shared/models/image-spec';
13 import { NotificationService } from '../../../shared/services/notification.service';
14 import { TaskManagerService } from '../../../shared/services/task-manager.service';
17 selector: 'cd-rbd-snapshot-form',
18 templateUrl: './rbd-snapshot-form.component.html',
19 styleUrls: ['./rbd-snapshot-form.component.scss']
21 export class RbdSnapshotFormComponent implements OnInit {
27 snapshotForm: CdFormGroup;
33 public onSubmit: Subject<string>;
36 public modalRef: BsModalRef,
37 private rbdService: RbdService,
38 private taskManagerService: TaskManagerService,
39 private notificationService: NotificationService,
41 private actionLabels: ActionLabelsI18n
43 this.action = this.actionLabels.CREATE;
44 this.resource = this.i18n('RBD Snapshot');
49 this.snapshotForm = new CdFormGroup({
50 snapshotName: new FormControl('', {
51 validators: [Validators.required]
57 this.onSubmit = new Subject();
60 setSnapName(snapName: string) {
61 this.snapName = snapName;
62 this.snapshotForm.get('snapshotName').setValue(snapName);
66 * Set the 'editing' flag. If set to TRUE, the modal dialog is in
67 * 'Edit' mode, otherwise in 'Create' mode.
68 * @param {boolean} editing
70 setEditing(editing: boolean = true) {
71 this.editing = editing;
72 this.action = this.editing ? this.actionLabels.RENAME : this.actionLabels.CREATE;
76 const snapshotName = this.snapshotForm.getValue('snapshotName');
77 const imageSpec = new ImageSpec(this.poolName, this.namespace, this.imageName);
78 const finishedTask = new FinishedTask();
79 finishedTask.name = 'rbd/snap/edit';
80 finishedTask.metadata = {
81 image_spec: imageSpec.toString(),
82 snapshot_name: snapshotName
85 .renameSnapshot(imageSpec, this.snapName, snapshotName)
88 this.taskManagerService.subscribe(
90 finishedTask.metadata,
91 (asyncFinishedTask: FinishedTask) => {
92 this.notificationService.notifyTask(asyncFinishedTask);
96 this.onSubmit.next(this.snapName);
99 this.snapshotForm.setErrors({ cdSubmitButton: true });
104 const snapshotName = this.snapshotForm.getValue('snapshotName');
105 const imageSpec = new ImageSpec(this.poolName, this.namespace, this.imageName);
106 const finishedTask = new FinishedTask();
107 finishedTask.name = 'rbd/snap/create';
108 finishedTask.metadata = {
109 image_spec: imageSpec.toString(),
110 snapshot_name: snapshotName
113 .createSnapshot(imageSpec, snapshotName)
116 this.taskManagerService.subscribe(
118 finishedTask.metadata,
119 (asyncFinishedTask: FinishedTask) => {
120 this.notificationService.notifyTask(asyncFinishedTask);
123 this.modalRef.hide();
124 this.onSubmit.next(snapshotName);
127 this.snapshotForm.setErrors({ cdSubmitButton: true });