1 import { Component, Inject, OnInit, Optional } from '@angular/core';
2 import { UntypedFormControl, Validators } from '@angular/forms';
4 import { BaseModal } from 'carbon-components-angular';
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 { ModalCdsService } from '~/app/shared/services/modal-cds.service';
14 import { NotificationService } from '~/app/shared/services/notification.service';
15 import { TaskManagerService } from '~/app/shared/services/task-manager.service';
18 selector: 'cd-rbd-snapshot-form-modal',
19 templateUrl: './rbd-snapshot-form-modal.component.html',
20 styleUrls: ['./rbd-snapshot-form-modal.component.scss']
22 export class RbdSnapshotFormModalComponent extends BaseModal implements OnInit {
23 snapshotForm: CdFormGroup;
29 public onSubmit: Subject<string> = new Subject();
31 peerConfigured$: Observable<any>;
34 private cdsModalService: ModalCdsService,
35 private rbdService: RbdService,
36 private taskManagerService: TaskManagerService,
37 private notificationService: NotificationService,
38 private actionLabels: ActionLabelsI18n,
39 private rbdMirrorService: RbdMirroringService,
41 @Inject('poolName') public poolName: string,
42 @Optional() @Inject('namespace') public namespace = '',
43 @Optional() @Inject('imageName') public imageName = '',
44 @Optional() @Inject('mirroring') public mirroring = '',
45 @Optional() @Inject('snapName') public snapName = ''
48 this.action = this.actionLabels.CREATE;
49 this.resource = $localize`RBD Snapshot`;
54 this.snapshotForm = new CdFormGroup({
55 snapshotName: new UntypedFormControl('', {
56 validators: [Validators.required]
58 mirrorImageSnapshot: new UntypedFormControl(false, {})
63 this.peerConfigured$ = this.rbdMirrorService.getPeerForPool(this.poolName);
66 setSnapName(snapName: string) {
67 this.snapName = snapName;
68 this.snapshotForm.get('snapshotName').setValue(snapName);
71 onMirrorCheckBoxChange() {
72 if (this.snapshotForm.getValue('mirrorImageSnapshot') === true) {
73 this.snapshotForm.get('snapshotName').setValue('');
74 this.snapshotForm.get('snapshotName').clearValidators();
76 this.snapshotForm.get('snapshotName').setValue(this.snapName);
77 this.snapshotForm.get('snapshotName').setValidators([Validators.required]);
78 this.snapshotForm.get('snapshotName').updateValueAndValidity();
83 * Set the 'editing' flag. If set to TRUE, the modal dialog is in
84 * 'Edit' mode, otherwise in 'Create' mode.
85 * @param {boolean} editing
87 setEditing(editing: boolean = true) {
88 this.editing = editing;
89 this.action = this.editing ? this.actionLabels.RENAME : this.actionLabels.CREATE;
93 const snapshotName = this.snapshotForm.getValue('snapshotName');
94 const imageSpec = new ImageSpec(this.poolName, this.namespace, this.imageName);
95 const finishedTask = new FinishedTask();
96 finishedTask.name = 'rbd/snap/edit';
97 finishedTask.metadata = {
98 image_spec: imageSpec.toString(),
99 snapshot_name: snapshotName
102 .renameSnapshot(imageSpec, this.snapName, snapshotName)
105 this.taskManagerService.subscribe(
107 finishedTask.metadata,
108 (asyncFinishedTask: FinishedTask) => {
109 this.notificationService.notifyTask(asyncFinishedTask);
112 this.cdsModalService.dismissAll();
113 this.onSubmit.next(this.snapName);
116 this.snapshotForm.setErrors({ cdSubmitButton: true });
121 const snapshotName = this.snapshotForm.getValue('snapshotName');
122 const mirrorImageSnapshot = this.snapshotForm.getValue('mirrorImageSnapshot');
123 const imageSpec = new ImageSpec(this.poolName, this.namespace, this.imageName);
124 const finishedTask = new FinishedTask();
125 finishedTask.name = 'rbd/snap/create';
126 finishedTask.metadata = {
127 image_spec: imageSpec.toString(),
128 snapshot_name: snapshotName
131 .createSnapshot(imageSpec, snapshotName, mirrorImageSnapshot)
134 this.taskManagerService.subscribe(
136 finishedTask.metadata,
137 (asyncFinishedTask: FinishedTask) => {
138 this.notificationService.notifyTask(asyncFinishedTask);
141 this.cdsModalService.dismissAll();
142 this.onSubmit.next(snapshotName);
145 this.snapshotForm.setErrors({ cdSubmitButton: true });