Fix for deleting audit template
We need to update sqlalchemy/api and sqlalchemy/models (and appropriate tests) to support deleting audit templates and recreating them with the same names. Change-Id: Icf54cf1ed989a3f2ad689e25be4474b16a3a3eb2 Related-Bug: #1510179
This commit is contained in:
@@ -308,6 +308,13 @@ class Connection(api.BaseConnection):
|
|||||||
if not values.get('uuid'):
|
if not values.get('uuid'):
|
||||||
values['uuid'] = utils.generate_uuid()
|
values['uuid'] = utils.generate_uuid()
|
||||||
|
|
||||||
|
query = model_query(models.AuditTemplate)
|
||||||
|
query = query.filter_by(name=values.get('name'),
|
||||||
|
deleted_at=None)
|
||||||
|
|
||||||
|
if len(query.all()) > 0:
|
||||||
|
raise exception.AuditTemplateAlreadyExists(uuid=values['uuid'])
|
||||||
|
|
||||||
audit_template = models.AuditTemplate()
|
audit_template = models.AuditTemplate()
|
||||||
audit_template.update(values)
|
audit_template.update(values)
|
||||||
|
|
||||||
@@ -349,14 +356,10 @@ class Connection(api.BaseConnection):
|
|||||||
|
|
||||||
def get_audit_template_by_name(self, context, audit_template_name):
|
def get_audit_template_by_name(self, context, audit_template_name):
|
||||||
query = model_query(models.AuditTemplate)
|
query = model_query(models.AuditTemplate)
|
||||||
query = query.filter_by(name=audit_template_name)
|
query = query.filter_by(name=audit_template_name,
|
||||||
|
deleted_at=None)
|
||||||
try:
|
try:
|
||||||
audit_template = query.one()
|
return query.one()
|
||||||
if not context.show_deleted:
|
|
||||||
if audit_template.deleted_at is not None:
|
|
||||||
raise exception.AuditTemplateNotFound(
|
|
||||||
audit_template=audit_template_name)
|
|
||||||
return audit_template
|
|
||||||
except exc.MultipleResultsFound:
|
except exc.MultipleResultsFound:
|
||||||
raise exception.Conflict(
|
raise exception.Conflict(
|
||||||
_('Multiple audit templates exist with the same name.'
|
_('Multiple audit templates exist with the same name.'
|
||||||
|
|||||||
@@ -116,7 +116,6 @@ class AuditTemplate(Base):
|
|||||||
__tablename__ = 'audit_templates'
|
__tablename__ = 'audit_templates'
|
||||||
__table_args__ = (
|
__table_args__ = (
|
||||||
schema.UniqueConstraint('uuid', name='uniq_audit_templates0uuid'),
|
schema.UniqueConstraint('uuid', name='uniq_audit_templates0uuid'),
|
||||||
schema.UniqueConstraint('name', name='uniq_audit_templates0name'),
|
|
||||||
table_args()
|
table_args()
|
||||||
)
|
)
|
||||||
id = Column(Integer, primary_key=True)
|
id = Column(Integer, primary_key=True)
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ from six.moves.urllib import parse as urlparse
|
|||||||
from wsme import types as wtypes
|
from wsme import types as wtypes
|
||||||
|
|
||||||
from watcher.api.controllers.v1 import audit_template as api_audit_template
|
from watcher.api.controllers.v1 import audit_template as api_audit_template
|
||||||
|
from watcher.common import exception
|
||||||
from watcher.common import utils
|
from watcher.common import utils
|
||||||
from watcher.db import api as db_api
|
from watcher.db import api as db_api
|
||||||
from watcher import objects
|
from watcher import objects
|
||||||
@@ -496,11 +497,10 @@ class TestDelete(api_base.FunctionalTest):
|
|||||||
self.assertTrue(response.json['error_message'])
|
self.assertTrue(response.json['error_message'])
|
||||||
|
|
||||||
self.context.show_deleted = True
|
self.context.show_deleted = True
|
||||||
audit_template = objects.AuditTemplate.get_by_name(
|
self.assertRaises(exception.AuditTemplateNotFound,
|
||||||
self.context, self.audit_template.name)
|
objects.AuditTemplate.get_by_name,
|
||||||
|
self.context,
|
||||||
return_deleted_at = timeutils.strtime(audit_template['deleted_at'])
|
self.audit_template.name)
|
||||||
self.assertEqual(timeutils.strtime(test_time), return_deleted_at)
|
|
||||||
|
|
||||||
def test_delete_audit_template_not_found(self):
|
def test_delete_audit_template_not_found(self):
|
||||||
uuid = utils.generate_uuid()
|
uuid = utils.generate_uuid()
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ class SqlAlchemyCustomTypesTestCase(base.DbTestCase):
|
|||||||
.filter_by(uuid=audit_template1_id).one()
|
.filter_by(uuid=audit_template1_id).one()
|
||||||
self.assertEqual({}, audit_template1.extra)
|
self.assertEqual({}, audit_template1.extra)
|
||||||
|
|
||||||
|
def test_JSONEncodedDict_extra_value(self):
|
||||||
# Create audit_template with extra
|
# Create audit_template with extra
|
||||||
audit_template2_id = w_utils.generate_uuid()
|
audit_template2_id = w_utils.generate_uuid()
|
||||||
self.dbapi.create_audit_template({'uuid': audit_template2_id,
|
self.dbapi.create_audit_template({'uuid': audit_template2_id,
|
||||||
|
|||||||
Reference in New Issue
Block a user