#!/usr/bin/env python
from mistansible.helpers import authenticate
from ansible.module_utils.basic import *

DOCUMENTATION = '''
module: mist_images
short_description: Lists all available OS images for a cloud
description:
  - Returns a list of all available OS images that the given cloud supports.
  - I(mist_email) and I(mist_password) can be skipped if I(~/.mist) config file is present.
  - See documentation for config file U(http://mistclient.readthedocs.org/en/latest/cmd/cmd.html).
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
  cloud:
    description:
      - Can be either cloud's id or name
    required: true
  search:
    description:
      - If not provided will return a list with default OS Images for the given cloud
      - If I(all) is provided, will return ALL available OS images
      - If I(other search term) then it will search for specific images
    required: true
author: "Mist.io Inc"
version_added: "1.7.1"

'''

EXAMPLES = '''
- name: List default images for NephoScale cloud
  mist_images:
    mist_email: your@email.com
    mist_password: yourpassword
    cloud: NephoScale
  register: images

- name: Search for gentoo images in cloud with id i984JHdkjhKj
  mist_images:
    mist_email: your@email.com
    mist_password: yourpassword
    cloud: i984JHdkjhKj
    search: gentoo
  register: images
'''


def list_images(module, client):
    cloud_name = module.params.get('cloud')
    clouds = client.clouds(search=cloud_name)
    if not clouds:
        module.fail_json(msg="You have to provide a valid cloud id or title")
    cloud = clouds[0]

    search = module.params.get('search')

    if not search:
        images = []
        for image in cloud.images:
            if image['star']:
                images.append(image)
    elif search == "all":
        images = cloud.images
    else:
        images = cloud.search_image(search)

    return [{'name': image['name'], 'id': image['id']} for image in images]


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'),
            cloud=dict(required=True, type='str'),
            search=dict(required=False, type='str')
        )
    )

    client = authenticate(module)

    images = list_images(module, client)

    module.exit_json(changed=False, images=images)


main()