1 import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
3 import { I18n } from '@ngx-translate/i18n-polyfill';
4 import * as _ from 'lodash';
5 import * as moment from 'moment';
6 import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
8 import { RbdService } from '../../../shared/api/rbd.service';
9 import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
10 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
11 import { TableComponent } from '../../../shared/datatable/table/table.component';
12 import { CellTemplate } from '../../../shared/enum/cell-template.enum';
13 import { ViewCacheStatus } from '../../../shared/enum/view-cache-status.enum';
14 import { CdTableAction } from '../../../shared/models/cd-table-action';
15 import { CdTableColumn } from '../../../shared/models/cd-table-column';
16 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
17 import { ExecutingTask } from '../../../shared/models/executing-task';
18 import { FinishedTask } from '../../../shared/models/finished-task';
19 import { Permission } from '../../../shared/models/permissions';
20 import { CdDatePipe } from '../../../shared/pipes/cd-date.pipe';
21 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
22 import { TaskListService } from '../../../shared/services/task-list.service';
23 import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
24 import { RbdTrashPurgeModalComponent } from '../rbd-trash-purge-modal/rbd-trash-purge-modal.component';
25 import { RbdTrashRestoreModalComponent } from '../rbd-trash-restore-modal/rbd-trash-restore-modal.component';
28 selector: 'cd-rbd-trash-list',
29 templateUrl: './rbd-trash-list.component.html',
30 styleUrls: ['./rbd-trash-list.component.scss'],
31 providers: [TaskListService]
33 export class RbdTrashListComponent implements OnInit {
34 @ViewChild(TableComponent)
35 table: TableComponent;
36 @ViewChild('expiresTpl')
37 expiresTpl: TemplateRef<any>;
38 @ViewChild('deleteTpl')
39 deleteTpl: TemplateRef<any>;
41 columns: CdTableColumn[];
42 executingTasks: ExecutingTask[] = [];
45 permission: Permission;
47 selection = new CdTableSelection();
48 tableActions: CdTableAction[];
49 viewCacheStatusList: any[];
52 private authStorageService: AuthStorageService,
53 private rbdService: RbdService,
54 private modalService: BsModalService,
55 private cdDatePipe: CdDatePipe,
56 private taskListService: TaskListService,
57 private taskWrapper: TaskWrapperService,
59 public actionLabels: ActionLabelsI18n
61 this.permission = this.authStorageService.getPermissions().rbdImage;
63 const restoreAction: CdTableAction = {
66 click: () => this.restoreModal(),
67 name: this.actionLabels.RESTORE
69 const deleteAction: CdTableAction = {
72 click: () => this.deleteModal(),
73 name: this.actionLabels.DELETE
75 this.tableActions = [restoreAction, deleteAction];
81 name: this.i18n('ID'),
84 cellTransformation: CellTemplate.executing
87 name: this.i18n('Name'),
92 name: this.i18n('Pool'),
97 name: this.i18n('Status'),
98 prop: 'deferment_end_time',
100 cellTemplate: this.expiresTpl
103 name: this.i18n('Deleted At'),
104 prop: 'deletion_time',
106 pipe: this.cdDatePipe
110 this.taskListService.init(
111 () => this.rbdService.listTrash(),
112 (resp) => this.prepareResponse(resp),
113 (images) => (this.images = images),
114 () => this.onFetchError(),
121 prepareResponse(resp: any[]): any[] {
123 const viewCacheStatusMap = {};
124 resp.forEach((pool) => {
125 if (_.isUndefined(viewCacheStatusMap[pool.status])) {
126 viewCacheStatusMap[pool.status] = [];
128 viewCacheStatusMap[pool.status].push(pool.pool_name);
129 images = images.concat(pool.value);
132 const viewCacheStatusList = [];
133 _.forEach(viewCacheStatusMap, (value: any, key) => {
134 viewCacheStatusList.push({
135 status: parseInt(key, 10),
137 (value.length > 1 ? 'pools ' : 'pool ') +
139 value.join('</strong>, <strong>') +
143 this.viewCacheStatusList = viewCacheStatusList;
144 images.forEach((image) => {
145 image.cdIsExpired = moment().isAfter(image.deferment_end_time);
151 this.table.reset(); // Disable loading indicator.
152 this.viewCacheStatusList = [{ status: ViewCacheStatus.ValueException }];
155 itemFilter(entry, task) {
156 return entry.id === task.metadata['image_id'];
160 return ['rbd/trash/remove', 'rbd/trash/restore'].includes(task.name);
163 updateSelection(selection: CdTableSelection) {
164 this.selection = selection;
168 const initialState = {
170 poolName: this.selection.first().pool_name,
171 imageName: this.selection.first().name,
172 imageId: this.selection.first().id
175 this.modalRef = this.modalService.show(RbdTrashRestoreModalComponent, { initialState });
179 const poolName = this.selection.first().pool_name;
180 const imageName = this.selection.first().name;
181 const imageId = this.selection.first().id;
182 const expiresAt = this.selection.first().deferment_end_time;
184 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
186 itemDescription: 'RBD',
187 itemNames: [`${poolName}/${imageName}`],
188 bodyTemplate: this.deleteTpl,
189 bodyContext: { $implicit: expiresAt },
190 submitActionObservable: () =>
191 this.taskWrapper.wrapTaskAroundCall({
192 task: new FinishedTask('rbd/trash/remove', {
195 image_name: imageName
197 call: this.rbdService.removeTrash(poolName, imageId, imageName, true)
203 isExpired(expiresAt): boolean {
204 return moment().isAfter(expiresAt);
208 this.modalService.show(RbdTrashPurgeModalComponent);