Better __repr__() for data model

This greatly enhances debugability of data model objects as repr() and
objects contain now all attributes. Most subclasses implement their
own __str__() so it shows only the most relevant attributes.

Change-Id: I2babd600516cfe98673627c68e97cb581ee54834
This commit is contained in:
Tom Weininger 2023-04-27 10:55:15 +02:00
parent 53200266b3
commit 93c5e17ba8
2 changed files with 76 additions and 1 deletions

View File

@ -76,6 +76,11 @@ class BaseDataModel():
def __ne__(self, other):
return not self.__eq__(other)
def __repr__(self):
attrs = ", ".join(
"{}={!r}".format(k, v) for k, v in sorted(self.__dict__.items()))
return f"{self.__class__.__name__}({attrs})"
@classmethod
def from_dict(cls, dict):
return cls(**dict)
@ -117,6 +122,11 @@ class LoadBalancer(BaseDataModel):
self.additional_vips = additional_vips
self.availability_zone = availability_zone
def __str__(self):
return (f"LoadBalancer(loadbalancer_id="
f"{self.loadbalancer_id!r}, name={self.name!r}, "
f"project_id={self.project_id!r})")
class Listener(BaseDataModel):
def __init__(self, admin_state_up=Unset, connection_limit=Unset,
@ -166,6 +176,11 @@ class Listener(BaseDataModel):
self.tls_ciphers = tls_ciphers
self.alpn_protocols = alpn_protocols
def __str__(self):
return (f"Listener(listener_id="
f"{self.listener_id!r}, name={self.name!r}, "
f"project_id={self.project_id!r})")
class Pool(BaseDataModel):
def __init__(self, admin_state_up=Unset, description=Unset,
@ -202,6 +217,11 @@ class Pool(BaseDataModel):
self.tls_ciphers = tls_ciphers
self.alpn_protocols = alpn_protocols
def __str__(self):
return (f"Pool(loadbalancer_id={self.loadbalancer_id!r}, "
f"name={self.name!r}, pool_id={self.pool_id!r}, "
f"project_id={self.project_id!r})")
class Member(BaseDataModel):
def __init__(self, address=Unset, admin_state_up=Unset, member_id=Unset,
@ -222,6 +242,12 @@ class Member(BaseDataModel):
self.backup = backup
self.project_id = project_id
def __str__(self):
return (f"Member(address={self.address!r}, "
f"member_id={self.member_id!r}, name={self.name!r}, "
f"pool_id={self.pool_id!r}, project_id={self.project_id!r}, "
f"subnet_id={self.subnet_id!r}, weight={self.weight!r})")
class HealthMonitor(BaseDataModel):
def __init__(self, admin_state_up=Unset, delay=Unset, expected_codes=Unset,
@ -246,6 +272,11 @@ class HealthMonitor(BaseDataModel):
self.domain_name = domain_name
self.project_id = project_id
def __str__(self):
return (f"HealthMonitor(healthmonitor_id="
f"{self.healthmonitor_id!r}, name={self.name!r}, "
f"project_id={self.project_id!r})")
class L7Policy(BaseDataModel):
def __init__(self, action=Unset, admin_state_up=Unset, description=Unset,
@ -268,12 +299,16 @@ class L7Policy(BaseDataModel):
self.redirect_http_code = redirect_http_code
self.project_id = project_id
def __str__(self):
return (f"L7Policy(l7policy_id="
f"{self.l7policy_id!r}, name={self.name!r}, "
f"project_id={self.project_id!r})")
class L7Rule(BaseDataModel):
def __init__(self, admin_state_up=Unset, compare_type=Unset, invert=Unset,
key=Unset, l7policy_id=Unset, l7rule_id=Unset, type=Unset,
value=Unset, project_id=Unset):
self.admin_state_up = admin_state_up
self.compare_type = compare_type
self.invert = invert
@ -284,6 +319,11 @@ class L7Rule(BaseDataModel):
self.value = value
self.project_id = project_id
def __str__(self):
return (f"L7Rule(l7policy_id={self.l7policy_id!r}, "
f"l7rule_id={self.l7rule_id!r}, "
f"project_id={self.project_id!r})")
class VIP(BaseDataModel):
def __init__(self, vip_address=Unset, vip_network_id=Unset,

View File

@ -488,3 +488,38 @@ class TestProviderDataModels(base.TestCase):
def test_unset_repr(self):
self.assertEqual('Unset', repr(data_models.Unset))
def test_str(self):
self.assertEqual(
f"HealthMonitor(healthmonitor_id="
f"{self.healthmonitor_id!r}, name='member', "
f"project_id={self.project_id!r})", str(self.ref_healthmonitor))
self.assertEqual(
f"L7Policy(l7policy_id={self.l7policy_id!r}, name='l7policy', "
f"project_id={self.project_id!r})", str(self.ref_l7policy))
self.assertEqual(
f"L7Rule(l7policy_id={self.l7policy_id!r}, "
f"l7rule_id={self.l7rule_id!r}, project_id={self.project_id!r})",
str(self.ref_l7rule))
self.assertEqual(
f"LoadBalancer(loadbalancer_id={self.loadbalancer_id!r}, "
f"name='favorite_lb', project_id={self.project_id!r})",
str(self.ref_lb))
self.assertEqual(
f"Member(address='192.0.2.10', member_id={self.member_id!r}, "
f"name='member', pool_id={self.pool_id!r}, "
f"project_id={self.project_id!r}, "
f"subnet_id={self.ref_member.subnet_id!r}, weight=1)",
str(self.ref_member))
self.assertEqual(
f"Pool(loadbalancer_id={self.loadbalancer_id!r}, "
f"name='pool', pool_id={self.pool_id!r}, "
f"project_id={self.project_id!r})",
str(self.ref_pool))
self.assertEqual(
f"VIP(vip_address='192.0.2.83', "
f"vip_network_id={self.vip_network_id!r}, "
f"vip_port_id={self.vip_port_id!r}, vip_qos_policy_id="
f"{self.vip_qos_policy_id!r}, "
f"vip_subnet_id={self.vip_subnet_id!r})",
str(self.ref_vip))