1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-23 04:09:04 +00:00

Reformat everything.

This commit is contained in:
Felix Fontein 2025-11-01 12:08:41 +01:00
parent 3f2213791a
commit 340ff8586d
1008 changed files with 61301 additions and 58309 deletions

View file

@ -161,38 +161,40 @@ from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.urls import fetch_url
ROCKETCHAT_INCOMING_WEBHOOK = '%s://%s/hooks/%s'
ROCKETCHAT_INCOMING_WEBHOOK = "%s://%s/hooks/%s"
def build_payload_for_rocketchat(module, text, channel, username, icon_url, icon_emoji, link_names, color, attachments, is_pre740):
def build_payload_for_rocketchat(
module, text, channel, username, icon_url, icon_emoji, link_names, color, attachments, is_pre740
):
payload = {}
if color == "normal" and text is not None:
payload = dict(text=text)
elif text is not None:
payload = dict(attachments=[dict(text=text, color=color)])
if channel is not None:
if channel[0] == '#' or channel[0] == '@':
payload['channel'] = channel
if channel[0] == "#" or channel[0] == "@":
payload["channel"] = channel
else:
payload['channel'] = f"#{channel}"
payload["channel"] = f"#{channel}"
if username is not None:
payload['username'] = username
payload["username"] = username
if icon_emoji is not None:
payload['icon_emoji'] = icon_emoji
payload["icon_emoji"] = icon_emoji
else:
payload['icon_url'] = icon_url
payload["icon_url"] = icon_url
if link_names is not None:
payload['link_names'] = link_names
payload["link_names"] = link_names
if attachments is not None:
if 'attachments' not in payload:
payload['attachments'] = []
if "attachments" not in payload:
payload["attachments"] = []
if attachments is not None:
for attachment in attachments:
if 'fallback' not in attachment:
attachment['fallback'] = attachment['text']
payload['attachments'].append(attachment)
if "fallback" not in attachment:
attachment["fallback"] = attachment["text"]
payload["attachments"].append(attachment)
payload = module.jsonify(payload)
if is_pre740:
@ -201,51 +203,50 @@ def build_payload_for_rocketchat(module, text, channel, username, icon_url, icon
def do_notify_rocketchat(module, domain, token, protocol, payload, is_pre740):
if token.count('/') < 1:
if token.count("/") < 1:
module.fail_json(msg="Invalid Token specified, provide a valid token")
rocketchat_incoming_webhook = ROCKETCHAT_INCOMING_WEBHOOK % (protocol, domain, token)
headers = None
if not is_pre740:
headers = {'Content-type': 'application/json'}
headers = {"Content-type": "application/json"}
response, info = fetch_url(module, rocketchat_incoming_webhook, data=payload, headers=headers)
if info['status'] != 200:
if info["status"] != 200:
module.fail_json(msg=f"failed to send message, return status={info['status']}")
def main():
module = AnsibleModule(
argument_spec=dict(
domain=dict(type='str', required=True),
token=dict(type='str', required=True, no_log=True),
protocol=dict(type='str', default='https', choices=['http', 'https']),
msg=dict(type='str'),
channel=dict(type='str'),
username=dict(type='str', default='Ansible'),
icon_url=dict(type='str', default='https://docs.ansible.com/favicon.ico'),
icon_emoji=dict(type='str'),
link_names=dict(type='int', default=1, choices=[0, 1]),
validate_certs=dict(default=True, type='bool'),
color=dict(type='str', default='normal', choices=['normal', 'good', 'warning', 'danger']),
attachments=dict(type='list', elements='dict'),
is_pre740=dict(type='bool')
domain=dict(type="str", required=True),
token=dict(type="str", required=True, no_log=True),
protocol=dict(type="str", default="https", choices=["http", "https"]),
msg=dict(type="str"),
channel=dict(type="str"),
username=dict(type="str", default="Ansible"),
icon_url=dict(type="str", default="https://docs.ansible.com/favicon.ico"),
icon_emoji=dict(type="str"),
link_names=dict(type="int", default=1, choices=[0, 1]),
validate_certs=dict(default=True, type="bool"),
color=dict(type="str", default="normal", choices=["normal", "good", "warning", "danger"]),
attachments=dict(type="list", elements="dict"),
is_pre740=dict(type="bool"),
)
)
domain = module.params['domain']
token = module.params['token']
protocol = module.params['protocol']
text = module.params['msg']
channel = module.params['channel']
username = module.params['username']
icon_url = module.params['icon_url']
icon_emoji = module.params['icon_emoji']
link_names = module.params['link_names']
color = module.params['color']
attachments = module.params['attachments']
is_pre740 = module.params['is_pre740']
domain = module.params["domain"]
token = module.params["token"]
protocol = module.params["protocol"]
text = module.params["msg"]
channel = module.params["channel"]
username = module.params["username"]
icon_url = module.params["icon_url"]
icon_emoji = module.params["icon_emoji"]
link_names = module.params["link_names"]
color = module.params["color"]
attachments = module.params["attachments"]
is_pre740 = module.params["is_pre740"]
if is_pre740 is None:
module.deprecate(
@ -256,11 +257,13 @@ def main():
)
is_pre740 = True
payload = build_payload_for_rocketchat(module, text, channel, username, icon_url, icon_emoji, link_names, color, attachments, is_pre740)
payload = build_payload_for_rocketchat(
module, text, channel, username, icon_url, icon_emoji, link_names, color, attachments, is_pre740
)
do_notify_rocketchat(module, domain, token, protocol, payload, is_pre740)
module.exit_json(msg="OK")
if __name__ == '__main__':
if __name__ == "__main__":
main()