1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-13 07:25:10 +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

@ -149,75 +149,64 @@ class icinga2_api:
def __init__(self, module):
self.module = module
def call_url(self, path, data='', method='GET'):
def call_url(self, path, data="", method="GET"):
headers = {
'Accept': 'application/json',
'X-HTTP-Method-Override': method,
"Accept": "application/json",
"X-HTTP-Method-Override": method,
}
url = f"{self.module.params.get('url')}/{path}"
rsp, info = fetch_url(module=self.module, url=url, data=data, headers=headers, method=method, use_proxy=self.module.params['use_proxy'])
body = ''
rsp, info = fetch_url(
module=self.module,
url=url,
data=data,
headers=headers,
method=method,
use_proxy=self.module.params["use_proxy"],
)
body = ""
if rsp:
body = json.loads(rsp.read())
if info['status'] >= 400:
body = info['body']
return {'code': info['status'], 'data': body}
if info["status"] >= 400:
body = info["body"]
return {"code": info["status"], "data": body}
def check_connection(self):
ret = self.call_url('v1/status')
if ret['code'] == 200:
ret = self.call_url("v1/status")
if ret["code"] == 200:
return True
return False
def exists(self, hostname):
data = {
"filter": f"match(\"{hostname}\", host.name)",
"filter": f'match("{hostname}", host.name)',
}
ret = self.call_url(
path="v1/objects/hosts",
data=self.module.jsonify(data)
)
if ret['code'] == 200:
if len(ret['data']['results']) == 1:
ret = self.call_url(path="v1/objects/hosts", data=self.module.jsonify(data))
if ret["code"] == 200:
if len(ret["data"]["results"]) == 1:
return True
return False
def create(self, hostname, data):
ret = self.call_url(
path=f"v1/objects/hosts/{hostname}",
data=self.module.jsonify(data),
method="PUT"
)
ret = self.call_url(path=f"v1/objects/hosts/{hostname}", data=self.module.jsonify(data), method="PUT")
return ret
def delete(self, hostname):
data = {"cascade": 1}
ret = self.call_url(
path=f"v1/objects/hosts/{hostname}",
data=self.module.jsonify(data),
method="DELETE"
)
ret = self.call_url(path=f"v1/objects/hosts/{hostname}", data=self.module.jsonify(data), method="DELETE")
return ret
def modify(self, hostname, data):
ret = self.call_url(
path=f"v1/objects/hosts/{hostname}",
data=self.module.jsonify(data),
method="POST"
)
ret = self.call_url(path=f"v1/objects/hosts/{hostname}", data=self.module.jsonify(data), method="POST")
return ret
def diff(self, hostname, data):
ret = self.call_url(
path=f"v1/objects/hosts/{hostname}",
method="GET"
)
ret = self.call_url(path=f"v1/objects/hosts/{hostname}", method="GET")
changed = False
ic_data = ret['data']['results'][0]
for key in data['attrs']:
if key not in ic_data['attrs'].keys():
ic_data = ret["data"]["results"][0]
for key in data["attrs"]:
if key not in ic_data["attrs"].keys():
changed = True
elif data['attrs'][key] != ic_data['attrs'][key]:
elif data["attrs"][key] != ic_data["attrs"][key]:
changed = True
return changed
@ -231,20 +220,17 @@ def main():
# add our own arguments
argument_spec.update(
state=dict(default="present", choices=["absent", "present"]),
name=dict(required=True, aliases=['host']),
name=dict(required=True, aliases=["host"]),
zone=dict(),
template=dict(),
check_command=dict(default="hostalive"),
display_name=dict(),
ip=dict(),
variables=dict(type='dict'),
variables=dict(type="dict"),
)
# Define the main module
module = AnsibleModule(
argument_spec=argument_spec,
supports_check_mode=True
)
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
state = module.params["state"]
name = module.params["name"]
@ -266,16 +252,16 @@ def main():
module.fail_json(msg=f"unable to connect to Icinga. Exception message: {e}")
data = {
'templates': template,
'attrs': {
'address': ip,
'display_name': display_name,
'check_command': check_command,
'zone': zone,
'vars.made_by': "ansible"
}
"templates": template,
"attrs": {
"address": ip,
"display_name": display_name,
"check_command": check_command,
"zone": zone,
"vars.made_by": "ansible",
},
}
data['attrs'].update({f"vars.{key}": value for key, value in variables.items()})
data["attrs"].update({f"vars.{key}": value for key, value in variables.items()})
changed = False
if icinga.exists(name):
@ -285,7 +271,7 @@ def main():
else:
try:
ret = icinga.delete(name)
if ret['code'] == 200:
if ret["code"] == 200:
changed = True
else:
module.fail_json(msg=f"bad return code ({ret['code']}) deleting host: '{ret['data']}'")
@ -297,11 +283,11 @@ def main():
module.exit_json(changed=False, name=name, data=data)
# Template attribute is not allowed in modification
del data['templates']
del data["templates"]
ret = icinga.modify(name, data)
if ret['code'] == 200:
if ret["code"] == 200:
changed = True
else:
module.fail_json(msg=f"bad return code ({ret['code']}) modifying host: '{ret['data']}'")
@ -313,7 +299,7 @@ def main():
else:
try:
ret = icinga.create(name, data)
if ret['code'] == 200:
if ret["code"] == 200:
changed = True
else:
module.fail_json(msg=f"bad return code ({ret['code']}) creating host: '{ret['data']}'")
@ -324,5 +310,5 @@ def main():
# import module snippets
if __name__ == '__main__':
if __name__ == "__main__":
main()