我有这段代码来获取GCP项目中实例的外部ip
private static void printInstances(Compute compute, String projectId) throws IOException {
final Compute.Instances.List instances = compute.instances().list(projectId, zoneName);
final InstanceList list = instances.execute();
if ( list.getItems() == null ) {
System.out.println("No instances found. Sign in to the Google APIs Console and create an instance at: code.google.com/apis/console");
} else {
for ( final Instance instance : list.getItems() ) {
//System.out.println(instance.toPrettyString());
System.out.println("------------- " + instance.getName() + " (" + instance.getId() + ")");
final List<NetworkInterface> networkInterfaces = instance.getNetworkInterfaces();
for ( final NetworkInterface networkInterface : networkInterfaces ) {
String extIP = null;
final List<AccessConfig> accessConfigs = networkInterface.getAccessConfigs();
for ( final AccessConfig accessConfig : accessConfigs ) { // More than one?
extIP = accessConfig.getNatIP();
}
System.out.println(" Private=[" + networkInterface.getNetworkIP() + "] Public=[" + extIP + "]");
}
}
}
}
我希望获得与GCP accessConfig.getNatIP
相同的(意味着ManagedInstance
)表单实例。
像这样:
Compute.InstanceGroupManagers.ListManagedInstances listInstances =
compute.instanceGroupManagers().listManagedInstances(projectId, zoneName, groupName);
List<ManagedInstance> list = listInstances.execute().getManagedInstances();
但我发现没办法得到这个。