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:
idan kinory 2018-01-23 16:04:30 +00:00
parent 510f736db4
commit 56ed1e8646
4 changed files with 65 additions and 49 deletions

View File

@ -181,23 +181,13 @@ class TemplateController(RootRestController):
} }
@staticmethod @staticmethod
def _delete(uuids): def _delete(uuid):
try: try:
if type(uuids) != list: results = pecan.request.client.call(
uuids = [uuids] pecan.request.context,
storage = pecan.request.storage.templates 'delete_template',
templates = [t for _id in uuids for t in storage.query(uuid=_id) uuids=uuid)
if t.status != TStatus.DELETED] return results
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')
except Exception as e: 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)) abort(404, str(e))

View File

@ -15,7 +15,7 @@
import json import json
from oslo_log import log from oslo_log import log
from osprofiler import profiler from osprofiler import profiler
from vitrage.common.constants import TemplateStatus as TStatus
from vitrage.evaluator.template_db import template_repository as template_repo from vitrage.evaluator.template_db import template_repository as template_repo
@ -34,7 +34,7 @@ class TemplateApis(object):
self.db = db self.db = db
def validate_template(self, ctx, templates, template_type): 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)) str(template_type), str(templates))
files_content = [t[1] for t in 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 A new template has been added to the database with a status of
LOADING that needs to be handled. 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)) str(template_type), str(templates))
files_content = [t[1] for t in 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) template_type)
LOG.info("Add Template Running") if self._is_evaluator_reload_required(db_rows):
self.notifier.notify("add template", {'template_action': 'add'}) LOG.info("Adding templates..")
results = [_db_template_to_dict(r) for r in results] self.notifier.notify("add template", {'template_action': 'add'})
return results
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 """Signal the evaluator
A template status has been changed to DELETING. A template status has been changed to DELETING.
""" """
LOG.info("Delete Template Running") db = self.db
self.notifier.notify("delete template", {'template_action': 'delete'})
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): def _to_result(result, template_path):

View File

@ -15,6 +15,7 @@ from oslo_log import log
from oslo_utils import uuidutils from oslo_utils import uuidutils
from vitrage.common.constants import TemplateStatus 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.constants import TemplateTypes as TType
from vitrage.common.exception import VitrageError from vitrage.common.exception import VitrageError
from vitrage.evaluator.base import Template from vitrage.evaluator.base import Template
@ -28,23 +29,34 @@ METADATA = 'metadata'
NAME = 'name' NAME = 'name'
def add_templates_to_db(db, templates, template_type): def add_templates_to_db(db, templates, cli_type):
results = list() db_rows = list()
for template in templates: 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): 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) db.templates.create(db_row)
results.append(db_row) db_rows.append(db_row)
else: else:
results.append(_get_duplicate_result(template, template_type)) db_rows.append(_get_error_result(template, final_type,
return results "Duplicate template name"))
return db_rows
def validate_templates(db, templates, template_type): def validate_templates(db, templates, cli_type):
results = list() results = list()
for template in templates: 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 return results
@ -69,12 +81,12 @@ def _is_duplicate(db, template, result):
return True return True
def _get_duplicate_result(template, template_type): def _get_error_result(template, template_type, msg):
return models.Template( return models.Template(
name=template[METADATA][NAME], name=template[METADATA][NAME],
uuid="", uuid="",
status="Failed", status=TemplateStatus.ERROR,
status_details="Duplicate template name", status_details=msg,
file_content=template, file_content=template,
template_type=template_type) template_type=template_type)

View File

@ -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.tempest_clients import TempestClients
from vitrage_tempest_tests.tests.common import vitrage_utils from vitrage_tempest_tests.tests.common import vitrage_utils
import vitrage_tempest_tests.tests.utils as utils import vitrage_tempest_tests.tests.utils as utils
from vitrageclient.exceptions import ClientException
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -234,14 +233,6 @@ class TemplatesDBTest(BaseTemplateTest):
name='host_high_memory_usage_scenarios', type=TTypes.STANDARD) name='host_high_memory_usage_scenarios', type=TTypes.STANDARD)
self.assertIsNone(db_row, 'Template should not appear in list') 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: except Exception as e:
self._handle_exception(e) self._handle_exception(e)
raise raise