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