summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/notification/catapult.py
blob: ebfc803c2aa9ae25e87bcefa0203fd138a158b9a (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
165
#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2016, Jonathan Mainguy <jon@soh.re>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

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


DOCUMENTATION = '''
---
module: catapult
version_added: 2.4
short_description: Send a sms / mms using the catapult bandwidth api
description:
     - Allows notifications to be sent using sms / mms via the catapult bandwidth api.
options:
  src:
    description:
      - One of your catapult telephone numbers the message should come from (must be in E.164 format, like C(+19195551212)).
    required: true
    default: null
  dest:
    description:
      - The phone number or numbers the message should be sent to (must be in E.164 format, like C(+19195551212)).
    required: true
    default: null
  msg:
    description:
      - The contents of the text message (must be 2048 characters or less).
    required: true
    default: null
  media:
    description:
      - For MMS messages, a media url to the location of the media to be sent with the message.
  user_id:
    description:
      - User Id from Api account page.
    required: true
    default: null
  api_token:
    description:
      - Api Token from Api account page.
    required: true
    default: null
  api_secret:
    description:
      - Api Secret from Api account page.
    required: true
    default: null

author: "Jonathan Mainguy (@Jmainguy)"
notes:
    - Will return changed even if the media url is wrong.
    - Will return changed if the destination number is invalid.

'''

EXAMPLES = '''
- name: Send a mms to multiple users
  catapult:
    src: "+15035555555"
    dest:
      - "+12525089000"
      - "+12018994225"
    media: "http://example.com/foobar.jpg"
    msg: "Task is complete"
    user_id: "{{ user_id }}"
    api_token: "{{ api_token }}"
    api_secret: "{{ api_secret }}"

- name: Send a sms to a single user
  catapult:
    src: "+15035555555"
    dest: "+12018994225"
    msg: "Consider yourself notified"
    user_id: "{{ user_id }}"
    api_token: "{{ api_token }}"
    api_secret: "{{ api_secret }}"

'''

RETURN = '''
changed:
    description: Whether the api accepted the message.
    returned: always
    type: boolean
    sample: True
'''


import json
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.urls import fetch_url
# Stole most of this code from the ansible twillio and nexmo modules


def send(module, src, dest, msg, media, user_id, api_token, api_secret):
    """
    Send the message
    """
    AGENT = "Ansible"
    URI = "https://api.catapult.inetwork.com/v1/users/%s/messages" % user_id
    data = {'from': src, 'to': dest, 'text': msg}
    if media:
        data['media'] = media

    headers = {'User-Agent': AGENT, 'Content-type': 'application/json'}

    # Hack module params to have the Basic auth params that fetch_url expects
    module.params['url_username'] = api_token.replace('\n', '')
    module.params['url_password'] = api_secret.replace('\n', '')

    return fetch_url(module, URI, data=json.dumps(data), headers=headers, method="post")


def main():
    module = AnsibleModule(
        argument_spec=dict(
            src=dict(required=True),
            dest=dict(required=True, type='list'),
            msg=dict(required=True),
            user_id=dict(required=True),
            api_token=dict(required=True, no_log=True),
            api_secret=dict(required=True, no_log=True),
            media=dict(default=None, required=False),
        ),
    )

    src = module.params['src']
    dest = module.params['dest']
    msg = module.params['msg']
    media = module.params['media']
    user_id = module.params['user_id']
    api_token = module.params['api_token']
    api_secret = module.params['api_secret']

    for number in dest:
        rc, info = send(module, src, number, msg, media, user_id, api_token, api_secret)
        if info["status"] != 201:
            body = json.loads(info["body"])
            fail_msg = body["message"]
            module.fail_json(msg=fail_msg)

    changed = True
    module.exit_json(changed=changed)

if __name__ == '__main__':
    main()