def update(self, session, id, **model_kwargs): with session.begin(subtransactions=True): l7rule_db = session.query(self.model_class).filter_by( id=id).first() if not l7rule_db: raise exceptions.NotFound( resource=data_models.L7Rule._name(), id=id) l7rule_dict = l7rule_db.to_data_model().to_dict() # Ignore values that are None for k, v in model_kwargs.items(): if v is not None: l7rule_dict.update({k: v}) # Clear out the 'key' attribute for rule types that don't use it. if ('type' in l7rule_dict.keys() and l7rule_dict['type'] in (constants.L7RULE_TYPE_HOST_NAME, constants.L7RULE_TYPE_PATH, constants.L7RULE_TYPE_FILE_TYPE)): l7rule_dict['key'] = None model_kwargs.update({'key': None}) validate.l7rule_data(self.model_class(**l7rule_dict)) l7rule_db.update(model_kwargs) l7rule_db = self.get(session, id=id) return l7rule_db
def put(self, id, l7rule): """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) try: validate.l7rule_data(new_l7rule) except Exception as e: raise exceptions.L7RuleValidation(error=e) self._test_lb_and_listener_statuses(context.session) try: LOG.info(_LI("Sending Update of L7Rule %s to handler"), id) self.handler.update(db_l7rule, l7rule) except Exception: with excutils.save_and_reraise_exception(reraise=False): self.repositories.listener.update( context.session, self.listener_id, operating_status=constants.ERROR) db_l7rule = self._get_db_l7rule(context.session, id) return self._convert_db_to_type(db_l7rule, l7rule_types.L7RuleResponse)
def put(self, id, l7rule_): """Updates a l7rule.""" l7rule = l7rule_.rule context = pecan.request.context.get('octavia_context') db_l7rule = self._get_db_l7rule(context.session, id) 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: raise exceptions.L7RuleValidation(error=e) self._test_lb_listener_policy_statuses(context.session) self.repositories.l7rule.update( context.session, db_l7rule.id, provisioning_status=constants.PENDING_UPDATE) try: LOG.info(_LI("Sending Update of L7Rule %s to handler"), id) self.handler.update(db_l7rule, l7rule) except Exception: with excutils.save_and_reraise_exception( reraise=False), db_api.get_lock_session() as lock_session: self._reset_lb_listener_policy_statuses(lock_session) # L7Rule now goes to ERROR self.repositories.l7rule.update( lock_session, db_l7rule.id, provisioning_status=constants.ERROR) db_l7rule = self._get_db_l7rule(context.session, id) result = self._convert_db_to_type(db_l7rule, l7rule_types.L7RuleResponse) return l7rule_types.L7RuleRootResponse(rule=result)
def put(self, id, l7rule): """Updates a l7rule.""" context = pecan.request.context.get('octavia_context') db_l7rule = self._get_db_l7rule(context.session, id) 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: raise exceptions.L7RuleValidation(error=e) self._test_lb_and_listener_statuses(context.session) self.repositories.l7rule.update( context.session, id, provisioning_status=constants.PENDING_UPDATE) try: LOG.info("Sending Update of L7Rule %s to handler", id) self.handler.update(db_l7rule, l7rule) except Exception: with excutils.save_and_reraise_exception(reraise=False): self.repositories.listener.update( context.session, self.listener_id, operating_status=constants.ERROR) db_l7rule = self._get_db_l7rule(context.session, id) return self._convert_db_to_type(db_l7rule, l7rule_types.L7RuleResponse)
def post(self, rule_): """Creates a l7rule on an l7policy.""" l7rule = rule_.rule context = pecan_request.context.get('octavia_context') db_l7policy = self._get_db_l7policy(context.session, self.l7policy_id, show_deleted=False) load_balancer_id, listener_id = self._get_listener_and_loadbalancer_id( db_l7policy) l7rule.project_id, provider = self._get_lb_project_id_provider( context.session, load_balancer_id) self._auth_validate_action(context, l7rule.project_id, constants.RBAC_POST) try: validate.l7rule_data(l7rule) except Exception as e: raise exceptions.L7RuleValidation(error=e) self._check_l7policy_max_rules(context.session) # Load the driver early as it also provides validation driver = driver_factory.get_driver(provider) lock_session = db_api.get_session(autocommit=False) try: if self.repositories.check_quota_met( context.session, lock_session, data_models.L7Rule, l7rule.project_id): raise exceptions.QuotaException( resource=data_models.L7Rule._name()) l7rule_dict = db_prepare.create_l7rule( l7rule.to_dict(render_unsets=True), self.l7policy_id) self._test_lb_listener_policy_statuses(context.session) db_l7rule = self._validate_create_l7rule(lock_session, l7rule_dict) # Prepare the data for the driver data model provider_l7rule = ( driver_utils.db_l7rule_to_provider_l7rule(db_l7rule)) # Dispatch to the driver LOG.info("Sending create L7 Rule %s to provider %s", db_l7rule.id, driver.name) driver_utils.call_provider( driver.name, driver.l7rule_create, provider_l7rule) lock_session.commit() except Exception: with excutils.save_and_reraise_exception(): lock_session.rollback() db_l7rule = self._get_db_l7rule(context.session, db_l7rule.id) result = self._convert_db_to_type(db_l7rule, l7rule_types.L7RuleResponse) return l7rule_types.L7RuleRootResponse(rule=result)
def update(self, session, id, **model_kwargs): with session.begin(subtransactions=True): l7rule_db = session.query( self.model_class).filter_by(id=id).first() if not l7rule_db: raise exceptions.NotFound(resource=data_models.L7Rule._name(), id=id) l7rule_dict = l7rule_db.to_data_model().to_dict() # Ignore values that are None for k, v in model_kwargs.items(): if v is not None: l7rule_dict.update({k: v}) # Clear out the 'key' attribute for rule types that don't use it. if ('type' in l7rule_dict.keys() and l7rule_dict['type'] in (constants.L7RULE_TYPE_HOST_NAME, constants.L7RULE_TYPE_PATH, constants.L7RULE_TYPE_FILE_TYPE)): l7rule_dict['key'] = None model_kwargs.update({'key': None}) validate.l7rule_data(self.model_class(**l7rule_dict)) l7rule_db.update(model_kwargs) l7rule_db = self.get(session, id=id) return l7rule_db
def create_l7policy(l7policy_dict, lb_id, listener_id): l7policy_dict = validate.sanitize_l7policy_api_args(l7policy_dict, create=True) l7policy_dict['provisioning_status'] = constants.PENDING_CREATE l7policy_dict['operating_status'] = constants.OFFLINE if not l7policy_dict.get('id'): l7policy_dict['id'] = uuidutils.generate_uuid() l7policy_dict['listener_id'] = listener_id if l7policy_dict.get('redirect_pool'): pool_dict = l7policy_dict.pop('redirect_pool') prepped_pool = create_pool(pool_dict, lb_id) l7policy_dict['redirect_pool'] = prepped_pool l7policy_dict['redirect_pool_id'] = prepped_pool['id'] rules = l7policy_dict.pop('rules', None) if rules: l7policy_dict['l7rules'] = rules if l7policy_dict.get('l7rules'): if (len(l7policy_dict.get('l7rules')) > constants.MAX_L7RULES_PER_L7POLICY): raise exceptions.TooManyL7RulesOnL7Policy(id=l7policy_dict['id']) prepped_l7rules = [] for l7rule_dict in l7policy_dict.get('l7rules'): try: validate.l7rule_data(l7rule.L7RulePOST(**l7rule_dict)) except Exception as e: raise exceptions.L7RuleValidation(error=e) prepped_l7rule = create_l7rule(l7rule_dict, l7policy_dict['id']) prepped_l7rules.append(prepped_l7rule) return l7policy_dict
def post(self, rule_): """Creates a l7rule on an l7policy.""" l7rule = rule_.rule try: validate.l7rule_data(l7rule) except Exception as e: raise exceptions.L7RuleValidation(error=e) context = pecan.request.context.get('octavia_context') l7rule.project_id = self._get_l7policy_project_id(context.session, self.l7policy_id) self._check_l7policy_max_rules(context.session) self._auth_validate_action(context, l7rule.project_id, constants.RBAC_POST) lock_session = db_api.get_session(autocommit=False) l7rule_dict = db_prepare.create_l7rule( l7rule.to_dict(render_unsets=True), self.l7policy_id) try: self._test_lb_listener_policy_statuses(context.session) db_l7rule = self._validate_create_l7rule(lock_session, l7rule_dict) lock_session.commit() except Exception: with excutils.save_and_reraise_exception(): lock_session.rollback() return self._send_l7rule_to_handler(context.session, db_l7rule)
def put(self, id, l7rule_): """Updates a l7rule.""" l7rule = l7rule_.rule context = pecan.request.context.get('octavia_context') db_l7rule = self._get_db_l7rule(context.session, id, show_deleted=False) new_l7rule = db_l7rule.to_dict() new_l7rule.update(l7rule.to_dict()) new_l7rule = data_models.L7Rule.from_dict(new_l7rule) db_l7policy = self._get_db_l7policy(context.session, self.l7policy_id, show_deleted=False) load_balancer_id, listener_id = self._get_listener_and_loadbalancer_id( db_l7policy) project_id, provider = self._get_lb_project_id_provider( context.session, load_balancer_id) self._auth_validate_action(context, project_id, constants.RBAC_PUT) try: validate.l7rule_data(new_l7rule) except Exception as e: raise exceptions.L7RuleValidation(error=e) # Load the driver early as it also provides validation driver = driver_factory.get_driver(provider) with db_api.get_lock_session() as lock_session: self._test_lb_listener_policy_statuses(lock_session) # Prepare the data for the driver data model l7rule_dict = l7rule.to_dict(render_unsets=False) l7rule_dict['id'] = id provider_l7rule_dict = ( driver_utils.l7rule_dict_to_provider_dict(l7rule_dict)) # Dispatch to the driver LOG.info("Sending update L7 Rule %s to provider %s", id, driver.name) driver_utils.call_provider( driver.name, driver.l7rule_update, driver_dm.L7Rule.from_dict(provider_l7rule_dict)) # Update the database to reflect what the driver just accepted l7rule.provisioning_status = constants.PENDING_UPDATE db_l7rule_dict = l7rule.to_dict(render_unsets=False) self.repositories.l7rule.update(lock_session, id, **db_l7rule_dict) # Force SQL alchemy to query the DB, otherwise we get inconsistent # results context.session.expire_all() db_l7rule = self._get_db_l7rule(context.session, id) result = self._convert_db_to_type(db_l7rule, l7rule_types.L7RuleResponse) return l7rule_types.L7RuleRootResponse(rule=result)
def _graph_create(self, lock_session, rule_dict): try: validate.l7rule_data(l7rule_types.L7RulePOST(**rule_dict)) except Exception as e: raise exceptions.L7RuleValidation(error=e) rule_dict = db_prepare.create_l7rule(rule_dict, self.l7policy_id) db_rule = self._validate_create_l7rule(lock_session, rule_dict) return db_rule
def create(self, session, **model_kwargs): with session.begin(subtransactions=True): if not model_kwargs.get('id'): model_kwargs.update(id=uuidutils.generate_uuid()) l7rule = self.model_class(**model_kwargs) validate.l7rule_data(l7rule) session.add(l7rule) l7rule_db = self.get(session, id=l7rule.id) return l7rule_db
def create_l7policy(l7policy_dict, lb_id, listener_id): if not l7policy_dict.get('id'): l7policy_dict['id'] = uuidutils.generate_uuid() l7policy_dict['listener_id'] = listener_id if l7policy_dict.get('redirect_pool'): pool_dict = l7policy_dict.pop('redirect_pool') prepped_pool = create_pool(pool_dict, lb_id) l7policy_dict['redirect_pool'] = prepped_pool l7policy_dict['redirect_pool_id'] = prepped_pool['id'] if l7policy_dict.get('l7rules'): prepped_l7rules = [] for l7rule_dict in l7policy_dict.get('l7rules'): try: validate.l7rule_data(l7rule.L7RulePOST(**l7rule_dict)) except Exception as e: raise exceptions.L7RuleValidation(error=e) prepped_l7rule = create_l7rule(l7rule_dict, l7policy_dict['id']) prepped_l7rules.append(prepped_l7rule) return l7policy_dict
def post(self, l7rule): """Creates a l7rule on an l7policy.""" try: validate.l7rule_data(l7rule) except Exception as e: 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(render_unsets=True), self.l7policy_id) self._test_lb_and_listener_statuses(context.session) try: db_l7rule = self.repositories.l7rule.create( context.session, **l7rule_dict) except oslo_exc.DBDuplicateEntry as de: # Setting LB and Listener back to active because this is just a # validation failure self.repositories.load_balancer.update( context.session, self.load_balancer_id, provisioning_status=constants.ACTIVE) self.repositories.listener.update( context.session, self.listener_id, provisioning_status=constants.ACTIVE) if ['id'] == de.columns: raise exceptions.IDAlreadyExists() try: LOG.info(_LI("Sending Creation of L7Rule %s to handler"), db_l7rule.id) self.handler.create(db_l7rule) except Exception: with excutils.save_and_reraise_exception(reraise=False): self.repositories.listener.update( context.session, self.listener_id, operating_status=constants.ERROR) db_l7rule = self._get_db_l7rule(context.session, db_l7rule.id) return self._convert_db_to_type(db_l7rule, l7rule_types.L7RuleResponse)
def post(self, l7rule): """Creates a l7rule on an l7policy.""" try: validate.l7rule_data(l7rule) except Exception as e: 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(render_unsets=True), self.l7policy_id) self._test_lb_and_listener_statuses(context.session) try: db_l7rule = self.repositories.l7rule.create(context.session, **l7rule_dict) except oslo_exc.DBDuplicateEntry as de: # Setting LB and Listener back to active because this is just a # validation failure self.repositories.load_balancer.update( context.session, self.load_balancer_id, provisioning_status=constants.ACTIVE) self.repositories.listener.update( context.session, self.listener_id, provisioning_status=constants.ACTIVE) if ['id'] == de.columns: raise exceptions.IDAlreadyExists() try: LOG.info(_LI("Sending Creation of L7Rule %s to handler"), db_l7rule.id) self.handler.create(db_l7rule) except Exception: with excutils.save_and_reraise_exception(reraise=False): self.repositories.listener.update( context.session, self.listener_id, operating_status=constants.ERROR) db_l7rule = self._get_db_l7rule(context.session, db_l7rule.id) return self._convert_db_to_type(db_l7rule, l7rule_types.L7RuleResponse)
def create_l7policy(l7policy_dict, lb_id, listener_id): l7policy_dict = validate.sanitize_l7policy_api_args(l7policy_dict, create=True) if not l7policy_dict.get('id'): l7policy_dict['id'] = uuidutils.generate_uuid() l7policy_dict['listener_id'] = listener_id if l7policy_dict.get('redirect_pool'): pool_dict = l7policy_dict.pop('redirect_pool') prepped_pool = create_pool(pool_dict, lb_id) l7policy_dict['redirect_pool'] = prepped_pool l7policy_dict['redirect_pool_id'] = prepped_pool['id'] if l7policy_dict.get('l7rules'): if (len(l7policy_dict.get('l7rules')) > constants.MAX_L7RULES_PER_L7POLICY): raise exceptions.TooManyL7RulesOnL7Policy(id=l7policy_dict['id']) prepped_l7rules = [] for l7rule_dict in l7policy_dict.get('l7rules'): try: validate.l7rule_data(l7rule.L7RulePOST(**l7rule_dict)) except Exception as e: raise exceptions.L7RuleValidation(error=e) prepped_l7rule = create_l7rule(l7rule_dict, l7policy_dict['id']) prepped_l7rules.append(prepped_l7rule) return l7policy_dict