vendor: update github.com/hashicorp/go-discover

This commit is contained in:
Frank Schroeder 2017-06-22 12:46:23 +02:00
parent 29373fa2b7
commit da01cd112e
No known key found for this signature in database
GPG Key ID: 4D65C6EAEC87DECD
8 changed files with 113 additions and 63 deletions

View File

@ -1,57 +1,70 @@
# Go Discover Nodes for Cloud Providers # Go Discover Nodes for Cloud Providers
`go-discover` is a Go (golang) library to discover ip addresses of nodes `go-discover` is a Go (golang) library and command line tool to discover
in cloud environments based on meta information like tags provided by ip addresses of nodes in cloud environments based on meta information
the environment. like tags provided by the environment.
The following cloud providers have built-in support but additional providers The configuration for the providers is provided as a list of `key=val
key=val ...` tuples where the values can be URL encoded. The provider is
determined through the `provider` key. Effectively, only spaces have to
be encoded with a `+` and on the command line you have to observe
quoting rules with your shell.
### Example
```
# Amazon AWS
provider=aws region=eu-west-1 tag_key=consul tag_value=... access_key_id=... secret_access_key=...
# Google Cloud
provider=gce project_name=... zone_pattern=eu-west-* tag_value=consul credentials_file=...
# Microsoft Azure
provider=azure tag_name=consul tag_value=... tenant_id=... client_id=... subscription_id=... secret_access_key=...
```
### Supported Providers
The following cloud providers are supported but additional providers
can be added to the `discover.Disoverers` map. can be added to the `discover.Disoverers` map.
* Amazon AWS * Amazon AWS [Config options](http://godoc.org/github.com/hashicorp/go-discover/aws)
* Google Cloud * Google Cloud [Config options](http://godoc.org/github.com/hashicorp/go-discover/gce)
* Microsoft Azure * Microsoft Azure [Config options](http://godoc.org/github.com/hashicorp/go-discover/azure)
## Usage ## Command Line Tool Usage
First, install the library: Install the command line tool with:
```
go get -u github.com/hashicorp/go-discover/cmd/discover
```
Then run it with:
```
$ discover provider=aws region=eu-west-1 ...
```
## Library Usage
Install the library with:
``` ```
go get -u github.com/hashicorp/go-discover go get -u github.com/hashicorp/go-discover
``` ```
All providers are configured with a "key=val key=val ..." format Then call the `discover.Discover` function with the arguments
strings where the values are URL encoded. The `discover.Discover` for the provider you want to use:
function determines the provider through the `provider` key.
Example:
``` ```go
provider=aws region=eu-west-1 ... # use ioutil.Discard for no log output
```
### Amazon AWS
```
l := log.New(os.Stderr, "", log.LstdFlags) l := log.New(os.Stderr, "", log.LstdFlags)
args := "provider=aws region=eu-west-1 tag_key=consul tag_value=... access_key_id=... secret_access_key=..." args := "provider=aws region=eu-west-1 ..."
nodes, err := discover.Discover(args, l) addrs, err := discover.Discover(args, l)
``` ```
### Google Cloud For complete API documentation, see [GoDoc](https://godoc.org/github.com/hashicorp/go-discover) and
the [supported providers](http://godoc.org/github.com/hashicorp/go-discover#pkg-subdirectories).
```
l := log.New(os.Stderr, "", log.LstdFlags)
args := "provider=gce project_name=... zone_pattern=eu-west-* tag_value=consul credentials_file=..."
nodes, err := discover.Discover(args, l)
```
### Microsoft Azure
```
l := log.New(os.Stderr, "", log.LstdFlags)
args := "provider=azure tag_name=consul tag_value=... tenant_id=... client_id=... subscription_id=... secret_access_key=..."
nodes, err := discover.Discover(args, l)
```
For complete API documentation, see [GoDoc](https://godoc.org/github.com/hashicorp/go-discover).

View File

@ -2,6 +2,7 @@
package aws package aws
import ( import (
"fmt"
"log" "log"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
@ -35,7 +36,7 @@ import (
func Discover(cfg string, l *log.Logger) ([]string, error) { func Discover(cfg string, l *log.Logger) ([]string, error) {
m, err := config.Parse(cfg) m, err := config.Parse(cfg)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("discover-aws: %s", err)
} }
region := m["region"] region := m["region"]
@ -49,7 +50,7 @@ func Discover(cfg string, l *log.Logger) ([]string, error) {
ec2meta := ec2metadata.New(session.New()) ec2meta := ec2metadata.New(session.New())
identity, err := ec2meta.GetInstanceIdentityDocument() identity, err := ec2meta.GetInstanceIdentityDocument()
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("discover-aws: %s", err)
} }
region = identity.Region region = identity.Region
} }
@ -80,7 +81,7 @@ func Discover(cfg string, l *log.Logger) ([]string, error) {
}, },
}) })
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("discover-aws: %s", err)
} }
l.Printf("[INFO] discover-aws: Filter instances by %s=%s", tagKey, tagValue) l.Printf("[INFO] discover-aws: Filter instances by %s=%s", tagKey, tagValue)

View File

@ -34,7 +34,7 @@ import (
func Discover(cfg string, l *log.Logger) ([]string, error) { func Discover(cfg string, l *log.Logger) ([]string, error) {
m, err := config.Parse(cfg) m, err := config.Parse(cfg)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("discover-azure: %s", err)
} }
tenantID := m["tenant_id"] tenantID := m["tenant_id"]
@ -47,13 +47,13 @@ func Discover(cfg string, l *log.Logger) ([]string, error) {
// Only works for the Azure PublicCLoud for now; no ability to test other Environment // Only works for the Azure PublicCLoud for now; no ability to test other Environment
oauthConfig, err := azure.PublicCloud.OAuthConfigForTenant(tenantID) oauthConfig, err := azure.PublicCloud.OAuthConfigForTenant(tenantID)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("discover-azure: %s", err)
} }
// Get the ServicePrincipalToken for use searching the NetworkInterfaces // Get the ServicePrincipalToken for use searching the NetworkInterfaces
sbt, err := azure.NewServicePrincipalToken(*oauthConfig, clientID, secretKey, azure.PublicCloud.ResourceManagerEndpoint) sbt, err := azure.NewServicePrincipalToken(*oauthConfig, clientID, secretKey, azure.PublicCloud.ResourceManagerEndpoint)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("discover-azure: %s", err)
} }
// Setup the client using autorest; followed the structure from Terraform // Setup the client using autorest; followed the structure from Terraform
@ -66,7 +66,7 @@ func Discover(cfg string, l *log.Logger) ([]string, error) {
// unless there is a compelling reason to restrict // unless there is a compelling reason to restrict
netres, err := vmnet.ListAll() netres, err := vmnet.ListAll()
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("discover-azure: %s", err)
} }
// For now, ignore Primary interfaces, choose any PrivateIPAddress with the matching tags // For now, ignore Primary interfaces, choose any PrivateIPAddress with the matching tags

View File

@ -0,0 +1,38 @@
// discover provides node discovery on the command line.
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
discover "github.com/hashicorp/go-discover"
)
func main() {
help := flag.Bool("h", false, "show help")
quiet := flag.Bool("q", false, "no output")
flag.Parse()
args := strings.Join(flag.Args(), " ")
if *help || args == "" {
fmt.Println("Usage: discover key=val key=val ...")
os.Exit(0)
}
var w io.Writer = os.Stderr
if *quiet {
w = ioutil.Discard
}
l := log.New(w, "", log.LstdFlags)
addrs, err := discover.Discover(args, l)
if err != nil {
l.Fatal("[ERR] ", err)
}
fmt.Println(strings.Join(addrs, " "))
}

View File

@ -19,12 +19,12 @@ func Parse(s string) (map[string]string, error) {
for _, v := range strings.Fields(s) { for _, v := range strings.Fields(s) {
p := strings.SplitN(v, "=", 2) p := strings.SplitN(v, "=", 2)
if len(p) != 2 { if len(p) != 2 {
return nil, fmt.Errorf("discover: invalid format: %s", v) return nil, fmt.Errorf("invalid format: %s", v)
} }
key := p[0] key := p[0]
val, err := url.QueryUnescape(p[1]) val, err := url.QueryUnescape(p[1])
if err != nil { if err != nil {
return nil, fmt.Errorf("discover: invalid format: %s", v) return nil, fmt.Errorf("invalid format: %s", v)
} }
m[key] = val m[key] = val
} }

View File

@ -3,7 +3,6 @@ package discover
import ( import (
"fmt" "fmt"
"log" "log"
"strings"
"github.com/hashicorp/go-discover/aws" "github.com/hashicorp/go-discover/aws"
"github.com/hashicorp/go-discover/azure" "github.com/hashicorp/go-discover/azure"
@ -36,10 +35,9 @@ func init() {
// provider=aws region=eu-west-1 ... // provider=aws region=eu-west-1 ...
// //
func Discover(cfg string, l *log.Logger) ([]string, error) { func Discover(cfg string, l *log.Logger) ([]string, error) {
args := strings.SplitN(cfg, " ", 2) m, err := config.Parse(cfg)
m, err := config.Parse(args[0])
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("discover: %s", err)
} }
p := m["provider"] p := m["provider"]
if p == "" { if p == "" {
@ -49,5 +47,5 @@ func Discover(cfg string, l *log.Logger) ([]string, error) {
if d == nil { if d == nil {
return nil, fmt.Errorf("discover: unknown provider %q", p) return nil, fmt.Errorf("discover: unknown provider %q", p)
} }
return d(args[1], l) return d(cfg, l)
} }

View File

@ -44,7 +44,7 @@ import (
func Discover(cfg string, l *log.Logger) ([]string, error) { func Discover(cfg string, l *log.Logger) ([]string, error) {
m, err := config.Parse(cfg) m, err := config.Parse(cfg)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("discover-gce: %s", err)
} }
project := m["project_name"] project := m["project_name"]
@ -57,7 +57,7 @@ func Discover(cfg string, l *log.Logger) ([]string, error) {
l.Println("[INFO] discover-gce: Looking up project name") l.Println("[INFO] discover-gce: Looking up project name")
p, err := lookupProject() p, err := lookupProject()
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("discover-gce: %s", err)
} }
project = p project = p
} }
@ -69,11 +69,11 @@ func Discover(cfg string, l *log.Logger) ([]string, error) {
} }
client, err := client(creds) client, err := client(creds)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("discover-gce: %s", err)
} }
svc, err := compute.New(client) svc, err := compute.New(client)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("discover-gce: %s", err)
} }
// lookup the project zones to look in // lookup the project zones to look in
@ -84,7 +84,7 @@ func Discover(cfg string, l *log.Logger) ([]string, error) {
} }
zones, err := lookupZones(svc, project, zone) zones, err := lookupZones(svc, project, zone)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("discover-gce: %s", err)
} }
l.Printf("[INFO] discover-gce: Found zones %v", zones) l.Printf("[INFO] discover-gce: Found zones %v", zones)
@ -93,7 +93,7 @@ func Discover(cfg string, l *log.Logger) ([]string, error) {
for _, zone := range zones { for _, zone := range zones {
a, err := lookupAddrs(svc, project, zone, tagValue) a, err := lookupAddrs(svc, project, zone, tagValue)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("discover-gce: %s", err)
} }
l.Printf("[INFO] discover-gce: Zone %q has %v", zone, a) l.Printf("[INFO] discover-gce: Zone %q has %v", zone, a)
addrs = append(addrs, a...) addrs = append(addrs, a...)

6
vendor/vendor.json vendored
View File

@ -246,10 +246,10 @@
"revisionTime": "2017-02-11T01:34:15Z" "revisionTime": "2017-02-11T01:34:15Z"
}, },
{ {
"checksumSHA1": "8emlOr8dXbbAfm/JrNiHJcSpCLg=", "checksumSHA1": "SZ5/I9XKRS2myif+8kRgZbhVUHE=",
"path": "github.com/hashicorp/go-discover", "path": "github.com/hashicorp/go-discover",
"revision": "403f22046550d37902eb57353ad20431377f2967", "revision": "2c6c2771ed739d39922779b1a81e8b99c5eef6b2",
"revisionTime": "2017-06-20T11:59:42Z", "revisionTime": "2017-06-22T10:42:22Z",
"tree": true "tree": true
}, },
{ {