Movatterモバイル変換


[0]ホーム

URL:


metadata

packagemodule
v0.9.0Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 24, 2025 License:Apache-2.0Imports:16Imported by:2,156

Details

Repository

github.com/googleapis/google-cloud-go

Links

README

Compute API

Go Reference

This is a utility library for communicating with Google Cloud metadata serviceon Google Cloud.

Install

go get cloud.google.com/go/compute/metadata

Go Version Support

See theGo Versions Supportedsection in the root directory's README.

Contributing

Contributions are welcome. Please, see theCONTRIBUTINGdocument for details.

Please note that this project is released with a Contributor Code of Conduct.By participating in this project you agree to abide by its terms. SeeContributor Code of Conductfor more information.

Documentation

Overview

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.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

funcEmaildeprecated

func Email(serviceAccountstring) (string,error)

Email calls Client.EmailWithContext on the default client.

Deprecated: Please use the context aware variantEmailWithContext.

funcEmailWithContextadded inv0.4.0

func EmailWithContext(ctxcontext.Context, serviceAccountstring) (string,error)

EmailWithContext calls Client.EmailWithContext on the default client.

funcExternalIPdeprecated

func ExternalIP() (string,error)

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

Deprecated: Please use the context aware variantExternalIPWithContext.

funcExternalIPWithContextadded inv0.4.0

func ExternalIPWithContext(ctxcontext.Context) (string,error)

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

funcGetdeprecated

func Get(suffixstring) (string,error)

Get calls Client.GetWithContext on the default client.

Deprecated: Please use the context aware variantGetWithContext.

funcGetWithContextadded inv0.3.0

func GetWithContext(ctxcontext.Context, suffixstring) (string,error)

GetWithContext calls Client.GetWithContext on the default client.

funcHostnamedeprecated

func Hostname() (string,error)

Hostname returns the instance's hostname. This will be of the form"<instanceID>.c.<projID>.internal".

Deprecated: Please use the context aware variantHostnameWithContext.

funcHostnameWithContextadded inv0.4.0

func HostnameWithContext(ctxcontext.Context) (string,error)

HostnameWithContext returns the instance's hostname. This will be of the form"<instanceID>.c.<projID>.internal".

funcInstanceAttributeValuedeprecated

func InstanceAttributeValue(attrstring) (string,error)

InstanceAttributeValue calls Client.InstanceAttributeValueWithContext on the default client.

Deprecated: Please use the context aware variantInstanceAttributeValueWithContext.

funcInstanceAttributeValueWithContextadded inv0.4.0

func InstanceAttributeValueWithContext(ctxcontext.Context, attrstring) (string,error)

InstanceAttributeValueWithContext calls Client.InstanceAttributeValueWithContext on the default client.

funcInstanceAttributesdeprecated

func InstanceAttributes() ([]string,error)

InstanceAttributes calls Client.InstanceAttributesWithContext on the default client.

Deprecated: Please use the context aware variant [InstanceAttributesWithContext.

funcInstanceAttributesWithContextadded inv0.4.0

func InstanceAttributesWithContext(ctxcontext.Context) ([]string,error)

InstanceAttributesWithContext calls Client.ProjectAttributesWithContext on the default client.

funcInstanceIDdeprecated

func InstanceID() (string,error)

InstanceID returns the current VM's numeric instance ID.

Deprecated: Please use the context aware variantInstanceIDWithContext.

funcInstanceIDWithContextadded inv0.4.0

func InstanceIDWithContext(ctxcontext.Context) (string,error)

InstanceIDWithContext returns the current VM's numeric instance ID.

funcInstanceNamedeprecated

func InstanceName() (string,error)

InstanceName returns the current VM's instance ID string.

Deprecated: Please use the context aware variantInstanceNameWithContext.

funcInstanceNameWithContextadded inv0.4.0

func InstanceNameWithContext(ctxcontext.Context) (string,error)

InstanceNameWithContext returns the current VM's instance ID string.

funcInstanceTagsdeprecated

func InstanceTags() ([]string,error)

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

Deprecated: Please use the context aware variantInstanceTagsWithContext.

funcInstanceTagsWithContextadded inv0.4.0

func InstanceTagsWithContext(ctxcontext.Context) ([]string,error)

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

funcInternalIPdeprecated

func InternalIP() (string,error)

InternalIP returns the instance's primary internal IP address.

Deprecated: Please use the context aware variantInternalIPWithContext.

funcInternalIPWithContextadded inv0.4.0

func InternalIPWithContext(ctxcontext.Context) (string,error)

InternalIPWithContext returns the instance's primary internal IP address.

funcNumericProjectIDdeprecated

func NumericProjectID() (string,error)

NumericProjectID returns the current instance's numeric project ID.

Deprecated: Please use the context aware variantNumericProjectIDWithContext.

funcNumericProjectIDWithContextadded inv0.4.0

func NumericProjectIDWithContext(ctxcontext.Context) (string,error)

NumericProjectIDWithContext returns the current instance's numeric project ID.

funcOnGCE

func OnGCE()bool

OnGCE reports whether this process is running on Google Compute Platforms.NOTE: True returned from `OnGCE` does not guarantee that the metadata serveris accessible from this process and have all the metadata defined.

funcOnGCEWithContextadded inv0.7.0

func OnGCEWithContext(ctxcontext.Context)bool

OnGCEWithContext reports whether this process is running on Google Compute Platforms.This function's return value is memoized for better performance.NOTE: True returned from `OnGCEWithContext` does not guarantee that the metadata serveris accessible from this process and have all the metadata defined.

funcProjectAttributeValuedeprecated

func ProjectAttributeValue(attrstring) (string,error)

ProjectAttributeValue calls Client.ProjectAttributeValueWithContext on the default client.

Deprecated: Please use the context aware variantProjectAttributeValueWithContext.

funcProjectAttributeValueWithContextadded inv0.4.0

func ProjectAttributeValueWithContext(ctxcontext.Context, attrstring) (string,error)

ProjectAttributeValueWithContext calls Client.ProjectAttributeValueWithContext on the default client.

funcProjectAttributesdeprecated

func ProjectAttributes() ([]string,error)

ProjectAttributes calls Client.ProjectAttributesWithContext on the default client.

Deprecated: Please use the context aware variantProjectAttributesWithContext.

funcProjectAttributesWithContextadded inv0.4.0

func ProjectAttributesWithContext(ctxcontext.Context) ([]string,error)

ProjectAttributesWithContext calls Client.ProjectAttributesWithContext on the default client.

funcProjectIDdeprecated

func ProjectID() (string,error)

ProjectID returns the current instance's project ID string.

Deprecated: Please use the context aware variantProjectIDWithContext.

funcProjectIDWithContextadded inv0.4.0

func ProjectIDWithContext(ctxcontext.Context) (string,error)

ProjectIDWithContext returns the current instance's project ID string.

funcScopesdeprecated

func Scopes(serviceAccountstring) ([]string,error)

Scopes calls Client.ScopesWithContext on the default client.

Deprecated: Please use the context aware variantScopesWithContext.

funcScopesWithContextadded inv0.4.0

func ScopesWithContext(ctxcontext.Context, serviceAccountstring) ([]string,error)

ScopesWithContext calls Client.ScopesWithContext on the default client.

funcSubscribedeprecated

func Subscribe(suffixstring, fn func(vstring, okbool)error)error

Subscribe calls Client.SubscribeWithContext on the default client.

Deprecated: Please use the context aware variantSubscribeWithContext.

funcSubscribeWithContextadded inv0.3.0

func SubscribeWithContext(ctxcontext.Context, suffixstring, fn func(ctxcontext.Context, vstring, okbool)error)error

SubscribeWithContext calls Client.SubscribeWithContext on the default client.

funcZonedeprecated

func Zone() (string,error)

Zone returns the current VM's zone, such as "us-central1-b".

Deprecated: Please use the context aware variantZoneWithContext.

funcZoneWithContextadded inv0.4.0

func ZoneWithContext(ctxcontext.Context) (string,error)

ZoneWithContext returns the current VM's zone, such as "us-central1-b".

Types

typeClient

type Client struct {// contains filtered or unexported fields}

A Client provides metadata.

funcNewClient

func NewClient(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 internal Client that isalso used in functions such as GetWithContext. This is useful for sharinga single TCP connection pool across requests.

Example

This example demonstrates how to use your own transport when using this package.

package mainimport ("context""net/http""cloud.google.com/go/compute/metadata")// This example demonstrates how to use your own transport when using this package.func main() {ctx := context.Background()c := metadata.NewClient(&http.Client{Transport: userAgentTransport{userAgent: "my-user-agent",base:      http.DefaultTransport,}})pID, err := c.GetWithContext(ctx, "project/project-id")if err != nil {// TODO: Handle error.}_ = pID // TODO: Use p.}// userAgentTransport sets the User-Agent header before calling base.type userAgentTransport struct {userAgent stringbase      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)}

funcNewWithOptionsadded inv0.6.0

func NewWithOptions(opts *Options) *Client

NewWithOptions returns a Client that is configured with the provided Options.

func (*Client)Emaildeprecated

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

Email returns the email address associated with the service account.

Deprecated: Please use the context aware variantClient.EmailWithContext.

func (*Client)EmailWithContextadded inv0.4.0

func (c *Client) EmailWithContext(ctxcontext.Context, serviceAccountstring) (string,error)

EmailWithContext returns the email address associated with the service account.The serviceAccount parameter default value (empty string or "default" value)will use the instance's main account.

func (*Client)ExternalIPdeprecated

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

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

Deprecated: Please use the context aware variantClient.ExternalIPWithContext.

func (*Client)ExternalIPWithContextadded inv0.4.0

func (c *Client) ExternalIPWithContext(ctxcontext.Context) (string,error)

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

func (*Client)Getdeprecated

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.

Deprecated: Please use the context aware variantClient.GetWithContext.

func (*Client)GetWithContextadded inv0.3.0

func (c *Client) GetWithContext(ctxcontext.Context, suffixstring) (string,error)

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

NOTE: Without an extra deadline in the context this call can take in theworst case, with internal backoff retries, up to 15 seconds (e.g. when serveris responding slowly). Pass context with additional timeouts when needed.

func (*Client)Hostnamedeprecated

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

Hostname returns the instance's hostname. This will be of the form"<instanceID>.c.<projID>.internal".

Deprecated: Please use the context aware variantClient.HostnameWithContext.

func (*Client)HostnameWithContextadded inv0.4.0

func (c *Client) HostnameWithContext(ctxcontext.Context) (string,error)

HostnameWithContext returns the instance's hostname. This will be of the form"<instanceID>.c.<projID>.internal".

func (*Client)InstanceAttributeValuedeprecated

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.

Deprecated: Please use the context aware variantClient.InstanceAttributeValueWithContext.

func (*Client)InstanceAttributeValueWithContextadded inv0.4.0

func (c *Client) InstanceAttributeValueWithContext(ctxcontext.Context, attrstring) (string,error)

InstanceAttributeValueWithContext 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)InstanceAttributesdeprecated

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.

Deprecated: Please use the context aware variantClient.InstanceAttributesWithContext.

func (*Client)InstanceAttributesWithContextadded inv0.4.0

func (c *Client) InstanceAttributesWithContext(ctxcontext.Context) ([]string,error)

InstanceAttributesWithContext 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)InstanceIDdeprecated

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

InstanceID returns the current VM's numeric instance ID.

Deprecated: Please use the context aware variantClient.InstanceIDWithContext.

func (*Client)InstanceIDWithContextadded inv0.4.0

func (c *Client) InstanceIDWithContext(ctxcontext.Context) (string,error)

InstanceIDWithContext returns the current VM's numeric instance ID.

func (*Client)InstanceNamedeprecated

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

InstanceName returns the current VM's instance ID string.

Deprecated: Please use the context aware variantClient.InstanceNameWithContext.

func (*Client)InstanceNameWithContextadded inv0.4.0

func (c *Client) InstanceNameWithContext(ctxcontext.Context) (string,error)

InstanceNameWithContext returns the current VM's instance ID string.

func (*Client)InstanceTagsdeprecated

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

InstanceTags returns the list of user-defined instance tags.

Deprecated: Please use the context aware variantClient.InstanceTagsWithContext.

func (*Client)InstanceTagsWithContextadded inv0.4.0

func (c *Client) InstanceTagsWithContext(ctxcontext.Context) ([]string,error)

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

func (*Client)InternalIPdeprecated

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

InternalIP returns the instance's primary internal IP address.

Deprecated: Please use the context aware variantClient.InternalIPWithContext.

func (*Client)InternalIPWithContextadded inv0.4.0

func (c *Client) InternalIPWithContext(ctxcontext.Context) (string,error)

InternalIPWithContext returns the instance's primary internal IP address.

func (*Client)NumericProjectIDdeprecated

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

NumericProjectID returns the current instance's numeric project ID.

Deprecated: Please use the context aware variantClient.NumericProjectIDWithContext.

func (*Client)NumericProjectIDWithContextadded inv0.4.0

func (c *Client) NumericProjectIDWithContext(ctxcontext.Context) (string,error)

NumericProjectIDWithContext returns the current instance's numeric project ID.

func (*Client)OnGCEWithContextadded inv0.7.0

func (c *Client) OnGCEWithContext(ctxcontext.Context)bool

OnGCEWithContext reports whether this process is running on Google Compute Platforms.NOTE: True returned from `OnGCEWithContext` does not guarantee that the metadata serveris accessible from this process and have all the metadata defined.

func (*Client)ProjectAttributeValuedeprecated

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.

Deprecated: Please use the context aware variantClient.ProjectAttributeValueWithContext.

func (*Client)ProjectAttributeValueWithContextadded inv0.4.0

func (c *Client) ProjectAttributeValueWithContext(ctxcontext.Context, attrstring) (string,error)

ProjectAttributeValueWithContext 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)ProjectAttributesdeprecated

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.

Deprecated: Please use the context aware variantClient.ProjectAttributesWithContext.

func (*Client)ProjectAttributesWithContextadded inv0.4.0

func (c *Client) ProjectAttributesWithContext(ctxcontext.Context) ([]string,error)

ProjectAttributesWithContext 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)ProjectIDdeprecated

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

ProjectID returns the current instance's project ID string.

Deprecated: Please use the context aware variantClient.ProjectIDWithContext.

func (*Client)ProjectIDWithContextadded inv0.4.0

func (c *Client) ProjectIDWithContext(ctxcontext.Context) (string,error)

ProjectIDWithContext returns the current instance's project ID string.

func (*Client)Scopesdeprecated

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.

Deprecated: Please use the context aware variantClient.ScopesWithContext.

func (*Client)ScopesWithContextadded inv0.4.0

func (c *Client) ScopesWithContext(ctxcontext.Context, serviceAccountstring) ([]string,error)

ScopesWithContext 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)Subscribedeprecated

func (c *Client) Subscribe(suffixstring, fn func(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.

Deprecated: Please use the context aware variantClient.SubscribeWithContext.

func (*Client)SubscribeWithContextadded inv0.3.0

func (c *Client) SubscribeWithContext(ctxcontext.Context, suffixstring, fn func(ctxcontext.Context, vstring, okbool)error)error

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

SubscribeWithContext calls fn with the latest metadata value indicated by theprovided suffix. If the metadata value is deleted, fn is called with theempty string and ok false. Subscribe blocks until fn returns a non-nil erroror the value is deleted. Subscribe returns the error value returned from thelast call to fn, which may be nil when ok == false.

func (*Client)Zonedeprecated

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

Zone returns the current VM's zone, such as "us-central1-b".

Deprecated: Please use the context aware variantClient.ZoneWithContext.

func (*Client)ZoneWithContextadded inv0.4.0

func (c *Client) ZoneWithContext(ctxcontext.Context) (string,error)

ZoneWithContext returns the current VM's zone, such as "us-central1-b".

typeError

type Error struct {// 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

typeNotDefinedError

type NotDefinedErrorstring

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

typeOptionsadded inv0.6.0

type Options struct {// Client is the HTTP client used to make requests. Optional.// If UseDefaultClient is true, this field is ignored.// If this field is nil, a new default http.Client will be created.Client *http.Client// Logger is used to log information about HTTP request and responses.// If not provided, nothing will be logged. Optional.Logger *slog.Logger// UseDefaultClient specifies that the client should use the same default// internal http.Client that is used in functions such as GetWithContext.// This is useful for sharing a single TCP connection pool across requests.// The difference vs GetWithContext is the ability to use this struct// to provide a custom logger. If this field is true, the Client// field is ignored.UseDefaultClientbool}

Options for configuring aClient.

Source Files

View all Source files

Directories

PathSynopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp