Debian Codegraph @

ansible_collections/community/general/plugins/modules/campfire.py

raw
1 #!/usr/bin/python
2 #
3 # Copyright Ansible Project
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: campfire
11 short_description: Send a message to Campfire
12 description:
13 - Send a message to Campfire.
14 - Messages with newlines result in a "Paste" message being sent.
15 extends_documentation_fragment:
16 - community.general._attributes
17 attributes:
18 check_mode:
19 support: none
20 diff_mode:
21 support: none
22 options:
23 subscription:
24 type: str
25 description:
26 - The subscription name to use.
27 required: true
28 token:
29 type: str
30 description:
31 - API token.
32 required: true
33 room:
34 type: str
35 description:
36 - Room number to which the message should be sent.
37 required: true
38 msg:
39 type: str
40 description:
41 - The message body.
42 required: true
43 notify:
44 type: str
45 description:
46 - Send a notification sound before the message.
47 choices:
48 - 56k
49 - bell
50 - bezos
51 - bueller
52 - clowntown
53 - cottoneyejoe
54 - crickets
55 - dadgummit
56 - dangerzone
57 - danielsan
58 - deeper
59 - drama
60 - greatjob
61 - greyjoy
62 - guarantee
63 - heygirl
64 - horn
65 - horror
66 - inconceivable
67 - live
68 - loggins
69 - makeitso
70 - noooo
71 - nyan
72 - ohmy
73 - ohyeah
74 - pushit
75 - rimshot
76 - rollout
77 - rumble
78 - sax
79 - secret
80 - sexyback
81 - story
82 - tada
83 - tmyk
84 - trololo
85 - trombone
86 - unix
87 - vuvuzela
88 - what
89 - whoomp
90 - yeah
91 - yodel
92
93 # informational: requirements for nodes
94 requirements: []
95 author: "Adam Garside (@fabulops)"
96 """
97
98 EXAMPLES = r"""
99 - name: Send a message to Campfire
100 community.general.campfire:
101 subscription: foo
102 token: 12345
103 room: 123
104 msg: Task completed.
105
106 - name: Send a message to Campfire
107 community.general.campfire:
108 subscription: foo
109 token: 12345
110 room: 123
111 notify: loggins
112 msg: Task completed ... with feeling.
113 """
114
115 from html import escape as html_escape
116
117 from ansible.module_utils.basic import AnsibleModule
118 from ansible.module_utils.urls import fetch_url
119
120
121 def main():
122 module = AnsibleModule(
123 argument_spec=dict(
124 subscription=dict(required=True),
125 token=dict(required=True, no_log=True),
126 room=dict(required=True),
127 msg=dict(required=True),
128 notify=dict(
129 choices=[
130 "56k",
131 "bell",
132 "bezos",
133 "bueller",
134 "clowntown",
135 "cottoneyejoe",
136 "crickets",
137 "dadgummit",
138 "dangerzone",
139 "danielsan",
140 "deeper",
141 "drama",
142 "greatjob",
143 "greyjoy",
144 "guarantee",
145 "heygirl",
146 "horn",
147 "horror",
148 "inconceivable",
149 "live",
150 "loggins",
151 "makeitso",
152 "noooo",
153 "nyan",
154 "ohmy",
155 "ohyeah",
156 "pushit",
157 "rimshot",
158 "rollout",
159 "rumble",
160 "sax",
161 "secret",
162 "sexyback",
163 "story",
164 "tada",
165 "tmyk",
166 "trololo",
167 "trombone",
168 "unix",
169 "vuvuzela",
170 "what",
171 "whoomp",
172 "yeah",
173 "yodel",
174 ]
175 ),
176 ),
177 supports_check_mode=False,
178 )
179
180 subscription = module.params["subscription"]
181 token = module.params["token"]
182 room = module.params["room"]
183 msg = module.params["msg"]
184 notify = module.params["notify"]
185
186 URI = f"https://{subscription}.campfirenow.com"
187 AGENT = "Ansible/1.2"
188
189 # Hack to add basic auth username and password the way fetch_url expects
190 module.params["url_username"] = token
191 module.params["url_password"] = "X"
192
193 target_url = f"{URI}/room/{room}/speak.xml"
194 headers = {"Content-Type": "application/xml", "User-agent": AGENT}
195
196 # Send some audible notification if requested
197 if notify:
198 response, info = fetch_url(
199 module,
200 target_url,
201 data=f"<message><type>SoundMessage</type><body>{html_escape(notify)}</body></message>",
202 headers=headers,
203 )
204 if info["status"] not in [200, 201]:
205 module.fail_json(
206 msg=f"unable to send msg: '{notify}', campfire api returned error code: '{info['status']}'"
207 )
208
209 # Send the message
210 response, info = fetch_url(
211 module, target_url, data=f"<message><body>{html_escape(msg)}</body></message>", headers=headers
212 )
213 if info["status"] not in [200, 201]:
214 module.fail_json(msg=f"unable to send msg: '{msg}', campfire api returned error code: '{info['status']}'")
215
216 module.exit_json(changed=True, room=room, msg=msg, notify=notify)
217
218
219 if __name__ == "__main__":
220 main()