Skip to content

Commit 0fc8c16

Browse files
authored
Merge pull request #22 from copartit/master
1.3.1
2 parents 77649f5 + ec3a61c commit 0fc8c16

File tree

6 files changed

+50
-21
lines changed

6 files changed

+50
-21
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
4+
## 1.3.1
5+
6+
- Code refactoring/clean-up.
7+
38
## 1.3.0
49

510
- Added `st2.rules.enable`, `st2.rules.disable` actions and chatops commands.

actions/lib/action.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313

1414
class St2BaseAction(Action):
15+
def run(self, **kwargs):
16+
pass
17+
1518
def __init__(self, config):
1619
super(St2BaseAction, self).__init__(config)
1720
self._client = Client
@@ -111,6 +114,22 @@ def _run_client_method(self, method, method_kwargs, format_func, format_kwargs=N
111114
return result
112115

113116
def _manipulate_rule(self, name, pack, enabled):
117+
"""
118+
Enable or disable rule.
119+
120+
:param name: Name of the rule
121+
:type name: ``str``
122+
123+
:param pack: Pack where the rule is
124+
:type pack: ``str``
125+
126+
:param enabled:
127+
:type enabled: ``bool``
128+
129+
:return: Updated rule or string in case of an error
130+
:rtype: ``dict`` or ``str``
131+
"""
132+
114133
rule_name = '{}.{}'.format(pack, name)
115134
failure_reason = None
116135
rule = None
@@ -124,14 +143,21 @@ def _manipulate_rule(self, name, pack, enabled):
124143
if failure_reason:
125144
return 'Could not get rule {}: {}'.format(rule_name, failure_reason)
126145

127-
rule_enabled = rule.enabled
128-
129-
if isinstance(rule_enabled, bool) and rule_enabled == enabled:
130-
# already enabled, so just return true and formatted results
146+
try:
147+
rule_enabled = rule.enabled
148+
except AttributeError:
149+
# this should not happen, but better be safe than sorry
150+
self.logger.debug("Hmm, rule {}.{} doesn't have attribute 'enabled', "
151+
"assuming it's off then.".format(name, pack))
152+
rule_enabled = False
153+
154+
if rule_enabled == enabled:
155+
# already enabled/disabled, so just return true and formatted results
131156
return rule
132157

133-
rule.enabled = enabled
134158
try:
159+
rule.enabled = enabled
160+
135161
self.client.rules.update(rule)
136162
except Exception as exc:
137163
return 'Could not update rule {}: {}'.format(rule_name, exc)

actions/lib/formatters.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,12 @@ def format_result(item):
2222

2323
def format_rule_update_result(result, exclude_attributes):
2424
return format_client_list_result(result=[result], exclude_attributes=exclude_attributes)[0]
25+
26+
27+
def format_rule_result(rule, exclude):
28+
if rule is None or isinstance(rule, str):
29+
# error happened
30+
return False, rule
31+
else:
32+
# all good here
33+
return True, format_rule_update_result(rule, exclude)

actions/rules_disable.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from lib.action import St2BaseAction
2-
from lib.formatters import format_rule_update_result
2+
from lib.formatters import format_rule_result
33

44
__all__ = [
55
'St2RulesDisableAction'
@@ -10,9 +10,4 @@ class St2RulesDisableAction(St2BaseAction):
1010
def run(self, pack=None, name=None, exclude=None):
1111

1212
rule = self._manipulate_rule(pack=pack, name=name, enabled=False)
13-
if rule is None or isinstance(rule, str):
14-
# error happened
15-
return False, rule
16-
else:
17-
# all good here
18-
return True, format_rule_update_result(rule, exclude)
13+
return format_rule_result(rule, exclude)

actions/rules_enable.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from lib.action import St2BaseAction
2-
from lib.formatters import format_rule_update_result
2+
from lib.formatters import format_rule_result
33

44
__all__ = [
55
'St2RulesEnableAction'
@@ -10,9 +10,4 @@ class St2RulesEnableAction(St2BaseAction):
1010
def run(self, pack=None, name=None, exclude=None):
1111

1212
rule = self._manipulate_rule(pack=pack, name=name, enabled=True)
13-
if rule is None or isinstance(rule, str):
14-
# error happened
15-
return False, rule
16-
else:
17-
# all good here
18-
return True, format_rule_update_result(rule, exclude)
13+
return format_rule_result(rule, exclude)

pack.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
ref: st2
33
name: st2
44
description: StackStorm utility actions and aliases
5-
version: 1.3.0
5+
version: 1.3.1
66
python_versions:
77
- "2"
88
- "3"
99
author: StackStorm, Inc.
1010
email: info@stackstorm.com
1111
contributors:
1212
- Igor Cherkaev <igor.cherkaev@copart.com>
13-

0 commit comments

Comments
 (0)