def get_policy_by_name(name, session=None, deleted=False): session = session or db.get_session() try: return (session.query(Policy).filter(Policy.name == name).filter( Policy.deleted == is_soft_deleted(name, deleted)).one()) except db_exc.NoResultFound: pass
def add_policy(id_, name, abbreviation, description, owner, kind, deleted=False, session=None): if session: # IMPORTANT: if session provided, do not interrupt existing transaction # with BEGIN which can drop db locks and change desired transaction # boundaries for proper commit and rollback try: policy = Policy(id_, name, abbreviation, description, owner, kind, deleted) session.add(policy) return policy except oslo_db_exc.DBDuplicateEntry: raise KeyError("Policy with name %s already exists" % name) # else session = db.get_session() try: with session.begin(subtransactions=True): policy = Policy(id_, name, abbreviation, description, owner, kind, deleted) session.add(policy) return policy except oslo_db_exc.DBDuplicateEntry: raise KeyError("Policy with name %s already exists" % name)
def get_datasource_by_name(name, session=None): session = session or db.get_session() try: return _decrypt_secret_config_fields( session.query(Datasource).filter(Datasource.name == name).one()) except db_exc.NoResultFound: pass
def add_policy_rule(id, policy_name, rule, comment, deleted=False, rule_name="", session=None): if session: # IMPORTANT: if session provided, do not interrupt existing transaction # with BEGIN which can drop db locks and change desired transaction # boundaries for proper commit and rollback policy_rule = PolicyRule(id, policy_name, rule, comment, deleted, rule_name=rule_name) session.add(policy_rule) return policy_rule # else session = db.get_session() with session.begin(subtransactions=True): policy_rule = PolicyRule(id, policy_name, rule, comment, deleted, rule_name=rule_name) session.add(policy_rule) return policy_rule
def store_ds_table_data(ds_id, tablename, tabledata, session=None): session = session or db.get_session() tabledata = _json_encode_table_data(tabledata) with session.begin(subtransactions=True): new_row = session.merge( DSTableData(ds_id=ds_id, tablename=tablename, tabledata=tabledata)) return new_row
def policy_name(name_or_id, session=None): session = session or db.get_session() try: ans = (session.query(Policy).filter(Policy.deleted == '').filter( Policy.id == name_or_id).one()) except db_exc.NoResultFound: return name_or_id return ans.name
def delete_ds_table_data(ds_id, tablename=None, session=None): session = session or db.get_session() if tablename is None: return session.query(DSTableData).filter( DSTableData.ds_id == ds_id).delete() else: return session.query(DSTableData).filter( DSTableData.ds_id == ds_id, DSTableData.tablename == tablename).delete()
def get_policy_rule(id, policy_name, session=None, deleted=False): session = session or db.get_session() rule_query = (session.query(PolicyRule).filter(PolicyRule.id == id).filter( PolicyRule.deleted == is_soft_deleted(id, deleted))) if policy_name: rule_query = (rule_query.filter(PolicyRule.policy_name == policy_name)) try: return rule_query.one() except db_exc.NoResultFound: pass
def get_policy_rules(policy_name=None, session=None, deleted=False): session = session or db.get_session() rule_query = session.query(PolicyRule) if not deleted: rule_query = rule_query.filter(PolicyRule.deleted == '') else: rule_query = rule_query.filter(PolicyRule.deleted != '') if policy_name: rule_query = rule_query.filter(PolicyRule.policy_name == policy_name) return rule_query.all()
def delete_policy(id_, session=None): session = session or db.get_session() with session.begin(subtransactions=True): # delete all rules for that policy from database policy = get_policy_by_id(id_, session=session) for rule in get_policy_rules(policy.name, session=session): delete_policy_rule(rule.id, session=session) policy_deleted = PolicyDeleted(policy) session.add(policy_deleted) # hard delete policy in Policy table session.query(Policy).filter(Policy.id == id_).delete() # soft delete policy in PolicyDeleted table return session.query(PolicyDeleted).filter( PolicyDeleted.id == id_).soft_delete()
def add_datasource(id_, name, driver, config, description, enabled, session=None, secret_config_fields=None): secret_config_fields = secret_config_fields or [] session = session or db.get_session() with session.begin(subtransactions=True): datasource = Datasource(id_=id_, name=name, driver=driver, config=config, description=description, enabled=enabled) _encrypt_secret_config_fields(datasource, secret_config_fields) session.add(datasource) return datasource
def get_ds_table_data(ds_id, tablename=None, session=None): session = session or db.get_session() try: if tablename is None: rows = session.query(DSTableData).filter( DSTableData.ds_id == ds_id) return_list = [] for row in rows: return_list.append({ 'tablename': row.tablename, 'tabledata': _json_decode_table_data(row.tabledata) }) return return_list else: return _json_decode_table_data( session.query(DSTableData).filter( DSTableData.ds_id == ds_id, DSTableData.tablename == tablename).one().tabledata) except db_exc.NoResultFound: pass
def get_datasources(session=None, deleted=False): session = session or db.get_session() return [ _decrypt_secret_config_fields(ds_obj) for ds_obj in session.query(Datasource).all() ]
def get_datasource_name(name_or_id, session=None): session = session or db.get_session() datasource_obj = get_datasource(name_or_id, session) if datasource_obj is not None: return datasource_obj.name return name_or_id
def delete_datasource_with_data(id_, session=None): session = session or db.get_session() with session.begin(subtransactions=True): deleted = delete_datasource(id_, session) table_data.delete_ds_table_data(id_, session=session) return deleted
def delete_datasource(id_, session=None): session = session or db.get_session() return session.query(Datasource).filter(Datasource.id == id_).delete()
def get_policies(session=None, deleted=False): session = session or db.get_session() return (session.query(Policy).filter(Policy.deleted == '').all())
def delete_policy_rule(id, session=None): """Specify either the ID or the NAME, and that policy is deleted.""" session = session or db.get_session() return session.query(PolicyRule).filter(PolicyRule.id == id).soft_delete()