1 import { TitleCasePipe } from '@angular/common';
2 import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
3 import { forkJoin as observableForkJoin, Observable, Subscriber } from 'rxjs';
4 import { RgwMultisiteService } from '~/app/shared/api/rgw-multisite.service';
5 import { ListWithDetails } from '~/app/shared/classes/list-with-details.class';
6 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
7 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
8 import { TableComponent } from '~/app/shared/datatable/table/table.component';
9 import { CellTemplate } from '~/app/shared/enum/cell-template.enum';
10 import { Icons } from '~/app/shared/enum/icons.enum';
11 import { CdTableAction } from '~/app/shared/models/cd-table-action';
12 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
13 import { CdTableFetchDataContext } from '~/app/shared/models/cd-table-fetch-data-context';
14 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
15 import { FinishedTask } from '~/app/shared/models/finished-task';
16 import { Permission } from '~/app/shared/models/permissions';
17 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
18 import { ModalService } from '~/app/shared/services/modal.service';
19 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
20 import { URLBuilderService } from '~/app/shared/services/url-builder.service';
22 const BASE_URL = 'rgw/multisite/sync-policy';
25 selector: 'cd-rgw-multisite-sync-policy',
26 templateUrl: './rgw-multisite-sync-policy.component.html',
27 styleUrls: ['./rgw-multisite-sync-policy.component.scss'],
28 providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }]
30 export class RgwMultisiteSyncPolicyComponent extends ListWithDetails implements OnInit {
31 @ViewChild(TableComponent, { static: true })
32 table: TableComponent;
33 @ViewChild('deleteTpl', { static: true })
34 deleteTpl: TemplateRef<any>;
36 columns: Array<CdTableColumn> = [];
37 syncPolicyData: any = [];
38 tableActions: CdTableAction[];
39 selection = new CdTableSelection();
40 permission: Permission;
43 private rgwMultisiteService: RgwMultisiteService,
44 private titleCasePipe: TitleCasePipe,
45 private actionLabels: ActionLabelsI18n,
46 private urlBuilder: URLBuilderService,
47 private authStorageService: AuthStorageService,
48 private modalService: ModalService,
49 private taskWrapper: TaskWrapperService
55 this.permission = this.authStorageService.getPermissions().rgw;
62 name: $localize`Group Name`,
67 name: $localize`Status`,
70 cellTransformation: CellTemplate.tooltip,
71 customTemplateConfig: {
73 Enabled: { class: 'badge-success', tooltip: 'sync is allowed and enabled' },
74 Allowed: { class: 'badge-info', tooltip: 'sync is allowed' },
76 class: 'badge-warning',
78 'sync (as defined by this group) is not allowed and can override other groups'
82 pipe: this.titleCasePipe
85 name: $localize`Zonegroup`,
90 name: $localize`Bucket`,
95 const getSyncGroupName = () => {
96 if (this.selection.first() && this.selection.first().groupName) {
97 if (this.selection.first().bucket) {
98 return `${encodeURIComponent(this.selection.first().groupName)}/${encodeURIComponent(
99 this.selection.first().bucket
102 return `${encodeURIComponent(this.selection.first().groupName)}`;
106 const addAction: CdTableAction = {
107 permission: 'create',
109 routerLink: () => this.urlBuilder.getCreate(),
110 name: this.actionLabels.CREATE,
111 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection
113 const editAction: CdTableAction = {
114 permission: 'update',
116 routerLink: () => this.urlBuilder.getEdit(getSyncGroupName()),
117 name: this.actionLabels.EDIT
119 const deleteAction: CdTableAction = {
120 permission: 'delete',
122 click: () => this.deleteAction(),
123 disable: () => !this.selection.hasSelection,
124 name: this.actionLabels.DELETE,
125 canBePrimary: (selection: CdTableSelection) => selection.hasMultiSelection
127 this.tableActions = [addAction, editAction, deleteAction];
130 transformSyncPolicyData(allSyncPolicyData: any) {
131 if (allSyncPolicyData && allSyncPolicyData.length > 0) {
132 allSyncPolicyData.forEach((policy: any) => {
133 this.syncPolicyData.push({
134 uniqueId: policy['id'] + (policy['bucketName'] ? policy['bucketName'] : ''),
135 groupName: policy['id'],
136 status: policy['status'],
137 bucket: policy['bucketName'],
141 this.syncPolicyData = [...this.syncPolicyData];
145 updateSelection(selection: CdTableSelection) {
146 this.selection = selection;
149 getPolicyList(context: CdTableFetchDataContext) {
150 this.rgwMultisiteService.getSyncPolicy('', '', true).subscribe(
151 (resp: object[]) => {
152 this.syncPolicyData = [];
153 this.transformSyncPolicyData(resp);
162 const groupNames = this.selection.selected.map((policy: any) => policy.groupName);
163 this.modalService.show(CriticalConfirmationModalComponent, {
164 itemDescription: this.selection.hasSingleSelection
165 ? $localize`Policy Group`
166 : $localize`Policy Groups`,
167 itemNames: groupNames,
168 bodyTemplate: this.deleteTpl,
169 submitActionObservable: () => {
170 return new Observable((observer: Subscriber<any>) => {
172 .wrapTaskAroundCall({
173 task: new FinishedTask('rgw/multisite/sync-policy/delete', {
174 group_names: groupNames
176 call: observableForkJoin(
177 this.selection.selected.map((policy: any) => {
178 return this.rgwMultisiteService.removeSyncPolicyGroup(
186 error: (error: any) => {
187 // Forward the error to the observer.
188 observer.error(error);
189 // Reload the data table content because some deletions might
190 // have been executed successfully in the meanwhile.
191 this.table.refreshBtn();
194 // Notify the observer that we are done.
196 // Reload the data table content.
197 this.table.refreshBtn();