add name for audit, changes for watcher api/db
Change-Id: Ibe04f5375d741d15999fde6faf767f15311c6351 Implements:blueprint add-name-for-audit
This commit is contained in:
@@ -401,6 +401,16 @@ class BaseConnection(object):
|
||||
:raises: :py:class:`~.AuditNotFound`
|
||||
"""
|
||||
|
||||
def get_audit_by_name(self, context, audit_name, eager=False):
|
||||
"""Return an audit.
|
||||
|
||||
:param context: The security context
|
||||
:param audit_name: The name of an audit.
|
||||
:param eager: If True, also loads One-to-X data (Default: False)
|
||||
:returns: An audit.
|
||||
:raises: :py:class:`~.AuditNotFound`
|
||||
"""
|
||||
|
||||
@abc.abstractmethod
|
||||
def destroy_audit(self, audit_id):
|
||||
"""Destroy an audit and all associated action plans.
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
"""add name for audit
|
||||
|
||||
Revision ID: 3cfc94cecf4e
|
||||
Revises: d098df6021e2
|
||||
Create Date: 2017-07-19 15:44:57.661099
|
||||
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '3cfc94cecf4e'
|
||||
down_revision = 'd09a5945e4a0'
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.add_column('audits', sa.Column('name', sa.String(length=63), nullable=True))
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_column('audits', 'name')
|
||||
@@ -659,6 +659,14 @@ class Connection(api.BaseConnection):
|
||||
if not values.get('uuid'):
|
||||
values['uuid'] = utils.generate_uuid()
|
||||
|
||||
query = model_query(models.Audit)
|
||||
query = query.filter_by(name=values.get('name'),
|
||||
deleted_at=None)
|
||||
|
||||
if len(query.all()) > 0:
|
||||
raise exception.AuditAlreadyExists(
|
||||
audit=values['name'])
|
||||
|
||||
if values.get('state') is None:
|
||||
values['state'] = objects.audit.State.PENDING
|
||||
|
||||
@@ -668,7 +676,7 @@ class Connection(api.BaseConnection):
|
||||
try:
|
||||
audit = self._create(models.Audit, values)
|
||||
except db_exc.DBDuplicateEntry:
|
||||
raise exception.AuditAlreadyExists(uuid=values['uuid'])
|
||||
raise exception.AuditAlreadyExists(audit=values['uuid'])
|
||||
return audit
|
||||
|
||||
def _get_audit(self, context, fieldname, value, eager):
|
||||
@@ -686,6 +694,10 @@ class Connection(api.BaseConnection):
|
||||
return self._get_audit(
|
||||
context, fieldname="uuid", value=audit_uuid, eager=eager)
|
||||
|
||||
def get_audit_by_name(self, context, audit_name, eager=False):
|
||||
return self._get_audit(
|
||||
context, fieldname="name", value=audit_name, eager=eager)
|
||||
|
||||
def destroy_audit(self, audit_id):
|
||||
def is_audit_referenced(session, audit_id):
|
||||
"""Checks whether the audit is referenced by action_plan(s)."""
|
||||
|
||||
@@ -166,10 +166,12 @@ class Audit(Base):
|
||||
__tablename__ = 'audits'
|
||||
__table_args__ = (
|
||||
UniqueConstraint('uuid', name='uniq_audits0uuid'),
|
||||
UniqueConstraint('name', 'deleted', name='uniq_audits0name'),
|
||||
table_args()
|
||||
)
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
uuid = Column(String(36))
|
||||
name = Column(String(63), nullable=True)
|
||||
audit_type = Column(String(20))
|
||||
state = Column(String(20), nullable=True)
|
||||
parameters = Column(JSONEncodedDict, nullable=True)
|
||||
|
||||
Reference in New Issue
Block a user