#!/usr/bin/env python
import os
import ConfigParser

from ansible.module_utils.basic import *

from mist.client import MistClient

DOCUMENTATION = '''
---
module: mist_providers
short_description: Lists all available providers supported by the mist.io service
description:
  - Returns a list of all available providers that you can add and control through mist.io service
  - I(mist_email) and I(mist_password) can be skipped if I(~/.mist) config file is present.
  - See mist.client documentation for config file U(http://mistclient.readthedocs.org/en/latest/cmd/cmd.html)
requirements:
  - mist.client
options:
  mist_email:
    description:
      - Email to login to the mist.io service
    required: false
  mist_password:
    description:
      - Password to login to the mist.io service
    required: false
  mist_uri:
    default: https://mist.io
    description:
      - Url of the mist.io service. By default https://mist.io. But if you have a custom installation of mist.io you can provide the url here
    required: false
  provider:
    choices:
      - ec2
      - rackspace
      - bare_metal
      - gce
      - openstack
      - linode
      - nephoscale
      - digitalocean
      - docker
      - hpcloud
      - softlayer
      - all
    default: all
    description:
      - By default I(all), which returns all supported providers by the mist.io service.
      - You can explicitly set it to one of the choices to see only this provider-specific information
    required: false
author: "Chris Loukas <commixon@gmail.com>"
version_added: "1.7.1"
'''

EXAMPLES = '''
- name: List supported providers, simple case
  mist_providers:
    mist_email: your@email.com
    mist_password: yourpassword
    provider: all
  register: providers

- name: List supported provider having ~/.mist config file present
  mist_providers:
    provider: all
  register: providers

- name: List only ec2 provider options
  mist_providers:
    mist_email: your@email.com
    mist_password: yourpassword
    provider: ec2
  register: providers
'''


def authenticate(module):
    home_path = os.getenv("HOME")
    config_path = os.path.join(home_path, ".mist")
    config = ConfigParser.ConfigParser()

    mist_uri = module.params.get('mist_uri')
    mist_email = module.params.get('mist_email')
    mist_password = module.params.get('mist_password')


    # Set default mist uri
    config.add_section("mist.io")
    config.set("mist.io", "mist_uri", "https://mist.io")

    # Set default credentials
    config.add_section("mist.credentials")
    config.set("mist.credentials", "email", None)
    config.set("mist.credentials", "password", None)

    # Read configuration file
    if os.path.isfile(config_path):
            config.readfp(open(config_path))

    mist_uri = config.get("mist.io", "mist_uri")
    if not mist_email:
        mist_email = config.get("mist.credentials", "email") or ""

    if not mist_password:
        mist_password = config.get("mist.credentials", "password") or ""

    return init_client(mist_uri, mist_email, mist_password)


def init_client(mist_uri="https://mist.io", email=None, password=None):
    client = MistClient(mist_uri, email, password)
    return client


def supported_providers(module, client):
    provider = module.params.get('provider')

    providers = client.supported_providers

    if provider == "all":
        return providers
    else:
        chosen_providers = []
        for prov in providers:
            if provider in prov['provider']:
                chosen_providers.append(prov)
        return chosen_providers


def main():
    module = AnsibleModule(
        argument_spec=dict(
            mist_uri=dict(default='https://mist.io', type='str'),
            mist_email=dict(required=False, type='str'),
            mist_password=dict(required=False, type='str'),
            provider=dict(required=False, default='all', type='str', choices=['ec2', 'rackspace', 'bare_metal', 'gce',
                                                                              'openstack', 'linode', 'nephoscale',
                                                                              'digitalocean', 'docker', 'hpcloud',
                                                                              'softlayer', 'all'])
        )
    )

    client = authenticate(module)

    result = supported_providers(module, client)

    module.exit_json(changed=True, supported_providers=result)


main()