Skip to content

nickgarlis/go-cloudmeta

Repository files navigation

CloudMeta

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

Go Reference License: MIT

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.

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages