1 import { Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core';
2 import { Bucket } from '../models/rgw-bucket';
3 import { RgwBucketService } from '~/app/shared/api/rgw-bucket.service';
4 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
5 import { Icons } from '~/app/shared/enum/icons.enum';
6 import { CdTableAction } from '~/app/shared/models/cd-table-action';
7 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
8 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
9 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
10 import { Permission } from '~/app/shared/models/permissions';
11 import { TableComponent } from '~/app/shared/datatable/table/table.component';
12 import { CdTableFetchDataContext } from '~/app/shared/models/cd-table-fetch-data-context';
13 import { ModalCdsService } from '~/app/shared/services/modal-cds.service';
14 import { RgwBucketTieringFormComponent } from '../rgw-bucket-tiering-form/rgw-bucket-tiering-form.component';
15 import { DeleteConfirmationModalComponent } from '~/app/shared/components/delete-confirmation-modal/delete-confirmation-modal.component';
16 import { Observable, of } from 'rxjs';
17 import { catchError, map, tap } from 'rxjs/operators';
18 import { NotificationService } from '~/app/shared/services/notification.service';
19 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
22 selector: 'cd-rgw-bucket-lifecycle-list',
23 templateUrl: './rgw-bucket-lifecycle-list.component.html',
24 styleUrls: ['./rgw-bucket-lifecycle-list.component.scss']
26 export class RgwBucketLifecycleListComponent implements OnInit {
27 @Input() bucket: Bucket;
28 @Output() updateBucketDetails = new EventEmitter();
29 @ViewChild(TableComponent, { static: true })
30 table: TableComponent;
31 permission: Permission;
32 tableActions: CdTableAction[];
33 columns: CdTableColumn[] = [];
34 selection: CdTableSelection = new CdTableSelection();
35 filteredLifecycleRules$: Observable<any[]>;
36 lifecycleRuleList: any = [];
40 private rgwBucketService: RgwBucketService,
41 private authStorageService: AuthStorageService,
42 public actionLabels: ActionLabelsI18n,
43 private modalService: ModalCdsService,
44 private notificationService: NotificationService
48 this.permission = this.authStorageService.getPermissions().rgw;
51 name: $localize`Name`,
56 name: $localize`Days`,
57 prop: 'Transition.Days',
61 name: $localize`Storage class`,
62 prop: 'Transition.StorageClass',
66 name: $localize`Status`,
71 const createAction: CdTableAction = {
74 click: () => this.openTieringModal(this.actionLabels.CREATE),
75 name: this.actionLabels.CREATE
77 const editAction: CdTableAction = {
80 disable: () => this.selection.hasMultiSelection,
81 click: () => this.openTieringModal(this.actionLabels.EDIT),
82 name: this.actionLabels.EDIT
84 const deleteAction: CdTableAction = {
87 click: () => this.deleteAction(),
88 disable: () => !this.selection.hasSelection,
89 name: this.actionLabels.DELETE,
90 canBePrimary: (selection: CdTableSelection) => selection.hasMultiSelection
92 this.tableActions = [createAction, editAction, deleteAction];
95 loadLifecyclePolicies(context: CdTableFetchDataContext) {
96 const allLifecycleRules$ = this.rgwBucketService
97 .getLifecycle(this.bucket.bucket, this.bucket.owner)
100 this.lifecycleRuleList = lifecycle;
108 this.filteredLifecycleRules$ = allLifecycleRules$.pipe(
111 lifecycle?.LifecycleConfiguration?.Rules?.filter((rule: object) =>
112 rule.hasOwnProperty('Transition')
118 openTieringModal(type: string) {
119 const modalRef = this.modalService.show(RgwBucketTieringFormComponent, {
121 selectedLifecycle: this.selection.first(),
122 editing: type === this.actionLabels.EDIT ? true : false
124 modalRef?.close?.subscribe(() => this.updateBucketDetails.emit());
127 updateSelection(selection: CdTableSelection) {
128 this.selection = selection;
132 const ruleNames = this.selection.selected.map((rule) => rule.ID);
133 const filteredRules = this.lifecycleRuleList.LifecycleConfiguration.Rules.filter(
134 (rule: any) => !ruleNames.includes(rule.ID)
136 const rules = filteredRules.length > 0 ? { Rules: filteredRules } : {};
137 this.modalRef = this.modalService.show(DeleteConfirmationModalComponent, {
138 itemDescription: $localize`Rule`,
139 itemNames: ruleNames,
140 actionDescription: $localize`remove`,
141 submitAction: () => this.submitLifecycleConfig(rules)
145 submitLifecycleConfig(rules: any) {
146 this.rgwBucketService
147 .setLifecycle(this.bucket.bucket, JSON.stringify(rules), this.bucket.owner)
150 this.notificationService.show(
151 NotificationType.success,
152 $localize`Lifecycle rule deleted successfully`
154 this.updateBucketDetails.emit();
157 this.modalRef.componentInstance.stopLoadingSpinner();
160 this.modalService.dismissAll();