]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { Component, OnInit } from '@angular/core'; |
2 | import { FormControl, Validators } from '@angular/forms'; | |
3 | ||
92f5a8d4 | 4 | import { I18n } from '@ngx-translate/i18n-polyfill'; |
11fdf7f2 TL |
5 | import { BsModalRef } from 'ngx-bootstrap/modal'; |
6 | import { Subject } from 'rxjs'; | |
7 | ||
8 | import { RbdService } from '../../../shared/api/rbd.service'; | |
92f5a8d4 | 9 | import { ActionLabelsI18n } from '../../../shared/constants/app.constants'; |
11fdf7f2 TL |
10 | import { CdFormGroup } from '../../../shared/forms/cd-form-group'; |
11 | import { FinishedTask } from '../../../shared/models/finished-task'; | |
12 | import { NotificationService } from '../../../shared/services/notification.service'; | |
13 | import { TaskManagerService } from '../../../shared/services/task-manager.service'; | |
14 | ||
15 | @Component({ | |
16 | selector: 'cd-rbd-snapshot-form', | |
17 | templateUrl: './rbd-snapshot-form.component.html', | |
18 | styleUrls: ['./rbd-snapshot-form.component.scss'] | |
19 | }) | |
20 | export class RbdSnapshotFormComponent implements OnInit { | |
21 | poolName: string; | |
22 | imageName: string; | |
23 | snapName: string; | |
24 | ||
25 | snapshotForm: CdFormGroup; | |
26 | ||
27 | editing = false; | |
92f5a8d4 TL |
28 | action: string; |
29 | resource: string; | |
11fdf7f2 TL |
30 | |
31 | public onSubmit: Subject<string>; | |
32 | ||
33 | constructor( | |
34 | public modalRef: BsModalRef, | |
35 | private rbdService: RbdService, | |
36 | private taskManagerService: TaskManagerService, | |
92f5a8d4 TL |
37 | private notificationService: NotificationService, |
38 | private i18n: I18n, | |
39 | private actionLabels: ActionLabelsI18n | |
11fdf7f2 | 40 | ) { |
92f5a8d4 TL |
41 | this.action = this.actionLabels.CREATE; |
42 | this.resource = this.i18n('RBD Snapshot'); | |
11fdf7f2 TL |
43 | this.createForm(); |
44 | } | |
45 | ||
46 | createForm() { | |
47 | this.snapshotForm = new CdFormGroup({ | |
48 | snapshotName: new FormControl('', { | |
49 | validators: [Validators.required] | |
50 | }) | |
51 | }); | |
52 | } | |
53 | ||
54 | ngOnInit() { | |
55 | this.onSubmit = new Subject(); | |
56 | } | |
57 | ||
58 | setSnapName(snapName) { | |
59 | this.snapName = snapName; | |
60 | this.snapshotForm.get('snapshotName').setValue(snapName); | |
61 | } | |
62 | ||
63 | /** | |
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 | |
67 | */ | |
68 | setEditing(editing: boolean = true) { | |
69 | this.editing = editing; | |
92f5a8d4 | 70 | this.action = this.editing ? this.actionLabels.RENAME : this.actionLabels.CREATE; |
11fdf7f2 TL |
71 | } |
72 | ||
73 | editAction() { | |
74 | const snapshotName = this.snapshotForm.getValue('snapshotName'); | |
75 | const finishedTask = new FinishedTask(); | |
76 | finishedTask.name = 'rbd/snap/edit'; | |
77 | finishedTask.metadata = { | |
78 | pool_name: this.poolName, | |
79 | image_name: this.imageName, | |
80 | snapshot_name: snapshotName | |
81 | }; | |
82 | this.rbdService | |
83 | .renameSnapshot(this.poolName, this.imageName, this.snapName, snapshotName) | |
84 | .toPromise() | |
85 | .then(() => { | |
86 | this.taskManagerService.subscribe( | |
87 | finishedTask.name, | |
88 | finishedTask.metadata, | |
89 | (asyncFinishedTask: FinishedTask) => { | |
90 | this.notificationService.notifyTask(asyncFinishedTask); | |
91 | } | |
92 | ); | |
93 | this.modalRef.hide(); | |
94 | this.onSubmit.next(this.snapName); | |
95 | }) | |
96 | .catch(() => { | |
97 | this.snapshotForm.setErrors({ cdSubmitButton: true }); | |
98 | }); | |
99 | } | |
100 | ||
101 | createAction() { | |
102 | const snapshotName = this.snapshotForm.getValue('snapshotName'); | |
103 | const finishedTask = new FinishedTask(); | |
104 | finishedTask.name = 'rbd/snap/create'; | |
105 | finishedTask.metadata = { | |
106 | pool_name: this.poolName, | |
107 | image_name: this.imageName, | |
108 | snapshot_name: snapshotName | |
109 | }; | |
110 | this.rbdService | |
111 | .createSnapshot(this.poolName, this.imageName, snapshotName) | |
112 | .toPromise() | |
113 | .then(() => { | |
114 | this.taskManagerService.subscribe( | |
115 | finishedTask.name, | |
116 | finishedTask.metadata, | |
117 | (asyncFinishedTask: FinishedTask) => { | |
118 | this.notificationService.notifyTask(asyncFinishedTask); | |
119 | } | |
120 | ); | |
121 | this.modalRef.hide(); | |
122 | this.onSubmit.next(snapshotName); | |
123 | }) | |
124 | .catch(() => { | |
125 | this.snapshotForm.setErrors({ cdSubmitButton: true }); | |
126 | }); | |
127 | } | |
128 | ||
129 | submit() { | |
130 | if (this.editing) { | |
131 | this.editAction(); | |
132 | } else { | |
133 | this.createAction(); | |
134 | } | |
135 | } | |
136 | } |