diff --git a/octavia_lib/api/drivers/data_models.py b/octavia_lib/api/drivers/data_models.py index f00f0a5..5386664 100644 --- a/octavia_lib/api/drivers/data_models.py +++ b/octavia_lib/api/drivers/data_models.py @@ -77,6 +77,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) @@ -118,6 +123,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, @@ -172,6 +182,11 @@ class Listener(BaseDataModel): hsts_include_subdomains) self.hsts_preload: tp.Union[bool, UnsetType] = hsts_preload + 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, @@ -208,6 +223,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, @@ -228,6 +248,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, @@ -252,6 +278,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, @@ -274,12 +305,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 @@ -290,6 +325,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, diff --git a/octavia_lib/tests/unit/api/drivers/test_data_models.py b/octavia_lib/tests/unit/api/drivers/test_data_models.py index 24e2ce2..1727663 100644 --- a/octavia_lib/tests/unit/api/drivers/test_data_models.py +++ b/octavia_lib/tests/unit/api/drivers/test_data_models.py @@ -497,3 +497,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))