metadata

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2022 License: BSD-3-Clause Imports: 13 Imported by: 0

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 at https://developers.google.com/compute/docs/metadata.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExternalIP

func ExternalIP(ctx context.Context) (string, error)

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

func Get

func Get(ctx context.Context, suffix string) (string, error)

Get calls Client.Get on the default client.

func Hostname

func Hostname(ctx context.Context) (string, error)

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

func InstanceAttributeValue

func InstanceAttributeValue(ctx context.Context, attr string) (string, error)

InstanceAttributeValue calls Client.InstanceAttributeValue on the default client.

func InstanceAttributes

func InstanceAttributes(ctx context.Context) ([]string, error)

InstanceAttributes calls Client.InstanceAttributes on the default client.

func InstanceID

func InstanceID(ctx context.Context) (string, error)

InstanceID returns the current VM's numeric instance ID.

func InstanceName

func InstanceName(ctx context.Context) (string, error)

InstanceName returns the current VM's instance ID string.

func InstanceTags

func InstanceTags(ctx context.Context) ([]string, error)

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

func InternalIP

func InternalIP(ctx context.Context) (string, error)

InternalIP returns the instance's primary internal IP address.

func NumericProjectID

func NumericProjectID(ctx context.Context) (string, error)

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

func ProjectAttributeValue(ctx context.Context, attr string) (string, error)

ProjectAttributeValue calls Client.ProjectAttributeValue on the default client.

func ProjectAttributes

func ProjectAttributes(ctx context.Context) ([]string, error)

ProjectAttributes calls Client.ProjectAttributes on the default client.

func ProjectID

func ProjectID(ctx context.Context) (string, error)

ProjectID returns the current instance's project ID string.

func Scopes

func Scopes(ctx context.Context, serviceAccount string) ([]string, error)

Scopes calls Client.Scopes on the default client.

func Subscribe

func Subscribe(ctx context.Context, suffix string, fn func(v string, ok bool) error) error

Subscribe calls Client.Subscribe on a client designed for subscribing (one with no ResponseHeaderTimeout).

func Zone

func Zone(ctx context.Context) (string, error)

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

Types

type Client

type Client ctxclient.Func

A Client provides a client for retrieving metadata.

func NewClient

func NewClient(c *http.Client) Client

NewClient returns a Client that can be used to fetch metadata. All HTTP requests will use the given http.Client instead of the default client.

func (Client) ExternalIP

func (c Client) ExternalIP(ctx context.Context) (string, error)

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

func (Client) Get

func (c Client) Get(ctx context.Context, suffix string) (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 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

func (c Client) Hostname(ctx context.Context) (string, error)

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

func (Client) InstanceAttributeValue

func (c Client) InstanceAttributeValue(ctx context.Context, attr string) (string, error)

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

func (c Client) InstanceAttributes(ctx context.Context) ([]string, error)

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

func (c Client) InstanceID(ctx context.Context) (string, error)

InstanceID returns the current VM's numeric instance ID.

func (Client) InstanceName

func (c Client) InstanceName(ctx context.Context) (string, error)

InstanceName returns the current VM's instance ID string.

func (Client) InstanceTags

func (c Client) InstanceTags(ctx context.Context) ([]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(ctx context.Context) (string, error)

InternalIP returns the instance's primary internal IP address.

func (Client) NumericProjectID

func (c Client) NumericProjectID(ctx context.Context) (string, error)

NumericProjectID returns the current instance's numeric project ID.

func (Client) ProjectAttributeValue

func (c Client) ProjectAttributeValue(ctx context.Context, attr string) (string, error)

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

func (c Client) ProjectAttributes(ctx context.Context) ([]string, error)

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

func (c Client) ProjectID(ctx context.Context) (string, error)

ProjectID returns the current instance's project ID string.

func (Client) Scopes

func (c Client) Scopes(ctx context.Context, serviceAccount string) ([]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's main account.

func (Client) Subscribe

func (c Client) Subscribe(ctx context.Context, suffix string, fn func(v string, ok bool) 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 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

func (c Client) Zone(ctx context.Context) (string, error)

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

type 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

func (e *Error) Error() string

type 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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL