From e5a77ea15a390ad725234d8693a701b2d0b1bd0e Mon Sep 17 00:00:00 2001 From: Piotr Pekala Date: Wed, 20 Mar 2019 02:58:42 -0700 Subject: [PATCH] Bug with creating snapshots fixed --- .../create-snapshot-dialog.component.html | 11 +++-- .../create-snapshot-dialog.component.ts | 43 ++++++++++++++++++- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/app/components/snapshots/create-snapshot-dialog/create-snapshot-dialog.component.html b/src/app/components/snapshots/create-snapshot-dialog/create-snapshot-dialog.component.html index ca13ce5e..43959d22 100644 --- a/src/app/components/snapshots/create-snapshot-dialog/create-snapshot-dialog.component.html +++ b/src/app/components/snapshots/create-snapshot-dialog/create-snapshot-dialog.component.html @@ -3,9 +3,14 @@
- - - +
+ + + +
diff --git a/src/app/components/snapshots/create-snapshot-dialog/create-snapshot-dialog.component.ts b/src/app/components/snapshots/create-snapshot-dialog/create-snapshot-dialog.component.ts index 739c543a..3518b334 100644 --- a/src/app/components/snapshots/create-snapshot-dialog/create-snapshot-dialog.component.ts +++ b/src/app/components/snapshots/create-snapshot-dialog/create-snapshot-dialog.component.ts @@ -3,6 +3,11 @@ import { Snapshot } from '../../../models/snapshot'; import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material'; import { Server } from '../../../models/server'; import { Project } from '../../../models/project'; +import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms'; +import { ToasterService } from '../../../services/toaster.service'; +import { SnapshotService } from '../../../services/snapshot.service'; +import { NodesDataSource } from '../../../cartography/datasources/nodes-datasource'; +import { Node } from '../../../cartography/models/node'; @Component({ @@ -14,17 +19,53 @@ export class CreateSnapshotDialogComponent { server: Server; project: Project; snapshot: Snapshot = new Snapshot(); + inputForm: FormGroup; + snapshots: string[] = []; + isInRunningState: boolean; constructor( public dialogRef: MatDialogRef, + private formBuilder: FormBuilder, + private toasterService: ToasterService, + private snapshotService: SnapshotService, + private nodesDataSource: NodesDataSource, @Inject(MAT_DIALOG_DATA) public data: any ) { this.server = data['server']; this.project = data['project']; + + this.inputForm = this.formBuilder.group({ + snapshotName: new FormControl('', Validators.required) + }); + + this.snapshotService.list(this.server, this.project.project_id).subscribe((snapshots: Snapshot[]) => { + snapshots.forEach((snapshot: Snapshot) => { + this.snapshots.push(snapshot.name); + }); + }); + + this.nodesDataSource.getItems().forEach((node: Node) => { + if (node.status !== 'stopped' && !this.isAlwaysRunningNode(node.node_type)) { + this.isInRunningState = true; + } + }); + } + + isAlwaysRunningNode(nodeType: string) { + return !["qemu", "docker", "dynamips", "vpcs", "vmware", "virtualbox", "iou", "traceng"].includes(nodeType); } onAddClick(): void { - this.dialogRef.close(this.snapshot); + if (this.inputForm.invalid) { + this.toasterService.error(`Fill all required fields`); + } else if (this.snapshots.includes(this.inputForm.get('snapshotName').value)) { + this.toasterService.error(`Snapshot with this name already exists`); + } else if (this.isInRunningState) { + this.toasterService.error(`Project must be stopped in order to export it`); + } else { + this.snapshot.name = this.inputForm.get('snapshotName').value; + this.dialogRef.close(this.snapshot); + } } onNoClick(): void {