]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
ba1ccbed0c6577631b4b6c7f45bad364156875a9
[ceph.git] /
1 import { Component, OnInit } from '@angular/core';
2
3 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
4 import * as moment from 'moment';
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   // initial state
22   poolName: string;
23   namespace: string;
24   imageName: string;
25   hasSnapshots: boolean;
26
27   imageSpec: ImageSpec;
28   imageSpecStr: string;
29   executingTasks: ExecutingTask[];
30
31   moveForm: CdFormGroup;
32   pattern: string;
33
34   constructor(
35     private rbdService: RbdService,
36     public activeModal: NgbActiveModal,
37     private fb: CdFormBuilder,
38     private taskWrapper: TaskWrapperService
39   ) {
40     this.createForm();
41   }
42
43   createForm() {
44     this.moveForm = this.fb.group({
45       expiresAt: [
46         '',
47         [
48           CdValidators.custom('format', (expiresAt: string) => {
49             const result = expiresAt === '' || moment(expiresAt, 'YYYY-MM-DD HH:mm:ss').isValid();
50             return !result;
51           }),
52           CdValidators.custom('expired', (expiresAt: string) => {
53             const result = moment().isAfter(expiresAt);
54             return result;
55           })
56         ]
57       ]
58     });
59   }
60
61   ngOnInit() {
62     this.imageSpec = new ImageSpec(this.poolName, this.namespace, this.imageName);
63     this.imageSpecStr = this.imageSpec.toString();
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, 'YYYY-MM-DD HH:mm:ss').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.imageSpecStr
83         }),
84         call: this.rbdService.moveTrash(this.imageSpec, delay)
85       })
86       .subscribe({
87         complete: () => {
88           this.activeModal.close();
89         }
90       });
91   }
92 }