]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
2cb712b6aebd23e75ca885fb80582c3ddf6d2d14
[ceph-ci.git] /
1 import { Component, OnInit } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
3
4 import { I18n } from '@ngx-translate/i18n-polyfill';
5 import { BsModalRef } from 'ngx-bootstrap/modal';
6 import { Subject } from 'rxjs';
7
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 { 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   namespace: string;
23   imageName: string;
24   snapName: string;
25
26   snapshotForm: CdFormGroup;
27
28   editing = false;
29   action: string;
30   resource: string;
31
32   public onSubmit: Subject<string>;
33
34   constructor(
35     public modalRef: BsModalRef,
36     private rbdService: RbdService,
37     private taskManagerService: TaskManagerService,
38     private notificationService: NotificationService,
39     private i18n: I18n,
40     private actionLabels: ActionLabelsI18n
41   ) {
42     this.action = this.actionLabels.CREATE;
43     this.resource = this.i18n('RBD Snapshot');
44     this.createForm();
45   }
46
47   createForm() {
48     this.snapshotForm = new CdFormGroup({
49       snapshotName: new FormControl('', {
50         validators: [Validators.required]
51       })
52     });
53   }
54
55   ngOnInit() {
56     this.onSubmit = new Subject();
57   }
58
59   setSnapName(snapName) {
60     this.snapName = snapName;
61     this.snapshotForm.get('snapshotName').setValue(snapName);
62   }
63
64   /**
65    * Set the 'editing' flag. If set to TRUE, the modal dialog is in
66    * 'Edit' mode, otherwise in 'Create' mode.
67    * @param {boolean} editing
68    */
69   setEditing(editing: boolean = true) {
70     this.editing = editing;
71     this.action = this.editing ? this.actionLabels.RENAME : this.actionLabels.CREATE;
72   }
73
74   editAction() {
75     const snapshotName = this.snapshotForm.getValue('snapshotName');
76     const finishedTask = new FinishedTask();
77     finishedTask.name = 'rbd/snap/edit';
78     finishedTask.metadata = {
79       image_spec: this.rbdService.getImageSpec(this.poolName, this.namespace, this.imageName),
80       snapshot_name: snapshotName
81     };
82     this.rbdService
83       .renameSnapshot(this.poolName, this.namespace, 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       image_spec: this.rbdService.getImageSpec(this.poolName, this.namespace, this.imageName),
107       snapshot_name: snapshotName
108     };
109     this.rbdService
110       .createSnapshot(this.poolName, this.namespace, this.imageName, snapshotName)
111       .toPromise()
112       .then(() => {
113         this.taskManagerService.subscribe(
114           finishedTask.name,
115           finishedTask.metadata,
116           (asyncFinishedTask: FinishedTask) => {
117             this.notificationService.notifyTask(asyncFinishedTask);
118           }
119         );
120         this.modalRef.hide();
121         this.onSubmit.next(snapshotName);
122       })
123       .catch(() => {
124         this.snapshotForm.setErrors({ cdSubmitButton: true });
125       });
126   }
127
128   submit() {
129     if (this.editing) {
130       this.editAction();
131     } else {
132       this.createAction();
133     }
134   }
135 }