Search code examples
phpvirtual-machinegoogle-cloud-endpointsinstances

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.


Solution

  • 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,
                )
              ));