metadata

package module
v0.0.0-...-e3650a3 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2025 License: MIT Imports: 9 Imported by: 19

README

metadata Documentation

A Go client to interact with the DigitalOcean Metadata API.

Usage

// Create a client
client := metadata.NewClient(opts)

// Request all the metadata about the current droplet
all, err := client.Metadata()
if err != nil {
    log.Fatal(err)
}

// Lookup what our IPv4 address is on our first public
// network interface.
publicIPv4Addr := all.Interfaces["public"][0].IPv4.IPAddress

fmt.Println(publicIPv4Addr)

License

MIT license

Documentation

Overview

Package metadata implements a client for the DigitalOcean metadata API. This API allows a droplet to inspect information about itself, like it's region, droplet ID, and so on.

Documentation for the API is available at:

https://developers.digitalocean.com/documentation/metadata/
Example

Create a client and query it for all available metadata.

// Create a client
client := metadata.NewClient(opts)

// Request all the metadata about the current droplet
all, err := client.Metadata()
if err != nil {
	log.Fatal(err)
}

// Lookup what our IPv4 address is on our first public
// network interface.
publicIPv4Addr := all.Interfaces["public"][0].IPv4.IPAddress

fmt.Println(publicIPv4Addr)
Output:

192.168.0.100

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client to interact with the DigitalOcean metadata API, from inside a droplet.

func NewClient

func NewClient(opts ...ClientOption) *Client

NewClient creates a client for the metadata API.

func (*Client) AuthToken

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

AuthToken returns the authentication token.

func (*Client) DropletID

func (c *Client) DropletID() (int, error)

DropletID returns the Droplet's unique identifier. This is automatically generated upon Droplet creation.

func (*Client) FloatingIPv4Active

func (c *Client) FloatingIPv4Active() (bool, error)

FloatingIPv4Active returns true if an IPv4 Floating IP Address is assigned to the Droplet.

func (*Client) Hostname

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

Hostname returns the Droplet's hostname, as specified by the user during Droplet creation.

func (*Client) Metadata

func (c *Client) Metadata() (*Metadata, error)

Metadata contains the entire contents of a Droplet's metadata. This method is unique because it returns all of the metadata at once, instead of individual metadata items.

func (*Client) Nameservers

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

Nameservers returns the nameserver entries that are added to a Droplet's /etc/resolv.conf file during creation.

func (*Client) PublicKeys

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

PublicKeys returns the public SSH key(s) that were added to the Droplet's root user's authorized_keys file during Droplet creation.

func (*Client) Region

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

Region returns the region code of where the Droplet resides.

func (*Client) ReservedIPv4Active

func (c *Client) ReservedIPv4Active() (bool, error)

ReservedIPv4Active returns true if an IPv4 Reserved IP Address is assigned to the Droplet.

func (*Client) Tags

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

Tags returns a list of DigitalOcean tags that have been applied to the Droplet.

func (*Client) UserData

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

UserData returns the user data that was provided by the user during Droplet creation. User data can contain arbitrary data for miscellaneous use or, with certain Linux distributions, an arbitrary shell script or cloud-config file that will be consumed by a variation of cloud-init upon boot. At this time, cloud-config support is included with CoreOS, Ubuntu 14.04, and CentOS 7 images on DigitalOcean.

func (*Client) VendorData

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

VendorData provided data that can be used to configure Droplets upon their creation. This is similar to user data, but it is provided by DigitalOcean instead of the user.

type ClientOption

type ClientOption func(*Client)

ClientOption modifies the default behavior of a metadata client. This is usually not needed.

func WithBaseURL

func WithBaseURL(base *url.URL) ClientOption

WithBaseURL makes the metadata client reach the metadata API using the given base URL.

func WithHTTPClient

func WithHTTPClient(client *http.Client) ClientOption

WithHTTPClient makes the metadata client use the given HTTP client.

type Metadata

type Metadata struct {
	DropletID  int      `json:"droplet_id,omitempty"`
	Hostname   string   `json:"hostname,omitempty"`
	UserData   string   `json:"user_data,omitempty"`
	VendorData string   `json:"vendor_data,omitempty"`
	PublicKeys []string `json:"public_keys,omitempty"`
	Region     string   `json:"region,omitempty"`
	Tags       []string `json:"tags,omitempty"`

	DNS struct {
		Nameservers []string `json:"nameservers,omitempty"`
	} `json:"dns,omitempty"`

	Interfaces map[string][]struct {
		MACAddress string `json:"mac,omitempty"`
		Type       string `json:"type,omitempty"`

		IPv4 *struct {
			IPAddress string `json:"ip_address,omitempty"`
			Netmask   string `json:"netmask,omitempty"`
			Gateway   string `json:"gateway,omitempty"`
		} `json:"ipv4,omitempty"`

		IPv6 *struct {
			IPAddress string `json:"ip_address,omitempty"`
			CIDR      int    `json:"cidr,omitempty"`
			Gateway   string `json:"gateway,omitempty"`
		} `json:"ipv6,omitempty"`

		AnchorIPv4 *struct {
			IPAddress string `json:"ip_address,omitempty"`
			Netmask   string `json:"netmask,omitempty"`
			Gateway   string `json:"gateway,omitempty"`
		} `json:"anchor_ipv4,omitempty"`
	} `json:"interfaces,omitempty"`

	FloatingIP struct {
		IPv4 struct {
			IPAddress string `json:"ip_address,omitempty"`
			Active    bool   `json:"active,omitempty"`
		} `json:"ipv4,omitempty"`
	} `json:"floating_ip,omitempty"`

	ReservedIP struct {
		IPv4 struct {
			IPAddress string `json:"ip_address,omitempty"`
			Active    bool   `json:"active,omitempty"`
		} `json:"ipv4,omitempty"`
		IPv6 struct {
			IPAddress string `json:"ip_address,omitempty"`
			Active    bool   `json:"active,omitempty"`
		} `json:"ipv6,omitempty"`
	} `json:"reserved_ip,omitempty"`

	Features struct {
		DHCPEnabled       bool `json:"dhcp_enabled,omitempty"`
		VPCPeeringEnabled bool `json:"vpc_peering_enabled,omitempty"`
	} `json:"features"`
}

Jump to

Keyboard shortcuts

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