From a61742e070f399af223e7faff585612f4f30ba99 Mon Sep 17 00:00:00 2001
From: Karim Boumedhel <karimboumedhel@gmail.com>
Date: Fri, 15 Apr 2016 20:23:37 +0200
Subject: [PATCH] Add cloudinit support to ovirt.py module

---
 cloud/misc/ovirt.py | 95 ++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 89 insertions(+), 6 deletions(-)

diff --git a/cloud/misc/ovirt.py b/cloud/misc/ovirt.py
index 02596b441ea..86c769bdb24 100644
--- a/cloud/misc/ovirt.py
+++ b/cloud/misc/ovirt.py
@@ -144,6 +144,48 @@ options:
     default: null
     required: false
     aliases: []
+  instance_dns:
+    description:
+     - define the instance's Primary DNS server
+    required: false
+    aliases: [ dns ]
+    version_added: "2.1"
+  instance_domain:
+    description:
+     - define the instance's Domain
+    required: false
+    aliases: [ domain ]
+    version_added: "2.1"
+  instance_hostname:
+    description:
+     - define the instance's Hostname
+    required: false
+    aliases: [ hostname ]
+    version_added: "2.1"
+  instance_ip:
+    description:
+     - define the instance's IP
+    required: false
+    aliases: [ ip ]
+    version_added: "2.1"
+  instance_netmask:
+    description:
+     - define the instance's Netmask
+    required: false
+    aliases: [ netmask ]
+    version_added: "2.1"
+  instance_rootpw:
+    description:
+     - define the instance's Root password
+    required: false
+    aliases: [ rootpw ]
+    version_added: "2.1"
+  instance_key:
+    description:
+     - define the instance's Authorized key
+    required: false
+    aliases: [ key ]
+    version_added: "2.1"
   state:
     description:
      - create, terminate or remove instances
@@ -205,6 +247,19 @@ ovirt:
     password: secret
     url: https://ovirt.example.com
 
+# starting an instance with cloud init information
+ovirt:
+    instance_name: testansible
+    state: started
+    user: admin@internal
+    password: secret
+    url: https://ovirt.example.com
+    hostname: testansible
+    domain: ansible.local
+    ip: 192.168.1.100
+    netmask: 255.255.255.0
+    gateway: 192.168.1.1
+    rootpw: bigsecret
 
 '''
 
@@ -273,9 +328,23 @@ def create_vm_template(conn, vmname, image, zone):
 
 
 # start instance
-def vm_start(conn, vmname):
+def vm_start(conn, vmname, hostname=None, ip=None, netmask=None, gateway=None,
+             domain=None, dns=None, rootpw=None, key=None):
     vm = conn.vms.get(name=vmname)
-    vm.start()
+    use_cloud_init = False
+    nics = None
+    if hostname or ip or netmask or gateway or domain or dns or rootpw or key:
+        use_cloud_init = True
+    if ip and netmask and gateway:
+        ipinfo = params.IP(address=ip, netmask=netmask, gateway=gateway)
+        nic = params.GuestNicConfiguration(name='eth0', boot_protocol='STATIC', ip=ipinfo, on_boot=True)
+        nics = params.Nics()
+    nics = params.GuestNicsConfiguration(nic_configuration=[nic])
+    initialization=params.Initialization(regenerate_ssh_keys=True, host_name=hostname, domain=domain, user_name='root',
+                                         root_password=rootpw, nic_configurations=nics, dns_servers=dns,
+                                         authorized_ssh_keys=key)
+    action = params.Action(use_cloud_init=use_cloud_init, vm=params.VM(initialization=initialization))
+    vm.start(action=action)
 
 # Stop instance
 def vm_stop(conn, vmname):
@@ -302,7 +371,6 @@ def vm_remove(conn, vmname):
 # Get the VMs status
 def vm_status(conn, vmname):
     status = conn.vms.get(name=vmname).status.state
-    print "vm status is : %s" % status
     return status
 
 
@@ -311,10 +379,8 @@ def get_vm(conn, vmname):
     vm = conn.vms.get(name=vmname)
     if vm == None:
         name = "empty"
-        print "vmname: %s" % name
     else:
         name = vm.get_name()
-        print "vmname: %s" % name
     return name
 
 # ------------------------------------------------------------------- #
@@ -347,6 +413,14 @@ def main():
             disk_int = dict(default='virtio', choices=['virtio', 'ide']),
             instance_os = dict(aliases=['vmos']),
             instance_cores = dict(default=1, aliases=['vmcores']),
+            instance_hostname = dict(aliases=['hostname']),
+            instance_ip = dict(aliases=['ip']),
+            instance_netmask = dict(aliases=['netmask']),
+            instance_gateway = dict(aliases=['gateway']),
+            instance_domain = dict(aliases=['domain']),
+            instance_dns = dict(aliases=['dns']),
+            instance_rootpw = dict(aliases=['rootpw']),
+            instance_key = dict(aliases=['key']),
             sdomain = dict(),
             region = dict(),
         )
@@ -375,6 +449,14 @@ def main():
     vmcores       = module.params['instance_cores']     # number of cores
     sdomain       = module.params['sdomain']            # storage domain to store disk on
     region        = module.params['region']             # oVirt Datacenter
+    hostname      = module.params['instance_hostname']
+    ip            = module.params['instance_ip']
+    netmask       = module.params['instance_netmask']
+    gateway       = module.params['instance_gateway']
+    domain        = module.params['instance_domain']
+    dns           = module.params['instance_dns']
+    rootpw        = module.params['instance_rootpw']
+    key            = module.params['instance_key']
     #initialize connection
     try:
         c = conn(url+"/api", user, password)
@@ -405,7 +487,8 @@ def main():
         if vm_status(c, vmname) == 'up':
             module.exit_json(changed=False, msg="VM %s is already running" % vmname)
         else:
-            vm_start(c, vmname)
+            #vm_start(c, vmname)
+            vm_start(c, vmname, hostname, ip, netmask, gateway, domain, dns, rootpw, key)
             module.exit_json(changed=True, msg="VM %s started" % vmname)
 
     if state == 'shutdown':