Skip to content

nikhil-prabhu/clouddetect

Repository files navigation

clouddetect

maintenance-status Go Reference License: GPL v3 CI CD Go Report Card

A Go library to detect the cloud service provider of a host.

This library is the Go version of my Rust crate cloud-detect, which in itself is inspired by the Python-based cloud-detect and the Go-based satellite modules.

Like these modules, clouddetect uses a combination of checking vendor files and metadata endpoints to accurately determine the cloud provider of a host.

While this library is structured similarly to the Rust crate, it follows Go conventions and idioms and is not a direct port.

Features

  • Currently, this module supports the identification of the following providers:
    • Amazon Web Services (aws)
    • Microsoft Azure (azure)
    • Google Cloud Platform (gcp)
    • Alibaba Cloud (alibaba)
    • OpenStack (openstack)
    • DigitalOcean (digitalocean)
    • Oracle Cloud Infrastructure (oci)
    • Vultr (vultr)
  • Fast, simple and extensible.
  • Real-time console logging using the zap module.

Usage

Add the library to your project by running:

$ go get github.com/nikhil-prabhu/clouddetect@latest

Detect the cloud provider and print the result (with default timeout).

package main

import (
  "fmt"

  "github.com/nikhil-prabhu/clouddetect"
  "github.com/nikhil-prabhu/clouddetect/logging"
  "go.uber.org/zap"
)

func main() {
  // Optional; only if logging is required.
  logger := zap.Must(zap.NewProduction()) // Use zap.NewDevelopment() for development mode
  defer logger.Sync()
  logging.SetLogger(logger)

  provider := clouddetect.Detect(0)

  // When tested on AWS:
  fmt.Println(provider) // "aws"

  // When tested on local/non-supported cloud environment:
  fmt.Println(provider) // "unknown"
}

Detect the cloud provider and print the result (with custom timeout).

package main

import (
	"fmt"

	"github.com/nikhil-prabhu/clouddetect"
	"github.com/nikhil-prabhu/clouddetect/logging"
	"go.uber.org/zap"
)

func main() {
	// Optional; only if logging is required.
	logger := zap.Must(zap.NewProduction()) // Use zap.NewDevelopment() for development mode
	defer logger.Sync()
	logging.SetLogger(logger)

	provider := clouddetect.Detect(10)

	// When tested on AWS:
	fmt.Println(provider) // "aws"

	// When tested on local/non-supported cloud environment:
	fmt.Println(provider) // "unknown"
}

You can also check the list of currently supported cloud providers.

package main

import (
	"fmt"

	"github.com/nikhil-prabhu/clouddetect"
)

func main() {
	fmt.Println(clouddetect.SupportedProviders)
}

For more detailed documentation, please refer to the Module Documentation.

Contributing

Contributions are welcome and greatly appreciated! If you’d like to contribute to clouddetect, here’s how you can help.

1. Report Issues

If you encounter a bug, unexpected behavior, or have a feature request, please open an issue. Be sure to include:

  • A clear description of the issue.
  • Steps to reproduce, if applicable.
  • Details about your environment.

2. Submit Pull Requests

If you're submitting a pull request, please ensure the following.

  • Your code is formatted using go fmt
$ go fmt ./...
  • Code lints pass with (use --fix to autofix):
$ golangci-lint run -v --fix

NOTE: To install golangci-lint, follow the steps outlined here

  • Your code contains sufficient unit tests and that all tests pass.
$ go test ./...

3. Improve Documentation

If you find areas in the documentation that are unclear or incomplete, feel free to update the README or module-level documentation. Open a pull request with your improvements.

4. Review Pull Requests

You can also contribute by reviewing open pull requests. Providing constructive feedback helps maintain a high-quality codebase.