]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
14a7543fbdcfe8476a3cefc9c5a69b6c32965cc3
[ceph.git] /
1 import { Component, OnInit } from '@angular/core';
2
3 import * as moment from 'moment';
4 import { BsModalRef } from 'ngx-bootstrap/modal';
5
6 import { RbdService } from '../../../shared/api/rbd.service';
7 import { CdFormBuilder } from '../../../shared/forms/cd-form-builder';
8 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
9 import { CdValidators } from '../../../shared/forms/cd-validators';
10 import { ExecutingTask } from '../../../shared/models/executing-task';
11 import { FinishedTask } from '../../../shared/models/finished-task';
12 import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
13
14 @Component({
15   selector: 'cd-rbd-trash-move-modal',
16   templateUrl: './rbd-trash-move-modal.component.html',
17   styleUrls: ['./rbd-trash-move-modal.component.scss']
18 })
19 export class RbdTrashMoveModalComponent implements OnInit {
20   metaType: string;
21   poolName: string;
22   namespace: string;
23   imageName: string;
24   imageSpec: string;
25   executingTasks: ExecutingTask[];
26
27   moveForm: CdFormGroup;
28   minDate = new Date();
29   bsConfig = {
30     dateInputFormat: 'YYYY-MM-DD HH:mm:ss',
31     containerClass: 'theme-default'
32   };
33   pattern: string;
34
35   constructor(
36     private rbdService: RbdService,
37     public modalRef: BsModalRef,
38     private fb: CdFormBuilder,
39     private taskWrapper: TaskWrapperService
40   ) {
41     this.createForm();
42   }
43
44   createForm() {
45     this.moveForm = this.fb.group({
46       expiresAt: [
47         '',
48         [
49           CdValidators.custom('format', (expiresAt) => {
50             const result = expiresAt === '' || moment(expiresAt, 'YYYY-MM-DD HH:mm:ss').isValid();
51             return !result;
52           }),
53           CdValidators.custom('expired', (expiresAt) => {
54             const result = moment().isAfter(expiresAt);
55             return result;
56           })
57         ]
58       ]
59     });
60   }
61
62   ngOnInit() {
63     this.imageSpec = this.rbdService.getImageSpec(this.poolName, this.namespace, this.imageName);
64     this.pattern = `${this.poolName}/${this.imageName}`;
65   }
66
67   moveImage() {
68     let delay = 0;
69     const expiresAt = this.moveForm.getValue('expiresAt');
70
71     if (expiresAt) {
72       delay = moment(expiresAt).diff(moment(), 'seconds', true);
73     }
74
75     if (delay < 0) {
76       delay = 0;
77     }
78
79     this.taskWrapper
80       .wrapTaskAroundCall({
81         task: new FinishedTask('rbd/trash/move', {
82           image_spec: this.imageSpec
83         }),
84         call: this.rbdService.moveTrash(this.poolName, this.namespace, this.imageName, delay)
85       })
86       .subscribe(undefined, undefined, () => {
87         this.modalRef.hide();
88       });
89   }
90 }