1 import { TitleCasePipe } from '@angular/common';
2 import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
3 import { Router } from '@angular/router';
4 import { forkJoin as observableForkJoin, Observable, Subscriber } from 'rxjs';
5 import { RgwDaemonService } from '~/app/shared/api/rgw-daemon.service';
6 import { RgwMultisiteService } from '~/app/shared/api/rgw-multisite.service';
7 import { ListWithDetails } from '~/app/shared/classes/list-with-details.class';
8 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
9 import { ActionLabelsI18n, URLVerbs } from '~/app/shared/constants/app.constants';
10 import { TableComponent } from '~/app/shared/datatable/table/table.component';
11 import { CellTemplate } from '~/app/shared/enum/cell-template.enum';
12 import { Icons } from '~/app/shared/enum/icons.enum';
13 import { CdTableAction } from '~/app/shared/models/cd-table-action';
14 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
15 import { CdTableFetchDataContext } from '~/app/shared/models/cd-table-fetch-data-context';
16 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
17 import { FinishedTask } from '~/app/shared/models/finished-task';
18 import { Permission } from '~/app/shared/models/permissions';
19 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
20 import { ModalCdsService } from '~/app/shared/services/modal-cds.service';
21 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
22 import { URLBuilderService } from '~/app/shared/services/url-builder.service';
24 const BASE_URL = 'rgw/multisite/sync-policy';
27 selector: 'cd-rgw-multisite-sync-policy',
28 templateUrl: './rgw-multisite-sync-policy.component.html',
29 styleUrls: ['./rgw-multisite-sync-policy.component.scss'],
30 providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }]
32 export class RgwMultisiteSyncPolicyComponent extends ListWithDetails implements OnInit {
33 @ViewChild(TableComponent, { static: true })
34 table: TableComponent;
35 @ViewChild('deleteTpl', { static: true })
36 deleteTpl: TemplateRef<any>;
38 columns: Array<CdTableColumn> = [];
39 syncPolicyData: any = [];
40 tableActions: CdTableAction[];
41 selection = new CdTableSelection();
42 permission: Permission;
45 private rgwMultisiteService: RgwMultisiteService,
46 private titleCasePipe: TitleCasePipe,
47 private actionLabels: ActionLabelsI18n,
48 private authStorageService: AuthStorageService,
49 private modalService: ModalCdsService,
50 private taskWrapper: TaskWrapperService,
51 private router: Router,
52 private rgwDaemonService: RgwDaemonService
58 this.permission = this.authStorageService.getPermissions().rgw;
66 name: $localize`Group Name`,
71 name: $localize`Status`,
74 cellTransformation: CellTemplate.tooltip,
75 customTemplateConfig: {
77 Enabled: { class: 'badge-success', tooltip: 'sync is allowed and enabled' },
78 Allowed: { class: 'badge-info', tooltip: 'sync is allowed' },
80 class: 'badge-warning',
82 'sync (as defined by this group) is not allowed and can override other groups'
86 pipe: this.titleCasePipe
89 name: $localize`Zonegroup`,
92 cellTransformation: CellTemplate.map,
93 customTemplateConfig: {
99 name: $localize`Bucket`,
102 cellTransformation: CellTemplate.map,
103 customTemplateConfig: {
109 this.rgwDaemonService.list().subscribe();
110 const getEditURL = () => {
111 if (this.selection.first().groupName && this.selection.first().bucket) {
112 return `${URLVerbs.EDIT}/${this.selection.first().groupName}/${
113 this.selection.first().bucket
116 return `${URLVerbs.EDIT}/${this.selection.first().groupName}`;
118 const addAction: CdTableAction = {
119 permission: 'create',
121 click: () => this.router.navigate([BASE_URL, { outlets: { modal: URLVerbs.CREATE } }]),
122 name: this.actionLabels.CREATE,
123 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection
125 const editAction: CdTableAction = {
126 permission: 'update',
128 click: () => this.router.navigate([BASE_URL, { outlets: { modal: getEditURL() } }]),
129 name: this.actionLabels.EDIT
131 const deleteAction: CdTableAction = {
132 permission: 'delete',
134 click: () => this.deleteAction(),
135 disable: () => !this.selection.hasSelection,
136 name: this.actionLabels.DELETE,
137 canBePrimary: (selection: CdTableSelection) => selection.hasMultiSelection
139 this.tableActions = [addAction, editAction, deleteAction];
142 transformSyncPolicyData(allSyncPolicyData: any) {
143 if (allSyncPolicyData && allSyncPolicyData.length > 0) {
144 allSyncPolicyData.forEach((policy: any) => {
145 this.syncPolicyData.push({
146 uniqueId: policy['id'] + (policy['bucketName'] ? policy['bucketName'] : ''),
147 groupName: policy['id'],
148 status: policy['status'],
149 bucket: policy['bucketName'],
150 zonegroup: policy['zonegroup']
153 this.syncPolicyData = [...this.syncPolicyData];
157 updateSelection(selection: CdTableSelection) {
158 this.selection = selection;
161 getPolicyList(context?: CdTableFetchDataContext) {
162 this.rgwMultisiteService.getSyncPolicy('', '', true).subscribe(
163 (resp: object[]) => {
164 this.syncPolicyData = [];
165 this.transformSyncPolicyData(resp);
176 const groupNames = this.selection.selected.map((policy: any) => policy.groupName);
177 this.modalService.show(CriticalConfirmationModalComponent, {
178 itemDescription: this.selection.hasSingleSelection
179 ? $localize`Policy Group`
180 : $localize`Policy Groups`,
181 itemNames: groupNames,
182 bodyTemplate: this.deleteTpl,
183 submitActionObservable: () => {
184 return new Observable((observer: Subscriber<any>) => {
186 .wrapTaskAroundCall({
187 task: new FinishedTask('rgw/multisite/sync-policy/delete', {
188 group_names: groupNames
190 call: observableForkJoin(
191 this.selection.selected.map((policy: any) => {
192 return this.rgwMultisiteService.removeSyncPolicyGroup(
200 error: (error: any) => {
201 // Forward the error to the observer.
202 observer.error(error);
203 // Reload the data table content because some deletions might
204 // have been executed successfully in the meanwhile.
205 this.table.refreshBtn();
208 // Notify the observer that we are done.
210 // Reload the data table content.
211 this.table.refreshBtn();