@@ -33,25 +33,25 @@ def parse_json(json_data):
3333 data ["username" ] = external_host_details ["username" ]
3434 data ["password" ] = external_host_details ["password" ]
3535 data ["network_switch" ] = external_host_details ["network_switch" ]
36- data ["default_vhd_path " ] = external_host_details . get ( "default_vhd_path" , "C: \\ ProgramData \\ Microsoft \\ Windows \\ Virtual Hard Disks" )
37- data ["default_vm_path " ] = external_host_details . get ( "default_vm_path" , "C: \\ ProgramData \\ Microsoft \\ Windows \\ Hyper-V" )
36+ data ["vhd_path " ] = external_host_details [ "vhd_path" ]
37+ data ["vm_path " ] = external_host_details [ "vm_path" ]
3838
3939 external_vm_details = json_data ["externaldetails" ].get ("virtualmachine" , [])
4040 if external_vm_details :
41- data ["template_type" ] = external_vm_details .get ("template_type" , "template" )
41+ data ["template_type" ] = external_vm_details ["template_type" ]
42+ data ["generation" ] = external_vm_details .get ("generation" , 1 )
4243 data ["template_path" ] = external_vm_details .get ("template_path" , "" )
43- data ["vhd_size_gb" ] = external_vm_details .get ("vhd_size_gb" , 25 )
4444 data ["iso_path" ] = external_vm_details .get ("iso_path" , "" )
45- data ["generation " ] = external_vm_details .get ("generation " , 2 )
45+ data ["vhd_size_gb " ] = external_vm_details .get ("vhd_size_gb " , "" )
4646
47- data ["cpus" ] = json_data ["cloudstack.vm.details" ]. get ( "cpus" , 2 )
48- data ["memory" ] = json_data ["cloudstack.vm.details" ]. get ( "minRam" , 536870912 )
47+ data ["cpus" ] = json_data ["cloudstack.vm.details" ][ "cpus" ]
48+ data ["memory" ] = json_data ["cloudstack.vm.details" ][ "minRam" ]
4949
5050 nics = json_data ["cloudstack.vm.details" ].get ("nics" , [])
5151 data ["nics" ] = []
5252 for nic in nics :
53- data ["interfaces " ].append ({
54- "mac" : nic ["mac_address " ],
53+ data ["nics " ].append ({
54+ "mac" : nic ["mac " ],
5555 "vlan" : nic ["broadcastUri" ].replace ("vlan://" , "" )
5656 })
5757
@@ -77,13 +77,13 @@ def succeed(data):
7777
7878
7979def run_powershell_ssh_int (command , url , username , password ):
80- # print(f"[INFO] Connecting to {url} as {username}...")
80+ print (f"[INFO] Connecting to { url } as { username } ..." )
8181 ssh = paramiko .SSHClient ()
8282 ssh .set_missing_host_key_policy (paramiko .AutoAddPolicy ())
8383 ssh .connect (url , username = username , password = password )
8484
8585 ps_command = f'powershell -NoProfile -Command "{ command .strip ()} "'
86- # print(f"[INFO] Executing: {ps_command}")
86+ print (f"[INFO] Executing: { ps_command } " )
8787 stdin , stdout , stderr = ssh .exec_command (ps_command )
8888
8989 output = stdout .read ().decode ().strip ()
@@ -108,17 +108,14 @@ def create(data):
108108 memory = data ["memory" ]
109109 memory_mb = int (memory ) / 1024 / 1024
110110 template_path = data ["template_path" ]
111- vhd_path = data ["default_vhd_path " ] + "\\ " + vm_name + ".vhdx"
111+ vhd_path = data ["vhd_path " ] + "\\ " + vm_name + ".vhdx"
112112 vhd_size_gb = data ["vhd_size_gb" ]
113113 generation = data ["generation" ]
114114 iso_path = data ["iso_path" ]
115- switch_name = data ["switch_name " ]
116- vm_path = data ["default_vm_path " ]
115+ network_switch = data ["network_switch " ]
116+ vm_path = data ["vm_path " ]
117117 template_type = data .get ("template_type" , "template" )
118118
119- if (data ["mac_address" ] == "" ):
120- fail ("Mac address not found" )
121-
122119 vhd_created = False
123120 vm_created = False
124121 vm_started = False
@@ -129,31 +126,35 @@ def create(data):
129126 )
130127 if template_type == "iso" :
131128 if (iso_path == "" ):
132- fail ("ISO path is required" )
129+ fail ("Missing required field in JSON: iso_path" )
130+ if (vhd_size_gb == "" ):
131+ fail ("Missing required field in JSON: vhd_size_gb" )
133132 command += (
134133 f'-NewVHDPath \\ "{ vhd_path } \\ " -NewVHDSizeBytes { vhd_size_gb } GB; '
135134 f'Add-VMDvdDrive -VMName \\ "{ vm_name } \\ " -Path \\ "{ iso_path } \\ "; '
136135 )
137136 else :
138137 if (template_path == "" ):
139- fail ("Template path is required " )
138+ fail ("Missing required field in JSON: template_path " )
140139 run_powershell_ssh_int (f'Copy-Item \\ "{ template_path } \\ " \\ "{ vhd_path } \\ "' , data ["url" ], data ["username" ], data ["password" ])
141140 vhd_created = True
142141 command += f'-VHDPath \\ "{ vhd_path } \\ "; '
143142
144143 run_powershell_ssh_int (command , data ["url" ], data ["username" ], data ["password" ])
145144 vm_created = True
146145
147- command = (
148- f'Set-VMProcessor -VMName \\ "{ vm_name } \\ " -Count \\ "{ cpus } \\ "; '
149- f'Connect-VMNetworkAdapter -VMName \\ "{ vm_name } \\ " -SwitchName \\ "{ switch_name } \\ "; '
150- f'Set-VMFirmware -VMName "{ vm_name } " -EnableSecureBoot Off; '
151- )
146+ command = f'Remove-VMNetworkAdapter -VMName \\ "{ vm_name } \\ " -Name \\ "Network Adapter\\ " -ErrorAction SilentlyContinue; '
147+ run_powershell_ssh_int (command , data ["url" ], data ["username" ], data ["password" ])
148+
149+ command = f'Set-VMProcessor -VMName \\ "{ vm_name } \\ " -Count \\ "{ cpus } \\ "; '
150+ if (generation == 2 ):
151+ command += f'Set-VMFirmware -VMName "{ vm_name } " -EnableSecureBoot Off; '
152+
152153 run_powershell_ssh_int (command , data ["url" ], data ["username" ], data ["password" ])
153154
154155 for idx , nic in enumerate (data ["nics" ]):
155156 adapter_name = f"NIC{ idx + 1 } "
156- run_powershell_ssh_int (f'Add-VMNetworkAdapter -VMName "{ vm_name } " -Name "{ adapter_name } "' , data ["url" ], data ["username" ], data ["password" ])
157+ run_powershell_ssh_int (f'Add-VMNetworkAdapter -VMName "{ vm_name } " -SwitchName \\ " { network_switch } \\ " - Name "{ adapter_name } "' , data ["url" ], data ["username" ], data ["password" ])
157158 run_powershell_ssh_int (f'Set-VMNetworkAdapter -VMName "{ vm_name } " -Name "{ adapter_name } " -StaticMacAddress "{ nic ["mac" ]} "' , data ["url" ], data ["username" ], data ["password" ])
158159 run_powershell_ssh_int (f'Set-VMNetworkAdapterVlan -VMName "{ vm_name } " -VMNetworkAdapterName "{ adapter_name } " -Access -VlanId "{ nic ["vlan" ]} "' , data ["url" ], data ["username" ], data ["password" ])
159160
@@ -216,7 +217,7 @@ def resume(data):
216217def create_snapshot (data ):
217218 snapshot_name = data ["snapshot_name" ]
218219 if snapshot_name == "" :
219- fail ("Missing snapshot_name in parameters " )
220+ fail ("Missing required field in JSON: snapshot_name " )
220221 command = f'Checkpoint-VM -VMName \\ "{ data ["vmname" ]} \\ " -SnapshotName \\ "{ snapshot_name } \\ "'
221222 run_powershell_ssh (command , data ["url" ], data ["username" ], data ["password" ])
222223 succeed ({"status" : "success" , "message" : f"Snapshot '{ snapshot_name } ' created" })
@@ -225,7 +226,7 @@ def create_snapshot(data):
225226def restore_snapshot (data ):
226227 snapshot_name = data ["snapshot_name" ]
227228 if snapshot_name == "" :
228- fail ("Missing snapshot_name in parameters " )
229+ fail ("Missing required field in JSON: snapshot_name " )
229230 command = f'Restore-VMSnapshot -VMName \\ "{ data ["vmname" ]} \\ " -Name \\ "{ snapshot_name } \\ " -Confirm:$false'
230231 run_powershell_ssh (command , data ["url" ], data ["username" ], data ["password" ])
231232 succeed ({"status" : "success" , "message" : f"Snapshot '{ snapshot_name } ' restored" })
@@ -234,7 +235,7 @@ def restore_snapshot(data):
234235def delete_snapshot (data ):
235236 snapshot_name = data ["snapshot_name" ]
236237 if snapshot_name == "" :
237- fail ("Missing snapshot_name in parameters " )
238+ fail ("Missing required field in JSON: snapshot_name " )
238239 command = f'Remove-VMSnapshot -VMName \\ "{ data ["vmname" ]} \\ " -Name \\ "{ snapshot_name } \\ " -Confirm:$false'
239240 run_powershell_ssh (command , data ["url" ], data ["username" ], data ["password" ])
240241 succeed ({"status" : "success" , "message" : f"Snapshot '{ snapshot_name } ' deleted" })
0 commit comments