-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/hashicorp/go-discover
- Loading branch information
Showing
458 changed files
with
276,769 additions
and
966 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
language: go | ||
|
||
go: | ||
- 1.8.x | ||
- tip | ||
- stable | ||
|
||
sudo: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
provider/packet/vendor/github.com/packethost/packngo/CHANGELOG.md
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
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.
Oops, something went wrong.
Oops, something went wrong.