From 650b6e9b4ff890b5664858c901668c62bac3f6f9 Mon Sep 17 00:00:00 2001 From: Nick Harring Date: Wed, 29 Apr 2015 14:26:16 -0700 Subject: [PATCH 1/6] Initial public revision. --- monitoring/circonus_annotation.py | 125 ++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 monitoring/circonus_annotation.py diff --git a/monitoring/circonus_annotation.py b/monitoring/circonus_annotation.py new file mode 100644 index 00000000000..6324a1b6111 --- /dev/null +++ b/monitoring/circonus_annotation.py @@ -0,0 +1,125 @@ +#!/usr/bin/env python + +import requests +import time +import json + +DOCUMENTATION = ''' +--- +module: circonus_annotation +short_description: create an annotation in circonus +description: + - Create an annotation event with a given category, title and description. Optionally start, end or durations can be provided +author: Nick Harring +requirements: + - urllib3 + - requests + - time +options: + api_key: + description: + - Circonus API key + required: true + category: + description: + - Annotation Category + required: true + description: + description: + - Description of annotation + required: true + title: + description: + - Title of annotation + required: true + start: + description: + - Unix timestamp of event start, defaults to now + required: false + stop: + description: + - Unix timestamp of event end, defaults to now + duration + required: false + duration: + description: + - Duration in seconds of annotation, defaults to 0 + required: false +''' +EXAMPLES = ''' +# Create a simple annotation event with a source +- circonus_annotation: + api_key: XXXXXXXXXXXXXXXXX + title: 'App Config Change' + description: 'This is a detailed description of the config change' + category: 'This category groups like annotations' +# Create an annotation with a duration of 5 minutes +- circonus_annotation: + api_key: XXXXXXXXXXXXXXXXX + title: 'App Config Change' + description: 'This is a detailed description of the config change' + category: 'This category groups like annotations' + duration: 300 +# Create an annotation with a start_time and end_time +- circonus_annotation: + api_key: XXXXXXXXXXXXXXXXX + title: 'App Config Change' + description: 'This is a detailed description of the config change' + category: 'This category groups like annotations' + start_time: 1395940006 + end_time: 1395954407 +''' +def post_annotation(annotation, api_key): + base_url = 'https://api.circonus.com/v2' + anootate_post_endpoint = '/annotation' + resp = requests.post(base_url + anootate_post_endpoint, headers=build_headers(api_key), data=json.dumps(annotation)) + resp.raise_for_status() + return resp + +def create_annotation(module): + annotation = {} + if module.params['duration'] != None: + duration = module.params['duration'] + else: + duration = 0 + if module.params['start'] != None: + start = module.params['start'] + else: + start = int(time.time()) + if module.params['stop'] != None: + stop = module.params['stop'] + else: + stop = int(time.time())+duration + annotation['start'] = int(start) + annotation['stop'] = int(stop) + annotation['category'] = module.params['category'] + annotation['description'] = module.params['description'] + annotation['title'] = module.params['title'] + return annotation + +def build_headers(api_token): + headers = {'X-Circonus-App-Name': 'ansible', + 'Host': 'api.circonus.com', 'X-Circonus-Auth-Token': api_token, + 'Accept': 'application/json'} + return headers + +def main(): + module = AnsibleModule( + argument_spec = dict( + start = dict(required=False, type='int'), + stop = dict(required=False, type='int'), + category = dict(required=True), + title = dict(required=True), + description = dict(required=True), + duration = dict(required=False, type='int'), + api_key = dict(required=True) + ) + ) + annotation = create_annotation(module) + try: + resp = post_annotation(annotation, module.params['api_key']) + except requests.exceptions.RequestException as e: + module.fail_json(msg='Request Failed', reason=e) + module.exit_json(changed=True, annotation=resp.json()) + +from ansible.module_utils.basic import * +main() From dd02f24df9cf56a52320314488fa2994e96291ae Mon Sep 17 00:00:00 2001 From: Nick Harring Date: Thu, 30 Apr 2015 10:16:26 -0700 Subject: [PATCH 2/6] Added Epic copyright notice, clarified examples. --- monitoring/circonus_annotation.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/monitoring/circonus_annotation.py b/monitoring/circonus_annotation.py index 6324a1b6111..dd95402a7c1 100644 --- a/monitoring/circonus_annotation.py +++ b/monitoring/circonus_annotation.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +# (c) 2014-2015, Epic Games, Inc. + import requests import time import json @@ -46,13 +48,13 @@ options: required: false ''' EXAMPLES = ''' -# Create a simple annotation event with a source +# Create a simple annotation event with a source, defaults to start and end time of now - circonus_annotation: api_key: XXXXXXXXXXXXXXXXX title: 'App Config Change' description: 'This is a detailed description of the config change' category: 'This category groups like annotations' -# Create an annotation with a duration of 5 minutes +# Create an annotation with a duration of 5 minutes and a default start time of now - circonus_annotation: api_key: XXXXXXXXXXXXXXXXX title: 'App Config Change' @@ -88,7 +90,7 @@ def create_annotation(module): if module.params['stop'] != None: stop = module.params['stop'] else: - stop = int(time.time())+duration + stop = int(time.time())+ duration annotation['start'] = int(start) annotation['stop'] = int(stop) annotation['category'] = module.params['category'] From c0ca227717f8ad85bb954da2873c41e7409dec8c Mon Sep 17 00:00:00 2001 From: Nick Harring Date: Wed, 6 May 2015 09:43:38 -0700 Subject: [PATCH 3/6] PEP-8 style fixes --- monitoring/circonus_annotation.py | 32 +++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/monitoring/circonus_annotation.py b/monitoring/circonus_annotation.py index dd95402a7c1..75ca6540cbe 100644 --- a/monitoring/circonus_annotation.py +++ b/monitoring/circonus_annotation.py @@ -71,13 +71,16 @@ EXAMPLES = ''' end_time: 1395954407 ''' def post_annotation(annotation, api_key): + ''' Takes annotation dict and api_key string''' base_url = 'https://api.circonus.com/v2' anootate_post_endpoint = '/annotation' - resp = requests.post(base_url + anootate_post_endpoint, headers=build_headers(api_key), data=json.dumps(annotation)) + resp = requests.post(base_url + anootate_post_endpoint, + headers=build_headers(api_key), data=json.dumps(annotation)) resp.raise_for_status() return resp def create_annotation(module): + ''' Takes ansible module object ''' annotation = {} if module.params['duration'] != None: duration = module.params['duration'] @@ -97,23 +100,24 @@ def create_annotation(module): annotation['description'] = module.params['description'] annotation['title'] = module.params['title'] return annotation - def build_headers(api_token): - headers = {'X-Circonus-App-Name': 'ansible', - 'Host': 'api.circonus.com', 'X-Circonus-Auth-Token': api_token, - 'Accept': 'application/json'} + '''Takes api token, returns headers with it included.''' + headers = {'X-Circonus-App-Name': 'ansible', + 'Host': 'api.circonus.com', 'X-Circonus-Auth-Token': api_token, + 'Accept': 'application/json'} return headers def main(): + '''Main function, dispatches logic''' module = AnsibleModule( - argument_spec = dict( - start = dict(required=False, type='int'), - stop = dict(required=False, type='int'), - category = dict(required=True), - title = dict(required=True), - description = dict(required=True), - duration = dict(required=False, type='int'), - api_key = dict(required=True) + argument_spec=dict( + start=dict(required=False, type='int'), + stop=dict(required=False, type='int'), + category=dict(required=True), + title=dict(required=True), + description=dict(required=True), + duration=dict(required=False, type='int'), + api_key=dict(required=True) ) ) annotation = create_annotation(module) @@ -123,5 +127,5 @@ def main(): module.fail_json(msg='Request Failed', reason=e) module.exit_json(changed=True, annotation=resp.json()) -from ansible.module_utils.basic import * +from ansible.module_utils.basic import AnsibleModule main() From 910761bd6da8391320d253121e70f78b5bd0db52 Mon Sep 17 00:00:00 2001 From: Nick Harring Date: Tue, 12 May 2015 14:27:22 -0700 Subject: [PATCH 4/6] Added version_added in Documentation output. Fixed shebang to be /usr/bin/python. --- monitoring/circonus_annotation.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/monitoring/circonus_annotation.py b/monitoring/circonus_annotation.py index 75ca6540cbe..9144df987f0 100644 --- a/monitoring/circonus_annotation.py +++ b/monitoring/circonus_annotation.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/python # (c) 2014-2015, Epic Games, Inc. @@ -13,6 +13,7 @@ short_description: create an annotation in circonus description: - Create an annotation event with a given category, title and description. Optionally start, end or durations can be provided author: Nick Harring +version_added: 2.0 requirements: - urllib3 - requests From 46f6e5f6fcbc57f223653d290445d63461a922d6 Mon Sep 17 00:00:00 2001 From: Nick Harring Date: Tue, 12 May 2015 14:35:47 -0700 Subject: [PATCH 5/6] Fixed ansible module import which I changed chasing a better PEP-8 score. --- monitoring/circonus_annotation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monitoring/circonus_annotation.py b/monitoring/circonus_annotation.py index 9144df987f0..ebb5ecc175e 100644 --- a/monitoring/circonus_annotation.py +++ b/monitoring/circonus_annotation.py @@ -128,5 +128,5 @@ def main(): module.fail_json(msg='Request Failed', reason=e) module.exit_json(changed=True, annotation=resp.json()) -from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.basic import * main() From cf2b734ab3daa08a92b5db619b3f0f108524ce44 Mon Sep 17 00:00:00 2001 From: Nick Harring Date: Tue, 12 May 2015 14:42:21 -0700 Subject: [PATCH 6/6] Removed as keyword for 2.4 and earlier backwards compat. --- monitoring/circonus_annotation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monitoring/circonus_annotation.py b/monitoring/circonus_annotation.py index ebb5ecc175e..e2b6d8d79ed 100644 --- a/monitoring/circonus_annotation.py +++ b/monitoring/circonus_annotation.py @@ -124,8 +124,8 @@ def main(): annotation = create_annotation(module) try: resp = post_annotation(annotation, module.params['api_key']) - except requests.exceptions.RequestException as e: - module.fail_json(msg='Request Failed', reason=e) + except requests.exceptions.RequestException, err_str: + module.fail_json(msg='Request Failed', reason=err_str) module.exit_json(changed=True, annotation=resp.json()) from ansible.module_utils.basic import *