#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2012, D square NV
# Written by Jeroen Hoekx <jeroen.hoekx@dsquare.be>
#
# 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 = '''
---
author: Jeroen Hoekx
module: virt_guest
short_description: Define a libvirt guest
description:
  - This module creates a libvirt guest based on a given XML definition.
  - This module requires the libvirt module.
version_added: "0.9"
options:
  connection:
    description:
    - The connection string for libvirt.
    default: qemu:///system
  guest:
    description:
    - The name of the libvirt domain.
    required: true
  state:
    choices: [ "present", "absent" ]
    default: "present"
    description:
    - Create or remove the VM
    required: false
  src:
    description:
    - The path of the libvirt XML definition.
    required: false
examples:
  - description: Create a guest
    code: virt_guest guest=${name} src=templates/virt-${name}.xml
  - description: Remove a guest
    code: virt_guest guest=${name} state=absent
requirements: [ "libvirt" ]
notes:
  - Run this on the libvirt host.
  - This returns a provisioning_status variable you can register to create groups.
    Possible values are: provisioned, unprovisioned, absent
'''

import sys

try:
    import libvirt
except ImportError:
    print "failed=True msg='libvirt python module unavailable'"
    sys.exit(1)

def main():

    module = AnsibleModule(
        argument_spec = dict(
            connection = dict(default='qemu:///system'),
            guest = dict(required=True, aliases=['domain']),
            state = dict(choices=['present','absent'], default='present'),
            src = dict(),
        ),
    )

    connection = module.params['connection']
    guest = module.params['guest']
    state = module.params['state']
    src = module.params['src']

    changed = False
    provisioning_status = ''

    try:
        conn = libvirt.open(connection)
    except Exception, e:
        module.fail_json(msg=str(e))
 
    try:
        domain = conn.lookupByName(guest)
    except libvirt.libvirtError:
        domain = None

    if state == 'present' and domain:
        provisioning_status = 'provisioned'
    if state == 'present' and not domain:
        if not src:
            module.fail_json(msg="Parameter 'src' not defined.")
        try:
            conn.defineXML( open(src,'r').read() )
        except OSError, e:
            module.fail_json(msg=str(e))
        changed = True
        provisioning_status = 'unprovisioned'
    elif state == 'absent' and domain:
        domain.undefine()
        changed = True
        provisioning_status = 'absent'

    module.exit_json(changed=changed, provisioning_status=provisioning_status)

# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()
