| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) Seth Edwards, 2014 |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: librato_annotation |
| 11 |
short_description: Create an annotation in Librato |
| 12 |
description: |
| 13 |
- Create an annotation event on the given annotation stream O(name). If the annotation stream does not exist, it creates |
| 14 |
one automatically. |
| 15 |
author: "Seth Edwards (@Sedward)" |
| 16 |
requirements: [] |
| 17 |
extends_documentation_fragment: |
| 18 |
- community.general._attributes |
| 19 |
attributes: |
| 20 |
check_mode: |
| 21 |
support: none |
| 22 |
diff_mode: |
| 23 |
support: none |
| 24 |
options: |
| 25 |
user: |
| 26 |
type: str |
| 27 |
description: |
| 28 |
- Librato account username. |
| 29 |
required: true |
| 30 |
api_key: |
| 31 |
type: str |
| 32 |
description: |
| 33 |
- Librato account API key. |
| 34 |
required: true |
| 35 |
name: |
| 36 |
type: str |
| 37 |
description: |
| 38 |
- The annotation stream name. |
| 39 |
- If the annotation stream does not exist, it creates one automatically. |
| 40 |
title: |
| 41 |
type: str |
| 42 |
description: |
| 43 |
- The title of an annotation is a string and may contain spaces. |
| 44 |
- The title should be a short, high-level summary of the annotation for example V(v45 Deployment). |
| 45 |
required: true |
| 46 |
source: |
| 47 |
type: str |
| 48 |
description: |
| 49 |
- A string which describes the originating source of an annotation when that annotation is tracked across multiple members |
| 50 |
of a population. |
| 51 |
description: |
| 52 |
type: str |
| 53 |
description: |
| 54 |
- The description contains extra metadata about a particular annotation. |
| 55 |
- The description should contain specifics on the individual annotation for example V(Deployed 9b562b2 shipped new feature |
| 56 |
foo!). |
| 57 |
start_time: |
| 58 |
type: int |
| 59 |
description: |
| 60 |
- The unix timestamp indicating the time at which the event referenced by this annotation started. |
| 61 |
end_time: |
| 62 |
type: int |
| 63 |
description: |
| 64 |
- The unix timestamp indicating the time at which the event referenced by this annotation ended. |
| 65 |
- For events that have a duration, this is a useful way to annotate the duration of the event. |
| 66 |
links: |
| 67 |
type: list |
| 68 |
elements: dict |
| 69 |
description: |
| 70 |
- See examples. |
| 71 |
""" |
| 72 |
|
| 73 |
EXAMPLES = r""" |
| 74 |
- name: Create a simple annotation event with a source |
| 75 |
community.general.librato_annotation: |
| 76 |
user: [email protected] |
| 77 |
api_key: XXXXXXXXXXXXXXXXX |
| 78 |
title: App Config Change |
| 79 |
source: foo.bar |
| 80 |
description: This is a detailed description of the config change |
| 81 |
|
| 82 |
- name: Create an annotation that includes a link |
| 83 |
community.general.librato_annotation: |
| 84 |
user: [email protected] |
| 85 |
api_key: XXXXXXXXXXXXXXXXXX |
| 86 |
name: code.deploy |
| 87 |
title: app code deploy |
| 88 |
description: this is a detailed description of a deployment |
| 89 |
links: |
| 90 |
- rel: example |
| 91 |
href: http://www.example.com/deploy |
| 92 |
|
| 93 |
- name: Create an annotation with a start_time and end_time |
| 94 |
community.general.librato_annotation: |
| 95 |
user: [email protected] |
| 96 |
api_key: XXXXXXXXXXXXXXXXXX |
| 97 |
name: maintenance |
| 98 |
title: Maintenance window |
| 99 |
description: This is a detailed description of maintenance |
| 100 |
start_time: 1395940006 |
| 101 |
end_time: 1395954406 |
| 102 |
""" |
| 103 |
|
| 104 |
from ansible.module_utils.basic import AnsibleModule |
| 105 |
from ansible.module_utils.urls import fetch_url |
| 106 |
|
| 107 |
|
| 108 |
def post_annotation(module): |
| 109 |
user = module.params["user"] |
| 110 |
api_key = module.params["api_key"] |
| 111 |
name = module.params["name"] |
| 112 |
title = module.params["title"] |
| 113 |
|
| 114 |
url = f"https://metrics-api.librato.com/v1/annotations/{name}" |
| 115 |
params = {} |
| 116 |
params["title"] = title |
| 117 |
|
| 118 |
if module.params["source"] is not None: |
| 119 |
params["source"] = module.params["source"] |
| 120 |
if module.params["description"] is not None: |
| 121 |
params["description"] = module.params["description"] |
| 122 |
if module.params["start_time"] is not None: |
| 123 |
params["start_time"] = module.params["start_time"] |
| 124 |
if module.params["end_time"] is not None: |
| 125 |
params["end_time"] = module.params["end_time"] |
| 126 |
if module.params["links"] is not None: |
| 127 |
params["links"] = module.params["links"] |
| 128 |
|
| 129 |
json_body = module.jsonify(params) |
| 130 |
|
| 131 |
headers = {} |
| 132 |
headers["Content-Type"] = "application/json" |
| 133 |
|
| 134 |
# Hack send parameters the way fetch_url wants them |
| 135 |
module.params["url_username"] = user |
| 136 |
module.params["url_password"] = api_key |
| 137 |
response, info = fetch_url(module, url, data=json_body, headers=headers) |
| 138 |
response_code = str(info["status"]) |
| 139 |
response_body = info["body"] |
| 140 |
if info["status"] != 201: |
| 141 |
if info["status"] >= 400: |
| 142 |
module.fail_json(msg=f"Request Failed. Response code: {response_code} Response body: {response_body}") |
| 143 |
else: |
| 144 |
module.fail_json(msg=f"Request Failed. Response code: {response_code}") |
| 145 |
response = response.read() |
| 146 |
module.exit_json(changed=True, annotation=response) |
| 147 |
|
| 148 |
|
| 149 |
def main(): |
| 150 |
module = AnsibleModule( |
| 151 |
argument_spec=dict( |
| 152 |
user=dict(required=True), |
| 153 |
api_key=dict(required=True, no_log=True), |
| 154 |
name=dict(), |
| 155 |
title=dict(required=True), |
| 156 |
source=dict(), |
| 157 |
description=dict(), |
| 158 |
start_time=dict(type="int"), |
| 159 |
end_time=dict(type="int"), |
| 160 |
links=dict(type="list", elements="dict"), |
| 161 |
) |
| 162 |
) |
| 163 |
|
| 164 |
post_annotation(module) |
| 165 |
|
| 166 |
|
| 167 |
if __name__ == "__main__": |
| 168 |
main() |