-
Notifications
You must be signed in to change notification settings - Fork 306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
updated reqs and test #447
Changes from 6 commits
828fdd4
8594abe
c2bf9b7
e711706
d6a4525
5ccf944
c71931c
953561b
34fdf1e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -8,6 +8,7 @@ | |||||||||||||||
# datadog | ||||||||||||||||
from datadog import api | ||||||||||||||||
from datadog.dogshell.common import report_errors, report_warnings | ||||||||||||||||
import warnings | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
class MonitorClient(object): | ||||||||||||||||
|
@@ -39,10 +40,24 @@ def setup_parser(cls, subparsers): | |||||||||||||||
|
||||||||||||||||
update_parser = verb_parsers.add_parser('update', help="Update existing monitor") | ||||||||||||||||
update_parser.add_argument('monitor_id', help="monitor to replace with the new definition") | ||||||||||||||||
update_parser.add_argument('type', help="type of the monitor, e.g. " | ||||||||||||||||
"'metric alert' 'service check'") | ||||||||||||||||
update_parser.add_argument('query', help="query to notify on with syntax varying" | ||||||||||||||||
" depending on what type of monitor you are creating") | ||||||||||||||||
update_parser.add_argument( | ||||||||||||||||
'type', | ||||||||||||||||
nargs='?', | ||||||||||||||||
help="[Deprecated] optional argument preferred" | ||||||||||||||||
"type of the monitor, e.g. 'metric alert' 'service check'", | ||||||||||||||||
default=None | ||||||||||||||||
) | ||||||||||||||||
update_parser.add_argument( | ||||||||||||||||
'query', | ||||||||||||||||
nargs='?', | ||||||||||||||||
help="[Deprecated] optional argument preferred" | ||||||||||||||||
"query to notify on with syntax varying depending on monitor type", | ||||||||||||||||
default=None | ||||||||||||||||
) | ||||||||||||||||
update_parser.add_argument('--type', help="type of the monitor, e.g. " | ||||||||||||||||
"'metric alert' 'service check'", default=None, dest='type_opt') | ||||||||||||||||
update_parser.add_argument('--query', help="query to notify on with syntax varying" | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, changing the type of the parameters from positional args to named args is a breaking change. Can we just add the defaults without changing the name ? I guess not, we have to make them named arguments if we want to make them optional... Not sure how best to deal with that honestly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a possible way forward is to:
Does that make sense? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. This solution seems ideal to me, thanks for the suggestions ! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok sounds good. Happy to work on this. Thanks for the eyes! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These updates are made. Working on tests next. |
||||||||||||||||
" depending on monitor type", default=None, dest='query_opt') | ||||||||||||||||
update_parser.add_argument('--name', help="name of the alert", default=None) | ||||||||||||||||
update_parser.add_argument('--message', help="message to include with " | ||||||||||||||||
"notifications for this monitor", default=None) | ||||||||||||||||
|
@@ -148,14 +163,41 @@ def _file_post(cls, args): | |||||||||||||||
def _update(cls, args): | ||||||||||||||||
api._timeout = args.timeout | ||||||||||||||||
format = args.format | ||||||||||||||||
options = None | ||||||||||||||||
|
||||||||||||||||
to_update = {} | ||||||||||||||||
if args.type: | ||||||||||||||||
if args.type_opt: | ||||||||||||||||
msg = '[WARNING] Duplicate arguments for `type`. Using optional value --type' | ||||||||||||||||
print(msg) | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For printing, let's use the datadogpy/datadog/dogshell/common.py Lines 11 to 16 in 9a463de
"WARNING: " for consistency with how we print other warnings, as in datadogpy/datadog/dogshell/common.py Line 35 in 9a463de
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is all done and tests adjusted accordingly |
||||||||||||||||
else: | ||||||||||||||||
to_update['type'] = args.type | ||||||||||||||||
msg = "[DEPRECATION] `type` is no longer required to `update` and may be omitted" | ||||||||||||||||
warnings.warn(msg, DeprecationWarning) | ||||||||||||||||
if args.query: | ||||||||||||||||
if args.query_opt: | ||||||||||||||||
msg = '[WARNING] Duplicate arguments for `query`. Using optional value --query' | ||||||||||||||||
print(msg) | ||||||||||||||||
else: | ||||||||||||||||
to_update['query'] = args.query | ||||||||||||||||
msg = "[DEPRECATION] `query` is no longer required to `update` and may be omitted" | ||||||||||||||||
warnings.warn(msg, DeprecationWarning) | ||||||||||||||||
if args.name: | ||||||||||||||||
to_update['name'] = args.name | ||||||||||||||||
if args.message: | ||||||||||||||||
to_update['message'] = args.message | ||||||||||||||||
if args.type_opt: | ||||||||||||||||
to_update['type'] = args.type_opt | ||||||||||||||||
if args.query_opt: | ||||||||||||||||
to_update['query'] = args.query_opt | ||||||||||||||||
|
||||||||||||||||
if args.options is not None: | ||||||||||||||||
try: | ||||||||||||||||
options = json.loads(args.options) | ||||||||||||||||
to_update['options'] = json.loads(args.options) | ||||||||||||||||
except: | ||||||||||||||||
raise Exception('bad json parameter') | ||||||||||||||||
res = api.Monitor.update(args.monitor_id, type=args.type, query=args.query, | ||||||||||||||||
name=args.name, message=args.message, options=options) | ||||||||||||||||
|
||||||||||||||||
res = api.Monitor.update(args.monitor_id, **to_update) | ||||||||||||||||
|
||||||||||||||||
report_warnings(res) | ||||||||||||||||
report_errors(res) | ||||||||||||||||
if format == 'pretty': | ||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -296,6 +296,7 @@ def test_monitors(self): | |||||
assert out["query"] == query | ||||||
assert out["type"] == type_alert | ||||||
monitor_id = str(out["id"]) | ||||||
monitor_name = out["name"] | ||||||
|
||||||
out, _, _ = self.dogshell(["monitor", "show", monitor_id]) | ||||||
out = json.loads(out) | ||||||
|
@@ -304,16 +305,41 @@ def test_monitors(self): | |||||
|
||||||
# Update options | ||||||
options = {"notify_no_data": True, "no_data_timeframe": 20} | ||||||
|
||||||
out, _, _ = self.dogshell( | ||||||
["monitor", "update", monitor_id, type_alert, query, "--options", json.dumps(options)] | ||||||
) | ||||||
|
||||||
out = json.loads(out) | ||||||
assert out["query"] == query | ||||||
assert query in out['query'] | ||||||
assert out["options"]["notify_no_data"] == options["notify_no_data"] | ||||||
assert out["options"]["no_data_timeframe"] == options["no_data_timeframe"] | ||||||
|
||||||
# Update message only | ||||||
updated_message = "monitor updated" | ||||||
current_options = out['options'] | ||||||
out, err, return_code = self.dogshell( | ||||||
["monitor", "update", monitor_id, "--message", updated_message] | ||||||
) | ||||||
|
||||||
out = json.loads(out) | ||||||
assert updated_message == out["message"] | ||||||
assert query == out['query'] | ||||||
assert monitor_name == out ['name'] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops! updated |
||||||
assert current_options == out['options'] | ||||||
|
||||||
# Updating query only | ||||||
updated_query = "avg(last_15m):sum:system.net.bytes_rcvd{*} by {env} > 222" | ||||||
|
||||||
out, err, return_code = self.dogshell( | ||||||
["monitor", "update", monitor_id, "--query", updated_query] | ||||||
) | ||||||
|
||||||
out = json.loads(out) | ||||||
assert updated_query in out["query"] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's also add an assert on the updated type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops yep sorry I overlooked this |
||||||
assert updated_message in out['message'] # updated_message updated in previous step | ||||||
assert monitor_name in out ['name'] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||
assert current_options == out['options'] | ||||||
|
||||||
# Mute monitor | ||||||
out, _, _ = self.dogshell(["monitor", "mute", str(out["id"])]) | ||||||
out = json.loads(out) | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove this import which is now unused and add back the blank line that's making flake8 tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated