From 89335a3851748e03dbab9311175d4dd8bc0cf573 Mon Sep 17 00:00:00 2001 From: Brandon Logan Date: Thu, 10 Mar 2016 00:17:30 -0600 Subject: [PATCH] Change render_unsets default to False The API WSME Types all inherit a to_dict method that accepts as a parameter render_unsets. It is currently set to True by default but should be False for consistency and also because it just makes better sense. This has caused some unnecessary workarounds and work before it was known this existed. Closes-Bug: 1555493 Change-Id: Idd21a003c0b1e3092f979013f8c19d75f17d0249 --- octavia/api/v1/controllers/health_monitor.py | 2 +- octavia/api/v1/controllers/l7policy.py | 2 +- octavia/api/v1/controllers/l7rule.py | 12 +++++------- octavia/api/v1/controllers/listener.py | 4 ++-- octavia/api/v1/controllers/load_balancer.py | 6 ++++-- octavia/api/v1/controllers/member.py | 3 ++- octavia/api/v1/controllers/pool.py | 2 +- octavia/api/v1/types/base.py | 7 ++++--- 8 files changed, 20 insertions(+), 18 deletions(-) diff --git a/octavia/api/v1/controllers/health_monitor.py b/octavia/api/v1/controllers/health_monitor.py index 4646702e08..caf402b71c 100644 --- a/octavia/api/v1/controllers/health_monitor.py +++ b/octavia/api/v1/controllers/health_monitor.py @@ -104,7 +104,7 @@ class HealthMonitorController(base.BaseController): except exceptions.NotFound: pass hm_dict = db_prepare.create_health_monitor( - health_monitor.to_dict(), self.pool_id) + health_monitor.to_dict(render_unsets=True), self.pool_id) self._test_lb_and_listener_statuses(context.session) try: diff --git a/octavia/api/v1/controllers/l7policy.py b/octavia/api/v1/controllers/l7policy.py index b41af9130c..da8ea75c02 100644 --- a/octavia/api/v1/controllers/l7policy.py +++ b/octavia/api/v1/controllers/l7policy.py @@ -78,7 +78,7 @@ class L7PolicyController(base.BaseController): """Creates a l7policy on a listener.""" context = pecan.request.context.get('octavia_context') l7policy_dict = validate.sanitize_l7policy_api_args( - l7policy.to_dict(), create=True) + l7policy.to_dict(render_unsets=True), create=True) # Make sure any pool specified by redirect_pool_id exists if 'redirect_pool_id' in l7policy_dict.keys(): self._get_db_pool( diff --git a/octavia/api/v1/controllers/l7rule.py b/octavia/api/v1/controllers/l7rule.py index 867cd78d30..601fd547df 100644 --- a/octavia/api/v1/controllers/l7rule.py +++ b/octavia/api/v1/controllers/l7rule.py @@ -89,8 +89,8 @@ class L7RuleController(base.BaseController): raise exceptions.L7RuleValidation(error=e) context = pecan.request.context.get('octavia_context') self._check_l7policy_max_rules(context.session) - l7rule_dict = db_prepare.create_l7rule(l7rule.to_dict(), - self.l7policy_id) + l7rule_dict = db_prepare.create_l7rule( + l7rule.to_dict(render_unsets=True), self.l7policy_id) self._test_lb_and_listener_statuses(context.session) try: @@ -127,11 +127,9 @@ class L7RuleController(base.BaseController): """Updates a l7rule.""" context = pecan.request.context.get('octavia_context') db_l7rule = self._get_db_l7rule(context.session, id) - new_l7rule_dict = db_l7rule.to_dict() - for k, v in l7rule.to_dict().items(): - if v is not None: - new_l7rule_dict.update({k: v}) - new_l7rule = data_models.L7Rule(**new_l7rule_dict) + new_l7rule = db_l7rule.to_dict() + new_l7rule.update(l7rule.to_dict()) + new_l7rule = data_models.L7Rule.from_dict(new_l7rule) try: validate.l7rule_data(new_l7rule) except Exception as e: diff --git a/octavia/api/v1/controllers/listener.py b/octavia/api/v1/controllers/listener.py index 2304f11e69..c673a2f63a 100644 --- a/octavia/api/v1/controllers/listener.py +++ b/octavia/api/v1/controllers/listener.py @@ -160,7 +160,7 @@ class ListenersController(base.BaseController): self._secure_data(listener) context = pecan.request.context.get('octavia_context') listener_dict = db_prepare.create_listener( - listener.to_dict(), self.load_balancer_id) + listener.to_dict(render_unsets=True), self.load_balancer_id) if listener_dict['default_pool_id']: self._validate_pool(context.session, listener_dict['default_pool_id']) @@ -181,7 +181,7 @@ class ListenersController(base.BaseController): context = pecan.request.context.get('octavia_context') db_listener = self._get_db_listener(context.session, id) listener_dict = listener.to_dict() - if listener_dict['default_pool_id']: + if listener_dict.get('default_pool_id'): self._validate_pool(context.session, listener_dict['default_pool_id']) self._test_lb_and_listener_statuses(context.session, id=id) diff --git a/octavia/api/v1/controllers/load_balancer.py b/octavia/api/v1/controllers/load_balancer.py index dfb5982853..52af08fa13 100644 --- a/octavia/api/v1/controllers/load_balancer.py +++ b/octavia/api/v1/controllers/load_balancer.py @@ -74,7 +74,7 @@ class LoadBalancersController(base.BaseController): def _create_load_balancer_graph(self, context, load_balancer): prepped_lb = db_prepare.create_load_balancer_tree( - load_balancer.to_dict()) + load_balancer.to_dict(render_unsets=True)) try: db_lb = self.repositories.create_load_balancer_tree( context.session, prepped_lb) @@ -100,7 +100,9 @@ class LoadBalancersController(base.BaseController): context = pecan.request.context.get('octavia_context') if load_balancer.listeners: return self._create_load_balancer_graph(context, load_balancer) - lb_dict = db_prepare.create_load_balancer(load_balancer.to_dict()) + lb_dict = db_prepare.create_load_balancer(load_balancer.to_dict( + render_unsets=True + )) vip_dict = lb_dict.pop('vip', {}) try: db_lb = self.repositories.create_load_balancer_and_vip( diff --git a/octavia/api/v1/controllers/member.py b/octavia/api/v1/controllers/member.py index 0f46c2dccf..df4ffbbcf8 100644 --- a/octavia/api/v1/controllers/member.py +++ b/octavia/api/v1/controllers/member.py @@ -91,7 +91,8 @@ class MembersController(base.BaseController): def post(self, member): """Creates a pool member on a pool.""" context = pecan.request.context.get('octavia_context') - member_dict = db_prepare.create_member(member.to_dict(), self.pool_id) + member_dict = db_prepare.create_member(member.to_dict( + render_unsets=True), self.pool_id) self._test_lb_and_listener_statuses(context.session) try: diff --git a/octavia/api/v1/controllers/pool.py b/octavia/api/v1/controllers/pool.py index 6f5d17e72e..8c1a34a7a2 100644 --- a/octavia/api/v1/controllers/pool.py +++ b/octavia/api/v1/controllers/pool.py @@ -140,7 +140,7 @@ class PoolsController(base.BaseController): # For some API requests the listener_id will be passed in the # pool_dict: context = pecan.request.context.get('octavia_context') - pool_dict = db_prepare.create_pool(pool.to_dict()) + pool_dict = db_prepare.create_pool(pool.to_dict(render_unsets=True)) if 'listener_id' in pool_dict: if pool_dict['listener_id'] is not None: self.listener_id = pool_dict.pop('listener_id') diff --git a/octavia/api/v1/types/base.py b/octavia/api/v1/types/base.py index ca112a6553..14272963d5 100644 --- a/octavia/api/v1/types/base.py +++ b/octavia/api/v1/types/base.py @@ -65,7 +65,7 @@ class BaseType(wtypes.Base): """ return cls(**data_model.to_dict()) - def to_dict(self, render_unsets=True): + def to_dict(self, render_unsets=False): """Converts Octavia WSME type to dictionary. :param render_unsets: If True, will convert items that are WSME Unset @@ -79,9 +79,10 @@ class BaseType(wtypes.Base): if value and callable(value): continue if value and isinstance(value, BaseType): - value = value.to_dict() + value = value.to_dict(render_unsets=render_unsets) if value and isinstance(value, list): - value = [val.to_dict() if isinstance(val, BaseType) else val + value = [val.to_dict(render_unsets=render_unsets) + if isinstance(val, BaseType) else val for val in value] if isinstance(value, wtypes.UnsetType): if render_unsets: