Skip to content

Commit

Permalink
add DigitalOcean provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulexus committed Apr 20, 2018
1 parent 3a58103 commit 785f89d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ information. There are two options:

## Supported providers

Currently, this tool supports three cloud providers:
Currently, this tool supports four cloud providers:

* `aws`: Amazon Web Services
* `azure`: Microsoft Azure Cloud
* `do`: Digital Ocean
* `gcp`: Google Cloud Platform

Not all providers support all network details.

I am happy to accept pull requests to implement more providers.

## Supported Fields
Expand Down
37 changes: 37 additions & 0 deletions discover/digitalocean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package discover

// TODO: decide out how, when, and if to return the Floating IP (URLs noted below)

import (
"net"
)

const (
doPrivateIPv4URL = "http://169.254.169.254/metadata/v1/interfaces/private/0/ipv4/address"
doPublicIPv4URL = "http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address"
doFloatingPublicIPv4EnabledURL = "http://169.254.169.254/metadata/v1/floating_ip/ipv4/active"
doFloatingPublicIPv4URL = "http://169.254.169.254/metadata/v1/interfaces/public/0/anchor_ipv4/address"
doPublicIPv6URL = "http://169.254.169.254/metadata/v1/interfaces/public/0/ipv6/address"
)

// NewDigitalOceanDiscoverer returns a new Digital Ocean network discoverer
func NewDigitalOceanDiscoverer() Discoverer {
return NewDiscoverer(
PublicIPv4DiscovererOption(doPublicIPv4),
PublicIPv6DiscovererOption(doPublicIPv6),
)

}

// FIXME: this URL never seems to be populated
func doPrivateIPv4() (net.IP, error) {
return StandardIPFromHTTP(doPrivateIPv4URL, nil)
}

func doPublicIPv4() (net.IP, error) {
return StandardIPFromHTTP(doPublicIPv4URL, nil)
}

func doPublicIPv6() (net.IP, error) {
return StandardIPFromHTTP(doPublicIPv6URL, nil)
}
2 changes: 1 addition & 1 deletion discover/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func ParseIPv4FromBody(in io.Reader) (net.IP, error) {
return nil, fmt.Errorf("failed to read response: %v", err)
}

if len(data) < 8 || len(data) > 18 {
if len(data) < 8 || len(data) > 42 {
return nil, fmt.Errorf("invalid response: %s", string(data))
}

Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (

func init() {
flag.BoolVar(&debug, "debug", false, `debug mode`)
flag.StringVar(&provider, "provider", "", `provider type. Options are: "aws", "azure", "gcp"`)
flag.StringVar(&provider, "provider", "", `provider type. Options are: "aws", "azure", "do", gcp"`)
flag.StringVar(&retField, "field", "", `return only a single field. Options are: "hostname", "publicv4", publicv6", "privatev4"`)
}

Expand Down Expand Up @@ -50,6 +50,8 @@ func main() {
discoverer = discover.NewAWSDiscoverer()
case "azure":
discoverer = discover.NewAzureDiscoverer()
case "do":
discoverer = discover.NewDigitalOceanDiscoverer()
case "gcp":
discoverer = discover.NewGCPDiscoverer()
default:
Expand Down

0 comments on commit 785f89d

Please sign in to comment.