From 79e57e4c07d93240997aaa7b288b74c82c02237c Mon Sep 17 00:00:00 2001 From: Dmitry Shulyak Date: Mon, 28 Sep 2015 10:40:31 +0300 Subject: [PATCH] Add discard routine to system log cli and api --- solar/solar/cli/system_log.py | 5 +++++ solar/solar/core/resource/resource.py | 4 ++++ solar/solar/system_log/change.py | 31 +++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/solar/solar/cli/system_log.py b/solar/solar/cli/system_log.py index fa7172f9..d0cba114 100644 --- a/solar/solar/cli/system_log.py +++ b/solar/solar/cli/system_log.py @@ -102,6 +102,11 @@ def revert(uid): except errors.SolarError as er: raise click.BadParameter(str(er)) +@changes.command() +def discard(): + change.discard_all() + + @changes.command() @click.option('--name', default=None) def test(name): diff --git a/solar/solar/core/resource/resource.py b/solar/solar/core/resource/resource.py index 214a03dd..6e3649b9 100644 --- a/solar/solar/core/resource/resource.py +++ b/solar/solar/core/resource/resource.py @@ -172,6 +172,10 @@ class Resource(object): self.db_obj.state = RESOURCE_STATE.error.name self.db_obj.save() + def set_created(self): + self.db_obj.state = RESOURCE_STATE.created.name + self.db_obj.save() + def to_be_removed(self): return self.db_obj.state == RESOURCE_STATE.removed.name diff --git a/solar/solar/system_log/change.py b/solar/solar/system_log/change.py index 0acf1eee..69ae8cb6 100644 --- a/solar/solar/system_log/change.py +++ b/solar/solar/system_log/change.py @@ -195,3 +195,34 @@ def _revert_run(logitem): def revert(uid): return revert_uids([uid]) + +def _discard_remove(item): + resource_obj = resource.load(item.res) + resource_obj.set_created() + +_discard_update = _revert_update +_discard_run = _revert_run + + +def discard_uids(uids): + staged_log = data.SL() + for uid in uids: + item = staged_log.get(uid) + if item.action == CHANGES.update.name: + _discard_update(item) + elif item.action == CHANGES.remove.name: + _discard_remove(item) + elif item.action == CHANGES.run.name: + _discard_run(item) + else: + log.debug('Action %s for resource %s is a side' + ' effect of another action', item.action, item.res) + staged_log.pop(uid) + + +def discard_uid(uid): + return discard_uids([uid]) + +def discard_all(): + staged_log = data.SL() + return discard_uids([l.uid for l in staged_log])