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