简体   繁体   中英

PHP: Error in creating Server Instance in Google Cloud Compute

While using Google Cloud Compute's API in PHP, I am able to start, stop, delete instances as well as create and delete disks.

However, when trying to create an Instance, I keep getting this error

Invalid value for field 'resource.disks'

PHP Fatal error:  Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/compute/v1/projects/project/zones/zone/instances: (400) Invalid value for field 'resource.disks': ''.  Boot disk must be specified.' in /var/www/html/google/google-api-php-client/src/Google/Http/REST

Here is the request I am making

self::connectClient();

$computeService = new Google_Service_Compute($this->client);

if ($this->client->getAccessToken()) 
{
    $googleNetworkInterfaceObj = new Google_Service_Compute_NetworkInterface();
    $network = self::DEFAULT_NETWORK;
    $googleNetworkInterfaceObj->setNetwork($network);

    $diskObj = self::getDisk($instance_name);

    $new_instance = new Google_Service_Compute_Instance();
    $new_instance->setName($instance_name);
    $new_instance->setMachineType(self::DEFAULT_MACHINE_TYPE);
    $new_instance->setNetworkInterfaces(array($googleNetworkInterfaceObj));
    $new_instance->setDisks(array(
                    "source"=>$diskObj->selfLink,
                    "boot"=>true,
                    "type"=>"PERSISTENT",
                    "deviceName"=>$diskObj->name,
                   ));

   $insertInstance = $computeService->instances->insert(self::DEFAULT_PROJECT,self::DEFAULT_ZONE_NAME, $new_instance);

Any help will be highly appreciated, thank you.

Ok the solution was really simple (and silly)

Instead of

$new_instance->setDisks(array(
                    "source"=>$diskObj->selfLink,
                    "boot"=>true,
                    "type"=>"PERSISTENT",
                    "deviceName"=>$diskObj->name,
                   ));

It's supposed to be

$new_instance->setDisks(array(
         array(
            'source'=>self::getDisk($instance_name)->selfLink,
            'boot'=>true,
            'type' => "PERSISTENT",
            'deviceName'=>self::getDisk($instance_name)->name,
            )
          ));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM