def create_l7policy(self, context, l7policy): if (l7policy.get('redirect_pool_id') and l7policy['redirect_pool_id'] == n_const.ATTR_NOT_SPECIFIED): l7policy['redirect_pool_id'] = None if not l7policy.get('position'): l7policy['position'] = 2147483647 self._validate_l7policy_data(context, l7policy) with context.session.begin(subtransactions=True): listener_id = l7policy.get('listener_id') listener_db = self._get_resource(context, models.Listener, listener_id) if not listener_db: raise loadbalancerv2.EntityNotFound(name=models.Listener.NAME, id=listener_id) self._load_id(context, l7policy) l7policy['provisioning_status'] = constants.PENDING_CREATE l7policy_db = models.L7Policy(**l7policy) # MySQL int fields are by default 32-bit whereas handy system # constants like sys.maxsize are 64-bit on most platforms today. # Hence the reason this is 2147483647 (2^31 - 1) instead of an # elsewhere-defined constant. if l7policy['position'] == 2147483647: listener_db.l7_policies.append(l7policy_db) else: listener_db.l7_policies.insert(l7policy['position'] - 1, l7policy_db) listener_db.l7_policies.reorder() return data_models.L7Policy.from_sqlalchemy_model(l7policy_db)
def update_l7policy(self, context, id, l7policy): with context.session.begin(subtransactions=True): l7policy_db = self._get_resource(context, models.L7Policy, id) if 'action' in l7policy: l7policy['listener_id'] = l7policy_db.listener_id self._validate_l7policy_data(context, l7policy) if ('position' not in l7policy or l7policy['position'] == 2147483647 or l7policy_db.position == l7policy['position']): l7policy_db.update(l7policy) else: listener_id = l7policy_db.listener_id listener_db = self._get_resource( context, models.Listener, listener_id) l7policy_db = listener_db.l7_policies.pop( l7policy_db.position - 1) # NOTE(ihrachys) create a new policy model because the one just # popped off the list became expired thanks to neutron db code # that automatically expires relationships when foreign keys # become obsolete l7policy_db = models.L7Policy(**dict(l7policy_db)) l7policy_db.update(l7policy) listener_db.l7_policies.insert(l7policy['position'] - 1, l7policy_db) listener_db.l7_policies.reorder() context.session.refresh(l7policy_db) return data_models.L7Policy.from_sqlalchemy_model(l7policy_db)