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

@@ -425,7 +425,7 @@ from ansible_collections.community.general.plugins.module_utils.oneandone import
get_monitoring_policy,
get_server,
OneAndOneResources,
wait_for_resource_creation_completion
wait_for_resource_creation_completion,
)
HAS_ONEANDONE_SDK = True
@@ -438,9 +438,7 @@ except ImportError:
def _check_mode(module, result):
if module.check_mode:
module.exit_json(
changed=result
)
module.exit_json(changed=result)
def _add_ports(module, oneandone_conn, monitoring_policy_id, ports):
@@ -452,10 +450,10 @@ def _add_ports(module, oneandone_conn, monitoring_policy_id, ports):
for _port in ports:
monitoring_policy_port = oneandone.client.Port(
protocol=_port['protocol'],
port=_port['port'],
alert_if=_port['alert_if'],
email_notification=_port['email_notification']
protocol=_port["protocol"],
port=_port["port"],
alert_if=_port["alert_if"],
email_notification=_port["email_notification"],
)
monitoring_policy_ports.append(monitoring_policy_port)
@@ -465,8 +463,8 @@ def _add_ports(module, oneandone_conn, monitoring_policy_id, ports):
return False
monitoring_policy = oneandone_conn.add_port(
monitoring_policy_id=monitoring_policy_id,
ports=monitoring_policy_ports)
monitoring_policy_id=monitoring_policy_id, ports=monitoring_policy_ports
)
return monitoring_policy
except Exception as ex:
module.fail_json(msg=str(ex))
@@ -479,15 +477,15 @@ def _delete_monitoring_policy_port(module, oneandone_conn, monitoring_policy_id,
try:
if module.check_mode:
monitoring_policy = oneandone_conn.delete_monitoring_policy_port(
monitoring_policy_id=monitoring_policy_id,
port_id=port_id)
monitoring_policy_id=monitoring_policy_id, port_id=port_id
)
if monitoring_policy:
return True
return False
monitoring_policy = oneandone_conn.delete_monitoring_policy_port(
monitoring_policy_id=monitoring_policy_id,
port_id=port_id)
monitoring_policy_id=monitoring_policy_id, port_id=port_id
)
return monitoring_policy
except Exception as ex:
module.fail_json(msg=str(ex))
@@ -500,23 +498,22 @@ def _modify_port(module, oneandone_conn, monitoring_policy_id, port_id, port):
try:
if module.check_mode:
cm_port = oneandone_conn.get_monitoring_policy_port(
monitoring_policy_id=monitoring_policy_id,
port_id=port_id)
monitoring_policy_id=monitoring_policy_id, port_id=port_id
)
if cm_port:
return True
return False
monitoring_policy_port = oneandone.client.Port(
protocol=port['protocol'],
port=port['port'],
alert_if=port['alert_if'],
email_notification=port['email_notification']
protocol=port["protocol"],
port=port["port"],
alert_if=port["alert_if"],
email_notification=port["email_notification"],
)
monitoring_policy = oneandone_conn.modify_port(
monitoring_policy_id=monitoring_policy_id,
port_id=port_id,
port=monitoring_policy_port)
monitoring_policy_id=monitoring_policy_id, port_id=port_id, port=monitoring_policy_port
)
return monitoring_policy
except Exception as ex:
module.fail_json(msg=str(ex))
@@ -531,9 +528,9 @@ def _add_processes(module, oneandone_conn, monitoring_policy_id, processes):
for _process in processes:
monitoring_policy_process = oneandone.client.Process(
process=_process['process'],
alert_if=_process['alert_if'],
email_notification=_process['email_notification']
process=_process["process"],
alert_if=_process["alert_if"],
email_notification=_process["email_notification"],
)
monitoring_policy_processes.append(monitoring_policy_process)
@@ -544,8 +541,8 @@ def _add_processes(module, oneandone_conn, monitoring_policy_id, processes):
return False
monitoring_policy = oneandone_conn.add_process(
monitoring_policy_id=monitoring_policy_id,
processes=monitoring_policy_processes)
monitoring_policy_id=monitoring_policy_id, processes=monitoring_policy_processes
)
return monitoring_policy
except Exception as ex:
module.fail_json(msg=str(ex))
@@ -558,16 +555,15 @@ def _delete_monitoring_policy_process(module, oneandone_conn, monitoring_policy_
try:
if module.check_mode:
process = oneandone_conn.get_monitoring_policy_process(
monitoring_policy_id=monitoring_policy_id,
process_id=process_id
monitoring_policy_id=monitoring_policy_id, process_id=process_id
)
if process:
return True
return False
monitoring_policy = oneandone_conn.delete_monitoring_policy_process(
monitoring_policy_id=monitoring_policy_id,
process_id=process_id)
monitoring_policy_id=monitoring_policy_id, process_id=process_id
)
return monitoring_policy
except Exception as ex:
module.fail_json(msg=str(ex))
@@ -580,22 +576,19 @@ def _modify_process(module, oneandone_conn, monitoring_policy_id, process_id, pr
try:
if module.check_mode:
cm_process = oneandone_conn.get_monitoring_policy_process(
monitoring_policy_id=monitoring_policy_id,
process_id=process_id)
monitoring_policy_id=monitoring_policy_id, process_id=process_id
)
if cm_process:
return True
return False
monitoring_policy_process = oneandone.client.Process(
process=process['process'],
alert_if=process['alert_if'],
email_notification=process['email_notification']
process=process["process"], alert_if=process["alert_if"], email_notification=process["email_notification"]
)
monitoring_policy = oneandone_conn.modify_process(
monitoring_policy_id=monitoring_policy_id,
process_id=process_id,
process=monitoring_policy_process)
monitoring_policy_id=monitoring_policy_id, process_id=process_id, process=monitoring_policy_process
)
return monitoring_policy
except Exception as ex:
module.fail_json(msg=str(ex))
@@ -610,9 +603,7 @@ def _attach_monitoring_policy_server(module, oneandone_conn, monitoring_policy_i
for _server_id in servers:
server_id = get_server(oneandone_conn, _server_id)
attach_server = oneandone.client.AttachServer(
server_id=server_id
)
attach_server = oneandone.client.AttachServer(server_id=server_id)
attach_servers.append(attach_server)
if module.check_mode:
@@ -621,8 +612,8 @@ def _attach_monitoring_policy_server(module, oneandone_conn, monitoring_policy_i
return False
monitoring_policy = oneandone_conn.attach_monitoring_policy_server(
monitoring_policy_id=monitoring_policy_id,
servers=attach_servers)
monitoring_policy_id=monitoring_policy_id, servers=attach_servers
)
return monitoring_policy
except Exception as ex:
module.fail_json(msg=str(ex))
@@ -635,15 +626,15 @@ def _detach_monitoring_policy_server(module, oneandone_conn, monitoring_policy_i
try:
if module.check_mode:
mp_server = oneandone_conn.get_monitoring_policy_server(
monitoring_policy_id=monitoring_policy_id,
server_id=server_id)
monitoring_policy_id=monitoring_policy_id, server_id=server_id
)
if mp_server:
return True
return False
monitoring_policy = oneandone_conn.detach_monitoring_policy_server(
monitoring_policy_id=monitoring_policy_id,
server_id=server_id)
monitoring_policy_id=monitoring_policy_id, server_id=server_id
)
return monitoring_policy
except Exception as ex:
module.fail_json(msg=str(ex))
@@ -661,19 +652,19 @@ def update_monitoring_policy(module, oneandone_conn):
oneandone_conn: authenticated oneandone object
"""
try:
monitoring_policy_id = module.params.get('monitoring_policy')
name = module.params.get('name')
description = module.params.get('description')
email = module.params.get('email')
thresholds = module.params.get('thresholds')
add_ports = module.params.get('add_ports')
update_ports = module.params.get('update_ports')
remove_ports = module.params.get('remove_ports')
add_processes = module.params.get('add_processes')
update_processes = module.params.get('update_processes')
remove_processes = module.params.get('remove_processes')
add_servers = module.params.get('add_servers')
remove_servers = module.params.get('remove_servers')
monitoring_policy_id = module.params.get("monitoring_policy")
name = module.params.get("name")
description = module.params.get("description")
email = module.params.get("email")
thresholds = module.params.get("thresholds")
add_ports = module.params.get("add_ports")
update_ports = module.params.get("update_ports")
remove_ports = module.params.get("remove_ports")
add_processes = module.params.get("add_processes")
update_processes = module.params.get("update_processes")
remove_processes = module.params.get("remove_processes")
add_servers = module.params.get("add_servers")
remove_servers = module.params.get("remove_servers")
changed = False
@@ -681,16 +672,12 @@ def update_monitoring_policy(module, oneandone_conn):
if monitoring_policy is None:
_check_mode(module, False)
_monitoring_policy = oneandone.client.MonitoringPolicy(
name=name,
description=description,
email=email
)
_monitoring_policy = oneandone.client.MonitoringPolicy(name=name, description=description, email=email)
_thresholds = None
if thresholds:
threshold_entities = ['cpu', 'ram', 'disk', 'internal_ping', 'transfer']
threshold_entities = ["cpu", "ram", "disk", "internal_ping", "transfer"]
_thresholds = []
for threshold in thresholds:
@@ -698,70 +685,56 @@ def update_monitoring_policy(module, oneandone_conn):
if key in threshold_entities:
_threshold = oneandone.client.Threshold(
entity=key,
warning_value=threshold[key]['warning']['value'],
warning_alert=str(threshold[key]['warning']['alert']).lower(),
critical_value=threshold[key]['critical']['value'],
critical_alert=str(threshold[key]['critical']['alert']).lower())
warning_value=threshold[key]["warning"]["value"],
warning_alert=str(threshold[key]["warning"]["alert"]).lower(),
critical_value=threshold[key]["critical"]["value"],
critical_alert=str(threshold[key]["critical"]["alert"]).lower(),
)
_thresholds.append(_threshold)
if name or description or email or thresholds:
_check_mode(module, True)
monitoring_policy = oneandone_conn.modify_monitoring_policy(
monitoring_policy_id=monitoring_policy['id'],
monitoring_policy_id=monitoring_policy["id"],
monitoring_policy=_monitoring_policy,
thresholds=_thresholds)
thresholds=_thresholds,
)
changed = True
if add_ports:
if module.check_mode:
_check_mode(module, _add_ports(module,
oneandone_conn,
monitoring_policy['id'],
add_ports))
_check_mode(module, _add_ports(module, oneandone_conn, monitoring_policy["id"], add_ports))
monitoring_policy = _add_ports(module, oneandone_conn, monitoring_policy['id'], add_ports)
monitoring_policy = _add_ports(module, oneandone_conn, monitoring_policy["id"], add_ports)
changed = True
if update_ports:
chk_changed = False
for update_port in update_ports:
if module.check_mode:
chk_changed |= _modify_port(module,
oneandone_conn,
monitoring_policy['id'],
update_port['id'],
update_port)
chk_changed |= _modify_port(
module, oneandone_conn, monitoring_policy["id"], update_port["id"], update_port
)
_modify_port(module,
oneandone_conn,
monitoring_policy['id'],
update_port['id'],
update_port)
monitoring_policy = get_monitoring_policy(oneandone_conn, monitoring_policy['id'], True)
_modify_port(module, oneandone_conn, monitoring_policy["id"], update_port["id"], update_port)
monitoring_policy = get_monitoring_policy(oneandone_conn, monitoring_policy["id"], True)
changed = True
if remove_ports:
chk_changed = False
for port_id in remove_ports:
if module.check_mode:
chk_changed |= _delete_monitoring_policy_port(module,
oneandone_conn,
monitoring_policy['id'],
port_id)
chk_changed |= _delete_monitoring_policy_port(
module, oneandone_conn, monitoring_policy["id"], port_id
)
_delete_monitoring_policy_port(module,
oneandone_conn,
monitoring_policy['id'],
port_id)
_delete_monitoring_policy_port(module, oneandone_conn, monitoring_policy["id"], port_id)
_check_mode(module, chk_changed)
monitoring_policy = get_monitoring_policy(oneandone_conn, monitoring_policy['id'], True)
monitoring_policy = get_monitoring_policy(oneandone_conn, monitoring_policy["id"], True)
changed = True
if add_processes:
monitoring_policy = _add_processes(module,
oneandone_conn,
monitoring_policy['id'],
add_processes)
monitoring_policy = _add_processes(module, oneandone_conn, monitoring_policy["id"], add_processes)
_check_mode(module, monitoring_policy)
changed = True
@@ -769,43 +742,32 @@ def update_monitoring_policy(module, oneandone_conn):
chk_changed = False
for update_process in update_processes:
if module.check_mode:
chk_changed |= _modify_process(module,
oneandone_conn,
monitoring_policy['id'],
update_process['id'],
update_process)
chk_changed |= _modify_process(
module, oneandone_conn, monitoring_policy["id"], update_process["id"], update_process
)
_modify_process(module,
oneandone_conn,
monitoring_policy['id'],
update_process['id'],
update_process)
_modify_process(module, oneandone_conn, monitoring_policy["id"], update_process["id"], update_process)
_check_mode(module, chk_changed)
monitoring_policy = get_monitoring_policy(oneandone_conn, monitoring_policy['id'], True)
monitoring_policy = get_monitoring_policy(oneandone_conn, monitoring_policy["id"], True)
changed = True
if remove_processes:
chk_changed = False
for process_id in remove_processes:
if module.check_mode:
chk_changed |= _delete_monitoring_policy_process(module,
oneandone_conn,
monitoring_policy['id'],
process_id)
chk_changed |= _delete_monitoring_policy_process(
module, oneandone_conn, monitoring_policy["id"], process_id
)
_delete_monitoring_policy_process(module,
oneandone_conn,
monitoring_policy['id'],
process_id)
_delete_monitoring_policy_process(module, oneandone_conn, monitoring_policy["id"], process_id)
_check_mode(module, chk_changed)
monitoring_policy = get_monitoring_policy(oneandone_conn, monitoring_policy['id'], True)
monitoring_policy = get_monitoring_policy(oneandone_conn, monitoring_policy["id"], True)
changed = True
if add_servers:
monitoring_policy = _attach_monitoring_policy_server(module,
oneandone_conn,
monitoring_policy['id'],
add_servers)
monitoring_policy = _attach_monitoring_policy_server(
module, oneandone_conn, monitoring_policy["id"], add_servers
)
_check_mode(module, monitoring_policy)
changed = True
@@ -815,17 +777,13 @@ def update_monitoring_policy(module, oneandone_conn):
server_id = get_server(oneandone_conn, _server_id)
if module.check_mode:
chk_changed |= _detach_monitoring_policy_server(module,
oneandone_conn,
monitoring_policy['id'],
server_id)
chk_changed |= _detach_monitoring_policy_server(
module, oneandone_conn, monitoring_policy["id"], server_id
)
_detach_monitoring_policy_server(module,
oneandone_conn,
monitoring_policy['id'],
server_id)
_detach_monitoring_policy_server(module, oneandone_conn, monitoring_policy["id"], server_id)
_check_mode(module, chk_changed)
monitoring_policy = get_monitoring_policy(oneandone_conn, monitoring_policy['id'], True)
monitoring_policy = get_monitoring_policy(oneandone_conn, monitoring_policy["id"], True)
changed = True
return (changed, monitoring_policy)
@@ -841,25 +799,27 @@ def create_monitoring_policy(module, oneandone_conn):
oneandone_conn: authenticated oneandone object
"""
try:
name = module.params.get('name')
description = module.params.get('description')
email = module.params.get('email')
agent = module.params.get('agent')
thresholds = module.params.get('thresholds')
ports = module.params.get('ports')
processes = module.params.get('processes')
wait = module.params.get('wait')
wait_timeout = module.params.get('wait_timeout')
wait_interval = module.params.get('wait_interval')
name = module.params.get("name")
description = module.params.get("description")
email = module.params.get("email")
agent = module.params.get("agent")
thresholds = module.params.get("thresholds")
ports = module.params.get("ports")
processes = module.params.get("processes")
wait = module.params.get("wait")
wait_timeout = module.params.get("wait_timeout")
wait_interval = module.params.get("wait_interval")
_monitoring_policy = oneandone.client.MonitoringPolicy(name,
description,
email,
agent, )
_monitoring_policy = oneandone.client.MonitoringPolicy(
name,
description,
email,
agent,
)
_monitoring_policy.specs['agent'] = str(_monitoring_policy.specs['agent']).lower()
_monitoring_policy.specs["agent"] = str(_monitoring_policy.specs["agent"]).lower()
threshold_entities = ['cpu', 'ram', 'disk', 'internal_ping', 'transfer']
threshold_entities = ["cpu", "ram", "disk", "internal_ping", "transfer"]
_thresholds = []
for threshold in thresholds:
@@ -867,44 +827,45 @@ def create_monitoring_policy(module, oneandone_conn):
if key in threshold_entities:
_threshold = oneandone.client.Threshold(
entity=key,
warning_value=threshold[key]['warning']['value'],
warning_alert=str(threshold[key]['warning']['alert']).lower(),
critical_value=threshold[key]['critical']['value'],
critical_alert=str(threshold[key]['critical']['alert']).lower())
warning_value=threshold[key]["warning"]["value"],
warning_alert=str(threshold[key]["warning"]["alert"]).lower(),
critical_value=threshold[key]["critical"]["value"],
critical_alert=str(threshold[key]["critical"]["alert"]).lower(),
)
_thresholds.append(_threshold)
_ports = []
for port in ports:
_port = oneandone.client.Port(
protocol=port['protocol'],
port=port['port'],
alert_if=port['alert_if'],
email_notification=str(port['email_notification']).lower())
protocol=port["protocol"],
port=port["port"],
alert_if=port["alert_if"],
email_notification=str(port["email_notification"]).lower(),
)
_ports.append(_port)
_processes = []
for process in processes:
_process = oneandone.client.Process(
process=process['process'],
alert_if=process['alert_if'],
email_notification=str(process['email_notification']).lower())
process=process["process"],
alert_if=process["alert_if"],
email_notification=str(process["email_notification"]).lower(),
)
_processes.append(_process)
_check_mode(module, True)
monitoring_policy = oneandone_conn.create_monitoring_policy(
monitoring_policy=_monitoring_policy,
thresholds=_thresholds,
ports=_ports,
processes=_processes
monitoring_policy=_monitoring_policy, thresholds=_thresholds, ports=_ports, processes=_processes
)
if wait:
wait_for_resource_creation_completion(
oneandone_conn,
OneAndOneResources.monitoring_policy,
monitoring_policy['id'],
monitoring_policy["id"],
wait_timeout,
wait_interval)
wait_interval,
)
changed = True if monitoring_policy else False
@@ -923,7 +884,7 @@ def remove_monitoring_policy(module, oneandone_conn):
oneandone_conn: authenticated oneandone object
"""
try:
mp_id = module.params.get('name')
mp_id = module.params.get("name")
monitoring_policy_id = get_monitoring_policy(oneandone_conn, mp_id)
if module.check_mode:
if monitoring_policy_id is None:
@@ -933,10 +894,7 @@ def remove_monitoring_policy(module, oneandone_conn):
changed = True if monitoring_policy else False
return (changed, {
'id': monitoring_policy['id'],
'name': monitoring_policy['name']
})
return (changed, {"id": monitoring_policy["id"], "name": monitoring_policy["name"]})
except Exception as ex:
module.fail_json(msg=str(ex))
@@ -944,74 +902,66 @@ def remove_monitoring_policy(module, oneandone_conn):
def main():
module = AnsibleModule(
argument_spec=dict(
auth_token=dict(
type='str', no_log=True,
default=os.environ.get('ONEANDONE_AUTH_TOKEN')),
api_url=dict(
type='str',
default=os.environ.get('ONEANDONE_API_URL')),
name=dict(type='str'),
monitoring_policy=dict(type='str'),
agent=dict(type='str'),
email=dict(type='str'),
description=dict(type='str'),
thresholds=dict(type='list', elements="dict", default=[]),
ports=dict(type='list', elements="dict", default=[]),
processes=dict(type='list', elements="dict", default=[]),
add_ports=dict(type='list', elements="dict", default=[]),
update_ports=dict(type='list', elements="dict", default=[]),
remove_ports=dict(type='list', elements="str", default=[]),
add_processes=dict(type='list', elements="dict", default=[]),
update_processes=dict(type='list', elements="dict", default=[]),
remove_processes=dict(type='list', elements="str", default=[]),
add_servers=dict(type='list', elements="str", default=[]),
remove_servers=dict(type='list', elements="str", default=[]),
wait=dict(type='bool', default=True),
wait_timeout=dict(type='int', default=600),
wait_interval=dict(type='int', default=5),
state=dict(type='str', default='present', choices=['present', 'absent', 'update']),
auth_token=dict(type="str", no_log=True, default=os.environ.get("ONEANDONE_AUTH_TOKEN")),
api_url=dict(type="str", default=os.environ.get("ONEANDONE_API_URL")),
name=dict(type="str"),
monitoring_policy=dict(type="str"),
agent=dict(type="str"),
email=dict(type="str"),
description=dict(type="str"),
thresholds=dict(type="list", elements="dict", default=[]),
ports=dict(type="list", elements="dict", default=[]),
processes=dict(type="list", elements="dict", default=[]),
add_ports=dict(type="list", elements="dict", default=[]),
update_ports=dict(type="list", elements="dict", default=[]),
remove_ports=dict(type="list", elements="str", default=[]),
add_processes=dict(type="list", elements="dict", default=[]),
update_processes=dict(type="list", elements="dict", default=[]),
remove_processes=dict(type="list", elements="str", default=[]),
add_servers=dict(type="list", elements="str", default=[]),
remove_servers=dict(type="list", elements="str", default=[]),
wait=dict(type="bool", default=True),
wait_timeout=dict(type="int", default=600),
wait_interval=dict(type="int", default=5),
state=dict(type="str", default="present", choices=["present", "absent", "update"]),
),
supports_check_mode=True
supports_check_mode=True,
)
if not HAS_ONEANDONE_SDK:
module.fail_json(msg='1and1 required for this module')
module.fail_json(msg="1and1 required for this module")
if not module.params.get('auth_token'):
module.fail_json(
msg='auth_token parameter is required.')
if not module.params.get("auth_token"):
module.fail_json(msg="auth_token parameter is required.")
if not module.params.get('api_url'):
oneandone_conn = oneandone.client.OneAndOneService(
api_token=module.params.get('auth_token'))
if not module.params.get("api_url"):
oneandone_conn = oneandone.client.OneAndOneService(api_token=module.params.get("auth_token"))
else:
oneandone_conn = oneandone.client.OneAndOneService(
api_token=module.params.get('auth_token'), api_url=module.params.get('api_url'))
api_token=module.params.get("auth_token"), api_url=module.params.get("api_url")
)
state = module.params.get('state')
state = module.params.get("state")
if state == 'absent':
if not module.params.get('name'):
module.fail_json(
msg="'name' parameter is required to delete a monitoring policy.")
if state == "absent":
if not module.params.get("name"):
module.fail_json(msg="'name' parameter is required to delete a monitoring policy.")
try:
(changed, monitoring_policy) = remove_monitoring_policy(module, oneandone_conn)
except Exception as ex:
module.fail_json(msg=str(ex))
elif state == 'update':
if not module.params.get('monitoring_policy'):
module.fail_json(
msg="'monitoring_policy' parameter is required to update a monitoring policy.")
elif state == "update":
if not module.params.get("monitoring_policy"):
module.fail_json(msg="'monitoring_policy' parameter is required to update a monitoring policy.")
try:
(changed, monitoring_policy) = update_monitoring_policy(module, oneandone_conn)
except Exception as ex:
module.fail_json(msg=str(ex))
elif state == 'present':
for param in ('name', 'agent', 'email', 'thresholds', 'ports', 'processes'):
elif state == "present":
for param in ("name", "agent", "email", "thresholds", "ports", "processes"):
if not module.params.get(param):
module.fail_json(
msg=f"{param} parameter is required for a new monitoring policy.")
module.fail_json(msg=f"{param} parameter is required for a new monitoring policy.")
try:
(changed, monitoring_policy) = create_monitoring_policy(module, oneandone_conn)
except Exception as ex:
@@ -1020,5 +970,5 @@ def main():
module.exit_json(changed=changed, monitoring_policy=monitoring_policy)
if __name__ == '__main__':
if __name__ == "__main__":
main()