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(required=False, 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'])]) 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) # we need to add or update the alert if state == "present": alert = manageiq_alerts.create_alert_dict(module.params) if not existing_alert: # an alert with this description doesn't exist yet, let's create it res_args = manageiq_alerts.add_alert(alert) else: # an alert with this description exists, we might need to update it res_args = manageiq_alerts.update_alert(existing_alert, alert) # this alert should not exist elif state == "absent": # if we have an alert with this description, delete it if existing_alert: res_args = manageiq_alerts.delete_alert(existing_alert) else: # it doesn't exist, and that's okay msg = "Alert '{description}' does not exist in ManageIQ" msg = msg.format(description=description) res_args = dict(changed=False, msg=msg) module.exit_json(**res_args)
def main(): argument_spec = dict( name=dict(type='str'), resource_type=dict(type='str', choices=[ 'Vm', 'ContainerNode', 'MiqServer', 'Host', 'Storage', 'EmsCluster', 'ExtManagementSystem', 'MiddlewareServer' ]), alerts=dict(type='list'), notes=dict(type='str'), 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', ['name', 'resource_type']), ('state', 'absent', ['name'])]) state = module.params['state'] name = module.params['name'] manageiq = ManageIQ(module) manageiq_alert_profiles = ManageIQAlertProfiles(manageiq) existing_profile = manageiq.find_collection_resource_by( "alert_definition_profiles", name=name) # we need to add or update the alert profile if state == "present": if not existing_profile: # a profile with this name doesn't exist yet, let's create it res_args = manageiq_alert_profiles.add_profile(module.params) else: # a profile with this name exists, we might need to update it res_args = manageiq_alert_profiles.update_profile( existing_profile, module.params) # this alert profile should not exist if state == "absent": # if we have an alert profile with this name, delete it if existing_profile: res_args = manageiq_alert_profiles.delete_profile(existing_profile) else: # This alert profile does not exist in ManageIQ, and that's okay msg = "Alert profile '{name}' does not exist in ManageIQ" msg = msg.format(name=name) res_args = dict(changed=False, msg=msg) module.exit_json(**res_args)
def main(): argument_spec = dict( name=dict(type='str'), resource_type=dict(type='str', choices=['Vm', 'ContainerNode', 'MiqServer', 'Host', 'Storage', 'EmsCluster', 'ExtManagementSystem', 'MiddlewareServer']), alerts=dict(type='list'), notes=dict(type='str'), 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', ['name', 'resource_type']), ('state', 'absent', ['name'])]) state = module.params['state'] name = module.params['name'] manageiq = ManageIQ(module) manageiq_alert_profiles = ManageIQAlertProfiles(manageiq) existing_profile = manageiq.find_collection_resource_by("alert_definition_profiles", name=name) # we need to add or update the alert profile if state == "present": if not existing_profile: # a profile with this name doesn't exist yet, let's create it res_args = manageiq_alert_profiles.add_profile(module.params) else: # a profile with this name exists, we might need to update it res_args = manageiq_alert_profiles.update_profile(existing_profile, module.params) # this alert profile should not exist if state == "absent": # if we have an alert profile with this name, delete it if existing_profile: res_args = manageiq_alert_profiles.delete_profile(existing_profile) else: # This alert profile does not exist in ManageIQ, and that's okay msg = "Alert profile '{name}' does not exist in ManageIQ" msg = msg.format(name=name) res_args = dict(changed=False, msg=msg) module.exit_json(**res_args)
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(require=False, 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'])]) 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) # we need to add or update the alert if state == "present": alert = manageiq_alerts.create_alert_dict(module.params) if not existing_alert: # an alert with this description doesn't exist yet, let's create it res_args = manageiq_alerts.add_alert(alert) else: # an alert with this description exists, we might need to update it res_args = manageiq_alerts.update_alert(existing_alert, alert) # this alert should not exist elif state == "absent": # if we have an alert with this description, delete it if existing_alert: res_args = manageiq_alerts.delete_alert(existing_alert) else: # it doesn't exist, and that's okay msg = "Alert '{description}' does not exist in ManageIQ" msg = msg.format(description=description) res_args = dict(changed=False, msg=msg) module.exit_json(**res_args)