]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
a48feebe2372373994a9842ef7ddbab0f5dfd4e2
[ceph-ci.git] /
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';
20
21 @Component({
22   selector: 'cd-rgw-bucket-lifecycle-list',
23   templateUrl: './rgw-bucket-lifecycle-list.component.html',
24   styleUrls: ['./rgw-bucket-lifecycle-list.component.scss']
25 })
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 = [];
37   modalRef: any;
38
39   constructor(
40     private rgwBucketService: RgwBucketService,
41     private authStorageService: AuthStorageService,
42     public actionLabels: ActionLabelsI18n,
43     private modalService: ModalCdsService,
44     private notificationService: NotificationService
45   ) {}
46
47   ngOnInit() {
48     this.permission = this.authStorageService.getPermissions().rgw;
49     this.columns = [
50       {
51         name: $localize`Name`,
52         prop: 'ID',
53         flexGrow: 2
54       },
55       {
56         name: $localize`Days`,
57         prop: 'Transition.Days',
58         flexGrow: 1
59       },
60       {
61         name: $localize`Storage class`,
62         prop: 'Transition.StorageClass',
63         flexGrow: 1
64       },
65       {
66         name: $localize`Status`,
67         prop: 'Status',
68         flexGrow: 1
69       }
70     ];
71     const createAction: CdTableAction = {
72       permission: 'create',
73       icon: Icons.add,
74       click: () => this.openTieringModal(this.actionLabels.CREATE),
75       name: this.actionLabels.CREATE
76     };
77     const editAction: CdTableAction = {
78       permission: 'update',
79       icon: Icons.edit,
80       disable: () => this.selection.hasMultiSelection,
81       click: () => this.openTieringModal(this.actionLabels.EDIT),
82       name: this.actionLabels.EDIT
83     };
84     const deleteAction: CdTableAction = {
85       permission: 'delete',
86       icon: Icons.destroy,
87       click: () => this.deleteAction(),
88       disable: () => !this.selection.hasSelection,
89       name: this.actionLabels.DELETE,
90       canBePrimary: (selection: CdTableSelection) => selection.hasMultiSelection
91     };
92     this.tableActions = [createAction, editAction, deleteAction];
93   }
94
95   loadLifecyclePolicies(context: CdTableFetchDataContext) {
96     const allLifecycleRules$ = this.rgwBucketService
97       .getLifecycle(this.bucket.bucket, this.bucket.owner)
98       .pipe(
99         tap((lifecycle) => {
100           this.lifecycleRuleList = lifecycle;
101         }),
102         catchError(() => {
103           context.error();
104           return of(null);
105         })
106       );
107
108     this.filteredLifecycleRules$ = allLifecycleRules$.pipe(
109       map(
110         (lifecycle: any) =>
111           lifecycle?.LifecycleConfiguration?.Rules?.filter((rule: object) =>
112             rule.hasOwnProperty('Transition')
113           ) || []
114       )
115     );
116   }
117
118   openTieringModal(type: string) {
119     const modalRef = this.modalService.show(RgwBucketTieringFormComponent, {
120       bucket: this.bucket,
121       selectedLifecycle: this.selection.first(),
122       editing: type === this.actionLabels.EDIT ? true : false
123     });
124     modalRef?.close?.subscribe(() => this.updateBucketDetails.emit());
125   }
126
127   updateSelection(selection: CdTableSelection) {
128     this.selection = selection;
129   }
130
131   deleteAction() {
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)
135     );
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)
142     });
143   }
144
145   submitLifecycleConfig(rules: any) {
146     this.rgwBucketService
147       .setLifecycle(this.bucket.bucket, JSON.stringify(rules), this.bucket.owner)
148       .subscribe({
149         next: () => {
150           this.notificationService.show(
151             NotificationType.success,
152             $localize`Lifecycle rule deleted successfully`
153           );
154           this.updateBucketDetails.emit();
155         },
156         error: () => {
157           this.modalRef.componentInstance.stopLoadingSpinner();
158         },
159         complete: () => {
160           this.modalService.dismissAll();
161         }
162       });
163   }
164 }