#!/usr/bin/python -tt
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

DOCUMENTATION = '''
---
module: rax_clb
short_description: create / delete a load balancer in Rackspace Public Cloud
description:
     - creates / deletes a Rackspace Public Cloud load balancer.
version_added: "1.4"
options:
  algorithm:
    description:
      - algorithm for the balancer being created
    choices: ['RANDOM', 'LEAST_CONNECTIONS', 'ROUND_ROBIN', 'WEIGHTED_LEAST_CONNECTIONS', 'WEIGHTED_ROUND_ROBIN']
    default: LEAST_CONNECTIONS
  api_key:
    description:
      - Rackspace API key (overrides C(credentials))
  credentials:
    description:
      - File to find the Rackspace credentials in (ignored if C(api_key) and
        C(username) are provided)
    default: null
    aliases: ['creds_file']
  meta:
    description:
      - A hash of metadata to associate with the instance
    default: null
  name:
    description:
      - Name to give the load balancer
    default: null
  port:
    description:
      - Port for the balancer being created
    default: 80
  protocol:
    description:
      - Protocol for the balancer being created
    choices: ['DNS_TCP', 'DNS_UDP' ,'FTP', 'HTTP', 'HTTPS', 'IMAPS', 'IMAPv4', 'LDAP', 'LDAPS', 'MYSQL', 'POP3', 'POP3S', 'SMTP', 'TCP', 'TCP_CLIENT_FIRST', 'UDP', 'UDP_STREAM', 'SFTP']
    default: HTTP
  region:
    description:
      - Region to create the load balancer in
    default: DFW
  state:
    description:
      - Indicate desired state of the resource
    choices: ['present', 'absent']
    default: present
  timeout:
    description:
      - timeout for communication between the balancer and the node
    default: 30
  type:
    description:
      - type of interface for the balancer being created
    choices: ['PUBLIC', 'SERVICENET']
    default: PUBLIC
  username:
    description:
      - Rackspace username (overrides C(credentials))
  wait:
    description:
      - wait for the balancer to be in state 'running' before returning
    default: "no"
    choices: [ "yes", "no" ]
  wait_timeout:
    description:
      - how long before wait gives up, in seconds
    default: 300
requirements: [ "pyrax" ]
author: Christopher H. Laco, Matt Martz
notes:
  - The following environment variables can be used, C(RAX_USERNAME),
    C(RAX_API_KEY), C(RAX_CREDS_FILE), C(RAX_CREDENTIALS), C(RAX_REGION).
  - C(RAX_CREDENTIALS) and C(RAX_CREDS_FILE) points to a credentials file
    appropriate for pyrax. See U(https://github.com/rackspace/pyrax/blob/master/docs/getting_started.md#authenticating)
  - C(RAX_USERNAME) and C(RAX_API_KEY) obviate the use of a credentials file
  - C(RAX_REGION) defines a Rackspace Public Cloud region (DFW, ORD, LON, ...)
'''

EXAMPLES = '''
- name: Build a Load Balancer
  gather_facts: False
  hosts: local
  connection: local
  tasks:
    - name: Load Balancer create request
      local_action:
        module: rax_clb
        credentials: ~/.raxpub
        name: my-lb
        port: 8080
        protocol: HTTP
        type: SERVICENET
        timeout: 30
        region: DFW
        wait: yes
        state: present
        meta:
          app: my-cool-app
      register: my_lb
'''

import sys
import os

from types import NoneType

try:
    import pyrax
except ImportError:
    print("failed=True msg='pyrax required for this module'")
    sys.exit(1)

NON_CALLABLES = (basestring, bool, dict, int, list, NoneType)
ALGORITHMS = ['RANDOM', 'LEAST_CONNECTIONS', 'ROUND_ROBIN',
              'WEIGHTED_LEAST_CONNECTIONS', 'WEIGHTED_ROUND_ROBIN']
PROTOCOLS = ['DNS_TCP', 'DNS_UDP', 'FTP', 'HTTP', 'HTTPS', 'IMAPS', 'IMAPv4',
             'LDAP', 'LDAPS', 'MYSQL', 'POP3', 'POP3S', 'SMTP', 'TCP',
             'TCP_CLIENT_FIRST', 'UDP', 'UDP_STREAM', 'SFTP']


def to_dict(obj):
    instance = {}
    for key in dir(obj):
        value = getattr(obj, key)
        if key == 'virtual_ips':
            instance[key] = []
            for vip in value:
                vip_dict = {}
                for vip_key, vip_value in vars(vip).iteritems():
                    if isinstance(vip_value, NON_CALLABLES):
                        vip_dict[vip_key] = vip_value
                instance[key].append(vip_dict)
        elif key == 'nodes':
            instance[key] = []
            for node in value:
                instance[key].append(node.to_dict())
        elif (isinstance(value, NON_CALLABLES) and
                not key.startswith('_')):
            instance[key] = value

    return instance


def cloud_load_balancer(module, state, name, meta, algorithm, port, protocol,
                        vip_type, timeout, wait, wait_timeout):
    for arg in (state, name, port, protocol, vip_type):
        if not arg:
            module.fail_json(msg='%s is required for rax_clb' % arg)

    if int(timeout) < 30:
        module.fail_json(msg='"timeout" must be greater than or equal to 30')

    changed = False
    balancers = []

    clb = pyrax.cloud_loadbalancers

    for balancer in clb.list():
        if name != balancer.name and name != balancer.id:
            continue

        balancers.append(balancer)

    if len(balancers) > 1:
        module.fail_json(msg='Multiple Load Balancers were matched by name, '
                             'try using the Load Balancer ID instead')

    if state == 'present':
        if isinstance(meta, dict):
            metadata = [dict(key=k, value=v) for k, v in meta.items()]

        if not balancers:
            try:
                virtual_ips = [clb.VirtualIP(type=vip_type)]
                balancer = clb.create(name, metadata=metadata, port=port,
                                      algorithm=algorithm, protocol=protocol,
                                      timeout=timeout, virtual_ips=virtual_ips)
                changed = True
            except Exception, e:
                module.fail_json(msg='%s' % e.message)
        else:
            balancer = balancers[0]
            setattr(balancer, 'metadata',
                    [dict(key=k, value=v) for k, v in
                     balancer.get_metadata().items()])
            atts = {
                'name': name,
                'algorithm': algorithm,
                'port': port,
                'protocol': protocol,
                'timeout': timeout
            }
            for att, value in atts.iteritems():
                current = getattr(balancer, att)
                if current != value:
                    changed = True

            if changed:
                balancer.update(**atts)

            if balancer.metadata != metadata:
                balancer.set_metadata(meta)
                changed = True

            virtual_ips = [clb.VirtualIP(type=vip_type)]
            current_vip_types = set([v.type for v in balancer.virtual_ips])
            vip_types = set([v.type for v in virtual_ips])
            if current_vip_types != vip_types:
                module.fail_json(msg='Load balancer Virtual IP type cannot '
                                     'be changed')

        if wait:
            attempts = wait_timeout / 5
            pyrax.utils.wait_for_build(balancer, interval=5, attempts=attempts)

        balancer.get()
        instance = to_dict(balancer)

        result = dict(changed=changed, balancer=instance)

        if balancer.status == 'ERROR':
            result['msg'] = '%s failed to build' % balancer.id
        elif wait and balancer.status not in ('ACTIVE', 'ERROR'):
            result['msg'] = 'Timeout waiting on %s' % balancer.id

        if 'msg' in result:
            module.fail_json(**result)
        else:
            module.exit_json(**result)

    elif state == 'absent':
        if balancers:
            balancer = balancers[0]
            try:
                balancer.delete()
                changed = True
            except Exception, e:
                module.fail_json(msg='%s' % e.message)

            instance = to_dict(balancer)

            if wait:
                attempts = wait_timeout / 5
                pyrax.utils.wait_until(balancer, 'status', ('DELETED'),
                                       interval=5, attempts=attempts)
        else:
            instance = {}

    module.exit_json(changed=changed, balancer=instance)


def main():
    argument_spec = rax_argument_spec()
    argument_spec.update(
        dict(
            algorithm=dict(choices=ALGORITHMS, default='LEAST_CONNECTIONS'),
            meta=dict(type='dict', default={}),
            name=dict(),
            port=dict(type='int', default=80),
            protocol=dict(choices=PROTOCOLS, default='HTTP'),
            state=dict(default='present', choices=['present', 'absent']),
            timeout=dict(type='int', default=30),
            type=dict(choices=['PUBLIC', 'SERVICENET'], default='PUBLIC'),
            wait=dict(type='bool'),
            wait_timeout=dict(default=300),
        )
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        required_together=rax_required_together(),
    )

    algorithm = module.params.get('algorithm')
    meta = module.params.get('meta')
    name = module.params.get('name')
    port = module.params.get('port')
    protocol = module.params.get('protocol')
    state = module.params.get('state')
    timeout = int(module.params.get('timeout'))
    vip_type = module.params.get('type')
    wait = module.params.get('wait')
    wait_timeout = int(module.params.get('wait_timeout'))

    setup_rax_module(module, pyrax)

    cloud_load_balancer(module, state, name, meta, algorithm, port, protocol,
                        vip_type, timeout, wait, wait_timeout)


# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.rax import *

### invoke the module
main()