File: triton_discover.go

package info (click to toggle)
golang-github-hashicorp-go-discover 0.0%2Bgit20190905.34a6505-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 480 kB
  • sloc: makefile: 9
file content (90 lines) | stat: -rw-r--r-- 2,217 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Package aws provides node discovery for Joyent Triton.
package triton

import (
	"context"
	"fmt"
	"io/ioutil"
	"log"

	"github.com/joyent/triton-go"
	"github.com/joyent/triton-go/authentication"
	"github.com/joyent/triton-go/compute"
)

type Provider struct{}

func (p *Provider) Help() string {
	return `triton:

    provider:     "triton"
    account: 	  The Triton account name
    key_id:       The Triton KeyID
    url:          The Triton URL
    tag_key:      The tag key to filter on
    tag_value:    The tag value to filter on
`
}

func (p *Provider) Addrs(args map[string]string, l *log.Logger) ([]string, error) {
	if args["provider"] != "triton" {
		return nil, fmt.Errorf("discover-triton: invalid provider " + args["provider"])
	}

	if l == nil {
		l = log.New(ioutil.Discard, "", 0)
	}

	account := args["account"]
	keyID := args["key_id"]
	url := args["url"]
	tagKey := args["tag_key"]
	tagValue := args["tag_value"]

	l.Printf("[INFO] discover-triton: Account is %q", account)
	l.Printf("[INFO] discover-triton: URL is %q", url)

	input := authentication.SSHAgentSignerInput{
		KeyID:       keyID,
		AccountName: account,
	}
	signer, err := authentication.NewSSHAgentSigner(input)
	if err != nil {
		return nil, fmt.Errorf("error Creating SSH Agent Signer: %v", err)
	}

	config := &triton.ClientConfig{
		TritonURL:   url,
		AccountName: account,
		Signers:     []authentication.Signer{signer},
	}

	c, err := compute.NewClient(config)
	if err != nil {
		return nil, fmt.Errorf("error constructing Compute Client: %v", err)
	}

	t := make(map[string]interface{}, 0)
	t[tagKey] = tagValue

	listInput := &compute.ListInstancesInput{
		Tags: t,
	}
	instances, err := c.Instances().List(context.Background(), listInput)
	if err != nil {
		return nil, fmt.Errorf("error getting instance list: %v", err)
	}
	var addrs []string
	for _, instance := range instances {
		l.Printf("[DEBUG] Instance ID: %q", instance.ID)
		l.Printf("[DEBUG] Instance PrimaryIP: %q", instance.PrimaryIP)
		if instance.PrimaryIP == "" {
			l.Printf("[DEBUG] discover-triton: Instance %s has no marked PrimaryIP", instance.ID)
			continue
		} else {
			addrs = append(addrs, instance.PrimaryIP)
		}
	}

	return addrs, nil
}