Service Metadata API - Package cloud.google.com/go/compute/metadata (v0.2.3)

Note: To get more information about this package, such as access to older versions, viewthis package on pkg.go.dev.

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

funcEmail(serviceAccountstring)(string,error)

Email calls Client.Email on the default client.

func ExternalIP

funcExternalIP()(string,error)

ExternalIP returns the instance's primary external (public) IP address.

func Get

funcGet(suffixstring)(string,error)

Get calls Client.Get on the default client.

func Hostname

funcHostname()(string,error)

Hostname returns the instance's hostname. This will be of the form"

func InstanceAttributeValue

funcInstanceAttributeValue(attrstring)(string,error)

InstanceAttributeValue calls Client.InstanceAttributeValue on the default client.

func InstanceAttributes

funcInstanceAttributes()([]string,error)

InstanceAttributes calls Client.InstanceAttributes on the default client.

func InstanceID

funcInstanceID()(string,error)

InstanceID returns the current VM's numeric instance ID.

func InstanceName

funcInstanceName()(string,error)

InstanceName returns the current VM's instance ID string.

func InstanceTags

funcInstanceTags()([]string,error)

InstanceTags returns the list of user-defined instance tags,assigned when initially creating a GCE instance.

func InternalIP

funcInternalIP()(string,error)

InternalIP returns the instance's primary internal IP address.

func NumericProjectID

funcNumericProjectID()(string,error)

NumericProjectID returns the current instance's numeric project ID.

func OnGCE

funcOnGCE()bool

OnGCE reports whether this process is running on Google Compute Engine.

func ProjectAttributeValue

funcProjectAttributeValue(attrstring)(string,error)

ProjectAttributeValue calls Client.ProjectAttributeValue on the default client.

func ProjectAttributes

funcProjectAttributes()([]string,error)

ProjectAttributes calls Client.ProjectAttributes on the default client.

func ProjectID

funcProjectID()(string,error)

ProjectID returns the current instance's project ID string.

func Scopes

funcScopes(serviceAccountstring)([]string,error)

Scopes calls Client.Scopes on the default client.

func Subscribe

funcSubscribe(suffixstring,fnfunc(vstring,okbool)error)error

Subscribe calls Client.Subscribe on the default client.

func Zone

funcZone()(string,error)

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

funcNewClient(c*http.Client)*Client

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

func(c*Client)Email(serviceAccountstring)(string,error)

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

func(c*Client)ExternalIP()(string,error)

ExternalIP returns the instance's primary external (public) IP address.

func (*Client) Get

func(c*Client)Get(suffixstring)(string,error)

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

func(c*Client)Hostname()(string,error)

Hostname returns the instance's hostname. This will be of the form"

func (*Client) InstanceAttributeValue

func(c*Client)InstanceAttributeValue(attrstring)(string,error)

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

func(c*Client)InstanceAttributes()([]string,error)

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

func(c*Client)InstanceID()(string,error)

InstanceID returns the current VM's numeric instance ID.

func (*Client) InstanceName

func(c*Client)InstanceName()(string,error)

InstanceName returns the current VM's instance ID string.

func (*Client) InstanceTags

func(c*Client)InstanceTags()([]string,error)

InstanceTags returns the list of user-defined instance tags,assigned when initially creating a GCE instance.

func (*Client) InternalIP

func(c*Client)InternalIP()(string,error)

InternalIP returns the instance's primary internal IP address.

func (*Client) NumericProjectID

func(c*Client)NumericProjectID()(string,error)

NumericProjectID returns the current instance's numeric project ID.

func (*Client) ProjectAttributeValue

func(c*Client)ProjectAttributeValue(attrstring)(string,error)

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

func(c*Client)ProjectAttributes()([]string,error)

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

func(c*Client)ProjectID()(string,error)

ProjectID returns the current instance's project ID string.

func (*Client) Scopes

func(c*Client)Scopes(serviceAccountstring)([]string,error)

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

func(c*Client)Subscribe(suffixstring,fnfunc(vstring,okbool)error)error

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

func(c*Client)Zone()(string,error)

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

func(e*Error)Error()string

NotDefinedError

typeNotDefinedErrorstring

NotDefinedError 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()string

Except 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.