summaryrefslogtreecommitdiff
path: root/library/notification/hipchat
blob: 4ff95b32bf61838b02ba0d9e75d0d9a35f51fa77 (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
#!/usr/bin/python
# -*- coding: utf-8 -*-

DOCUMENTATION = '''
---
module: hipchat
version_added: "1.2"
short_description: Send a message to hipchat
description:
   - Send a message to hipchat
options:
  token:
    description:
      - API token.
    required: true
  room:
    description:
      - ID or name of the room.
    required: true
  from:
    description:
      - Name the message will appear be sent from. max 15 characters.
        Over 15, will be shorten.
    required: false
    default: Ansible
  msg:
    description:
      - The message body.
    required: true
    default: null
  color:
    description:
      - Background color for the message. Default is yellow.
    required: false
    default: yellow
    choices: [ "yellow", "red", "green", "purple", "gray", "random" ]
  msg_format:
    description:
      - message format. html or text. Default is text.
    required: false
    default: text
    choices: [ "text", "html" ]
  notify:
    description:
      - notify or not (change the tab color, play a sound, etc)
    required: false
    default: 'yes'
    choices: [ "yes", "no" ]
  validate_certs:
    description:
      - If C(no), SSL certificates will not be validated. This should only be used
        on personally controlled sites using self-signed certificates.
    required: false
    default: 'yes'
    choices: ['yes', 'no']
    version_added: 1.5.1
  api:
    description:
      - API url if using a self-hosted hipchat server
    required: false
    default: 'https://api.hipchat.com/v1/rooms/message'
    version_added: 1.6.0


# informational: requirements for nodes
requirements: [ urllib, urllib2 ]
author: WAKAYAMA Shirou
'''

EXAMPLES = '''
- hipchat: token=AAAAAA room=notify msg="Ansible task finished"
'''

# ===========================================
# HipChat module specific support methods.
#

MSG_URI = "https://api.hipchat.com/v1/rooms/message"

def send_msg(module, token, room, msg_from, msg, msg_format='text',
             color='yellow', notify=False, api=MSG_URI):
    '''sending message to hipchat'''

    params = {}
    params['room_id'] = room
    params['from'] = msg_from[:15]  # max length is 15
    params['message'] = msg
    params['message_format'] = msg_format
    params['color'] = color
    params['api'] = api

    if notify:
        params['notify'] = 1
    else:
        params['notify'] = 0

    url = api + "?auth_token=%s" % (token)
    data = urllib.urlencode(params)
    response, info = fetch_url(module, url, data=data)
    if info['status'] == 200:
        return response.read()
    else:
        module.fail_json(msg="failed to send message, return status=%s" % str(info['status']))


# ===========================================
# Module execution.
#

def main():

    module = AnsibleModule(
        argument_spec=dict(
            token=dict(required=True),
            room=dict(required=True),
            msg=dict(required=True),
            msg_from=dict(default="Ansible", aliases=['from']),
            color=dict(default="yellow", choices=["yellow", "red", "green",
                                                  "purple", "gray", "random"]),
            msg_format=dict(default="text", choices=["text", "html"]),
            notify=dict(default=True, type='bool'),
            validate_certs = dict(default='yes', type='bool'),
            api = dict(default=MSG_URI),
        ),
        supports_check_mode=True
    )

    token = module.params["token"]
    room = module.params["room"]
    msg = module.params["msg"]
    msg_from = module.params["msg_from"]
    color = module.params["color"]
    msg_format = module.params["msg_format"]
    notify = module.params["notify"]
    api = module.params["api"]

    try:
        send_msg(module, token, room, msg_from, msg, msg_format, color, notify, api)
    except Exception, e:
        module.fail_json(msg="unable to sent msg: %s" % e)

    changed = True
    module.exit_json(changed=changed, room=room, msg_from=msg_from, msg=msg)

# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.urls import *

main()