From fbbc74d5fe6beb7dadba5b5bad9362914fe99109 Mon Sep 17 00:00:00 2001
From: Benno Joy <benno@ansible.com>
Date: Thu, 18 Jun 2015 21:24:22 +0530
Subject: [PATCH] enable the module to provision windows instances

---
 cloud/azure/azure.py | 81 ++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 74 insertions(+), 7 deletions(-)

diff --git a/cloud/azure/azure.py b/cloud/azure/azure.py
index 98984dfb9e6..59f425399cc 100644
--- a/cloud/azure/azure.py
+++ b/cloud/azure/azure.py
@@ -110,6 +110,34 @@ options:
     required: false
     default: 'present'
     aliases: []
+  reset_pass_atlogon:
+    description:
+      - Reset the admin password on first logon for windows hosts
+    required: false
+    default: "no"
+    version_added: "2.0"
+    choices: [ "yes", "no" ]
+  auto_updates:
+    description:
+      - Enable Auto Updates on Windows Machines
+    required: false
+    version_added: "2.0"
+    default: "no"
+    choices: [ "yes", "no" ]
+  enable_winrm:
+    description:
+      - Enable winrm on Windows Machines
+    required: false
+    version_added: "2.0"
+    default: "yes"
+    choices: [ "yes", "no" ]
+  os_type:
+    description:
+      - The type of the os that is gettings provisioned
+    required: false
+    version_added: "2.0"
+    default: "linux"
+    choices: [ "windows", "linux" ]
 
 requirements:
     - "python >= 2.6"
@@ -138,6 +166,29 @@ EXAMPLES = '''
     module: azure
     name: my-virtual-machine
     state: absent
+
+#Create windows machine
+- hosts: all
+  connection: local
+  tasks:
+   - local_action:
+      module: azure
+      name: "ben-Winows-23"
+      hostname: "win123"
+      os_type: windows
+      enable_winrm: yes
+      subscription_id: "{{ azure_sub_id }}"
+      management_cert_path: "{{ azure_cert_path }}"
+      role_size: Small
+      image: 'bd507d3a70934695bc2128e3e5a255ba__RightImage-Windows-2012-x64-v13.5'
+      location: 'East Asia'
+      password: "xxx"
+      storage_account: benooytes
+      user: admin
+      wait: yes
+      virtual_network_name: "{{ vnet_name }}"
+
+
 '''
 
 import base64
@@ -196,7 +247,7 @@ try:
     from azure import WindowsAzureError, WindowsAzureMissingResourceError
     from azure.servicemanagement import (ServiceManagementService, OSVirtualHardDisk, SSH, PublicKeys,
                                          PublicKey, LinuxConfigurationSet, ConfigurationSetInputEndpoints,
-                                         ConfigurationSetInputEndpoint)
+                                         ConfigurationSetInputEndpoint, Listener, WindowsConfigurationSet)
     HAS_AZURE = True
 except ImportError:
     HAS_AZURE = False
@@ -264,6 +315,7 @@ def create_virtual_machine(module, azure):
         True if a new virtual machine and/or cloud service was created, false otherwise
     """
     name = module.params.get('name')
+    os_type = module.params.get('os_type')
     hostname = module.params.get('hostname') or name + ".cloudapp.net"
     endpoints = module.params.get('endpoints').split(',')
     ssh_cert_path = module.params.get('ssh_cert_path')
@@ -295,10 +347,21 @@ def create_virtual_machine(module, azure):
         azure.get_role(name, name, name)
     except WindowsAzureMissingResourceError:
         # vm does not exist; create it
-
-        # Create linux configuration
-        disable_ssh_password_authentication = not password
-        linux_config = LinuxConfigurationSet(hostname, user, password, disable_ssh_password_authentication)
+        
+        if os_type == 'linux':
+            # Create linux configuration
+            disable_ssh_password_authentication = not password
+            vm_config = LinuxConfigurationSet(hostname, user, password, disable_ssh_password_authentication)
+        else:
+            #Create Windows Config
+            vm_config = WindowsConfigurationSet(hostname, password, module.params.get('reset_pass_atlogon'),\
+                                                 module.params.get('auto_updates'), None, user)
+            vm_config.domain_join = None
+            if module.params.get('enable_winrm'):
+                listener = Listener('Http')
+                vm_config.win_rm.listeners.listeners.append(listener)
+            else:
+                vm_config.win_rm = None
 
         # Add ssh certificates if specified
         if ssh_cert_path:
@@ -340,7 +403,7 @@ def create_virtual_machine(module, azure):
                                                              deployment_slot='production',
                                                              label=name,
                                                              role_name=name,
-                                                             system_config=linux_config,
+                                                             system_config=vm_config,
                                                              network_config=network_config,
                                                              os_virtual_hard_disk=os_hd,
                                                              role_size=role_size,
@@ -448,6 +511,7 @@ def main():
             ssh_cert_path=dict(),
             name=dict(),
             hostname=dict(),
+            os_type=dict(default='linux', choices=['linux', 'windows']),
             location=dict(choices=AZURE_LOCATIONS),
             role_size=dict(choices=AZURE_ROLE_SIZES),
             subscription_id=dict(no_log=True),
@@ -461,7 +525,10 @@ def main():
             state=dict(default='present'),
             wait=dict(type='bool', default=False),
             wait_timeout=dict(default=600),
-            wait_timeout_redirects=dict(default=300)
+            wait_timeout_redirects=dict(default=300),
+            reset_pass_atlogon=dict(type='bool', default=False),
+            auto_updates=dict(type='bool', default=False),
+            enable_winrm=dict(type='bool', default=True),
         )
     )
     if not HAS_AZURE: