首页 > 解决方案 > Google Compute API: new instance with external IP

问题描述

I'm trying to dynamically create Compute instances on Google Cloud via PHP using API.

Using csmartinez Cloud API sample, I managed to create my instance and can see it running on Google Console.

I need to assign an external IP address to this newly created instance. Based on the API and Google PHP library, I then added:

$ipName = "foo";
$addr = new Google_Service_Compute_Address();
$addr->setName($ipName);
$response = $service->addresses->insert($project, $ipZone, $addr);

Since this call has a "pending" status at first, I added a sleep(5) so I can then get the newly created static IP:

$response = $service->addresses->get($project, $ipZone, $ipName);
$ip = $response->address;

which runs well and gives me the correct IP address. I then continue and try to create my instance while assigning the new IP:

$networkConfig = new Google_Service_Compute_AccessConfig();
$networkConfig->setNatIP($ip);
$networkConfig->setType("ONE_TO_ONE_NAT");
$networkConfig->setName("External NAT");
$googleNetworkInterfaceObj->setAccessConfigs($networkConfig);

The static IP is created, the instance is created, but IP is not assigned to the instance. To remove my doubt about the pending status, I also tried to assign an already created static IP to my instance, thus using:

$networkConfig->setNatIP("xxx.xxx.xxx.xxx");

With no more success... What am I missing here ?

标签: google-apigoogle-cloud-platformgoogle-compute-engine

解决方案


我认为这一行:

$googleNetworkInterfaceObj->setAccessConfigs($networkConfig);

应该:

$googleNetworkInterfaceObj->setAccessConfigs(array($networkConfig));

如果这不起作用,则其他地方可能会出现另一个错误。


推荐阅读