From 18abaeec4ef3af054d50e20e7084e5b944786074 Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Tue, 17 Mar 2020 15:23:14 +0100 Subject: [PATCH] python-common: reduce output of ServiceSpec.to_json() Signed-off-by: Sebastian Wagner --- .../ceph/deployment/service_spec.py | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/python-common/ceph/deployment/service_spec.py b/src/python-common/ceph/deployment/service_spec.py index 08edc09170d..e1f6331c926 100644 --- a/src/python-common/ceph/deployment/service_spec.py +++ b/src/python-common/ceph/deployment/service_spec.py @@ -198,12 +198,16 @@ class PlacementSpec(object): return _cls def to_json(self): - return { - 'label': self.label, - 'hosts': [host.to_json() for host in self.hosts] if self.hosts else [], - 'count': self.count, - 'host_pattern': self.host_pattern, - } + r = {} + if self.label: + r['label'] = self.label + if self.hosts: + r['hosts'] = [host.to_json() for host in self.hosts] + if self.count: + r['count'] = self.count + if self.host_pattern: + r['host_pattern'] = self.host_pattern + return r def validate(self): if self.hosts and self.label: @@ -378,9 +382,12 @@ class ServiceSpec(object): def to_json(self): # type: () -> Dict[str, Any] - c = self.__dict__.copy() - if self.placement: - c['placement'] = self.placement.to_json() + c = {} + for key, val in self.__dict__.items(): + if hasattr(val, 'to_json'): + val = val.to_json() + if val: + c[key] = val return c def validate(self): -- 2.39.5