0

Here is the public function "should create a virtual machine" with validation,

public function deployVirtualMachine($serviceOfferingId, $templateId, $zoneId, $account = "", $diskOfferingId = "") {

        if (empty($serviceOfferingId)) {
            throw new CloudStackClientException(sprintf(MISSING_ARGUMENT_MSG, "serviceOfferingId"), MISSING_ARGUMENT);
        }

        if (empty($templateId)) {
            throw new CloudStackClientException(sprintf(MISSING_ARGUMENT_MSG, "templateId"), MISSING_ARGUMENT);
        }

        if (empty($zoneId)) {
            throw new CloudStackClientException(sprintf(MISSING_ARGUMENT_MSG, "zoneId"), MISSING_ARGUMENT);
        }

        return $this->request("deployVirtualMachine", array(
            'serviceofferingid' => $serviceOfferingId,
            'templateid' => $templateId,
            'zoneid' => $zoneId,
            'account' => $account,
            'diskofferingid' => $diskOfferingId,
            'displayname' => $displayName,
    }

I'm trying to call this function, but I keep getting exception " $templateId" is missing. but I'm pretty sure its defined here in my array.

  $params = array(

           $serviceOfferingId => '85d06496-bb75-41fb-9358-4ab919e03fe4',
            $templateId => 'c0989cf6-2da5-11e4-a846-726c7bbb864f',
           $zoneId => '7cd483ab-5aad-458b-b5e1-0e270310f41c',
           $name => 'srv11'
 );


echo $cloudstack->deployVirtualMachine($params);

Any help would be highly appreciated

Thank you

3 Answers 3

1

You're only passing 1 parameter, an array.

Try this instead:

$cloudstack->deployVirtualMachine('85d06496-bb75-41fb-9358-4ab919e03fe4', 'c0989cf6-2da5-11e4-a846-726c7bbb864f', '7cd483ab-5aad-458b-b5e1-0e270310f41c', 'srv11');

Your method is expecting 5 parameters (3 obligatory, 2 optional). $params is being received as $serviceOfferingId.

Either pass in the values individually, or change the signature of the method to accept a single parameter.

For example:

public function deployVirtualMachine($params) {

    if (!isset($params['serviceOfferingId'])) {
        throw new CloudStackClientException(sprintf(MISSING_ARGUMENT_MSG, "serviceOfferingId"), MISSING_ARGUMENT);
    }

    // etc...
}

One more thing, by using $serviceOfferingId as an array key, you're saying that the key should be the value of variable $serviceOfferingId. It doesn't seem that that's what you want. Use a string instead; 'serviceOfferingId'.

$serviceOfferingId = 'foo';

$params = array(
    $serviceOfferingId => 'bar'
);

Produces:

Array
(
    [foo] => bar
)

What you want is:

$params = array(
    'serviceOfferingId' => 'bar'
);

Which would produce:

Array
(
    [serviceOfferingId] => bar
)
Sign up to request clarification or add additional context in comments.

1 Comment

Hey Ayman, I'm getting "PHP Catchable fatal error: Object of class stdClass could not be converted to string" any idea why?
1

Your method expects 5 single parameters of type string, in the method invocation you pass one single parameter of type array - what do you expect?

call it this way:

 echo $cloudstack->deployVirtualMachine('85d06496-bb75-41fb-9358-4ab919e03fe4',
           'c0989cf6-2da5-11e4-a846-726c7bbb864f',
               '7cd483ab-5aad-458b-b5e1-0e270310f41c',
                  null,
                   'srv11' );

Comments

1

Another more weird solution would be, to pass the array $params into a closure:

      public $deployVirtualMachine = function ()
      {
          ....

Params Array (according to Aymans answer)

      $serviceOfferingId = 0,
      $templateId = 1;
      $zoneId = 2;
      $name = 3;

      $params = array(
        $serviceOfferingId => '85d06496-bb75-41fb-9358-4ab919e03fe4',
        $templateId => 'c0989cf6-2da5-11e4-a846-726c7bbb864f',
        $zoneId => '7cd483ab-5aad-458b-b5e1-0e270310f41c',
        $name => 'srv11'
      );

and then:

      call_user_func_array ( 'deployVirtualMachine ', $params );

That would pass the array, but renders as more or less unreadable code :/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.