PHP:在Google Cloud Compute中创建服务器实例时出错

时间:2015-10-02 22:53:34

标签: php virtual-machine google-cloud-endpoints instances

在PHP中使用Google Cloud Compute的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);

任何帮助都将受到高度赞赏,谢谢。

1 个答案:

答案 0 :(得分:0)

好的解决方案非常简单(而且很傻)

而不是

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