如何使用java API获取GCP`ManagedInstance`的`accessConfig.getNatIP`?

时间:2016-10-23 11:24:01

标签: java google-compute-engine

我有这段代码来获取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();

但我发现没办法得到这个。

1 个答案:

答案 0 :(得分:0)

%s/\.\n//c 包含实例的网址。然后使用instances.get API获取实例中的外部IP。