
    Vh^                       d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZ d dlmZ d dlmc mc mc mc mZ  G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona&k  
---
module: zabbix_action

short_description: Create/Delete/Update Zabbix actions


description:
    - This module allows you to create, modify and delete Zabbix actions.

author:
    - Ruben Tsirunyan (@rubentsirunyan)
    - Ruben Harutyunov (@K-DOT)

requirements:
    - "python >= 3.9"

options:
    name:
        type: str
        description:
            - Name of the action
        required: true
    event_source:
        type: str
        description:
            - Type of events that the action will handle.
            - Required when C(state=present).
        required: false
        choices: ["trigger", "discovery", "auto_registration", "internal"]
    state:
        type: str
        description:
            - State of the action.
            - On C(present), it will create an action if it does not exist or update the action if the associated data is different.
            - On C(absent), it will remove the action if it exists.
        choices: ["present", "absent"]
        default: "present"
    status:
        type: str
        description:
            - Status of the action.
        choices: ["enabled", "disabled"]
        default: "enabled"
    pause_in_maintenance:
        description:
            - Whether to pause escalation during maintenance periods or not.
            - Can be used when I(event_source=trigger).
        type: "bool"
        default: true
    notify_if_canceled:
        description:
            - Weather to notify when escalation is canceled.
            - Can be used when I(event_source=trigger).
        type: "bool"
        default: true
    esc_period:
        type: str
        description:
            - Default operation step duration. Must be greater than 60 seconds.
            - Accepts seconds, time unit with suffix and user macro since => Zabbix 3.4
            - Required when C(state=present).
        required: false
    conditions:
        type: list
        elements: dict
        description:
            - List of conditions to use for filtering results.
            - For more information about suboptions of this option please
              check out Zabbix API documentation U(https://www.zabbix.com/documentation/5.0/manual/api/reference/action/object#action_filter_condition)
        default: []
        suboptions:
            type:
                type: str
                description:
                    - Type (label) of the condition.
                    - "Possible values when I(event_source=trigger):"
                    - " - C(host_group)"
                    - " - C(host)"
                    - " - C(trigger)"
                    - " - C(trigger_name)"
                    - " - C(trigger_severity)"
                    - " - C(time_period)"
                    - " - C(host_template)"
                    - " - C(maintenance_status) known in Zabbix 4.0 and above as 'Problem is suppressed'"
                    - " - C(event_tag)"
                    - " - C(event_tag_value)"
                    - "Possible values when I(event_source=discovery):"
                    - " - C(host_IP)"
                    - " - C(discovered_service_type)"
                    - " - C(discovered_service_port)"
                    - " - C(discovery_status)"
                    - " - C(uptime_or_downtime_duration)"
                    - " - C(received_value)"
                    - " - C(discovery_rule)"
                    - " - C(discovery_check)"
                    - " - C(proxy)"
                    - " - C(discovery_object)"
                    - "Possible values when I(event_source=auto_registration):"
                    - " - C(proxy)"
                    - " - C(host_name)"
                    - " - C(host_metadata)"
                    - "Possible values when I(event_source=internal):"
                    - " - C(host_group)"
                    - " - C(host)"
                    - " - C(host_template)"
                    - " - C(event_type)"
                required: true
            value:
                type: str
                description:
                    - Value to compare with.
                    - "When I(type=discovery_status), the choices are:"
                    - " - C(up)"
                    - " - C(down)"
                    - " - C(discovered)"
                    - " - C(lost)"
                    - "When I(type=discovery_object), the choices are:"
                    - " - C(host)"
                    - " - C(service)"
                    - "When I(type=event_type), the choices are:"
                    - " - C(item in not supported state)"
                    - " - C(item in normal state)"
                    - " - C(LLD rule in not supported state)"
                    - " - C(LLD rule in normal state)"
                    - " - C(trigger in unknown state)"
                    - " - C(trigger in normal state)"
                    - "When I(type=trigger_severity), the choices are (case-insensitive):"
                    - " - C(not classified)"
                    - " - C(information)"
                    - " - C(warning)"
                    - " - C(average)"
                    - " - C(high)"
                    - " - C(disaster)"
                    - Irrespective of user-visible names being changed in Zabbix. Defaults to C(not classified) if omitted.
                    - Besides the above options, this is usually either the name
                      of the object or a string to compare with.
            value2:
                type: str
                description:
                    - Secondary value to compare with.
                    - Required for trigger actions when condition I(type=event_tag_value).
            operator:
                type: str
                description:
                    - Condition operator.
                    - When I(type) is set to C(time_period), the choices are C(in), C(not in).
                choices:
                    - "equals"
                    - "="
                    - "does not equal"
                    - "<>"
                    - "contains"
                    - "like"
                    - "does not contain"
                    - "not like"
                    - "in"
                    - "is greater than or equals"
                    - ">="
                    - "is less than or equals"
                    - "<="
                    - "not in"
                    - "matches"
                    - "does not match"
                    - "Yes"
                    - "No"
                required: true
            formulaid:
                type: str
                description:
                    - Arbitrary unique ID that is used to reference the condition from a custom expression.
                    - Can only contain upper-case letters.
                    - Required for custom expression filters and ignored otherwise.
    eval_type:
        type: str
        description:
            - Filter condition evaluation method.
            - Defaults to C(andor) if conditions are less then 2 or if
              I(formula) is not specified.
            - Defaults to C(custom_expression) when formula is specified.
        choices:
            - "andor"
            - "and"
            - "or"
            - "custom_expression"
    formula:
        type: str
        description:
            - User-defined expression to be used for evaluating conditions with a custom expression.
            - The expression must contain IDs that reference each condition by its formulaid.
            - The IDs used in the expression must exactly match the ones
              defined in the I(conditions). No condition can remain unused or omitted.
            - Required when I(eval_type=custom_expression).
            - Use sequential IDs that start at "A". If non-sequential IDs are used, Zabbix re-indexes them.
              This makes each module run notice the difference in IDs and update the action.
    operations:
        type: list
        elements: dict
        description:
            - List of action operations
        default: []
        suboptions:
            type:
                type: str
                description:
                    - Type of operation.
                    - "Valid choices when setting type for I(recovery_operations) and I(acknowledge_operations):"
                    - " - C(send_message)"
                    - " - C(remote_command)"
                    - " - C(notify_all_involved)"
                    - Choice C(notify_all_involved) only supported in I(recovery_operations) and I(acknowledge_operations).
                    - C(add_host_tags) and C(remove_host_tags) available since Zabbix 7.0.
                choices:
                    - send_message
                    - remote_command
                    - add_host
                    - remove_host
                    - add_to_host_group
                    - remove_from_host_group
                    - link_to_template
                    - unlink_from_template
                    - enable_host
                    - disable_host
                    - set_host_inventory_mode
                    - notify_all_involved
                    - add_host_tags
                    - remove_host_tags
                required: true
            esc_period:
                type: str
                description:
                    - Duration of an escalation step in seconds.
                    - Must be greater than 60 seconds.
                    - Accepts seconds, time unit with suffix and user macro.
                    - If set to 0 or 0s, the default action escalation period will be used.
                default: 0s
            esc_step_from:
                type: int
                description:
                    - Step to start escalation from.
                default: 1
            esc_step_to:
                type: int
                description:
                    - Step to end escalation at.
                    - Specify 0 for infinitely.
                default: 1
            send_to_groups:
                type: list
                elements: str
                description:
                    - User groups to send messages to.
            send_to_users:
                type: list
                elements: str
                description:
                    - Users (usernames or aliases) to send messages to.
            op_message:
                type: str
                description:
                    - Operation message text.
                    - If I(op_message) and I(subject) not defined then "default message" from media type will be used
            subject:
                type: str
                description:
                    - Operation message subject.
                    - If I(op_message) and I(subject) not defined then "default message" from media type will be used
            media_type:
                type: str
                description:
                    - Media type that will be used to send the message.
                    - Can be used with I(type=send_message) or I(type=notify_all_involved) inside I(acknowledge_operations).
                    - Set to C(all) for all media types
                default: "all"
            operation_condition:
                type: "str"
                description:
                    - The action operation condition object defines a condition that must be met to perform the current operation.
                choices:
                    - acknowledged
                    - not_acknowledged
            host_groups:
                type: list
                elements: str
                description:
                    - List of host groups host should be added to.
                    - Required when I(type=add_to_host_group) or I(type=remove_from_host_group).
            templates:
                type: list
                elements: str
                description:
                    - List of templates host should be linked to.
                    - Required when I(type=link_to_template) or I(type=unlink_from_template).
            inventory:
                type: str
                description:
                    - Host inventory mode.
                    - Required when I(type=set_host_inventory_mode).
                choices:
                    - manual
                    - automatic
            command_type:
                type: str
                description:
                    - Type of operation command.
                    - Required when I(type=remote_command).
                choices:
                    - custom_script
                    - ipmi
                    - ssh
                    - telnet
                    - global_script
            command:
                type: str
                description:
                    - Command to run.
                    - Required when I(type=remote_command) and I(command_type!=global_script).
            execute_on:
                type: str
                description:
                    - Target on which the custom script operation command will be executed.
                    - Required when I(type=remote_command) and I(command_type=custom_script).
                choices:
                    - agent
                    - server
                    - proxy
            run_on_groups:
                type: list
                elements: str
                description:
                    - Host groups to run remote commands on.
                    - Required when I(type=remote_command) and I(run_on_hosts) is not set.
            run_on_hosts:
                type: list
                elements: str
                description:
                    - Hosts to run remote commands on.
                    - Required when I(type=remote_command) and I(run_on_groups) is not set.
                    - If set to 0 the command will be run on the current host.
            ssh_auth_type:
                type: str
                description:
                    - Authentication method used for SSH commands.
                    - Required when I(type=remote_command) and I(command_type=ssh).
                choices:
                    - password
                    - public_key
            ssh_privatekey_file:
                type: str
                description:
                    - Name of the private key file used for SSH commands with public key authentication.
                    - Required when I(ssh_auth_type=public_key).
                    - Can be used when I(type=remote_command).
            ssh_publickey_file:
                type: str
                description:
                    - Name of the public key file used for SSH commands with public key authentication.
                    - Required when I(ssh_auth_type=public_key).
                    - Can be used when I(type=remote_command).
            username:
                type: str
                description:
                    - User name used for authentication.
                    - Required when I(ssh_auth_type in [public_key, password]) or I(command_type=telnet).
                    - Can be used when I(type=remote_command).
            password:
                type: str
                description:
                    - Password used for authentication.
                    - Required when I(ssh_auth_type=password) or I(command_type=telnet).
                    - Can be used when I(type=remote_command).
            port:
                type: int
                description:
                    - Port number used for authentication.
                    - Can be used when I(command_type in [ssh, telnet]) and I(type=remote_command).
            script_name:
                type: str
                description:
                    - The name of script used for global script commands.
                    - Required when I(command_type=global_script).
                    - Can be used when I(type=remote_command).
            tags:
                type: list
                elements: dict
                description:
                    - Host tags to adt have tag property defined.
                    - The value property is optional. or remove.
                    - upported if operationtype is set to C(add host tags) or C(remove host tags).
                suboptions:
                    tag:
                      type: str
                      description: Tag name
                    value:
                      type: str
                      description: Tag value (optional)
    recovery_operations:
        type: list
        elements: dict
        description:
            - List of recovery operations.
            - C(Suboptions) are the same as for I(operations).
        default: []
        suboptions:
            type:
                type: str
                description:
                    - Type of operation.
                choices:
                    - send_message
                    - remote_command
                    - notify_all_involved
                required: true
            command_type:
                type: str
                required: false
                description:
                    - Type of operation command.
                choices:
                    - custom_script
                    - ipmi
                    - ssh
                    - telnet
                    - global_script
            command:
                type: str
                required: false
                description:
                    - Command to run.
            execute_on:
                type: str
                required: false
                description:
                    - Target on which the custom script operation command will be executed.
                choices:
                    - agent
                    - server
                    - proxy
            ssh_auth_type:
                type: str
                description:
                    - Authentication method used for SSH commands.
                    - Required when I(type=remote_command) and I(command_type=ssh).
                choices:
                    - password
                    - public_key
            ssh_privatekey_file:
                type: str
                description:
                    - Name of the private key file used for SSH commands with public key authentication.
                    - Required when I(ssh_auth_type=public_key).
                    - Can be used when I(type=remote_command).
            ssh_publickey_file:
                type: str
                description:
                    - Name of the public key file used for SSH commands with public key authentication.
                    - Required when I(ssh_auth_type=public_key).
                    - Can be used when I(type=remote_command).
            run_on_groups:
                type: list
                elements: str
                description:
                    - Host groups to run remote commands on.
                    - Required when I(type=remote_command) and I(run_on_hosts) is not set.
            run_on_hosts:
                type: list
                elements: str
                description:
                    - Hosts to run remote commands on.
                    - Required when I(type=remote_command) and I(run_on_groups) is not set.
                    - If set to 0 the command will be run on the current host.
            send_to_groups:
                type: list
                elements: str
                description:
                    - User groups to send messages to.
            send_to_users:
                type: list
                elements: str
                description:
                    - Users (usernames or aliases) to send messages to.
            media_type:
                type: str
                description:
                    - Media type that will be used to send the message.
                    - Can be used with I(type=send_message) or I(type=notify_all_involved) inside I(acknowledge_operations).
                    - Set to C(all) for all media types
                default: "all"
            op_message:
                type: str
                description:
                    - Operation message text.
                    - If I(op_message) and I(subject) not defined then "default message" from media type will be used
            subject:
                type: str
                description:
                    - Operation message subject.
                    - If I(op_message) and I(subject) not defined then "default message" from media type will be used
            username:
                type: str
                description:
                    - User name used for authentication.
                    - Required when I(ssh_auth_type in [public_key, password]) or I(command_type=telnet).
                    - Can be used when I(type=remote_command).
            password:
                type: str
                description:
                    - Password used for authentication.
                    - Required when I(ssh_auth_type=password) or I(command_type=telnet).
                    - Can be used when I(type=remote_command).
            port:
                type: int
                description:
                    - Port number used for authentication.
                    - Can be used when I(command_type in [ssh, telnet]) and I(type=remote_command).
            script_name:
                type: str
                description:
                    - The name of script used for global script commands.
                    - Required when I(command_type=global_script).
                    - Can be used when I(type=remote_command).
    acknowledge_operations:
        type: list
        elements: dict
        description:
            - List of acknowledge operations.
            - Action acknowledge operations are known as update operations since Zabbix 4.0.
            - C(Suboptions) are the same as for I(operations).
        suboptions:
            type:
                type: str
                description:
                    - Type of operation.
                choices:
                    - send_message
                    - remote_command
                    - notify_all_involved
                required: true
            command_type:
                type: str
                description:
                    - Type of operation command.
                required: false
                choices:
                    - custom_script
                    - ipmi
                    - ssh
                    - telnet
                    - global_script
            execute_on:
                type: str
                required: false
                description:
                    - Target on which the custom script operation command will be executed.
                choices:
                    - agent
                    - server
                    - proxy
            command:
                type: str
                required: false
                description:
                    - Command to run.
            ssh_auth_type:
                type: str
                description:
                    - Authentication method used for SSH commands.
                    - Required when I(type=remote_command) and I(command_type=ssh).
                choices:
                    - password
                    - public_key
            ssh_privatekey_file:
                type: str
                description:
                    - Name of the private key file used for SSH commands with public key authentication.
                    - Required when I(ssh_auth_type=public_key).
                    - Can be used when I(type=remote_command).
            ssh_publickey_file:
                type: str
                description:
                    - Name of the public key file used for SSH commands with public key authentication.
                    - Required when I(ssh_auth_type=public_key).
                    - Can be used when I(type=remote_command).
            run_on_groups:
                type: list
                elements: str
                description:
                    - Host groups to run remote commands on.
                    - Required when I(type=remote_command) and I(run_on_hosts) is not set.
            run_on_hosts:
                type: list
                elements: str
                description:
                    - Hosts to run remote commands on.
                    - Required when I(type=remote_command) and I(run_on_groups) is not set.
                    - If set to 0 the command will be run on the current host.
            send_to_groups:
                type: list
                elements: str
                description:
                    - User groups to send messages to.
            send_to_users:
                type: list
                elements: str
                description:
                    - Users (usernames or aliases) to send messages to.
            media_type:
                type: str
                description:
                    - Media type that will be used to send the message.
                    - Can be used with I(type=send_message) or I(type=notify_all_involved) inside I(acknowledge_operations).
                    - Set to C(all) for all media types
                default: "all"
            op_message:
                type: str
                description:
                    - Operation message text.
                    - If I(op_message) and I(subject) not defined then "default message" from media type will be used
            subject:
                type: str
                description:
                    - Operation message subject.
                    - If I(op_message) and I(subject) not defined then "default message" from media type will be used
            username:
                type: str
                description:
                    - User name used for authentication.
                    - Required when I(ssh_auth_type in [public_key, password]) or I(command_type=telnet).
                    - Can be used when I(type=remote_command).
            password:
                type: str
                description:
                    - Password used for authentication.
                    - Required when I(ssh_auth_type=password) or I(command_type=telnet).
                    - Can be used when I(type=remote_command).
            port:
                type: int
                description:
                    - Port number used for authentication.
                    - Can be used when I(command_type in [ssh, telnet]) and I(type=remote_command).
            script_name:
                type: str
                description:
                    - The name of script used for global script commands.
                    - Required when I(command_type=global_script).
                    - Can be used when I(type=remote_command).
        aliases: [ update_operations ]
        default: []
    pause_symptoms:
        type: bool
        description:
            - Whether to pause escalation if event is a symptom event.
            - I(supported) if C(event_source) is set to C(trigger)
            - Works only with >= Zabbix 6.4
        default: true


extends_documentation_fragment:
- community.zabbix.zabbix

a  
# If you want to use Username and Password to be authenticated by Zabbix Server
- name: Set credentials to access Zabbix Server API
  ansible.builtin.set_fact:
    ansible_user: Admin
    ansible_httpapi_pass: zabbix

# If you want to use API token to be authenticated by Zabbix Server
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens
- name: Set API token
  ansible.builtin.set_fact:
    ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895

# Trigger action with only one condition
- name: Deploy trigger action
  # set task level variables as we change ansible_connection plugin here
  vars:
    ansible_network_os: community.zabbix.zabbix
    ansible_connection: httpapi
    ansible_httpapi_port: 443
    ansible_httpapi_use_ssl: true
    ansible_httpapi_validate_certs: false
    ansible_zabbix_url_path: "zabbixeu"  # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
    ansible_host: zabbix-example-fqdn.org
  community.zabbix.zabbix_action:
    name: "Send alerts to Admin"
    event_source: "trigger"
    state: present
    status: enabled
    esc_period: 60
    conditions:
      - type: "trigger_severity"
        operator: ">="
        value: "Information"
    operations:
      - type: send_message
        subject: "Something bad is happening"
        op_message: "Come on, guys do something"
        media_type: "Email"
        send_to_users:
          - "Admin"

# Trigger action with multiple conditions and operations
- name: Deploy trigger action
  # set task level  variables as we change ansible_connection plugin here
  vars:
    ansible_network_os: community.zabbix.zabbix
    ansible_connection: httpapi
    ansible_httpapi_port: 443
    ansible_httpapi_use_ssl: true
    ansible_httpapi_validate_certs: false
    ansible_zabbix_url_path: "zabbixeu"  # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
    ansible_host: zabbix-example-fqdn.org
  community.zabbix.zabbix_action:
    name: "Send alerts to Admin"
    event_source: "trigger"
    state: present
    status: enabled
    esc_period: 1m
    conditions:
      - type: "trigger_name"
        operator: "like"
        value: "Zabbix agent is unreachable"
        formulaid: A
      - type: "trigger_severity"
        operator: ">="
        value: "disaster"
        formulaid: B
    formula: A or B
    operations:
      - type: send_message
        media_type: "Email"
        send_to_users:
          - "Admin"
      - type: remote_command
        command: "systemctl restart zabbix-agent"
        command_type: custom_script
        execute_on: server
        run_on_hosts:
          - 0

# Trigger action with recovery and acknowledge operations
- name: Deploy trigger action
  # set task level variables as we change ansible_connection plugin here
  vars:
    ansible_network_os: community.zabbix.zabbix
    ansible_connection: httpapi
    ansible_httpapi_port: 443
    ansible_httpapi_use_ssl: true
    ansible_httpapi_validate_certs: false
    ansible_zabbix_url_path: "zabbixeu"  # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
    ansible_host: zabbix-example-fqdn.org
  community.zabbix.zabbix_action:
    name: "Send alerts to Admin"
    event_source: "trigger"
    state: present
    status: enabled
    esc_period: 1h
    conditions:
      - type: "trigger_severity"
        operator: ">="
        value: "Information"
    operations:
      - type: send_message
        subject: "Something bad is happening"
        op_message: "Come on, guys do something"
        media_type: "Email"
        send_to_users:
          - "Admin"
    recovery_operations:
      - type: send_message
        subject: "Host is down"
        op_message: "Come on, guys do something"
        media_type: "Email"
        send_to_users:
          - "Admin"
    acknowledge_operations:
      - type: send_message
        media_type: "Email"
        send_to_users:
          - "Admin"
z
msg:
    description: The result of the operation
    returned: success
    type: str
    sample: "Action Deleted: Register webservers, ID: 0001"
)AnsibleModule)
ZabbixBase)LooseVersionNc                   n     e Zd Zd fd	Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Z xZS )Zapic                 <    t         t        |   ||       | | _        y N)superr
   __init___zapi_wrapperselfmodulezbx	__class__s      r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/zabbix/plugins/modules/zabbix_action.pyr   zZapi.__init__.  s    dD"63/!    c                     	 ddddd|gid}| j                   j                  j                  |      }|S # t        $ r,}| j                  j                  d|d|       Y d}~yd}~ww xY w)zCheck if action exists.

        Args:
            name: Name of the action.

        Returns:
            The return value. True for success, False otherwise.

        extendname)selectOperationsselectRecoveryOperationsselectUpdateOperationsselectFilterfilterzFailed to check if action 'z
' exists: msgN)_zapiactionget	Exception_module	fail_json)r   r   _params_actiones        r   check_if_action_existszZapi.check_if_action_exists2  sw    	`$,,4*2 (!D6*G jj''++G4GN 	`LL""VZ\]'^"__	`s   14 	A)"A$$A)c                 0   	 | j                   j                  j                  dd|gid      }t        |      dk  r | j                  j                  d|z         y
|d   S # t        $ r,}| j                  j                  d|d	|       Y d
}~y
d
}~ww xY w)zGet action by name

        Args:
            name: Name of the action.

        Returns:
            dict: Zabbix action

        r   r   outputr      zAction not found: %sr   r   zFailed to get ID of '': N)r!   r"   r#   lenr%   r&   r$   )r   r   action_listr)   s       r   get_action_by_namezZapi.get_action_by_nameI  s    
	S**++//"!D6*1 K ;!#&&+AD+H&I"1~% 	SLL""q'Q"RR	S   AA  A   	B)"BBc                 2   	 | j                   j                  j                  ddd|gid      }t        |      dk  r | j                  j                  d|z         y
|d   S # t        $ r,}| j                  j                  d|d	|       Y d
}~y
d
}~ww xY w)zGet host by host name

        Args:
            host_name: host name.

        Returns:
            host matching host name

        r   host)r-   selectInventoryr   r.   zHost not found: %sr   r   zFailed to get host 'r/   N)r!   r5   r#   r0   r%   r&   r$   )r   	host_name	host_listr)   s       r   get_host_by_host_namezZapi.get_host_by_host_name_  s    	W

++"#+!I;/- I
 9~!&&+?)+K&L |# 	WLL""	ST'U"VV	Ws   AA! A! !	B*"BBc                 0   	 | j                   j                  j                  dd|gid      }t        |      dk  r | j                  j                  d|z         y
|d   S # t        $ r,}| j                  j                  d|d	|       Y d
}~y
d
}~ww xY w)zGet host group by host group name

        Args:
            hostgroup_name: host group name.

        Returns:
            host group matching host group name

        r   r   r,   r.   zHost group not found: %sr   r   zFailed to get host group 'r/   N)r!   	hostgroupr#   r0   r%   r&   r$   )r   hostgroup_namehostgroup_listr)   s       r   get_hostgroup_by_hostgroup_namez$Zapi.get_hostgroup_by_hostgroup_namev      
	b!ZZ1155"!N#347 N >"Q&&&+E+V&W%a(( 	bLL""n^_'`"aa	br3   c                 0   	 | j                   j                  j                  dd|gid      }t        |      dk  r | j                  j                  d|z         y
|d   S # t        $ r,}| j                  j                  d|d	|       Y d
}~y
d
}~ww xY w)zGet template by template name

        Args:
            template_name: template name.

        Returns:
            template matching template name

        r   r5   r,   r.   zTemplate not found: %sr   r   zFailed to get template 'r/   N)r!   templater#   r0   r%   r&   r$   )r   template_nametemplate_listr)   s       r   get_template_by_template_namez"Zapi.get_template_by_template_name  s    
	_ JJ//33"!M?35 M =!A%&&+Cm+S&T$Q'' 	_LL""M[\']"^^	_r3   c                 0   	 | j                   j                  j                  dd|gid      }t        |      dk  r | j                  j                  d|z         y
|d   S # t        $ r,}| j                  j                  d|d	|       Y d
}~y
d
}~ww xY w)zGet trigger by trigger name

        Args:
            trigger_name: trigger name.

        Returns:
            trigger matching trigger name

        r   descriptionr,   r.   zTrigger not found: %sr   r   zFailed to get trigger 'r/   N)r!   triggerr#   r0   r%   r&   r$   )r   trigger_nametrigger_listr)   s       r   get_trigger_by_trigger_namez Zapi.get_trigger_by_trigger_name  s    
	]::--11"(<.93 L < 1$&&+B\+Q&R#A& 	]LL""<YZ'["\\	]r3   c                 0   	 | j                   j                  j                  dd|gid      }t        |      dk  r | j                  j                  d|z         y
|d   S # t        $ r,}| j                  j                  d|d	|       Y d
}~y
d
}~ww xY w)zGet discovery rule by discovery rule name

        Args:
            discovery_rule_name: discovery rule name.

        Returns:
            discovery rule matching discovery rule name

        r   r   r,   r.   zDiscovery rule not found: %sr   r   zFailed to get discovery rule 'r/   N)r!   druler#   r0   r%   r&   r$   )r   discovery_rule_namediscovery_rule_listr)   s       r   )get_discovery_rule_by_discovery_rule_namez.Zapi.get_discovery_rule_by_discovery_rule_name  s    
	k"&**"2"2"6"6"!$7#898 # &'!+&&+IL_+_&`*1-- 	kLL""Regh'i"jj	kr3   c                 4   	 |j                  d      \  }}i dddddddd	d
dddddddddddddddddddddd d!}|j                  d"      r|j                  d#      d$   }||vr| j                  j                  d%|z  &       | j                  j
                  j                  d'gd(|gid)d*      }t        |      d+k  r| j                  j                  d,|z  &       |d$   d'   D ]M  }|j                  d"      r+||   |d-   k(  s |j                  d.      d+   |d/   k(  s;|c S ||   |d-   k(  sK|c S  | j                  j                  d,|z  &       y2# t        $ r,}| j                  j                  d0|d1|&       Y d2}~y2d2}~ww xY w)3zGet discovery check  by discovery check name

        Args:
            discovery_check_name: discovery check name.

        Returns:
            discovery check matching discovery check name

        z: SSH0LDAP1SMTP2FTP3HTTP4POP5NNTP6IMAP7TCP8Zabbix agent9SNMPv1 agent10SNMPv2 agent11	ICMP ping12SNMPv3 agent13HTTPS14Telnet15SNMPz "r   z'Discovery check type: %s does not existr   dchecksr   r   )r-   r   selectDChecksr.   zDiscovery check not found: %stype"key_zFailed to get discovery check 'r/   N)	split
startswithr%   r&   r!   rL   r#   r0   r$   )r   discovery_check_namerM   dcheck_typedcheck_type_to_numberrN   dcheckr)   s           r   +get_discovery_check_by_discovery_check_namez0Zapi.get_discovery_check_by_discovery_check_name  sL   .	m/C/I/I$/O,%s%% % s	%
 % s% % % s% % % % T% % %  $!%!& %%f- *//6q9"77&&+TWb+b&c"&**"2"2"6"6$+!$7#89!)8 #
 &'!+&&+JMa+a&b-a0; "))&1-k:fVnL 4 : :4 @ Cvf~ U%*;76&>I!M" LL""'FI]']"^ 	mLL""Sgij'k"ll	ms6   DE" E" -E" 1E" =E"  E" "	F+"FFc                    	 t        | j                        t        d      k\  rd|gi}nd|gi}| j                  j                  j	                  d|d      }t        |      dk  r | j                  j                  d|z         y|d	   S # t        $ r,}| j                  j                  d
|d|       Y d}~yd}~ww xY w)zGet proxy by proxy name

        Args:
            proxy_name: proxy name.

        Returns:
            proxy matching proxy name

        7.0r   r5   r   r,   r.   zProxy not found: %sr   r   zFailed to get proxy 'r/   N)	r   _zbx_api_versionr!   proxyr#   r0   r%   r&   r$   )r   
proxy_namer   
proxy_listr)   s        r   get_proxy_by_proxy_namezZapi.get_proxy_by_proxy_name  s    	YD112l56II :,/ :,/))--" / J :"&&+@:+M&N!!}$ 	YLL""UV'W"XX	Ys   BB	 B	 		B>"B99B>c                 t   d|gi}	 t        |      j                         dk(  ry| j                  j                  j	                  d|d      }t        |      dk  r | j                  j                  d|z         y|d	   d
   S # t        $ r,}| j                  j                  d|d|       Y d}~yd}~ww xY w)zGet mediatype by mediatype name

        Args:
            mediatype_name: mediatype name

        Returns:
            mediatype matching mediatype name

        r   allrR   r   r,   r.   zMedia type not found: %sr   r   mediatypeidzFailed to get mediatype 'r/   N)	strlowerr!   	mediatyper#   r0   r%   r&   r$   )r   mediatype_namer   mediatype_listr)   s        r   get_mediatype_by_mediatype_namez$Zapi.get_mediatype_by_mediatype_name"  s     >*+	a>"((*e3!ZZ1155" 7 N >"Q&&&+E+V&W%a(77 	aLL""^]^'_"``	as#   B AB :B 	B7"B22B7c                 4   	 d|gi}| j                   j                  j                  d|d      }t        |      dk  r | j                  j                  d|z         y
|d   S # t        $ r,}| j                  j                  d|d	|       Y d
}~y
d
}~ww xY w)zGet user by user name

        Args:
            user_name: user name

        Returns:
            user matching user name

        usernamer   r,   r.   zUser not found: %sr   r   zFailed to get user 'r/   N)r!   userr#   r0   r%   r&   r$   )r   	user_namer   	user_listr)   s        r   get_user_by_user_namezZapi.get_user_by_user_name<  s    	W 9+.F

++" - I 9~!&&+?)+K&L |# 	WLL""	ST'U"VV	Ws   AA" A" "	B+"BBc                 0   	 | j                   j                  j                  dd|gid      }t        |      dk  r | j                  j                  d|z         y
|d   S # t        $ r,}| j                  j                  d|d	|       Y d
}~y
d
}~ww xY w)zGet usergroup by usergroup name

        Args:
            usergroup_name: usergroup name

        Returns:
            usergroup matching usergroup name

        r   r   r,   r.   zUser group not found: %sr   r   zFailed to get user group 'r/   N)r!   	usergroupr#   r0   r%   r&   r$   )r   usergroup_nameusergroup_listr)   s       r   get_usergroup_by_usergroup_namez$Zapi.get_usergroup_by_usergroup_nameS  r?   r3   c                 8   	 |i S | j                   j                  j                  dd|gid      }t        |      dk  r | j                  j                  d|z         y|d   S # t        $ r,}| j                  j                  d	|d
|       Y d}~yd}~ww xY w)zGet script by script name

        Args:
            script_name: script name

        Returns:
            script matching script name

        Nr   r   r,   r.   zScript not found: %sr   r   zFailed to get script 'r/   )r!   scriptr#   r0   r%   r&   r$   )r   script_namescript_listr)   s       r   get_script_by_script_namezZapi.get_script_by_script_namej  s    	["	**++//"!K=11 K ;!#&&+AK+O&P"1~% 	[LL""+WX'Y"ZZ	[s#   A$ AA$ A$ $	B-"BBr   )__name__
__module____qualname__r   r*   r2   r9   r>   rD   rJ   rO   r}   r   r   r   r   r   __classcell__r   s   @r   r
   r
   -  sW    "`.S,W.b,_,],k,8mtY4a4W.b.[r   r
   c                   >     e Zd Zd fd	Zd Zd Zd Zd Zd Z xZ	S )Actionc                 <    t         t        |   ||       d | _        y r   )r   r   r   existing_datar   s      r   r   zAction.__init__  s    fd$VS1!r   c                    |d   t        j                  g d|d         |j                  d      |d   |d   |j                  d      |j                  d      t        j                  d	d
g|d         d}|d   dk(  rE|d   rdnd|d<   t        | j                        t        d      k\  r|d   rdnd|d<   |d   rdnd|d<   |j                  d      |d<   d|v r8t        |j                  dd      t        d            s|j                  dd       nMt        |j                  dd      t              r|j                  dg       |d<   ng |d<   |j                  dd       d|v r6t        |j                  dd      t        d            r|j                  d       d|v rTt        |j                  dd      t        d            st        |j                  dg             dk(  r|j                  d       d|v rTt        |j                  dd      t        d            st        |j                  dg             dk(  r|j                  d       |d   dvr|j                  d       |S )zConstruct parameters.

        Args:
            **kwargs: Arbitrary keyword parameters.

        Returns:
            dict: dictionary of specified parameters
        r   rG   	discoveryauto_registrationinternalevent_source
esc_period
conditions
operationsrecovery_operationsacknowledge_operationsenableddisabledstatus)r   eventsourcer   r   r   r   r   r   rG   pause_in_maintenancerT   rR   pause_suppressed6.4pause_symptomsnotify_if_canceledupdate_operationsNr   r   )r      )
zabbix_utilshelper_to_numeric_valuer#   r   r   
isinstancert   poplistr0   )r   kwargsr'   s      r   _construct_parameterszAction._construct_parameters  sr    6N'?? A $N3	5
 !**\2\* .#)::.C#D&,jj1I&J"::<#H-/
" .!Y.178N1O#UXG&'D112l56II39:J3KCQT()39:N3OCUXG()'-zz2E'F#$')*W[[I\^b=ceijneo2pKK0$7$<dCTJ+2;;7OQS+TG'(+-G'(KK0$77"z'++lD2QSWX\S]'^KK% G+'++&;TBDJOSVW^WbWbcxz|W}S~  CD  TD12')'++&94@$t*MQTU\U`U`atvxUyQz~Q/0=!/KK%r   c                     t        j                  | j                  |d         d         }t        j                   | j                  di |      }i }t        j                  t        j
                  |||            }|S )zCheck difference between action and user specified parameters.

        Args:
            **kwargs: Arbitrary keyword parameters.

        Returns:
            dict: dictionary of differences
        r   r    )r   helper_convert_unicode_to_strr*   r   helper_cleanup_datahelper_compare_dictionaries)r   r   existing_action
parameterschange_parameters_diffs         r   check_differencezAction.check_difference  s     'DDTE`E`aghnaoEpqrEst!??@Z@Z@Z@d]c@de
001Y1YZdfu  xI  2J  Kr   c                 X   	 | j                   j                  r | j                   j                  d|z  d       |j                  d      |d<   | j                  j
                  j                  |      S # t        $ r/}| j                   j                  d|d   d|       Y d	}~y	d	}~ww xY w)
zUpdate action.

        Args:
            **kwargs: Arbitrary keyword parameters.

        Returns:
            action: updated action
        z;Action would be updated if check mode was not specified: %sTr    changed	action_idactionidzFailed to update action 'r/   r   N)	r%   
check_mode	exit_jsonr   r!   r"   updater$   r&   )r   r   r)   s      r   update_actionzAction.update_action  s    	e||&&&&+hkq+q{&  A!'K!8F:::$$++F33 	eLL""VT^M_ab'c"dd	es   A.A1 1	B):%B$$B)c                 ^   	 | j                   j                  r| j                   j                  dd        | j                  di |}| j                  j
                  j                  |      }|d   d   S # t        $ r/}| j                   j                  d|d   d|	       Y d
}~y
d
}~ww xY w)zAdd action.

        Args:
            **kwargs: Arbitrary keyword parameters.

        Returns:
            action: added action
        z5Action would be added if check mode was not specifiedTr   	actionidsr   zFailed to create action 'r   r/   r   Nr   )	r%   r   r   r   r!   r"   creater$   r&   )r   r   r   r1   r)   s        r   
add_actionzAction.add_action  s    	a||&&&&+blp&q333=f=J**++22:>K{+A.. 	aLL""VTZ^]^'_"``	as   A1A4 4	B,=%B''B,c                 &   	 | j                   j                  r| j                   j                  dd       | j                  j                  j                  |g      S # t        $ r,}| j                   j                  d|d|       Y d}~yd}~ww xY w)z|Delete action.

        Args:
            action_id: Action id

        Returns:
            action: deleted action
        z7Action would be deleted if check mode was not specifiedTr   zFailed to delete action 'r/   r   N)r%   r   r   r!   r"   deleter$   r&   )r   r   r)   s      r   delete_actionzAction.delete_action  s{    	\||&&&&+dnr&s::$$++YK88 	\LL""YXY'Z"[[	\s   AA 	B$"BBr   )
r   r   r   r   r   r   r   r   r   r   r   s   @r   r   r     s%    "8te"a$\r   r   c                   T    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zy)
Operationsc                     	 t        j                  g d|d         S # t        $ r% | j                  j	                  d|d   z         Y yw xY w)zConstruct operation type.

        Args:
            operation: operation to construct

        Returns:
            str: constructed operation
        )send_messageremote_commandadd_hostremove_hostadd_to_host_groupremove_from_host_grouplink_to_templateunlink_from_templateenable_hostdisable_hostset_host_inventory_modenoner   add_host_tagsremove_host_tagsrt   z*Unsupported value '%s' for operation type.r   Nr   r   r$   r%   r&   r   	operations     r   _construct_operationtypez#Operations._construct_operationtype  s_    	i77 9$  &!! $  	iLL""'SV_`fVg'g"h	i    +A
Ac                 v   	 |j                  d      |j                  d      dnd|j                  d      *| j                  j                  |j                  d            nd|j                  d      |j                  d      dS # t        $ r)}| j                  j                  d|z  	       Y d}~yd}~ww xY w)
zConstruct operation message.

        Args:
            operation: operation to construct the message

        Returns:
            dict: constructed operation message
        
op_messageNsubjectrR   rT   
media_type)default_msgr   messager   z8Failed to construct operation message. The error was: %sr   )r#   r   r   r$   r%   r&   )r   r   r)   s      r   _construct_opmessagezOperations._construct_opmessage%  s    
	g&/mmL&A&MQZQ^Q^_hQiQus{~ ]]<0<  $11QQMM,/ BE$==6$==3   	gLL""'ade'e"ff	gs   BB 	B8B33B8c                     |j                  d      y|j                  d      D cg c]"  }d| j                  j                  |      d   i$ c}S c c}w )zConstruct operation message user.

        Args:
            operation: operation to construct the message user

        Returns:
            list: constructed operation message user or None if operation not found
        send_to_usersNuserid)r#   r   r   )r   r   _users      r   _construct_opmessage_usrz#Operations._construct_opmessage_usr:  s^     ==)1 !}}_57 d((>>uEhO
 7 	7 7   'Ac                     |j                  d      y|j                  d      D cg c]"  }d| j                  j                  |      d   i$ c}S c c}w )zConstruct operation message group.

        Args:
            operation: operation to construct the message group

        Returns:
            list: constructed operation message group or None if operation not found
        send_to_groupsNusrgrpid)r#   r   r   r   r   _groups      r   _construct_opmessage_grpz#Operations._construct_opmessage_grpI  sa     ==)*2 "&679 **JJ6RS]^
 9 	9 9r   c                     	 d| j                   j                  |j                  d            j                  d      i}|S # t        $ r)}| j                  j                  d|z         Y d}~yd}~ww xY w)zConstruct operation command.

        Args:
            operation: operation to construct command

        Returns:
            list: constructed operation command
        scriptidr   z8Failed to construct operation command. The error was: %sr   N)r   r   r#   r$   r%   r&   )r   r   	opcommandr)   s       r   _construct_opcommandzOperations._construct_opcommandX  ss    
	gD..HHMM-0#j/I  	gLL""'ade'e"ff	gs   <? 	A1A,,A1c                     |j                  d      y|j                  d      D cg c]4  }t        |      dk7  r d| j                  j                  |      d   inddi6 c}S c c}w )zConstruct operation command host.

        Args:
            operation: operation to construct command host

        Returns:
            list: constructed operation command host
        run_on_hostsNrR   hostid)r#   r   r   r9   )r   r   _hosts      r   _construct_opcommand_hstz#Operations._construct_opcommand_hstm  s{     ==(0 BK~A^`8=SZ3 d((>>uEhO
%-sO4 ` 	` `s   9A"c                     |j                  d      y|j                  d      D cg c]"  }d| j                  j                  |      d   i$ c}S c c}w )zConstruct operation command group.

        Args:
            operation: operation to construct command group

        Returns:
            list: constructed operation command group
        run_on_groupsNgroupidr#   r   r>   r  s      r   _construct_opcommand_grpz#Operations._construct_opcommand_grp|  s_     ==)1 "o68 t))II&QR[\
 8 	8 8r   c                     |j                  dg       D cg c]"  }d| j                  j                  |      d   i$ c}S c c}w )zConstruct operation group.

        Args:
            operation: operation to construct group

        Returns:
            list: constructed operation group
        host_groupsr  r  r  s      r   _construct_opgroupzOperations._construct_opgroup  sM     "mR8: t))II&QR[\
 : 	: :   '?c                     |j                  dg       D cg c]"  }d| j                  j                  |      d   i$ c}S c c}w )zConstruct operation template.

        Args:
            operation: operation to construct template

        Returns:
            list: constructed operation template
        	templates
templateid)r#   r   rD   )r   r   	_templates      r   _construct_optemplatez Operations._construct_optemplate  sM     %==b9; $,,JJ9UVbc
 ; 	; ;r  c                 T    dt        j                  ddg|j                  d            iS )zConstruct operation inventory.

        Args:
            operation: operation to construct inventory

        Returns:
            dict: constructed operation inventory
        inventory_modemanual	automatic	inventory)r   r   r#   r   s     r   _construct_opinventoryz!Operations._construct_opinventory  s8     lBBD }}[)+
 	
r   c                 X    |j                  d      }||dk(  rd}n|dk(  rd}dddgS g S )zConstruct operation conditions.

        Args:
            operation: operation to construct the conditions

        Returns:
            list: constructed operation conditions
        operation_conditionacknowledgedrT   not_acknowledgedrR   rn   )conditiontypeoperatorvalue)r#   )r   r   _opcond_values       r   _construct_opconditionsz"Operations._construct_opconditions  sS     -- 56.(..!%  
 	r   c                 (   g }|D ]  }| j                  |      }||j                  d      |j                  d      |j                  d      d}|d   dk(  rU| j                  |      |d<   | j                  |      |d<   | j	                  |      |d	<   |d
k(  r| j                  |      |d<   |d   dk(  rU| j                  |      |d<   | j                  |      |d<   | j                  |      |d<   |d
k(  r| j                  |      |d<   |d   dv r| j                  |      |d<   t        | j                        t        d      k\  r|d   dv r|d   |d<   |d   dv r| j                  |      |d<   |d   dk(  r| j                  |      |d<   |dv r*t        |j                  d      t        d            r9d|d<   n3|j!                  d       |j!                  d       |j!                  d       |j#                  |        t%        j&                  |      S )zConstruct the operation data using helper methods.

        Args:
            operation: operation to construct

        Returns:
            list: constructed operation data
        r   esc_step_fromesc_step_to)operationtyper   r/  r0  r1  r   	opmessageopmessage_usropmessage_grprG   opconditionsr.   r	  opcommand_hstopcommand_grp)      opgroupr   )      tagsoptag)      
optemplate
   opinventory)rG   r   N)r   r#   r   r   r  r-  r
  r  r  r  r   r   r  r#  r   rt   r   appendr   r   )r   r   r   constructed_dataopoperation_typeconstructed_operations          r   construct_the_datazOperations.construct_the_data  s`     4	;B!::2>N!/ ff\2!#!8!vvm4	%! %_5:595N5Nr5R%k29=9V9VWY9Z%o69=9V9VWY9Z%o69,<@<X<XY[<\).9 %_5:595N5Nr5R%k29=9V9VWY9Z%o69=9V9VWY9Z%o69,<@<X<XY[<\).9 %_5?373J3J23N%i0D112l56II(9XE57Z)'2 %_5?6:6P6PQS6T%l3 %_5;7;7R7RSU7V%m4 66377EtDzR:;),7%)),7%))/:%))-8##$9:i4	;l //0@AAr   N)r   r   r   r   r   r   r  r
  r  r  r  r  r#  r-  rI  r   r   r   r   r     sD    i>g*79g*`8:;
 ,@Br   r   c                       e Zd ZdZd Zd Zy)RecoveryOperationszc
    Restructures the user defined recovery operations data to fit the Zabbix API requirements
    c                     	 t        j                  g d|d         S # t        $ r% | j                  j	                  d|d   z         Y yw xY w)Construct operation type.

        Args:
            operation: operation to construct type

        Returns:
            str: constructed operation type
        )r   r   NNNNNNNNNnotify_all_involvedrt   z3Unsupported value '%s' for recovery operation type.r   Nr   r   s     r   r   z+RecoveryOperations._construct_operationtype  s_    	r77 9' )2&(9   	rLL""'\_hio_p'p"q	rr   c                    g }|D ]  }| j                  |      }d|i}|d   dk(  r<| j                  |      |d<   | j                  |      |d<   | j                  |      |d<   |d   dk(  r(| j                  |      |d<   |d   j	                  d       |d   dk(  r<| j                  |      |d	<   | j                  |      |d
<   | j                  |      |d<   |j                  |        t        j                  |      S )zConstruct the recovery operations data using helper methods.

        Args:
            operation: operation to construct

        Returns:
            list: constructed recovery operations data
        r1  r   r2  r3  r4     r   r.   r	  r6  r7  r   r   r   r  r   r
  r  r  rD  r   r   r   r   rE  rF  rG  rH  s         r   rI  z%RecoveryOperations.construct_the_data.  5     	;B!::2>N%!
 %_5:595N5Nr5R%k29=9V9VWY9Z%o69=9V9VWY9Z%o6$_5;595N5Nr5R%k2%k266}E %_5:595N5Nr5R%k29=9V9VWY9Z%o69=9V9VWY9Z%o6##$9:-	;0 //0@AAr   Nr   r   r   __doc__r   rI  r   r   r   rK  rK    s    r6"Br   rK  c                       e Zd ZdZd Zd Zy)AcknowledgeOperationszf
    Restructures the user defined acknowledge operations data to fit the Zabbix API requirements
    c                     	 t        j                  g d|d         S # t        $ r% | j                  j	                  d|d   z         Y yw xY w)rM  )r   r   NNNNNNNNNNrN  rt   z6Unsupported value '%s' for acknowledge operation type.r   Nr   r   s     r   r   z.AcknowledgeOperations._construct_operationtypeX  s_    	u77 9' )2&(9   	uLL""'_bklrbs's"t	ur   c                    g }|D ]  }| j                  |      }d|i}|d   dk(  r<| j                  |      |d<   | j                  |      |d<   | j                  |      |d<   |d   dk(  r(| j                  |      |d<   |d   j	                  d       |d   dk(  r<| j                  |      |d	<   | j                  |      |d
<   | j                  |      |d<   |j                  |        t        j                  |      S )zConstruct the acknowledge operations data using helper methods.

        Args:
            operation: operation to construct

        Returns:
            list: constructed acknowledge operations data
        r1  r   r2  r3  r4     r   r.   r	  r6  r7  rQ  rR  s         r   rI  z(AcknowledgeOperations.construct_the_datat  rS  r   NrT  r   r   r   rW  rW  S  s    u8"Br   rW  c                   *    e Zd Zd Zd Zd Zd Zd Zy)Filterc                     t        |      dk  rdddS |dk(  rdddS |dk(  rdddS |dk(  rd	ddS |d
k(  r#|d|dS | j                  j                  d       |d|dS dddS )zConstruct the eval type

        Args:
            _formula: zabbix condition evaluation formula
            _conditions: list of conditions to check

        Returns:
            dict: constructed acknowledge operations data
        r.   rR   N)evaltypeformulaandorandrT   orrV   custom_expressionrX   zD'formula' is required when 'eval_type' is set to 'custom_expression'r   )r0   r%   r&   )r   
_eval_type_formula_conditionss       r   _construct_evaltypezFilter._construct_evaltype  s     {q          ,,# #' 
 &&+q&r# 
 
 	
r   c                     	 t        j                  g d|d         S # t        $ r% | j                  j	                  d|d   z         Y yw xY w)zConstruct the condition type

        Args:
            _condition: condition to check

        Returns:
            str: constructed condition type data
        )
host_groupr5   rG   rH   trigger_severitytrigger_valuetime_periodhost_ipdiscovered_service_typediscovered_service_portdiscovery_statusuptime_or_downtime_durationreceived_valuehost_templateNNmaintenance_statusNdiscovery_rulediscovery_checkr   discovery_objectr7   
event_typehost_metadata	event_tagevent_tag_valuert   z*Unsupported value '%s' for condition type.r   Nr   r   
_conditions     r   _construct_conditiontypezFilter._construct_conditiontype  s_    	j77 9#6 %/v$67 :  	jLL""'SV`agVh'h"i	jr   c                     	 t        j                  ddgddgddgddgd	d
dgddgdddddg|d         S # t        $ r% | j                  j	                  d|d   z         Y yw xY w)zConstruct operator

        Args:
            _condition: condition to construct

        Returns:
            str: constructed operator
        equals=does not equal<>containslikedoes not containnot likeinis greater than or equals>=is less than or equals<=not inmatchesdoes not matchYesNor)  z$Unsupported value '%s' for operator.r   Nr   r|  s     r   _construct_operatorzFilter._construct_operator  s    	h773!4(V$#Z0,d3)40 9 "*-   	hLL""'MPZ[ePf'f"g	hs   03 +A! A!c                    	 |dk(  r| j                   j                  |      d   S |dk(  r| j                   j                  |      d   S |dk(  r| j                   j                  |      d   S |dk(  rt	        j
                  g d|xs d	      S |d
k(  rt	        j
                  ddg|xs d      S |dk(  rt	        j
                  g d|      S |dk(  rt	        j
                  g d|      S |dk(  r| j                   j                  |      d   S |dk(  rt	        j
                  ddg|      S |dk(  r| j                   j                  |      d   S |dk(  r| j                   j                  |      d   S |dk(  r| j                   j                  |      d   S |dk(  rt	        j
                  g d|      S |dk(  rt	        j
                  g d|      S |S # t        $ r& | j                  j                  d |d!|d"#       Y y$w xY w)%zConstruct operator

        Args:
            conditiontype: type of condition to construct
            value: value to construct

        Returns:
            str: constructed value
        r   r  r.   r     	triggeridr8  )not classifiedinformationwarningaveragehighdisasterr  r9  okproblem   )rQ   rS   rU   rW   rY   r[   r]   r_   ra   rc   re   rg   ri   rk   rm   ro   rB  )updown
discoveredlostr;  r     r  r     druleid   dcheckid   proxyid   )
pchldrfor0r5   service   )zitem in not supported statezitem in normal statezLLD rule in not supported statezLLD rule in normal stateztrigger in unknown stateztrigger in normal statezUnsupported value 'z' for specified condition type.
                       Check out Zabbix API documentation for supported values for
                       condition type 'z' at
                       https://www.zabbix.com/documentation/3.4/manual/api/reference/action/object#action_filter_conditionr   N)r   r>   r9   rJ   r   r   rD   rO   r}   r   r$   r%   r&   )r   r(  r*  s      r   _construct_valuezFilter._construct_value  sm   _	!))II%PQZ[[!))??FxPP!))EEeL[YY !#;; =  "'!:*:  !#;;= %  !#;; =   %! ( "#;; = #	  "))GGN|\\"#;;=   "))SSTYZ[dee"))UUV[\]ghh"))AA%HSS"#;; = !& 
 "#;; =/ 16  L 	LL"" BG  IVW # 	sd   "G "G "G + G  G -G 
G '"G 
G '"G 
"G -"G G -G 
G ,G;:G;c           
         |yi }g |d<   |D ]y  }| j                  |      }|d   j                  || j                  ||j                  d            |j                  d      |j                  d      | j	                  |      d       { | j                  |||d         }|d   |d<   |d   |d<   t        j                  |      S )	aD  Construct the user defined filter conditions to fit the Zabbix API
        requirements operations data using helper methods.

        Args:
            _formula:  zabbix condition evaluation formula
            _conditions: conditions to construct

        Returns:
            dict: user defined filter conditions
        Nr   r*  value2	formulaid)r(  r*  r  r  r)  r^  r_  )r~  rD  r  r#   r  rg  r   r   )r   rd  re  rf  rE  condcondition_type_constructed_evaltypes           r   rI  zFilter.construct_the_dataz  s     )+& 	D!::4@N\*11!/..~txx?PQ((8,!XXk2 44T:3 	 !% 8 8\*!

 (=Z'H$&;I&F#//0@AAr   N)r   r   r   rg  r~  r  r  rI  r   r   r   r\  r\    s"    .
`(jTh6iVBr   r\  c            (         t        j                         } | j                  t        t        dd      t        dd      t        ddg d      t        dddddg	      t        ddd
d
dg	      t        ddd      t        ddg dt        t        dd      t        ddg d      t        dd      t        dd      t        dd            dddggg      t        ddd      t        dddg d	      t        ddg dt        doi dt        ddg d      dt        ddd      dt        ddd      dt        ddd      dt        dddd d!g	      d"t        ddg d#      d$t        dd      d%t        ddg d&      d't        ddd(      d)t        dd      d*t        ddd+      d,t        ddd+      d-t        dd      d.t        ddd'd/g      d0t        dd      d1t        dd      d2t        dd      d3t        ddd4      d5t        dd      d6t        dd      d7t        ddd+      d8t        ddd+      d9t        ddd+      d:t        ddd;d<g      d=t        ddd+      d>t        ddd+      dd?d"ggdd?d*d,gdgd"d@d$d%ggd"dAd$ggd"dBd$d.ggd.d'd2d'ggd.d/g dCgd"dDg dEgd"dFd-ggddGd9ggddHd9ggddId=ggddJd=ggddKd:ggddLd8d7gdgddMd>gdgddNd>gdgg      t        ddg dt        doi dt        ddg dO      d"t        ddg d#      d$t        dd      d%t        ddg d&      d't        ddd(      d)t        dd      d*t        ddd+      d,t        ddd+      d-t        dd      d.t        ddd'd/g      d0t        dd      d1t        dd      d2t        dd      d3t        ddd4      d5t        dd      d6t        dd      d7t        ddd+      d8t        ddd+      dd?d"ggdd?d*d,gdgd"d@d$d%ggd"dAd$ggd"dBd$d.ggd.d'd2d'ggd.d/g dCgd"dDg dEgd"dFd-ggddLd8d7gdgg
      t        ddg ddPgt        doi dt        ddg dO      d"t        ddg d#      d$t        dd      d%t        ddg d&      d't        ddd(      d)t        dd      d*t        ddd+      d,t        ddd+      d-t        dd      d.t        ddd'd/g      d0t        dd      d1t        dd      d2t        dd      d3t        ddd4      d5t        dd      d6t        dd      d7t        ddd+      d8t        ddd+      dd?d"ggdd?d*d,gdgd"d@d$d%ggd"dAd$ggd"dBd$d.ggd.d'd2d'ggd.d/g dCgd"dDg dEgd"dFd-ggddLd8d7gdgg
Q      t        ddd      t        ddd      R             t	        | dSddTgggdU      }|j
                  dV   }|j
                  d   }|j
                  dT   }|j
                  dS   }|j
                  dW   }|j
                  dX   }|j
                  dY   }|j
                  dZ   }	|j
                  d[   }
|j
                  d\   }|j
                  d]   }|j
                  d^   }|j
                  d_   }|j
                  d`   }t        |      }t        |      }|j                  |      }t        ||      }t        ||      }t        ||      }t        ||      }|r(|j                  |      da   }|dk(  r+|j                  |      }|j                  ddb|dc|d       yt        |||||||j!                  ||      |j!                  |      |j!                  |
|	|      |e
      }t#        |j$                        t#        df      k\  r||d_<   |j!                  |      || d^   dg   dh   <    |j&                  doi |}|i k(  r|j                  ddi|z  d       y |j(                  dodj|i|}|j                  ddk|dc|d       y|dk(  r|j                  dl       yt        ||||||j!                  ||      |j!                  |      |j!                  |
|	|      |m	      }|j!                  |      || d^   dg   dh   <   t#        |j$                        t#        df      k\  r||d_<    |j*                  doi |}|j                  ddn|dc|d       y)pz!Main ansible module function
    r   F)rt   requiredTr   )rt   r  choicespresentabsent)rt   r  defaultr  r   r   bool)rt   r  r  r   dict)r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  )r  r)  rt   r*  r  rt   r{  r  )rt   r  r  elementsoptionsrequired_ifN)r`  ra  rb  rc  )r   r   r   r   r   r   r   r   r   r   r   rN  r   r   r   0sr/  intr.   r0  r%  r&  r'  command_type)custom_scriptipmisshtelnetglobal_scriptcommand
execute_on)agentserverr   password)rt   r  no_logportr  )rt   r  r  r  r   ssh_auth_type
public_keyssh_privatekey_filessh_publickey_filer   r   r   r   r   r  r   r  r"  r   r!  r  r=  r   r  r  r  )r   r  r  r  )r  r   r  r  r   r   r   r   r   r   r   r   )r   r   rN  r   )rt   r  r  r  aliasesr  r  )r   r   r   stater   r   r   r_  	eval_typer   r   r   r   r   r  r   )argument_specr  supports_check_moder   r   r   r   r_  r  r   r   r   r   r   r   zAction Deleted: z, ID: )r   r    )
r   r   r   r   r   r   r   r   r   r   r   r  r   zAction is up to date: %sr   zAction Updated: )r   )	r   r   r   r   r   r   r   r   r   zAction created: r   )r   zabbix_common_argument_specr   r  r   paramsr
   r   r*   r   rK  rW  r\  r2   r   r   rI  r   r   r   r   r   )r  r   r   r   r   r  r   r   r   r_  r  r   r   r   r   r   zapi_wrapperr"   action_existsopsrecovery_opsacknowledge_opsfltrr   resultr   
differences                              r   mainr    s    !<<>MUU3ut,uu>wxy9V^J_`	IWaKbc!vtLEE:!0 ut4679> *XJ7G&
N %%>EE4Itu F!F*  UUDI+F, #qI-F. !eeQG/F0 %)" +-?@	%1F> ""
?FT %%8UFV  "8WF` 55FaFb uu5cFd #OeFf "vNgFh !ee<iFj #
T`GabkFl %)ee$DmFn $(UU#CoFp 559qFt  UUEJuFv %%8wFx  UU;yFz  $%%P{F| #O}F@ !fuuMAFD EEHkCZ[EFH FUUKIFJ vGKFP )N+;<)O^+LdS9l2KL)5O(DE *z:.FG ,0ij+NO=/B,}o>1M?C+k];/+?2[MB/;K)LdS6(D9+fXt<#Y_
@ ! +!+ ""
+, %%8-+.  "8/+8 55F9+: uu5;+< #O=+> "vN?+@ !ee<A+B #
T`GabC+D %)ee$DE+F $(UU#CG+H 559I+L  UUEJM+N %%8O+P  UU;Q+R  $%%PS+T #OU+Z )N+;<)#",    3   )5O(DE *z:.FG ,0ij+NO=/B/;K)LdS!cC
H  $() +!+ ""
+, %%8-+.  "8/+8 55F9+: uu5;+< #O=+> "vN?+@ !ee<A+B #
T`GabC+D %)ee$DE+F $(UU#CG+H 559I+L  UUEJM+N %%8O+P  UU;Q+R  $%%PS+T #OU+Z )N+;<)#",    3   )5O(DE *z:.FG ,0ij+NO=/B/;K)LdS!eD 
J %FVeTJu[ [x #i" 

 !F == D|,J==0LMM'"E]]8$F!==)?@|,JmmI&Gk*I|,J --(=>#]]+CD]]#34N';<<LF^F 77=M
V\
*C%fl;L+FLAO&,'D 33D9*E	H)))4FTtU[/\]#)%%911*lK$0$C$CDW$X229gzR#5F L99:l5>QQ+9'(L[LnLn  pF  MGF=!9:9EaHI000:6:JR  4NRV4W X--- '    SWY_3` aHU+)%%911*lK$0$C$CDW$X229gzR#5
F M\LnLn  pF  MGF=!9:9EaHIL99:l5>QQ+9'()))3F3ITtU^/_`r   __main__)
__future__r   r   r   rt   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   >ansible_collections.community.zabbix.plugins.module_utils.baser   #ansible.module_utils.compat.versionr   Aansible_collections.community.zabbix.plugins.module_utils.helpers	communityzabbixpluginsmodule_utilshelpersr   r
   r   r   rK  rW  r\  r  r   r   r   r   <module>r     s    A @T
lyv
 5 U < X X XS[: S[l
\T \DFB FBRBB BBJCBJ CBL@BT @BF{a| zF r   