Skip to content

Commit 567c9e7

Browse files
committed
mgr/dashboard: add xlmtodict import and fix lifecycle get request
Fixes: https://tracker.ceph.com/issues/70128 Signed-off-by: Pedro Gonzalez Gomez <[email protected]>
1 parent 812add8 commit 567c9e7

File tree

7 files changed

+38
-16
lines changed

7 files changed

+38
-16
lines changed

ceph.spec.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ Requires: ceph-prometheus-alerts = %{_epoch_prefix}%{version}-%{release}
695695
Requires: python%{python3_pkgversion}-grpcio
696696
Requires: python%{python3_pkgversion}-grpcio-tools
697697
Requires: python%{python3_pkgversion}-jmespath
698+
Requires: python%{python3_pkgversion}-xmltodict
698699
%endif
699700
%if 0%{?fedora} || 0%{?rhel} || 0%{?openEuler}
700701
Requires: python%{python3_pkgversion}-cherrypy

debian/control

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Build-Depends: automake,
100100
python3-dateutil <pkg.ceph.check>,
101101
python3-grpcio <pkg.ceph.check>,
102102
python3-jmespath (>=0.10) <pkg.ceph.check>,
103+
python3-xmltodict <pkg.ceph.check>,
103104
python3-openssl <pkg.ceph.check>,
104105
python3-prettytable <pkg.ceph.check>,
105106
python3-requests <pkg.ceph.check>,

src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-bucket-details/rgw-bucket-details.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,13 @@
152152
<button type="button"
153153
class="btn btn-light"
154154
[ngClass]="{'active': lifecycleFormat === 'json'}"
155-
(click)="lifecycleFormat = 'json'">
155+
(click)="updateLifecycleFormatTo('json')">
156156
JSON
157157
</button>
158158
<button type="button"
159159
class="btn btn-light"
160160
[ngClass]="{'active': lifecycleFormat === 'xml'}"
161-
(click)="lifecycleFormat = 'xml'">
161+
(click)="updateLifecycleFormatTo('xml')">
162162
XML
163163
</button>
164164
</div>

src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-bucket-details/rgw-bucket-details.component.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Input, OnChanges } from '@angular/core';
1+
import { ChangeDetectorRef, Component, Input, OnChanges } from '@angular/core';
22

33
import { RgwBucketService } from '~/app/shared/api/rgw-bucket.service';
44

@@ -24,7 +24,7 @@ export class RgwBucketDetailsComponent implements OnChanges {
2424
replicationStatus = $localize`Disabled`;
2525
bucketRateLimit: RgwRateLimitConfig;
2626

27-
constructor(private rgwBucketService: RgwBucketService) {}
27+
constructor(private rgwBucketService: RgwBucketService, private cd: ChangeDetectorRef) {}
2828

2929
ngOnChanges() {
3030
this.updateBucketDetails(this.extraxtDetailsfromResponse.bind(this));
@@ -102,4 +102,9 @@ export class RgwBucketDetailsComponent implements OnChanges {
102102
);
103103
}
104104
}
105+
106+
updateLifecycleFormatTo(format: 'json' | 'xml'): void {
107+
this.lifecycleFormat = format;
108+
this.cd.detectChanges();
109+
}
105110
}

src/pybind/mgr/dashboard/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ grpcio==1.46.5
1515
grpcio-tools==1.46.5
1616
jmespath
1717
lxml==4.8.0 # to fix https://github.com/xmlsec/python-xmlsec/issues/320
18+
xmltodict

src/pybind/mgr/dashboard/services/rgw_client.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
from urllib.parse import urlparse
1717

1818
import requests
19+
20+
try:
21+
import xmltodict
22+
except ModuleNotFoundError:
23+
logging.error("Module 'xmltodict' is not installed.")
24+
1925
from mgr_util import build_url, name_to_config_section
2026

2127
from .. import mgr
@@ -715,23 +721,30 @@ def _handle_rules(pairs):
715721
@RestClient.api_get('/{bucket_name}?lifecycle')
716722
def get_lifecycle(self, bucket_name, request=None):
717723
# pylint: disable=unused-argument
724+
725+
# xmltodict parser will prepend namespace to json keys as {ns0:key}
726+
def remove_namespace(xml: str):
727+
"""Remove namespace in given xml string."""
728+
root = ET.fromstring(xml)
729+
for elem in root.iter():
730+
tag_elements = elem.tag.split("}") # tag: {ns}tagname
731+
# Removing namespaces and attributes
732+
elem.tag = tag_elements[1]
733+
elem.attrib.clear()
734+
735+
return ET.tostring(root)
736+
718737
try:
719-
decoded_request = request(raw_content=True).decode("utf-8") # type: ignore
720-
result = {
721-
'LifecycleConfiguration':
722-
json.loads(
723-
decoded_request,
724-
object_pairs_hook=RgwClient._handle_rules
725-
)
726-
}
738+
result = request(
739+
raw_content=True, headers={'Accept': 'text/xml'}).decode() # type: ignore
740+
return xmltodict.parse(remove_namespace(result), process_namespaces=False)
727741
except RequestException as e:
728742
if e.content:
729-
content = json_str_to_object(e.content)
730-
if content.get(
731-
'Code') == 'NoSuchLifecycleConfiguration':
743+
root = ET.fromstring(e.content)
744+
code = root.find('Code')
745+
if code is not None and code.text == 'NoSuchLifecycleConfiguration':
732746
return None
733747
raise DashboardException(msg=str(e), component='rgw')
734-
return result
735748

736749
@staticmethod
737750
def dict_to_xml(data):

src/pybind/mgr/tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ deps =
8282
types-PyYAML
8383
types-jwt
8484
types-jmespath
85+
types-xmltodict
8586
commands =
8687
mypy --config-file=../../mypy.ini \
8788
-m alerts \

0 commit comments

Comments
 (0)