Commit b490571b authored by Anael Beutot's avatar Anael Beutot
Browse files

Check conflicts before inserting tags in TagDB for plugins

parent b72d9724
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ import imp
import logging
from collections import defaultdict

from cloudcontrol.common.client.exc import TagConflict
from cloudcontrol.common.client.tags import Tag
from cloudcontrol.common.jobs import Job
from cloudcontrol.common.tql.db.helpers import taggify
@@ -23,6 +24,7 @@ class Plugin(object):
        self.logger = logger
        self.name = name
        self.sha1 = sha1
        self.tags = []
        self.tag_db = tag_db
        self.methods = {}
        self.events = defaultdict(lambda: [])
@@ -38,6 +40,13 @@ class Plugin(object):
        module.on = self._decorator_event
        try:
            exec plugin_body in module.__dict__
            conflicts = self.tag_db.check_tags_conflict(*[tag.name for tag in
                                                          self.tags])
            if conflicts:
                raise TagConflict(
                    'Tags with names %s already exist' % ','.join(conflicts))
            else:
                self.tag_db.add_tags(self.tags)
        except Exception as exc:
            err_msg = 'Error during plugin installation (%s): %s' % (
                self.name, exc)
@@ -53,7 +62,7 @@ class Plugin(object):
        def decorator(func):
            tag = Tag(func.__name__ if name is None else name,
                      lambda: taggify(func()), ttl=ttl, refresh=refresh)
            self.tag_db.add_tag(tag)
            self.tags.append(tag)
            return _tag_direct_use_error
        return decorator