Merge pull request #60173 from rhcs-dashboard/fix-lc

mgr/dashboard: fix lifecycle issues

Reviewed-by: Afreen Misbah <afreen@ibm.com>
This commit is contained in:
afreen23 2024-10-17 17:11:54 +05:30 committed by GitHub
commit 6258acbfa0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 61 additions and 15 deletions

View File

@ -158,8 +158,14 @@
</div>
</td>
<td>
<pre *ngIf="lifecycleFormat === 'json'">{{selection.lifecycle | json}}</pre>
<pre *ngIf="lifecycleFormat === 'xml'">{{ (selection.lifecycle | xml) || '-'}}</pre>
<cds-code-snippet display="multi"
*ngIf="lifecycleFormat === 'json'">
{{selection.lifecycle | json}}
</cds-code-snippet>
<cds-code-snippet display="multi"
*ngIf="lifecycleFormat === 'xml'">
{{ (selection.lifecycle | xml:{'Rules':'Rule'}) || '-'}}
</cds-code-snippet>
</td>
</tr>
<tr>

View File

@ -70,7 +70,8 @@ import {
IconModule,
LoadingModule,
ModalModule,
ProgressIndicatorModule
ProgressIndicatorModule,
CodeSnippetModule
} from 'carbon-components-angular';
import { CephSharedModule } from '../shared/ceph-shared.module';
@ -94,6 +95,7 @@ import { CephSharedModule } from '../shared/ceph-shared.module';
ModalModule,
GridModule,
ProgressIndicatorModule,
CodeSnippetModule,
ButtonModule,
LoadingModule,
IconModule,

View File

@ -7,9 +7,13 @@ import { JsonToXmlService } from '../services/json-to-xml.service';
export class XmlPipe implements PipeTransform {
constructor(private jsonToXmlService: JsonToXmlService) {}
transform(value: string, valueFormat: string = 'json'): string {
transform(
value: string,
replaceKey: Record<string, string> = {},
valueFormat: string = 'json'
): string {
if (valueFormat === 'json') {
value = this.jsonToXmlService.format(value);
value = this.jsonToXmlService.format(value, replaceKey);
}
return value;
}

View File

@ -6,29 +6,39 @@ import { Injectable } from '@angular/core';
export class JsonToXmlService {
constructor() {}
format(json: any, indentSize: number = 2, currentIndent: number = 0): string {
format(
json: any,
replaceKey: Record<string, string> = null,
indentSize: number = 2,
currentIndent: number = 0
): string {
if (!json) return null;
let xml = '';
if (typeof json === 'string') {
json = JSON.parse(json);
}
for (const key in json) {
for (let key in json) {
if (json.hasOwnProperty(key)) {
const value = json[key];
const indentation = ' '.repeat(currentIndent);
if (replaceKey) {
const [oldKey, newKey] = Object.entries(replaceKey)[0];
if (key === oldKey) {
key = newKey;
}
}
if (Array.isArray(value)) {
value.forEach((item) => {
xml +=
`${indentation}<${key}>\n` +
this.format(item, indentSize, currentIndent + indentSize) +
this.format(item, replaceKey, indentSize, currentIndent + indentSize) +
`${indentation}</${key}>\n`;
});
} else if (typeof value === 'object') {
xml +=
`${indentation}<${key}>\n` +
this.format(value, indentSize, currentIndent + indentSize) +
this.format(value, replaceKey, indentSize, currentIndent + indentSize) +
`${indentation}</${key}>\n`;
} else {
xml += `${indentation}<${key}>${value}</${key}>\n`;

View File

@ -142,3 +142,10 @@ Dashboard page
cd-dashboard {
font-size: 12px;
}
/******************************************
Code snippet
******************************************/
.cds--snippet {
width: fit-content;
}

View File

@ -10,6 +10,7 @@ import re
import time
import uuid
import xml.etree.ElementTree as ET # noqa: N814
from collections import defaultdict
from enum import Enum
from subprocess import SubprocessError
from urllib.parse import urlparse
@ -701,12 +702,28 @@ class RgwClient(RestClient):
raise DashboardException(msg=str(e), component='rgw')
return result
@staticmethod
def _handle_rules(pairs):
result = defaultdict(list)
for key, value in pairs:
if key == 'Rule':
result['Rules'].append(value)
else:
result[key] = value
return result
@RestClient.api_get('/{bucket_name}?lifecycle')
def get_lifecycle(self, bucket_name, request=None):
# pylint: disable=unused-argument
try:
result = request() # type: ignore
result = {'LifecycleConfiguration': result}
decoded_request = request(raw_content=True).decode("utf-8") # type: ignore
result = {
'LifecycleConfiguration':
json.loads(
decoded_request,
object_pairs_hook=RgwClient._handle_rules
)
}
except RequestException as e:
if e.content:
content = json_str_to_object(e.content)
@ -758,15 +775,15 @@ class RgwClient(RestClient):
lifecycle = RgwClient.dict_to_xml(lifecycle)
try:
if lifecycle and '<LifecycleConfiguration>' not in str(lifecycle):
lifecycle = f'<LifecycleConfiguration>{lifecycle}</LifecycleConfiguration>'
lifecycle = f'<LifecycleConfiguration>\n{lifecycle}\n</LifecycleConfiguration>'
result = request(data=lifecycle) # type: ignore
except RequestException as e:
msg = ''
if e.content:
content = json_str_to_object(e.content)
if content.get("Code") == "MalformedXML":
msg = "Invalid Lifecycle document"
raise DashboardException(msg=msg, component='rgw')
raise DashboardException(msg=str(e), component='rgw')
raise DashboardException(msg=msg or str(e), component='rgw')
return result
@RestClient.api_delete('/{bucket_name}?lifecycle')