summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/monitoring/librato_annotation.py
blob: ce707a9817ab11c48af81accbb3ce6051af144de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# (C) Seth Edwards, 2014
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type


ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['preview'],
                    'supported_by': 'community'}


DOCUMENTATION = '''
---
module: librato_annotation
short_description: create an annotation in librato
description:
    - Create an annotation event on the given annotation stream :name. If the annotation stream does not exist, it will be created automatically
version_added: "1.6"
author: "Seth Edwards (@Sedward)"
requirements: []
options:
    user:
        description:
           - Librato account username
        required: true
    api_key:
        description:
           - Librato account api key
        required: true
    name:
        description:
            - The annotation stream name
            - If the annotation stream does not exist, it will be created automatically
        required: false
    title:
        description:
            - The title of an annotation is a string and may contain spaces
            - The title should be a short, high-level summary of the annotation e.g. v45 Deployment
        required: true
    source:
        description:
            - A string which describes the originating source of an annotation when that annotation is tracked across multiple members of a population
        required: false
    description:
        description:
            - The description contains extra metadata about a particular annotation
            - The description should contain specifics on the individual annotation e.g. Deployed 9b562b2 shipped new feature foo!
        required: false
    start_time:
        description:
            - The unix timestamp indicating the time at which the event referenced by this annotation started
        required: false
    end_time:
        description:
            - The unix timestamp indicating the time at which the event referenced by this annotation ended
            - For events that have a duration, this is a useful way to annotate the duration of the event
        required: false
    links:
        description:
            - See examples
        required: true
'''

EXAMPLES = '''
# Create a simple annotation event with a source
- librato_annotation:
    user: user@example.com
    api_key: XXXXXXXXXXXXXXXXX
    title: App Config Change
    source: foo.bar
    description: This is a detailed description of the config change

# Create an annotation that includes a link
- librato_annotation:
    user: user@example.com
    api_key: XXXXXXXXXXXXXXXXXX
    name: code.deploy
    title: app code deploy
    description: this is a detailed description of a deployment
    links:
      - rel: example
        href: http://www.example.com/deploy

# Create an annotation with a start_time and end_time
- librato_annotation:
    user: user@example.com
    api_key: XXXXXXXXXXXXXXXXXX
    name: maintenance
    title: Maintenance window
    description: This is a detailed description of maintenance
    start_time: 1395940006
    end_time: 1395954406
'''

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.urls import fetch_url


def post_annotation(module):
    user = module.params['user']
    api_key = module.params['api_key']
    name = module.params['name']
    title = module.params['title']

    url = 'https://metrics-api.librato.com/v1/annotations/%s' % name
    params = {}
    params['title'] = title

    if module.params['source'] is not None:
        params['source'] = module.params['source']
    if module.params['description'] is not None:
        params['description'] = module.params['description']
    if module.params['start_time'] is not None:
        params['start_time'] = module.params['start_time']
    if module.params['end_time'] is not None:
        params['end_time'] = module.params['end_time']
    if module.params['links'] is not None:
        params['links'] = module.params['links']

    json_body = module.jsonify(params)

    headers = {}
    headers['Content-Type'] = 'application/json'

    # Hack send parameters the way fetch_url wants them
    module.params['url_username'] = user
    module.params['url_password'] = api_key
    response, info = fetch_url(module, url, data=json_body, headers=headers)
    response_code = str(info['status'])
    response_body = info['body']
    if info['status'] != 201:
        if info['status'] >= 400:
            module.fail_json(msg="Request Failed. Response code: " + response_code + " Response body: " + response_body)
        else:
            module.fail_json(msg="Request Failed. Response code: " + response_code)
    response = response.read()
    module.exit_json(changed=True, annotation=response)


def main():

    module = AnsibleModule(
        argument_spec=dict(
            user=dict(required=True),
            api_key=dict(required=True),
            name=dict(required=False),
            title=dict(required=True),
            source=dict(required=False),
            description=dict(required=False),
            start_time=dict(required=False, default=None, type='int'),
            end_time=dict(required=False, default=None, type='int'),
            links=dict(type='list')
        )
    )

    post_annotation(module)


if __name__ == '__main__':
    main()