I am looking for zone for an instance. I have instance details like id and name. Is there an api for the same in GCP?
Generally Compute Engine service methods require the Project and Zone to enumerate instances but if you use the following gcloud
command to describe an instance without specifying a zone:
gcloud compute instances describe ${INSTANCE} \
--project=${PROJECT} \
--log-http
You'll discover that it uses:
GET /compute/v1/projects/{PROJECT}/aggregated/instances
This is documented by APIs Explorer as instances.aggregated
.
gcloud compute instances describe
(without a zone) then uses instances.get
to get details of the instance.
It's a more expensive method because it enumerates instances for every zone but, it permits you to find instances without having the zone.
You can simplify gcloud
's approach by using the equivalent of:
gcloud compute instances list \
--project=${PROJECT} \
--filter=name=${INSTANCE}
Which is:
GET compute/v1/projects/ackal-221102/aggregated/instances?filter=name%3D${INSTANCE}
As almost always, using Google's client libraries is the strongly-preferred way of coding use of this service.