- 0.116.0 (latest)
- 0.115.1
- 0.114.0
- 0.113.0
- 0.112.2
- 0.111.0
- 0.110.10
- 0.109.0
- 0.108.0
- 0.107.0
- 0.106.0
- 0.105.0
- 0.104.0
- 0.103.0
- 0.102.1
- 0.101.1
- 0.100.2
- 0.99.0
- 0.98.0
- 0.97.0
- 0.96.0
- 0.95.0
- 0.94.1
- 0.93.3
- 0.92.3
- 0.91.1
- 0.90.0
- 0.89.0
- 0.88.0
- 0.87.0
- 0.86.0
- 0.85.0
- 0.84.0
- 0.83.0
- 0.82.0
- 0.81.0
- 0.80.0
- 0.79.0
- 0.78.0
- 0.77.0
- 0.76.0
- 0.75.0
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 at https://developers.google.com/compute/docs/metadata.
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
func OnGCE() bool
OnGCE 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
type Client struct {
// 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
package main
import (
"net/http"
"cloud.google.com/go/compute/metadata"
)
// This example demonstrates how to use your own transport when using this package.
func main() {
c := metadata.NewClient(&http.Client{Transport: userAgentTransport{
userAgent: "my-user-agent",
base: http.DefaultTransport,
}})
p, err := c.ProjectID()
if err != nil {
// TODO: Handle error.
}
_ = p // TODO: Use p.
}
// userAgentTransport sets the User-Agent header before calling base.
type userAgentTransport struct {
userAgent string
base http.RoundTripper
}
// RoundTrip implements the http.RoundTripper interface.
func (t userAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("User-Agent", t.userAgent)
return t.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's main 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 of 169.254.169.254 will be used instead.
If the requested metadata is not defined, the returned error will be 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 VM instance attribute.
If the requested attribute is not defined, the returned error will be of type NotDefinedError.
InstanceAttributeValue may return ("", nil) if the attribute was defined 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 an attribute 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 provided project attribute.
If the requested attribute is not defined, the returned error will be of type NotDefinedError.
ProjectAttributeValue may return ("", nil) if the attribute was defined to be the empty string.
func (*Client) ProjectAttributes
ProjectAttributes returns the list of user-defined attributes applying to the project as a whole, not just this VM. The value of an 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's main 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 provided suffix. If the metadata value is deleted, fn is called with the empty string and ok false. Subscribe blocks until fn returns a non-nil error or the value is deleted. Subscribe returns the error value returned from the last call to fn, which may be nil when ok == false.
func (*Client) Zone
Zone returns the current VM's zone, such as "us-central1-b".
Error
type Error struct {
// Code is the HTTP response status code.
Code int
// Message is the server response message.
Message string
}
Error contains an error response from the server.
func (*Error) Error
NotDefinedError
type NotDefinedError string
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 empty string.
func (NotDefinedError) Error
func (suffix NotDefinedError) Error() string