#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright: (c) 2018, F5 Networks Inc.
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['preview'],
                    'supported_by': 'certified'}
DOCUMENTATION = r'''
---
module: bigip_monitor_ldap
short_description: Manages BIG-IP LDAP monitors
description:
  - Manages BIG-IP LDAP monitors.
version_added: 2.8
options:
  name:
    description:
      - Monitor name.
    required: True
  description:
    description:
      - Specifies descriptive text that identifies the monitor.
  parent:
    description:
      - The parent template of this monitor template. Once this value has
        been set, it cannot be changed.
      - By default, this value is the C(ldap) parent on the C(Common) partition.
    default: "/Common/ldap"
  ip:
    description:
      - IP address part of the IP/port definition. If this parameter is not
        provided when creating a new monitor, then the default value will be
        '*'.
  port:
    description:
      - Port address part of the IP/port definition. If this parameter is not
        provided when creating a new monitor, then the default value will be
        '*'.
      - Note that if specifying an IP address, a value between 1 and 65535
        must be specified.
  interval:
    description:
      - Specifies, in seconds, the frequency at which the system issues the
        monitor check when either the resource is down or the status of the
        resource is unknown.
  timeout:
    description:
      - Specifies the number of seconds the target has in which to respond to
        the monitor request.
      - If the target responds within the set time period, it is considered 'up'.
        If the target does not respond within the set time period, it is considered
        'down'. When this value is set to 0 (zero), the system uses the interval
        from the parent monitor.
      - Note that C(timeout) and C(time_until_up) combine to control when a
        resource is set to up.
  time_until_up:
    description:
      - Specifies the number of seconds to wait after a resource first responds
        correctly to the monitor before setting the resource to 'up'.
      - During the interval, all responses from the resource must be correct.
      - When the interval expires, the resource is marked 'up'.
      - A value of 0, means that the resource is marked up immediately upon
        receipt of the first correct response.
  up_interval:
    description:
      - Specifies the interval for the system to use to perform the health check
        when a resource is up.
      - When C(0), specifies that the system uses the interval specified in
        C(interval) to check the health of the resource.
      - When any other number, enables specification of a different interval to
        use when checking the health of a resource that is up.
  manual_resume:
    description:
      - Specifies whether the system automatically changes the status of a resource
        to B(enabled) at the next successful monitor check.
      - If you set this option to C(yes), you must manually re-enable the resource
        before the system can use it for load balancing connections.
      - When C(yes), specifies that you must manually re-enable the resource after an
        unsuccessful monitor check.
      - When C(no), specifies that the system automatically changes the status of a
        resource to B(enabled) at the next successful monitor check.
    type: bool
  target_username:
    description:
      - Specifies the user name, if the monitored target requires authentication.
  target_password:
    description:
      - Specifies the password, if the monitored target requires authentication.
  base:
    description:
      - Specifies the location in the LDAP tree from which the monitor starts the
        health check.
  filter:
    description:
      - Specifies an LDAP key for which the monitor searches.
  security:
    description:
      - Specifies the secure protocol type for communications with the target.
    choices:
      - none
      - ssl
      - tls
  mandatory_attributes:
    description:
      - Specifies whether the target must include attributes in its response to be
        considered up.
    type: bool
  chase_referrals:
    description:
      - Specifies whether, upon receipt of an LDAP referral entry, the target
        follows (or chases) that referral.
    type: bool
  debug:
    description:
      - Specifies whether the monitor sends error messages and additional information
        to a log file created and labeled specifically for this monitor.
    type: bool
  update_password:
    description:
      - C(always) will update passwords if the C(target_password) is specified.
      - C(on_create) will only set the password for newly created monitors.
    default: always
    choices:
      - always
      - on_create
  partition:
    description:
      - Device partition to manage resources on.
    default: Common
  state:
    description:
      - When C(present), ensures that the monitor exists.
      - When C(absent), ensures the monitor is removed.
    default: present
    choices:
      - present
      - absent
extends_documentation_fragment: f5
author:
  - Tim Rupp (@caphrim007)
'''
EXAMPLES = r'''
- name: Create a LDAP monitor
  bigip_monitor_ldap:
    name: foo
    provider:
      password: secret
      server: lb.mydomain.com
      user: admin
  delegate_to: localhost
'''
RETURN = r'''
parent:
  description: New parent template of the monitor.
  returned: changed
  type: str
  sample: ldap
description:
  description: The description of the monitor.
  returned: changed
  type: str
  sample: Important_Monitor
ip:
  description: The new IP of IP/port definition.
  returned: changed
  type: str
  sample: 10.12.13.14
interval:
  description: The new interval in which to run the monitor check.
  returned: changed
  type: int
  sample: 2
timeout:
  description: The new timeout in which the remote system must respond to the monitor.
  returned: changed
  type: int
  sample: 10
time_until_up:
  description: The new time in which to mark a system as up after first successful response.
  returned: changed
  type: int
  sample: 2
security:
  description: The new Security setting of the resource.
  returned: changed
  type: str
  sample: ssl
debug:
  description: The new Debug setting of the resource.
  returned: changed
  type: bool
  sample: yes
mandatory_attributes:
  description: The new Mandatory Attributes setting of the resource.
  returned: changed
  type: bool
  sample: no
chase_referrals:
  description: The new Chase Referrals setting of the resource.
  returned: changed
  type: bool
  sample: yes
manual_resume:
  description: The new Manual Resume setting of the resource.
  returned: changed
  type: bool
  sample: no
filter:
  description: The new LDAP Filter setting of the resource.
  returned: changed
  type: str
  sample: filter1
base:
  description: The new LDAP Base setting of the resource.
  returned: changed
  type: str
  sample: base
'''
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.basic import env_fallback
try:
    from library.module_utils.network.f5.bigip import F5RestClient
    from library.module_utils.network.f5.common import F5ModuleError
    from library.module_utils.network.f5.common import AnsibleF5Parameters
    from library.module_utils.network.f5.common import cleanup_tokens
    from library.module_utils.network.f5.common import fq_name
    from library.module_utils.network.f5.common import f5_argument_spec
    from library.module_utils.network.f5.common import exit_json
    from library.module_utils.network.f5.common import fail_json
    from library.module_utils.network.f5.common import transform_name
    from library.module_utils.network.f5.common import flatten_boolean
    from library.module_utils.network.f5.ipaddress import is_valid_ip
    from library.module_utils.network.f5.compare import cmp_str_with_none
except ImportError:
    from ansible.module_utils.network.f5.bigip import F5RestClient
    from ansible.module_utils.network.f5.common import F5ModuleError
    from ansible.module_utils.network.f5.common import AnsibleF5Parameters
    from ansible.module_utils.network.f5.common import cleanup_tokens
    from ansible.module_utils.network.f5.common import fq_name
    from ansible.module_utils.network.f5.common import f5_argument_spec
    from ansible.module_utils.network.f5.common import exit_json
    from ansible.module_utils.network.f5.common import fail_json
    from ansible.module_utils.network.f5.common import transform_name
    from ansible.module_utils.network.f5.common import flatten_boolean
    from ansible.module_utils.network.f5.ipaddress import is_valid_ip
    from ansible.module_utils.network.f5.compare import cmp_str_with_none
class Parameters(AnsibleF5Parameters):
    api_map = {
        'timeUntilUp': 'time_until_up',
        'defaultsFrom': 'parent',
        'mandatoryAttributes': 'mandatory_attributes',
        'chaseReferrals': 'chase_referrals',
        'manualResume': 'manual_resume',
        'username': 'target_username',
        'password': 'target_password',
    }
    api_attributes = [
        'timeUntilUp',
        'defaultsFrom',
        'interval',
        'timeout',
        'destination',
        'description',
        'security',
        'mandatoryAttributes',
        'chaseReferrals',
        'debug',
        'manualResume',
        'username',
        'password',
        'filter',
        'base',
    ]
    returnables = [
        'parent',
        'ip',
        'destination',
        'port',
        'interval',
        'timeout',
        'time_until_up',
        'description',
        'security',
        'debug',
        'mandatory_attributes',
        'chase_referrals',
        'manual_resume',
        'filter',
        'base',
    ]
    updatables = [
        'destination',
        'interval',
        'timeout',
        'time_until_up',
        'description',
        'security',
        'debug',
        'mandatory_attributes',
        'chase_referrals',
        'manual_resume',
        'target_username',
        'target_password',
        'filter',
        'base',
    ]
    @property
    def timeout(self):
        if self._values['timeout'] is None:
            return None
        return int(self._values['timeout'])
    @property
    def time_until_up(self):
        if self._values['time_until_up'] is None:
            return None
        return int(self._values['time_until_up'])
    @property
    def mandatory_attributes(self):
        return flatten_boolean(self._values['mandatory_attributes'])
    @property
    def chase_referrals(self):
        return flatten_boolean(self._values['chase_referrals'])
    @property
    def debug(self):
        return flatten_boolean(self._values['debug'])
    @property
    def manual_resume(self):
        return flatten_boolean(self._values['manual_resume'])
    @property
    def security(self):
        if self._values['security'] in ['none', None]:
            return ''
        return self._values['security']
class ApiParameters(Parameters):
    @property
    def ip(self):
        ip, port = self._values['destination'].split(':')
        return ip
    @property
    def port(self):
        ip, port = self._values['destination'].split(':')
        try:
            return int(port)
        except ValueError:
            return port
    @property
    def description(self):
        if self._values['description'] in [None, 'none']:
            return None
        return self._values['description']
class ModuleParameters(Parameters):
    @property
    def ip(self):
        if self._values['ip'] is None:
            return None
        if self._values['ip'] in ['*', '0.0.0.0']:
            return '*'
        elif is_valid_ip(self._values['ip']):
            return self._values['ip']
        else:
            raise F5ModuleError(
                "The provided 'ip' parameter is not an IP address."
            )
    @property
    def parent(self):
        if self._values['parent'] is None:
            return None
        result = fq_name(self.partition, self._values['parent'])
        return result
    @property
    def port(self):
        if self._values['port'] is None:
            return None
        elif self._values['port'] == '*':
            return '*'
        return int(self._values['port'])
    @property
    def destination(self):
        if self.ip is None and self.port is None:
            return None
        destination = '{0}:{1}'.format(self.ip, self.port)
        return destination
    @destination.setter
    def destination(self, value):
        ip, port = value.split(':')
        self._values['ip'] = ip
        self._values['port'] = port
    @property
    def interval(self):
        if self._values['interval'] is None:
            return None
        if 1 > int(self._values['interval']) > 86400:
            raise F5ModuleError(
                "Interval value must be between 1 and 86400"
            )
        return int(self._values['interval'])
    @property
    def type(self):
        return 'ldap'
    @property
    def description(self):
        if self._values['description'] is None:
            return None
        elif self._values['description'] in ['none', '']:
            return ''
        return self._values['description']
class Changes(Parameters):
    def to_return(self):
        result = {}
        try:
            for returnable in self.returnables:
                result[returnable] = getattr(self, returnable)
            result = self._filter_params(result)
        except Exception:
            pass
        return result
class UsableChanges(Changes):
    @property
    def manual_resume(self):
        if self._values['manual_resume'] is None:
            return None
        if self._values['manual_resume'] == 'yes':
            return 'enabled'
        return 'disabled'
class ReportableChanges(Changes):
    @property
    def manual_resume(self):
        return flatten_boolean(self._values['manual_resume'])
    @property
    def ip(self):
        ip, port = self._values['destination'].split(':')
        return ip
    @property
    def port(self):
        ip, port = self._values['destination'].split(':')
        return int(port)
class Difference(object):
    def __init__(self, want, have=None):
        self.want = want
        self.have = have
    def compare(self, param):
        try:
            result = getattr(self, param)
            return result
        except AttributeError:
            return self.__default(param)
    @property
    def parent(self):
        if self.want.parent != self.have.parent:
            raise F5ModuleError(
                "The parent monitor cannot be changed"
            )
    @property
    def destination(self):
        if self.want.ip is None and self.want.port is None:
            return None
        if self.want.port is None:
            self.want.update({'port': self.have.port})
        if self.want.ip is None:
            self.want.update({'ip': self.have.ip})
        if self.want.port in [None, '*'] and self.want.ip != '*':
            raise F5ModuleError(
                "Specifying an IP address requires that a port number be specified"
            )


Answer:         if self.want.destination != self.have.destination: