google-api - 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 ?
解决方案
我认为这一行:
$googleNetworkInterfaceObj->setAccessConfigs($networkConfig);
应该:
$googleNetworkInterfaceObj->setAccessConfigs(array($networkConfig));
如果这不起作用,则其他地方可能会出现另一个错误。
推荐阅读
- python - Python循环(for或while)添加只要不超过给定条件,就从列表中不断添加值
- firebase - 找不到变量:firebase
- c# - 来自先前结果的 LINQ 条件
- file - 多个较大的请求在 IIS 上冻结
- typescript - 使用 sequelize-typescript 指定空列的正确方法是什么?
- javascript - IndexedDb 更改时触发的事件?
- html - Django 图像 url 重定向是通过 HTML 模板的链接而不是媒体文件?
- java - 是否有另一种解决方案可以使用 Kafka Streams API 找到 Kafka 主题中组合键的最大重复次数?
- django - JSONDecodeError 异常值:预期值:第 1 行第 1 列(字符 0)
- java - 如何解决 Hibernate 中的“X 不是 Java 标识符”问题?