PHP:在 Google 云计算中创建服务器实例时出错

PHP: Error in creating Server Instance in Google Cloud Compute

在 PHP 中使用 Google 云计算的 API 时,我能够启动、停止、删除实例以及创建和删除磁盘。

但是,在尝试创建实例时,我不断收到此错误

字段值无效 '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

这是我提出的要求

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

非常感谢任何帮助,谢谢。

好的,这个解决方案真的很简单(而且很愚蠢)

而不是

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

应该是

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