diff --git a/doc/source/contributor/vitrage-api.rst b/doc/source/contributor/vitrage-api.rst index 2443cbdbf..fbb1245a8 100644 --- a/doc/source/contributor/vitrage-api.rst +++ b/doc/source/contributor/vitrage-api.rst @@ -1071,6 +1071,7 @@ Query Parameters - path (string, required) - the path to template file or directory - type (string, optional) - template type (standard,definition,equivalence) +- overwrite (boolean, optional) - if template already exists will overwrite it Request Body ============ diff --git a/releasenotes/notes/support_overwrite_when_adding_template-f6795771ae0eff8e.yaml b/releasenotes/notes/support_overwrite_when_adding_template-f6795771ae0eff8e.yaml new file mode 100644 index 000000000..30cdf74a6 --- /dev/null +++ b/releasenotes/notes/support_overwrite_when_adding_template-f6795771ae0eff8e.yaml @@ -0,0 +1,3 @@ +--- +features: + - Added support to overwrite existing template when adding one. \ No newline at end of file diff --git a/vitrage/api/controllers/v1/template.py b/vitrage/api/controllers/v1/template.py index b48fc9d33..3021c94c2 100644 --- a/vitrage/api/controllers/v1/template.py +++ b/vitrage/api/controllers/v1/template.py @@ -11,7 +11,10 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. + +from datetime import timedelta import json +import time import pecan from pytz import utc @@ -28,6 +31,8 @@ from vitrage.common.exception import VitrageError LOG = log.getLogger(__name__) +ONE_HOUR = int(timedelta(hours=1).total_seconds()) + @profiler.trace_cls("template controller", info={}, hide_args=False, trace_private=False) @@ -93,6 +98,16 @@ class TemplateController(RootRestController): {}) template_type = kwargs['template_type'] params = kwargs.get('params') + overwrite = kwargs.get('overwrite') + + if overwrite: + names = [ + template[1]['metadata']['name'] + for template in templates + ] + + uuids = self._to_uuids(names) + self._delete_templates_and_wait(uuids) try: return self._add(templates, template_type, params) @@ -218,6 +233,21 @@ class TemplateController(RootRestController): return template.uuid return val + def _delete_templates_and_wait(self, uuids): + self._delete(uuids) + + def check_deleted(): + for _id in uuids: + try: + self._show_template(_id) + except Exception: # if deleted we get exception + pass + else: + return False + return True + + return wait_for_action_to_end(ONE_HOUR, check_deleted) + def is_uuid(val): # unwrap the name or id @@ -226,3 +256,13 @@ def is_uuid(val): if type(val) is list: val, = val return is_uuid_like(val) + + +def wait_for_action_to_end(timeout, func, **kwargs): + count = 0 + while count < timeout: + if func(**kwargs): + return True + count += 1 + time.sleep(1) + return False