Templates api bug fix
* moved template delete impl to api_handler * support v2 template loading take template type from template file. Change-Id: Iab9f59b411238c3ac8e0e8f084c91ddba149d5eb
This commit is contained in:
parent
510f736db4
commit
56ed1e8646
@ -181,23 +181,13 @@ class TemplateController(RootRestController):
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _delete(uuids):
|
||||
|
||||
def _delete(uuid):
|
||||
try:
|
||||
if type(uuids) != list:
|
||||
uuids = [uuids]
|
||||
storage = pecan.request.storage.templates
|
||||
templates = [t for _id in uuids for t in storage.query(uuid=_id)
|
||||
if t.status != TStatus.DELETED]
|
||||
if not templates:
|
||||
raise VitrageError('Template not found')
|
||||
for t in templates:
|
||||
if t.status == TStatus.ERROR:
|
||||
storage.update(t.uuid, "status", TStatus.DELETED)
|
||||
else:
|
||||
storage.update(t.uuid, "status", TStatus.DELETING)
|
||||
pecan.request.client.call(pecan.request.context,
|
||||
'delete_template')
|
||||
results = pecan.request.client.call(
|
||||
pecan.request.context,
|
||||
'delete_template',
|
||||
uuids=uuid)
|
||||
return results
|
||||
except Exception as e:
|
||||
LOG.exception('failed to delete template file %s ', e)
|
||||
LOG.exception('failed to delete template %s ', e)
|
||||
abort(404, str(e))
|
||||
|
@ -15,7 +15,7 @@
|
||||
import json
|
||||
from oslo_log import log
|
||||
from osprofiler import profiler
|
||||
|
||||
from vitrage.common.constants import TemplateStatus as TStatus
|
||||
from vitrage.evaluator.template_db import template_repository as template_repo
|
||||
|
||||
|
||||
@ -34,7 +34,7 @@ class TemplateApis(object):
|
||||
self.db = db
|
||||
|
||||
def validate_template(self, ctx, templates, template_type):
|
||||
LOG.debug("TemplateApis validate_template type: %s content: ",
|
||||
LOG.debug("TemplateApis validate_template type: %s content: %s",
|
||||
str(template_type), str(templates))
|
||||
|
||||
files_content = [t[1] for t in templates]
|
||||
@ -50,24 +50,47 @@ class TemplateApis(object):
|
||||
A new template has been added to the database with a status of
|
||||
LOADING that needs to be handled.
|
||||
"""
|
||||
LOG.debug("TemplateApis add_template type: %s content: ",
|
||||
LOG.debug("TemplateApis add_template type: %s content: %s",
|
||||
str(template_type), str(templates))
|
||||
|
||||
files_content = [t[1] for t in templates]
|
||||
results = template_repo.add_templates_to_db(self.db, files_content,
|
||||
db_rows = template_repo.add_templates_to_db(self.db, files_content,
|
||||
template_type)
|
||||
LOG.info("Add Template Running")
|
||||
self.notifier.notify("add template", {'template_action': 'add'})
|
||||
results = [_db_template_to_dict(r) for r in results]
|
||||
return results
|
||||
if self._is_evaluator_reload_required(db_rows):
|
||||
LOG.info("Adding templates..")
|
||||
self.notifier.notify("add template", {'template_action': 'add'})
|
||||
|
||||
def delete_template(self, ctx):
|
||||
return [_db_template_to_dict(r) for r in db_rows]
|
||||
|
||||
def _is_evaluator_reload_required(self, db_rows):
|
||||
"""Is evaluator reload required
|
||||
|
||||
If all the templates have error status, no need to reload evaluators
|
||||
"""
|
||||
return any([True for t in db_rows if t.status != TStatus.ERROR])
|
||||
|
||||
def delete_template(self, ctx, uuids):
|
||||
"""Signal the evaluator
|
||||
|
||||
A template status has been changed to DELETING.
|
||||
"""
|
||||
LOG.info("Delete Template Running")
|
||||
self.notifier.notify("delete template", {'template_action': 'delete'})
|
||||
db = self.db
|
||||
|
||||
if type(uuids) != list:
|
||||
uuids = [uuids]
|
||||
LOG.info("Deleting templates %s ", str(uuids))
|
||||
templates = [t for _id in uuids for t in db.templates.query(uuid=_id)
|
||||
if t.status != TStatus.DELETED]
|
||||
if not templates:
|
||||
return
|
||||
for t in templates:
|
||||
if t.status == TStatus.ERROR:
|
||||
db.templates.update(t.uuid, "status", TStatus.DELETED)
|
||||
else:
|
||||
db.templates.update(t.uuid, "status", TStatus.DELETING)
|
||||
if self._is_evaluator_reload_required(templates):
|
||||
self.notifier.notify("delete template",
|
||||
{'template_action': 'delete'})
|
||||
|
||||
|
||||
def _to_result(result, template_path):
|
||||
|
@ -15,6 +15,7 @@ from oslo_log import log
|
||||
from oslo_utils import uuidutils
|
||||
|
||||
from vitrage.common.constants import TemplateStatus
|
||||
from vitrage.common.constants import TemplateTopologyFields as TFields
|
||||
from vitrage.common.constants import TemplateTypes as TType
|
||||
from vitrage.common.exception import VitrageError
|
||||
from vitrage.evaluator.base import Template
|
||||
@ -28,23 +29,34 @@ METADATA = 'metadata'
|
||||
NAME = 'name'
|
||||
|
||||
|
||||
def add_templates_to_db(db, templates, template_type):
|
||||
results = list()
|
||||
def add_templates_to_db(db, templates, cli_type):
|
||||
db_rows = list()
|
||||
for template in templates:
|
||||
result = _validate_template(db, template, template_type)
|
||||
final_type = template[METADATA].get(TFields.TYPE, cli_type)
|
||||
if not final_type or (cli_type and cli_type != final_type):
|
||||
db_rows.append(_get_error_result(template, final_type,
|
||||
"Unknown template type"))
|
||||
continue
|
||||
|
||||
result = _validate_template(db, template, final_type)
|
||||
if not _is_duplicate(db, template, result):
|
||||
db_row = _to_db_row(result, template, template_type)
|
||||
db_row = _to_db_row(result, template, final_type)
|
||||
db.templates.create(db_row)
|
||||
results.append(db_row)
|
||||
db_rows.append(db_row)
|
||||
else:
|
||||
results.append(_get_duplicate_result(template, template_type))
|
||||
return results
|
||||
db_rows.append(_get_error_result(template, final_type,
|
||||
"Duplicate template name"))
|
||||
return db_rows
|
||||
|
||||
|
||||
def validate_templates(db, templates, template_type):
|
||||
def validate_templates(db, templates, cli_type):
|
||||
results = list()
|
||||
for template in templates:
|
||||
results.append(_validate_template(db, template, template_type))
|
||||
final_type = template[METADATA].get(TFields.TYPE, cli_type)
|
||||
if not final_type or (cli_type and cli_type != final_type):
|
||||
results.append(base.Result("", False, "", "Unknown template type"))
|
||||
else:
|
||||
results.append(_validate_template(db, template, final_type))
|
||||
return results
|
||||
|
||||
|
||||
@ -69,12 +81,12 @@ def _is_duplicate(db, template, result):
|
||||
return True
|
||||
|
||||
|
||||
def _get_duplicate_result(template, template_type):
|
||||
def _get_error_result(template, template_type, msg):
|
||||
return models.Template(
|
||||
name=template[METADATA][NAME],
|
||||
uuid="",
|
||||
status="Failed",
|
||||
status_details="Duplicate template name",
|
||||
status=TemplateStatus.ERROR,
|
||||
status_details=msg,
|
||||
file_content=template,
|
||||
template_type=template_type)
|
||||
|
||||
|
@ -24,7 +24,6 @@ from vitrage_tempest_tests.tests.common import general_utils as g_utils
|
||||
from vitrage_tempest_tests.tests.common.tempest_clients import TempestClients
|
||||
from vitrage_tempest_tests.tests.common import vitrage_utils
|
||||
import vitrage_tempest_tests.tests.utils as utils
|
||||
from vitrageclient.exceptions import ClientException
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
@ -234,14 +233,6 @@ class TemplatesDBTest(BaseTemplateTest):
|
||||
name='host_high_memory_usage_scenarios', type=TTypes.STANDARD)
|
||||
self.assertIsNone(db_row, 'Template should not appear in list')
|
||||
|
||||
# delete the same template again - should raise VitrageError
|
||||
self.assertRaises(ClientException,
|
||||
vitrage_utils.delete_template, uuid)
|
||||
|
||||
# delete non-existing template - should raise VitrageError
|
||||
self.assertRaises(ClientException,
|
||||
vitrage_utils.delete_template, FAKE_UUID)
|
||||
|
||||
except Exception as e:
|
||||
self._handle_exception(e)
|
||||
raise
|
||||
|
Loading…
Reference in New Issue
Block a user