PCextreme / cloudstack-php

Native PHP Cloudstack client
MIT License
10 stars 9 forks source link

deployVirtualMachine build unexpected query string for parameter iptonetworklist #32

Open danielcosta opened 3 years ago

danielcosta commented 3 years ago

Hello there, I got the very same problem reported on issue #1 but here I'll try to explain how to reproduce the case.

On deployVirtualMachine command the API accepts an array for parameter iptonetworklist, which is formatted this way if you create a VM directly from CloudStack UI: &iptonetworklist[0].networkid=b6170452-820c-4a5b-bb76-6a22a0266c0d&iptonetworklist[0].ip=10.1.1.100&iptonetworklist[0].mac=DC-FE-49-00-66-0D. The key [0] on this array can be incremented when you associate more than one network to the new VM.

The point is that the method protected function buildQueryString(array $params) on PCextreme\Cloudstack\Client is not able to build the query string array like expected by CloudStack API on this command, giving Notice: Undefined index: key if we try to send the array data with something like this:

  "iptonetworklist" => array:1 [
    0 => array:3 [
      "mac" => "54-77-2F-BA-67-74"
      "networkid" => "b6170452-820c-4a5b-bb76-6a22a0266c0d"
      "ip" => "10.1.1.50"
    ]
  ]

Otherwise, if we try to send the array as expected by the method, like the example bellow:

  "iptonetworklist" => array:3 [
    0 => array:2 [
      "key" => "mac"
      "value" => "54-77-2F-BA-67-74"
    ]
    1 => array:2 [
      "key" => "networkid"
      "value" => "b6170452-820c-4a5b-bb76-6a22a0266c0d"
    ]
    2 => array:2 [
      "key" => "ip"
      "value" => "10.1.1.50"
    ]
  ]

We get the query string as this: &iptonetworklist[0].key=mac&iptonetworklist[0].value=54-77-2F-BA-67-74&iptonetworklist[1].key=networkid&iptonetworklist[1].value=b6170452-820c-4a5b-bb76-6a22a0266c0d&iptonetworklist[2].key=ip&iptonetworklist[2].value=10.1.1.50

This way the method will not give the notice, but CloudStack API will treat each key on the array as a different entry in the map, returning this error:

ClientException {#3412
  #response: array:1 [
    "deployvirtualmachineresponse" => array:4 [
      "uuidList" => []
      "errorcode" => 431
      "cserrorcode" => 4350
      "errortext" => "Unable to translate and find entity with networkId: null"
    ]
  ]
  #message: "Unable to translate and find entity with networkId: null"
  #code: 431
  #file: "/var/www/html/vendor/pcextreme/cloudstack/src/Client.php"
  #line: 405

I'm not sure if this is the only endpoint treating array on request this way, but the way buildQueryString() method parses input and build query string is not working for the command deployVirtualMachine.

If you could confirm if there are other commands receiving and parsing arrays for requests I could eventually send a PR to fix this, but I'm afraid we fix for this command and break for others.

Please let me know if I can help sending other evidences of the case.

For the reference, I'm using CloudStack 4.15.0.0 here.