Copilot commented on code in PR #11619:
URL: https://github.com/apache/cloudstack/pull/11619#discussion_r2341135578


##########
plugins/hypervisors/external/src/main/java/org/apache/cloudstack/hypervisor/external/provisioner/ExternalPathPayloadProvisioner.java:
##########
@@ -647,7 +654,7 @@ protected VirtualMachine.PowerState 
parsePowerStateFromResponse(UserVmVO userVmV
             return getPowerStateFromString(response);
         }
         try {
-            JsonObject jsonObj = new 
JsonParser().parse(response).getAsJsonObject();
+            JsonObject jsonObj = 
JsonParser.parseString(response).getAsJsonObject();

Review Comment:
   The deprecated `new JsonParser().parse()` method has been replaced with 
`JsonParser.parseString()`, but this change should be consistent throughout the 
file. The same pattern should be applied to line 702 where 
`JsonParser.parseString()` is used in the new code.



##########
extensions/HyperV/hyperv.py:
##########
@@ -210,6 +210,22 @@ def status(self):
             power_state = "poweroff"
         succeed({"status": "success", "power_state": power_state})
 
+    def statuses(self):
+        command = 'Get-VM | Select-Object Name, State | ConvertTo-Json'
+        vms = json.loads(self.run_ps(command))

Review Comment:
   This code doesn't handle the case where `Get-VM` returns no VMs, which would 
result in empty output and cause `json.loads()` to fail. Consider adding error 
handling for empty responses or PowerShell errors.
   ```suggestion
           output = self.run_ps(command)
           if not output or output.strip() in ("", "null"):
               vms = []
           else:
               try:
                   vms = json.loads(output)
               except json.JSONDecodeError:
                   fail("Failed to parse VM status output: " + output)
   ```



##########
extensions/Proxmox/proxmox.sh:
##########
@@ -285,6 +285,38 @@ status() {
     echo "{\"status\": \"success\", \"power_state\": \"$powerstate\"}"
 }
 
+statuses() {
+    local response
+    response=$(call_proxmox_api GET "/nodes/${node}/qemu")
+
+    if [[ -z "$response" ]]; then
+        echo '{"status":"error","message":"empty response from Proxmox API"}'
+        return 1
+    fi
+
+    if ! echo "$response" | jq empty >/dev/null 2>&1; then
+        echo '{"status":"error","message":"invalid JSON response from Proxmox 
API"}'
+        return 1
+    fi
+
+    echo "$response" | jq -c '
+      def map_state(s):
+        if   s=="running" then "poweron"
+        elif s=="stopped" then "poweroff"
+        else "unknown" end;
+
+      {
+        status: "success",
+        power_state: (
+          .data
+          | map(select(.template != 1))
+          | map({ ( (.name // (.vmid|tostring)) ): map_state(.status) })
+          | add

Review Comment:
   The `add` function will fail if the array is empty (no VMs found), causing a 
jq error. This should be handled by providing a fallback empty object: `| add 
// {}`
   ```suggestion
             | add // {}
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to