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

@@ -181,104 +181,113 @@ def main():
module = AnsibleModule(
supports_check_mode=True,
argument_spec=dict(
state=dict(type='str', choices=['present', 'absent'], default='present'),
name=dict(type='str', required=True),
type=dict(type='str', choices=['pipe', 'tcp', 'udp', 'transport', 'set']),
filter=dict(type='str'),
filters=dict(type='list', elements='str'),
severities=dict(type='list', elements='str'),
mutator=dict(type='str'),
timeout=dict(type='int', default=10),
handle_silenced=dict(type='bool', default=False),
handle_flapping=dict(type='bool', default=False),
command=dict(type='str'),
socket=dict(type='dict'),
pipe=dict(type='dict'),
handlers=dict(type='list', elements='str'),
state=dict(type="str", choices=["present", "absent"], default="present"),
name=dict(type="str", required=True),
type=dict(type="str", choices=["pipe", "tcp", "udp", "transport", "set"]),
filter=dict(type="str"),
filters=dict(type="list", elements="str"),
severities=dict(type="list", elements="str"),
mutator=dict(type="str"),
timeout=dict(type="int", default=10),
handle_silenced=dict(type="bool", default=False),
handle_flapping=dict(type="bool", default=False),
command=dict(type="str"),
socket=dict(type="dict"),
pipe=dict(type="dict"),
handlers=dict(type="list", elements="str"),
),
required_if=[
['state', 'present', ['type']],
['type', 'pipe', ['command']],
['type', 'tcp', ['socket']],
['type', 'udp', ['socket']],
['type', 'transport', ['pipe']],
['type', 'set', ['handlers']]
]
["state", "present", ["type"]],
["type", "pipe", ["command"]],
["type", "tcp", ["socket"]],
["type", "udp", ["socket"]],
["type", "transport", ["pipe"]],
["type", "set", ["handlers"]],
],
)
state = module.params['state']
name = module.params['name']
path = f'/etc/sensu/conf.d/handlers/{name}.json'
state = module.params["state"]
name = module.params["name"]
path = f"/etc/sensu/conf.d/handlers/{name}.json"
if state == 'absent':
if state == "absent":
if os.path.exists(path):
if module.check_mode:
msg = f'{path} would have been deleted'
msg = f"{path} would have been deleted"
module.exit_json(msg=msg, changed=True)
else:
try:
os.remove(path)
msg = f'{path} deleted successfully'
msg = f"{path} deleted successfully"
module.exit_json(msg=msg, changed=True)
except OSError as e:
msg = 'Exception when trying to delete {path}: {exception}'
module.fail_json(
msg=msg.format(path=path, exception=str(e)))
msg = "Exception when trying to delete {path}: {exception}"
module.fail_json(msg=msg.format(path=path, exception=str(e)))
else:
# Idempotency: it is okay if the file doesn't exist
msg = f'{path} already does not exist'
msg = f"{path} already does not exist"
module.exit_json(msg=msg)
# Build handler configuration from module arguments
config = {'handlers': {name: {}}}
args = ['type', 'filter', 'filters', 'severities', 'mutator', 'timeout',
'handle_silenced', 'handle_flapping', 'command', 'socket',
'pipe', 'handlers']
config = {"handlers": {name: {}}}
args = [
"type",
"filter",
"filters",
"severities",
"mutator",
"timeout",
"handle_silenced",
"handle_flapping",
"command",
"socket",
"pipe",
"handlers",
]
for arg in args:
if arg in module.params and module.params[arg] is not None:
config['handlers'][name][arg] = module.params[arg]
config["handlers"][name][arg] = module.params[arg]
# Load the current config, if there is one, so we can compare
current_config = None
try:
current_config = json.load(open(path, 'r'))
current_config = json.load(open(path, "r"))
except (IOError, ValueError):
# File either doesn't exist or it is invalid JSON
pass
if current_config is not None and current_config == config:
# Config is the same, let's not change anything
module.exit_json(msg='Handler configuration is already up to date',
config=config['handlers'][name],
file=path,
name=name)
module.exit_json(
msg="Handler configuration is already up to date", config=config["handlers"][name], file=path, name=name
)
# Validate that directory exists before trying to write to it
if not module.check_mode and not os.path.exists(os.path.dirname(path)):
try:
os.makedirs(os.path.dirname(path))
except OSError as e:
module.fail_json(msg=f'Unable to create {os.path.dirname(path)}: {e}')
module.fail_json(msg=f"Unable to create {os.path.dirname(path)}: {e}")
if module.check_mode:
module.exit_json(msg='Handler configuration would have been updated',
changed=True,
config=config['handlers'][name],
file=path,
name=name)
module.exit_json(
msg="Handler configuration would have been updated",
changed=True,
config=config["handlers"][name],
file=path,
name=name,
)
try:
with open(path, 'w') as handler:
with open(path, "w") as handler:
handler.write(json.dumps(config, indent=4))
module.exit_json(msg='Handler configuration updated',
changed=True,
config=config['handlers'][name],
file=path,
name=name)
module.exit_json(
msg="Handler configuration updated", changed=True, config=config["handlers"][name], file=path, name=name
)
except (OSError, IOError) as e:
module.fail_json(msg=f'Unable to write file {path}: {e}')
module.fail_json(msg=f"Unable to write file {path}: {e}")
if __name__ == '__main__':
if __name__ == "__main__":
main()