cloudmeta

package module
v0.0.0-...-28b3229 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: MIT Imports: 9 Imported by: 0

README

CloudMeta

A lightweight Go library for detecting cloud providers and accessing basic instance metadata.

Go Reference

Features

  • Auto-detection - Automatically detects cloud provider
  • Multi-cloud - Supports multiple cloud providers
  • Zero dependencies - Only uses Go standard library

Installation

go get github.com/nickgarlis/go-cloudmeta

Quick Start

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/nickgarlis/go-cloudmeta"
)

func main() {
    ctx := context.Background()

    provider, err := cloudmeta.GetProvider(ctx)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Running on: %s\n", provider.Name())

    privateIP, err := provider.GetPrivateIPv4(ctx)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Private IPv4: %s\n", privateIP)
}

API Reference

Common Interface
type Provider interface {
    Name() string
    GetInstanceID(ctx context.Context) (string, error)
    GetPrivateIPv4(ctx context.Context) (string, error)
    GetPublicIPv4(ctx context.Context) (string, error)
    GetHostname(ctx context.Context) (string, error)
    GetPrimaryIPv6(ctx context.Context) (string, error)
}

Error Handling

provider, err := cloudmeta.GetProvider(ctx)
if err != nil {
    if errors.Is(err, cloudmeta.ErrUnknownProvider) {
      // Handle unknown provider
    }
    // Handle other errors
}

ipv6, err := provider.GetPrimaryIPv6(ctx)
if err != nil {
    if errors.Is(err, cloudmeta.ErrNotFound) {
        // Handle not found case
    }
    // Handle error
}

Supported Platforms

  • AWS
  • Google Cloud Platform
  • Microsoft Azure
  • DigitalOcean
  • Hetzner Cloud
  • Oracle Cloud Infrastructure
  • OpenStack-based clouds

License

MIT License - see LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnknownProvider = errors.New("unknown cloud provider")
	ErrNotFound        = errors.New("not found")
)

Functions

This section is empty.

Types

type AWSProvider

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

func (*AWSProvider) GetHostname

func (p *AWSProvider) GetHostname(ctx context.Context) (string, error)

GetHostname returns the instance hostname

func (*AWSProvider) GetIMDSv2Token

func (p *AWSProvider) GetIMDSv2Token(ctx context.Context) (string, error)

GetIMDSv2Token gets an IMDSv2 token for secure metadata access

func (*AWSProvider) GetInstanceID

func (p *AWSProvider) GetInstanceID(ctx context.Context) (string, error)

func (*AWSProvider) GetPrimaryIPv6

func (p *AWSProvider) GetPrimaryIPv6(ctx context.Context) (string, error)

GetPrimaryIPv6 returns the primary IPv6 address

func (*AWSProvider) GetPrivateIPv4

func (p *AWSProvider) GetPrivateIPv4(ctx context.Context) (string, error)

GetPrivateIPv4 returns the private IPv4 address

func (*AWSProvider) GetPublicIPv4

func (p *AWSProvider) GetPublicIPv4(ctx context.Context) (string, error)

GetPublicIPv4 returns the public IPv4 address

func (*AWSProvider) Name

func (p *AWSProvider) Name() string

type AzureProvider

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

func (*AzureProvider) GetHostname

func (p *AzureProvider) GetHostname(ctx context.Context) (string, error)

func (*AzureProvider) GetInstanceID

func (p *AzureProvider) GetInstanceID(ctx context.Context) (string, error)

func (*AzureProvider) GetPrimaryIPv6

func (p *AzureProvider) GetPrimaryIPv6(ctx context.Context) (string, error)

func (*AzureProvider) GetPrivateIPv4

func (p *AzureProvider) GetPrivateIPv4(ctx context.Context) (string, error)

func (*AzureProvider) GetPublicIPv4

func (p *AzureProvider) GetPublicIPv4(ctx context.Context) (string, error)

func (*AzureProvider) Name

func (p *AzureProvider) Name() string

type DigitalOceanProvider

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

func (*DigitalOceanProvider) GetHostname

func (p *DigitalOceanProvider) GetHostname(ctx context.Context) (string, error)

func (*DigitalOceanProvider) GetInstanceID

func (p *DigitalOceanProvider) GetInstanceID(ctx context.Context) (string, error)

func (*DigitalOceanProvider) GetPrimaryIPv6

func (p *DigitalOceanProvider) GetPrimaryIPv6(ctx context.Context) (string, error)

func (*DigitalOceanProvider) GetPrivateIPv4

func (p *DigitalOceanProvider) GetPrivateIPv4(ctx context.Context) (string, error)

func (*DigitalOceanProvider) GetPublicIPv4

func (p *DigitalOceanProvider) GetPublicIPv4(ctx context.Context) (string, error)

func (*DigitalOceanProvider) Name

func (p *DigitalOceanProvider) Name() string

type GCPProvider

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

func (*GCPProvider) GetHostname

func (p *GCPProvider) GetHostname(ctx context.Context) (string, error)

GetHostname returns the instance hostname

func (*GCPProvider) GetInstanceID

func (p *GCPProvider) GetInstanceID(ctx context.Context) (string, error)

GetInstanceID returns the GCP instance ID

func (*GCPProvider) GetPrimaryIPv6

func (p *GCPProvider) GetPrimaryIPv6(ctx context.Context) (string, error)

func (*GCPProvider) GetPrivateIPv4

func (p *GCPProvider) GetPrivateIPv4(ctx context.Context) (string, error)

GetPrivateIPv4 returns the private IPv4 address

func (*GCPProvider) GetPublicIPv4

func (p *GCPProvider) GetPublicIPv4(ctx context.Context) (string, error)

GetPublicIPv4 returns the public IPv4 address

func (*GCPProvider) Name

func (p *GCPProvider) Name() string

type HetznerProvider

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

func (*HetznerProvider) GetHostname

func (p *HetznerProvider) GetHostname(ctx context.Context) (string, error)

func (*HetznerProvider) GetInstanceID

func (p *HetznerProvider) GetInstanceID(ctx context.Context) (string, error)

func (*HetznerProvider) GetPrimaryIPv6

func (p *HetznerProvider) GetPrimaryIPv6(ctx context.Context) (string, error)

func (*HetznerProvider) GetPrivateIPv4

func (p *HetznerProvider) GetPrivateIPv4(ctx context.Context) (string, error)

func (*HetznerProvider) GetPublicIPv4

func (p *HetznerProvider) GetPublicIPv4(ctx context.Context) (string, error)

func (*HetznerProvider) Name

func (p *HetznerProvider) Name() string

type OCIProvider

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

func (*OCIProvider) GetHostname

func (p *OCIProvider) GetHostname(ctx context.Context) (string, error)

func (*OCIProvider) GetInstanceID

func (p *OCIProvider) GetInstanceID(ctx context.Context) (string, error)

func (*OCIProvider) GetPrimaryIPv6

func (p *OCIProvider) GetPrimaryIPv6(ctx context.Context) (string, error)

func (*OCIProvider) GetPrivateIPv4

func (p *OCIProvider) GetPrivateIPv4(ctx context.Context) (string, error)

func (*OCIProvider) GetPublicIPv4

func (p *OCIProvider) GetPublicIPv4(ctx context.Context) (string, error)

func (*OCIProvider) Name

func (p *OCIProvider) Name() string

type OpenStackProvider

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

func (*OpenStackProvider) GetHostname

func (p *OpenStackProvider) GetHostname(ctx context.Context) (string, error)

func (*OpenStackProvider) GetInstanceID

func (p *OpenStackProvider) GetInstanceID(ctx context.Context) (string, error)

func (*OpenStackProvider) GetPrimaryIPv6

func (p *OpenStackProvider) GetPrimaryIPv6(ctx context.Context) (string, error)

func (*OpenStackProvider) GetPrivateIPv4

func (p *OpenStackProvider) GetPrivateIPv4(ctx context.Context) (string, error)

func (*OpenStackProvider) GetPublicIPv4

func (p *OpenStackProvider) GetPublicIPv4(ctx context.Context) (string, error)

func (*OpenStackProvider) Name

func (p *OpenStackProvider) Name() string

type Provider

type Provider interface {
	Name() string
	GetInstanceID(ctx context.Context) (string, error)
	GetHostname(ctx context.Context) (string, error)
	GetPrivateIPv4(ctx context.Context) (string, error)
	GetPublicIPv4(ctx context.Context) (string, error)
	GetPrimaryIPv6(ctx context.Context) (string, error)
}

func GetProvider

func GetProvider(ctx context.Context) (Provider, error)

GetProvider retrieves the cloud provider, caching the result

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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