Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
MagnumOpus21 committed Jul 18, 2018
2 parents 81a1a25 + fc7e9a8 commit 1b9cec3
Show file tree
Hide file tree
Showing 458 changed files with 276,769 additions and 966 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: go

go:
- 1.8.x
- tip
- stable

sudo: false
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ function.
* Scaleway [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/scaleway/scaleway_discover.go#L14-L22)
* SoftLayer [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/softlayer/softlayer_discover.go#L16-L25)
* Triton [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/triton/triton_discover.go#L17-L27)
* vSphere [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/vsphere/vsphere_discover.go#L148-L155)
* Packet [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/packet/packet_discover.go#L25-L35)

HashiCorp maintains acceptance tests that regularly allocate and run tests with
real resources to verify the behavior of several of these providers. Those
Expand Down Expand Up @@ -65,6 +67,11 @@ provider=softlayer datacenter=dal06 tag_value=consul username=... api_key=...
# Triton
provider=triton account=testaccount url=https://us-sw-1.api.joyentcloud.com key_id=... tag_key=consul-role tag_value=server
# vSphere
provider=vsphere category_name=consul-role tag_name=consul-server host=... user=... password=... insecure_ssl=[true|false]
# Packet
provider=packet auth_token=token project=uuid url=... address_type=...
```

## Command Line Tool Usage
Expand Down
4 changes: 4 additions & 0 deletions discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import (
"github.com/hashicorp/go-discover/provider/digitalocean"
"github.com/hashicorp/go-discover/provider/gce"
"github.com/hashicorp/go-discover/provider/os"
"github.com/hashicorp/go-discover/provider/packet"
"github.com/hashicorp/go-discover/provider/scaleway"
"github.com/hashicorp/go-discover/provider/softlayer"
"github.com/hashicorp/go-discover/provider/triton"
"github.com/hashicorp/go-discover/provider/vsphere"
)

// Provider has lookup functions for meta data in a
Expand Down Expand Up @@ -49,6 +51,8 @@ var Providers = map[string]Provider{
"scaleway": &scaleway.Provider{},
"softlayer": &softlayer.Provider{},
"triton": &triton.Provider{},
"vsphere": &vsphere.Provider{},
"packet": &packet.Provider{},
}

// Discover looks up metadata in different cloud environments.
Expand Down
8 changes: 4 additions & 4 deletions provider/os/os_discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,16 @@ func newClient(args map[string]string, l *log.Logger) (*gophercloud.ServiceClien
}
projectID := argsOrEnv(args, "project_id", "OS_PROJECT_ID")
insecure := argsOrEnv(args, "insecure", "OS_INSECURE")
domain_id := argsOrEnv(args, "domain_id", "OS_DOMAIN_ID")
domain_name := argsOrEnv(args, "domain_name", "OS_DOMAIN_NAME")

if url == "" {
return nil, fmt.Errorf("discover-os: Auth url must be provided")
}

ao := gophercloud.AuthOptions{
// "domain_id": OS_DOMAIN_ID
DomainID: "",
// "domain_name": OS_DOMAIN_NAME
DomainName: "",
DomainID: domain_id,
DomainName: domain_name,
IdentityEndpoint: url,
Username: username,
Password: password,
Expand Down
94 changes: 94 additions & 0 deletions provider/packet/packet_discover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package packet

import (
"fmt"
"log"
"os"

"github.com/packethost/packngo"
)

const baseURL = "https://api.packet.net/"

// Provider struct
type Provider struct {
userAgent string
}

// SetUserAgent setter
func (p *Provider) SetUserAgent(s string) {
p.userAgent = s
}

// Help function
func (p *Provider) Help() string {
return `Packet:
provider: "packet"
project: UUID of packet project. Required
auth_token: Packet authentication token. Required
url: Packet REST URL. Optional
address_type: "private_v4", "public_v4" or "public_v6". Defaults to "private_v4". Optional
Variables can also be provided by environmental variables:
export PACKET_PROJECT for project
export PACKET_URL for url
export PACKET_AUTH_TOKEN for auth_token
`
}

// Addrs function
func (p *Provider) Addrs(args map[string]string, l *log.Logger) ([]string, error) {
authToken := argsOrEnv(args, "auth_token", "PACKET_AUTH_TOKEN")
projectID := argsOrEnv(args, "project", "PACKET_PROJECT")
packetURL := argsOrEnv(args, "url", "PACKET_URL")
addressType := args["address_type"]

if addressType != "private_v4" && addressType != "public_v4" && addressType != "public_v6" {
l.Printf("[INFO] discover-packet: Address type %s is not supported. Valid values are {private_v4,public_v4,public_v6}. Falling back to 'private_v4'", addressType)
addressType = "private_v4"
}

c, err := client(p.userAgent, packetURL, authToken)
if err != nil {
return nil, fmt.Errorf("discover-packet: Initializing Packet client failed: %s", err)
}

var devices []packngo.Device

if projectID == "" {
return nil, fmt.Errorf("discover-packet: 'project' parameter must be provider")
}

devices, _, err = c.Devices.List(projectID, nil)
if err != nil {
return nil, fmt.Errorf("discover-packet: Fetching Packet devices failed: %s", err)
}
var addrs []string
for _, d := range devices {
addressFamily := 4
if addressType == "public_v6" {
addressFamily = 6
}
for _, n := range d.Network {

if (n.Public == (addressType == "public_v4" || addressType == "public_v6")) && n.AddressFamily == addressFamily {
addrs = append(addrs, n.Address)
}
}
}
return addrs, nil
}

func client(useragent, url, token string) (*packngo.Client, error) {
if url == "" {
url = baseURL
}

return packngo.NewClientWithBaseURL(useragent, token, nil, url)
}
func argsOrEnv(args map[string]string, key, env string) string {
if value := args[key]; value != "" {
return value
}
return os.Getenv(env)
}
102 changes: 102 additions & 0 deletions provider/packet/packet_discover_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package packet_test

import (
"log"
"os"
"testing"

discover "github.com/hashicorp/go-discover"
"github.com/hashicorp/go-discover/provider/packet"
)

var _ discover.Provider = (*packet.Provider)(nil)
var _ discover.ProviderWithUserAgent = (*packet.Provider)(nil)

func TestAddrsDefault(t *testing.T) {
args := discover.Config{
"provider": "packet",
"auth_token": os.Getenv("PACKET_TOKEN"),
"project": os.Getenv("PACKET_PROJECT"),
}

if args["auth_token"] == "" {
t.Skip("Packet credentials missing")
}

if args["project"] == "" {
t.Skip("Packet project UUID missing")
}

p := packet.Provider{}

l := log.New(os.Stderr, "", log.LstdFlags)
addrs, err := p.Addrs(args, l)

if err != nil {
t.Fatal(err)
}

if len(addrs) != 2 {
t.Fatalf("bad: %v", addrs)
}
}

func TestAddrsPublicV6(t *testing.T) {
args := discover.Config{
"provider": "packet",
"auth_token": os.Getenv("PACKET_TOKEN"),
"project": os.Getenv("PACKET_PROJECT"),
"address_type": "public_v6",
}

if args["auth_token"] == "" {
t.Skip("Packet credentials missing")
}

if args["project"] == "" {
t.Skip("Packet project UUID missing")
}

p := packet.Provider{}

l := log.New(os.Stderr, "", log.LstdFlags)
addrs, err := p.Addrs(args, l)

if err != nil {
t.Fatal(err)
}

if len(addrs) != 2 {
t.Fatalf("bad: %v", addrs)
}
}

func TestAddrsPublicV4(t *testing.T) {
args := discover.Config{
"provider": "packet",
"auth_token": os.Getenv("PACKET_TOKEN"),
"project": os.Getenv("PACKET_PROJECT"),
"address_type": "public_v4",
}

if args["auth_token"] == "" {
t.Skip("Packet credentials missing")
}

if args["project"] == "" {
t.Skip("Packet project UUID missing")
}

p := packet.Provider{}

l := log.New(os.Stderr, "", log.LstdFlags)
addrs, err := p.Addrs(args, l)

if err != nil {
t.Fatal(err)
}

if len(addrs) != 2 {
t.Fatalf("bad: %v", addrs)
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions provider/packet/vendor/github.com/packethost/packngo/LICENSE.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1b9cec3

Please sign in to comment.