mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 22:02:50 +00:00
Reformat everything.
This commit is contained in:
@@ -109,14 +109,14 @@ from ansible.module_utils.urls import fetch_url
|
||||
|
||||
def query(module, url, check, subscription):
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
url = url + '/silenced'
|
||||
url = url + "/silenced"
|
||||
|
||||
request_data = {
|
||||
'check': check,
|
||||
'subscription': subscription,
|
||||
"check": check,
|
||||
"subscription": subscription,
|
||||
}
|
||||
|
||||
# Remove keys with None value
|
||||
@@ -124,15 +124,10 @@ def query(module, url, check, subscription):
|
||||
if v is None:
|
||||
del request_data[k]
|
||||
|
||||
response, info = fetch_url(
|
||||
module, url, method='GET',
|
||||
headers=headers, data=json.dumps(request_data)
|
||||
)
|
||||
response, info = fetch_url(module, url, method="GET", headers=headers, data=json.dumps(request_data))
|
||||
|
||||
if info['status'] == 500:
|
||||
module.fail_json(
|
||||
msg="Failed to query silence %s. Reason: %s" % (subscription, info)
|
||||
)
|
||||
if info["status"] == 500:
|
||||
module.fail_json(msg="Failed to query silence %s. Reason: %s" % (subscription, info))
|
||||
|
||||
try:
|
||||
json_out = json.loads(to_native(response.read()))
|
||||
@@ -146,10 +141,10 @@ def clear(module, url, check, subscription):
|
||||
# Test if silence exists before clearing
|
||||
(rc, out, changed) = query(module, url, check, subscription)
|
||||
|
||||
d = {i['subscription']: i['check'] for i in out}
|
||||
d = {i["subscription"]: i["check"] for i in out}
|
||||
subscription_exists = subscription in d
|
||||
if check and subscription_exists:
|
||||
exists = (check == d[subscription])
|
||||
exists = check == d[subscription]
|
||||
else:
|
||||
exists = subscription_exists
|
||||
|
||||
@@ -161,14 +156,14 @@ def clear(module, url, check, subscription):
|
||||
# module.check_mode is inherited from the AnsibleMOdule class
|
||||
if not module.check_mode:
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
url = url + '/silenced/clear'
|
||||
url = url + "/silenced/clear"
|
||||
|
||||
request_data = {
|
||||
'check': check,
|
||||
'subscription': subscription,
|
||||
"check": check,
|
||||
"subscription": subscription,
|
||||
}
|
||||
|
||||
# Remove keys with None value
|
||||
@@ -176,15 +171,10 @@ def clear(module, url, check, subscription):
|
||||
if v is None:
|
||||
del request_data[k]
|
||||
|
||||
response, info = fetch_url(
|
||||
module, url, method='POST',
|
||||
headers=headers, data=json.dumps(request_data)
|
||||
)
|
||||
response, info = fetch_url(module, url, method="POST", headers=headers, data=json.dumps(request_data))
|
||||
|
||||
if info['status'] != 204:
|
||||
module.fail_json(
|
||||
msg="Failed to silence %s. Reason: %s" % (subscription, info)
|
||||
)
|
||||
if info["status"] != 204:
|
||||
module.fail_json(msg="Failed to silence %s. Reason: %s" % (subscription, info))
|
||||
|
||||
try:
|
||||
json_out = json.loads(to_native(response.read()))
|
||||
@@ -195,44 +185,34 @@ def clear(module, url, check, subscription):
|
||||
return False, out, True
|
||||
|
||||
|
||||
def create(
|
||||
module, url, check, creator, expire,
|
||||
expire_on_resolve, reason, subscription):
|
||||
def create(module, url, check, creator, expire, expire_on_resolve, reason, subscription):
|
||||
(rc, out, changed) = query(module, url, check, subscription)
|
||||
for i in out:
|
||||
if i['subscription'] == subscription:
|
||||
if i["subscription"] == subscription:
|
||||
if (
|
||||
(check is None or check == i['check']) and
|
||||
(
|
||||
creator == '' or
|
||||
creator == i['creator']) and
|
||||
(
|
||||
reason == '' or
|
||||
reason == i['reason']) and
|
||||
(
|
||||
expire is None or expire == i['expire']) and
|
||||
(
|
||||
expire_on_resolve is None or
|
||||
expire_on_resolve == i['expire_on_resolve']
|
||||
)
|
||||
(check is None or check == i["check"])
|
||||
and (creator == "" or creator == i["creator"])
|
||||
and (reason == "" or reason == i["reason"])
|
||||
and (expire is None or expire == i["expire"])
|
||||
and (expire_on_resolve is None or expire_on_resolve == i["expire_on_resolve"])
|
||||
):
|
||||
return False, out, False
|
||||
|
||||
# module.check_mode is inherited from the AnsibleMOdule class
|
||||
if not module.check_mode:
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
url = url + '/silenced'
|
||||
url = url + "/silenced"
|
||||
|
||||
request_data = {
|
||||
'check': check,
|
||||
'creator': creator,
|
||||
'expire': expire,
|
||||
'expire_on_resolve': expire_on_resolve,
|
||||
'reason': reason,
|
||||
'subscription': subscription,
|
||||
"check": check,
|
||||
"creator": creator,
|
||||
"expire": expire,
|
||||
"expire_on_resolve": expire_on_resolve,
|
||||
"reason": reason,
|
||||
"subscription": subscription,
|
||||
}
|
||||
|
||||
# Remove keys with None value
|
||||
@@ -240,16 +220,10 @@ def create(
|
||||
if v is None:
|
||||
del request_data[k]
|
||||
|
||||
response, info = fetch_url(
|
||||
module, url, method='POST',
|
||||
headers=headers, data=json.dumps(request_data)
|
||||
)
|
||||
response, info = fetch_url(module, url, method="POST", headers=headers, data=json.dumps(request_data))
|
||||
|
||||
if info['status'] != 201:
|
||||
module.fail_json(
|
||||
msg="Failed to silence %s. Reason: %s" %
|
||||
(subscription, info['msg'])
|
||||
)
|
||||
if info["status"] != 201:
|
||||
module.fail_json(msg="Failed to silence %s. Reason: %s" % (subscription, info["msg"]))
|
||||
|
||||
try:
|
||||
json_out = json.loads(to_native(response.read()))
|
||||
@@ -265,32 +239,29 @@ def main():
|
||||
argument_spec=dict(
|
||||
check=dict(),
|
||||
creator=dict(),
|
||||
expire=dict(type='int'),
|
||||
expire_on_resolve=dict(type='bool'),
|
||||
expire=dict(type="int"),
|
||||
expire_on_resolve=dict(type="bool"),
|
||||
reason=dict(),
|
||||
state=dict(default='present', choices=['present', 'absent']),
|
||||
state=dict(default="present", choices=["present", "absent"]),
|
||||
subscription=dict(required=True),
|
||||
url=dict(default='http://127.0.01:4567'),
|
||||
url=dict(default="http://127.0.01:4567"),
|
||||
),
|
||||
supports_check_mode=True
|
||||
supports_check_mode=True,
|
||||
)
|
||||
|
||||
url = module.params['url']
|
||||
check = module.params['check']
|
||||
creator = module.params['creator']
|
||||
expire = module.params['expire']
|
||||
expire_on_resolve = module.params['expire_on_resolve']
|
||||
reason = module.params['reason']
|
||||
subscription = module.params['subscription']
|
||||
state = module.params['state']
|
||||
url = module.params["url"]
|
||||
check = module.params["check"]
|
||||
creator = module.params["creator"]
|
||||
expire = module.params["expire"]
|
||||
expire_on_resolve = module.params["expire_on_resolve"]
|
||||
reason = module.params["reason"]
|
||||
subscription = module.params["subscription"]
|
||||
state = module.params["state"]
|
||||
|
||||
if state == 'present':
|
||||
(rc, out, changed) = create(
|
||||
module, url, check, creator,
|
||||
expire, expire_on_resolve, reason, subscription
|
||||
)
|
||||
if state == "present":
|
||||
(rc, out, changed) = create(module, url, check, creator, expire, expire_on_resolve, reason, subscription)
|
||||
|
||||
if state == 'absent':
|
||||
if state == "absent":
|
||||
(rc, out, changed) = clear(module, url, check, subscription)
|
||||
|
||||
if rc != 0:
|
||||
@@ -298,5 +269,5 @@ def main():
|
||||
module.exit_json(msg="success", result=out, changed=changed)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user