How to receive public and private IPs while creating an instance in GCP using java sdks?

Viewed 82

Whenever I create a virtual machine in AZURE and AWS with java SDK, the return object always give me public and private IPs.

Now I am exploring GCP java SDK and have successfully created an instance using it but how will I get ip addresses in return?

 Instance instance = new Instance()
                .setName(createInstance.getInstanceName())
                .setMachineType(createInstance.getMachineTypeUrl()) //can provide 'prepare url' option
                .setDisks(attachedDiskList)
                .setNetworkInterfaces(networkInterfaceList)
                .setCpuPlatform(createInstance.getCpuPlatform());

Operation instanceCreated = gcpCredentialService.getGcpClient()
            .instances()
            .insert(GcpContext.getContext().getServiceAccountProjectId(), completeRegion, instance)
            .execute();

This object instanceCreated does not return public and private IPS in return.

I am new to GCP and struggling in this part.

1 Answers

Google's API (!) documentation is excellent and I encourage you to become familiar with navigating it as it will prove very helpful. While each SDK (library) is documented too, the underlying REST API methods and types are definitive and it should be straightforward to work upwards into your preferred language(s).

Compute Engine's [instances.insert] returns an Operation because the method is asynchronous. You'll need to query the Operation's state for successful completion of the operation (i.e. instance created) and then you can query the instance (instances.get, the response to which contains the properties that you need.

The documentation contains:

Related