Service Metadata API - Package cloud.google.com/go/compute/metadata (v0.1.1) Stay organized with collections Save and categorize content based on your preferences.
Package metadata provides access to Google Compute Engine (GCE)metadata and API service accounts.
This package is a wrapper around the GCE metadata service,as documented athttps://cloud.google.com/compute/docs/metadata/overview.
Functions
func Email
Email calls Client.Email on the default client.
func ExternalIP
ExternalIP returns the instance's primary external (public) IP address.
func Get
Get calls Client.Get on the default client.
func Hostname
Hostname returns the instance's hostname. This will be of the form"
func InstanceAttributeValue
InstanceAttributeValue calls Client.InstanceAttributeValue on the default client.
func InstanceAttributes
InstanceAttributes calls Client.InstanceAttributes on the default client.
func InstanceID
InstanceID returns the current VM's numeric instance ID.
func InstanceName
InstanceName returns the current VM's instance ID string.
func InstanceTags
InstanceTags returns the list of user-defined instance tags,assigned when initially creating a GCE instance.
func InternalIP
InternalIP returns the instance's primary internal IP address.
func NumericProjectID
NumericProjectID returns the current instance's numeric project ID.
func OnGCE
funcOnGCE()boolOnGCE reports whether this process is running on Google Compute Engine.
func ProjectAttributeValue
ProjectAttributeValue calls Client.ProjectAttributeValue on the default client.
func ProjectAttributes
ProjectAttributes calls Client.ProjectAttributes on the default client.
func ProjectID
ProjectID returns the current instance's project ID string.
func Scopes
Scopes calls Client.Scopes on the default client.
func Subscribe
Subscribe calls Client.Subscribe on the default client.
func Zone
Zone returns the current VM's zone, such as "us-central1-b".
Client
typeClientstruct{// contains filtered or unexported fields}A Client provides metadata.
func NewClient
NewClient returns a Client that can be used to fetch metadata.Returns the client that uses the specified http.Client for HTTP requests.If nil is specified, returns the default client.
Example
packagemainimport("net/http""cloud.google.com/go/compute/metadata")// This example demonstrates how to use your own transport when using this package.funcmain(){c:=metadata.NewClient(&http.Client{Transport:userAgentTransport{userAgent:"my-user-agent",base:http.DefaultTransport,}})p,err:=c.ProjectID()iferr!=nil{// TODO: Handle error.}_=p// TODO: Use p.}// userAgentTransport sets the User-Agent header before calling base.typeuserAgentTransportstruct{userAgentstringbasehttp.RoundTripper}// RoundTrip implements the http.RoundTripper interface.func(tuserAgentTransport)RoundTrip(req*http.Request)(*http.Response,error){req.Header.Set("User-Agent",t.userAgent)returnt.base.RoundTrip(req)}func (*Client) Email
Email returns the email address associated with the service account.The account may be empty or the string "default" to use the instance'smain account.
func (*Client) ExternalIP
ExternalIP returns the instance's primary external (public) IP address.
func (*Client) Get
Get returns a value from the metadata service.The suffix is appended to "http://${GCE_METADATA_HOST}/computeMetadata/v1/".
If the GCE_METADATA_HOST environment variable is not defined, a default of169.254.169.254 will be used instead.
If the requested metadata is not defined, the returned error willbe of type NotDefinedError.
func (*Client) Hostname
Hostname returns the instance's hostname. This will be of the form"
func (*Client) InstanceAttributeValue
InstanceAttributeValue returns the value of the provided VMinstance attribute.
If the requested attribute is not defined, the returned error willbe of type NotDefinedError.
InstanceAttributeValue may return ("", nil) if the attribute wasdefined to be the empty string.
func (*Client) InstanceAttributes
InstanceAttributes returns the list of user-defined attributes,assigned when initially creating a GCE VM instance. The value of anattribute can be obtained with InstanceAttributeValue.
func (*Client) InstanceID
InstanceID returns the current VM's numeric instance ID.
func (*Client) InstanceName
InstanceName returns the current VM's instance ID string.
func (*Client) InstanceTags
InstanceTags returns the list of user-defined instance tags,assigned when initially creating a GCE instance.
func (*Client) InternalIP
InternalIP returns the instance's primary internal IP address.
func (*Client) NumericProjectID
NumericProjectID returns the current instance's numeric project ID.
func (*Client) ProjectAttributeValue
ProjectAttributeValue returns the value of the providedproject attribute.
If the requested attribute is not defined, the returned error willbe of type NotDefinedError.
ProjectAttributeValue may return ("", nil) if the attribute wasdefined to be the empty string.
func (*Client) ProjectAttributes
ProjectAttributes returns the list of user-defined attributesapplying to the project as a whole, not just this VM. The value ofan attribute can be obtained with ProjectAttributeValue.
func (*Client) ProjectID
ProjectID returns the current instance's project ID string.
func (*Client) Scopes
Scopes returns the service account scopes for the given account.The account may be empty or the string "default" to use the instance'smain account.
func (*Client) Subscribe
Subscribe subscribes to a value from the metadata service.The suffix is appended to "http://${GCE_METADATA_HOST}/computeMetadata/v1/".The suffix may contain query parameters.
Subscribe calls fn with the latest metadata value indicated by the providedsuffix. If the metadata value is deleted, fn is called with the empty stringand ok false. Subscribe blocks until fn returns a non-nil error or the valueis deleted. Subscribe returns the error value returned from the last call tofn, which may be nil when ok == false.
func (*Client) Zone
Zone returns the current VM's zone, such as "us-central1-b".
Error
typeErrorstruct{// Code is the HTTP response status code.Codeint// Message is the server response message.Messagestring}Error contains an error response from the server.
func (*Error) Error
NotDefinedError
typeNotDefinedErrorstringNotDefinedError is returned when requested metadata is not defined.
The underlying string is the suffix after "/computeMetadata/v1/".
This error is not returned if the value is defined to be the emptystring.
func (NotDefinedError) Error
func(suffixNotDefinedError)Error()stringExcept as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-10-30 UTC.