Debian Codegraph @

ansible_collections/community/general/plugins/modules/grove.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: grove
11 short_description: Sends a notification to a grove.io channel
12 description:
13 - The C(grove) module sends a message for a service to a Grove.io channel.
14 extends_documentation_fragment:
15 - community.general._attributes
16 attributes:
17 check_mode:
18 support: none
19 diff_mode:
20 support: none
21 options:
22 channel_token:
23 type: str
24 description:
25 - Token of the channel to post to.
26 required: true
27 service:
28 type: str
29 description:
30 - Name of the service (displayed as the "user" in the message).
31 default: ansible
32 message_content:
33 type: str
34 description:
35 - Message content.
36 - The alias O(ignore:message) has been removed in community.general 4.0.0.
37 required: true
38 url:
39 type: str
40 description:
41 - Service URL for the web client.
42 icon_url:
43 type: str
44 description:
45 - Icon for the service.
46 validate_certs:
47 description:
48 - If V(false), SSL certificates are not validated. This should only be used on personally controlled sites using self-signed
49 certificates.
50 default: true
51 type: bool
52 author: "Jonas Pfenniger (@zimbatm)"
53 """
54
55 EXAMPLES = r"""
56 - name: Sends a notification to a grove.io channel
57 community.general.grove:
58 channel_token: 6Ph62VBBJOccmtTPZbubiPzdrhipZXtg
59 service: my-app
60 message: 'deployed {{ target }}'
61 """
62
63 from urllib.parse import urlencode
64
65 from ansible.module_utils.basic import AnsibleModule
66 from ansible.module_utils.urls import fetch_url
67
68 # ==============================================================
69 # do_notify_grove
70
71
72 def do_notify_grove(module, channel_token, service, message, url=None, icon_url=None):
73 my_url = f"https://grove.io/api/notice/{channel_token}/"
74
75 my_data = dict(service=service, message=message)
76 if url is not None:
77 my_data["url"] = url
78 if icon_url is not None:
79 my_data["icon_url"] = icon_url
80
81 data = urlencode(my_data)
82 response, info = fetch_url(module, my_url, data=data)
83 if info["status"] != 200:
84 module.fail_json(msg=f"failed to send notification: {info['msg']}")
85
86
87 # ==============================================================
88 # main
89
90
91 def main():
92 module = AnsibleModule(
93 argument_spec=dict(
94 channel_token=dict(type="str", required=True, no_log=True),
95 message_content=dict(type="str", required=True),
96 service=dict(type="str", default="ansible"),
97 url=dict(type="str"),
98 icon_url=dict(type="str"),
99 validate_certs=dict(default=True, type="bool"),
100 )
101 )
102
103 channel_token = module.params["channel_token"]
104 service = module.params["service"]
105 message = module.params["message_content"]
106 url = module.params["url"]
107 icon_url = module.params["icon_url"]
108
109 do_notify_grove(module, channel_token, service, message, url, icon_url)
110
111 # Mission complete
112 module.exit_json(msg="OK")
113
114
115 if __name__ == "__main__":
116 main()