mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 14:22:46 +00:00
Reformat everything.
This commit is contained in:
@@ -132,36 +132,35 @@ from ansible_collections.community.general.plugins.module_utils.manageiq import
|
||||
|
||||
|
||||
class ManageIQAlert:
|
||||
""" Represent a ManageIQ alert. Can be initialized with both the format
|
||||
"""Represent a ManageIQ alert. Can be initialized with both the format
|
||||
we receive from the server and the format we get from the user.
|
||||
"""
|
||||
|
||||
def __init__(self, alert):
|
||||
self.description = alert['description']
|
||||
self.db = alert['db']
|
||||
self.enabled = alert['enabled']
|
||||
self.options = alert['options']
|
||||
self.description = alert["description"]
|
||||
self.db = alert["db"]
|
||||
self.enabled = alert["enabled"]
|
||||
self.options = alert["options"]
|
||||
self.hash_expression = None
|
||||
self.miq_expressipn = None
|
||||
|
||||
if 'hash_expression' in alert:
|
||||
self.hash_expression = alert['hash_expression']
|
||||
if 'miq_expression' in alert:
|
||||
self.miq_expression = alert['miq_expression']
|
||||
if 'exp' in self.miq_expression:
|
||||
if "hash_expression" in alert:
|
||||
self.hash_expression = alert["hash_expression"]
|
||||
if "miq_expression" in alert:
|
||||
self.miq_expression = alert["miq_expression"]
|
||||
if "exp" in self.miq_expression:
|
||||
# miq_expression is a field that needs a special case, because
|
||||
# it is returned surrounded by a dict named exp even though we don't
|
||||
# send it with that dict.
|
||||
self.miq_expression = self.miq_expression['exp']
|
||||
self.miq_expression = self.miq_expression["exp"]
|
||||
|
||||
def __eq__(self, other):
|
||||
""" Compare two ManageIQAlert objects
|
||||
"""
|
||||
"""Compare two ManageIQAlert objects"""
|
||||
return self.__dict__ == other.__dict__
|
||||
|
||||
|
||||
class ManageIQAlerts:
|
||||
""" Object to execute alert management operations in manageiq.
|
||||
"""
|
||||
"""Object to execute alert management operations in manageiq."""
|
||||
|
||||
def __init__(self, manageiq):
|
||||
self.manageiq = manageiq
|
||||
@@ -169,84 +168,80 @@ class ManageIQAlerts:
|
||||
self.module = self.manageiq.module
|
||||
self.api_url = self.manageiq.api_url
|
||||
self.client = self.manageiq.client
|
||||
self.alerts_url = f'{self.api_url}/alert_definitions'
|
||||
self.alerts_url = f"{self.api_url}/alert_definitions"
|
||||
|
||||
def get_alerts(self):
|
||||
""" Get all alerts from ManageIQ
|
||||
"""
|
||||
"""Get all alerts from ManageIQ"""
|
||||
try:
|
||||
response = self.client.get(f"{self.alerts_url}?expand=resources")
|
||||
except Exception as e:
|
||||
self.module.fail_json(msg=f"Failed to query alerts: {e}")
|
||||
return response.get('resources', [])
|
||||
return response.get("resources", [])
|
||||
|
||||
def validate_hash_expression(self, expression):
|
||||
""" Validate a 'hash expression' alert definition
|
||||
"""
|
||||
"""Validate a 'hash expression' alert definition"""
|
||||
# hash expressions must have the following fields
|
||||
for key in ['options', 'eval_method', 'mode']:
|
||||
for key in ["options", "eval_method", "mode"]:
|
||||
if key not in expression:
|
||||
msg = f"Hash expression is missing required field {key}"
|
||||
self.module.fail_json(msg)
|
||||
|
||||
def create_alert_dict(self, params):
|
||||
""" Create a dict representing an alert
|
||||
"""
|
||||
if params['expression_type'] == 'hash':
|
||||
"""Create a dict representing an alert"""
|
||||
if params["expression_type"] == "hash":
|
||||
# hash expression supports depends on https://github.com/ManageIQ/manageiq-api/pull/76
|
||||
self.validate_hash_expression(params['expression'])
|
||||
expression_type = 'hash_expression'
|
||||
self.validate_hash_expression(params["expression"])
|
||||
expression_type = "hash_expression"
|
||||
else:
|
||||
# actually miq_expression, but we call it "expression" for backwards-compatibility
|
||||
expression_type = 'expression'
|
||||
expression_type = "expression"
|
||||
|
||||
# build the alret
|
||||
alert = dict(description=params['description'],
|
||||
db=params['resource_type'],
|
||||
options=params['options'],
|
||||
enabled=params['enabled'])
|
||||
alert = dict(
|
||||
description=params["description"],
|
||||
db=params["resource_type"],
|
||||
options=params["options"],
|
||||
enabled=params["enabled"],
|
||||
)
|
||||
|
||||
# add the actual expression.
|
||||
alert.update({expression_type: params['expression']})
|
||||
alert.update({expression_type: params["expression"]})
|
||||
|
||||
return alert
|
||||
|
||||
def add_alert(self, alert):
|
||||
""" Add a new alert to ManageIQ
|
||||
"""
|
||||
"""Add a new alert to ManageIQ"""
|
||||
try:
|
||||
result = self.client.post(self.alerts_url, action='create', resource=alert)
|
||||
result = self.client.post(self.alerts_url, action="create", resource=alert)
|
||||
|
||||
msg = "Alert {description} created successfully: {details}"
|
||||
msg = msg.format(description=alert['description'], details=result)
|
||||
msg = msg.format(description=alert["description"], details=result)
|
||||
return dict(changed=True, msg=msg)
|
||||
except Exception as e:
|
||||
msg = "Creating alert {description} failed: {error}"
|
||||
if "Resource expression needs be specified" in str(e):
|
||||
# Running on an older version of ManageIQ and trying to create a hash expression
|
||||
msg = msg.format(description=alert['description'],
|
||||
error="Your version of ManageIQ does not support hash_expression")
|
||||
msg = msg.format(
|
||||
description=alert["description"], error="Your version of ManageIQ does not support hash_expression"
|
||||
)
|
||||
else:
|
||||
msg = msg.format(description=alert['description'], error=e)
|
||||
msg = msg.format(description=alert["description"], error=e)
|
||||
self.module.fail_json(msg=msg)
|
||||
|
||||
def delete_alert(self, alert):
|
||||
""" Delete an alert
|
||||
"""
|
||||
"""Delete an alert"""
|
||||
try:
|
||||
result = self.client.post(f"{self.alerts_url}/{alert['id']}",
|
||||
action="delete")
|
||||
result = self.client.post(f"{self.alerts_url}/{alert['id']}", action="delete")
|
||||
msg = "Alert {description} deleted: {details}"
|
||||
msg = msg.format(description=alert['description'], details=result)
|
||||
msg = msg.format(description=alert["description"], details=result)
|
||||
return dict(changed=True, msg=msg)
|
||||
except Exception as e:
|
||||
msg = "Deleting alert {description} failed: {error}"
|
||||
msg = msg.format(description=alert['description'], error=e)
|
||||
msg = msg.format(description=alert["description"], error=e)
|
||||
self.module.fail_json(msg=msg)
|
||||
|
||||
def update_alert(self, existing_alert, new_alert):
|
||||
""" Update an existing alert with the values from `new_alert`
|
||||
"""
|
||||
"""Update an existing alert with the values from `new_alert`"""
|
||||
new_alert_obj = ManageIQAlert(new_alert)
|
||||
if new_alert_obj == ManageIQAlert(existing_alert):
|
||||
# no change needed - alerts are identical
|
||||
@@ -261,13 +256,13 @@ class ManageIQAlerts:
|
||||
if new_alert_obj == ManageIQAlert(result):
|
||||
# success!
|
||||
msg = "Alert {description} updated successfully: {details}"
|
||||
msg = msg.format(description=existing_alert['description'], details=result)
|
||||
msg = msg.format(description=existing_alert["description"], details=result)
|
||||
|
||||
return dict(changed=True, msg=msg)
|
||||
else:
|
||||
# unexpected result
|
||||
msg = "Updating alert {description} failed, unexpected result {details}"
|
||||
msg = msg.format(description=existing_alert['description'], details=result)
|
||||
msg = msg.format(description=existing_alert["description"], details=result)
|
||||
|
||||
self.module.fail_json(msg=msg)
|
||||
|
||||
@@ -275,50 +270,55 @@ class ManageIQAlerts:
|
||||
msg = "Updating alert {description} failed: {error}"
|
||||
if "Resource expression needs be specified" in str(e):
|
||||
# Running on an older version of ManageIQ and trying to update a hash expression
|
||||
msg = msg.format(description=existing_alert['description'],
|
||||
error="Your version of ManageIQ does not support hash_expression")
|
||||
msg = msg.format(
|
||||
description=existing_alert["description"],
|
||||
error="Your version of ManageIQ does not support hash_expression",
|
||||
)
|
||||
else:
|
||||
msg = msg.format(description=existing_alert['description'], error=e)
|
||||
msg = msg.format(description=existing_alert["description"], error=e)
|
||||
self.module.fail_json(msg=msg)
|
||||
|
||||
|
||||
def main():
|
||||
argument_spec = dict(
|
||||
description=dict(type='str'),
|
||||
resource_type=dict(type='str', choices=['Vm',
|
||||
'ContainerNode',
|
||||
'MiqServer',
|
||||
'Host',
|
||||
'Storage',
|
||||
'EmsCluster',
|
||||
'ExtManagementSystem',
|
||||
'MiddlewareServer']),
|
||||
expression_type=dict(type='str', default='hash', choices=['miq', 'hash']),
|
||||
expression=dict(type='dict'),
|
||||
options=dict(type='dict'),
|
||||
enabled=dict(type='bool'),
|
||||
state=dict(default='present',
|
||||
choices=['present', 'absent']),
|
||||
description=dict(type="str"),
|
||||
resource_type=dict(
|
||||
type="str",
|
||||
choices=[
|
||||
"Vm",
|
||||
"ContainerNode",
|
||||
"MiqServer",
|
||||
"Host",
|
||||
"Storage",
|
||||
"EmsCluster",
|
||||
"ExtManagementSystem",
|
||||
"MiddlewareServer",
|
||||
],
|
||||
),
|
||||
expression_type=dict(type="str", default="hash", choices=["miq", "hash"]),
|
||||
expression=dict(type="dict"),
|
||||
options=dict(type="dict"),
|
||||
enabled=dict(type="bool"),
|
||||
state=dict(default="present", choices=["present", "absent"]),
|
||||
)
|
||||
# add the manageiq connection arguments to the arguments
|
||||
argument_spec.update(manageiq_argument_spec())
|
||||
|
||||
module = AnsibleModule(argument_spec=argument_spec,
|
||||
required_if=[('state', 'present', ['description',
|
||||
'resource_type',
|
||||
'expression',
|
||||
'enabled',
|
||||
'options']),
|
||||
('state', 'absent', ['description'])])
|
||||
module = AnsibleModule(
|
||||
argument_spec=argument_spec,
|
||||
required_if=[
|
||||
("state", "present", ["description", "resource_type", "expression", "enabled", "options"]),
|
||||
("state", "absent", ["description"]),
|
||||
],
|
||||
)
|
||||
|
||||
state = module.params['state']
|
||||
description = module.params['description']
|
||||
state = module.params["state"]
|
||||
description = module.params["description"]
|
||||
|
||||
manageiq = ManageIQ(module)
|
||||
manageiq_alerts = ManageIQAlerts(manageiq)
|
||||
|
||||
existing_alert = manageiq.find_collection_resource_by("alert_definitions",
|
||||
description=description)
|
||||
existing_alert = manageiq.find_collection_resource_by("alert_definitions", description=description)
|
||||
|
||||
# we need to add or update the alert
|
||||
if state == "present":
|
||||
|
||||
Reference in New Issue
Block a user