-
Notifications
You must be signed in to change notification settings - Fork 123
/
softlayer_discover.go
67 lines (54 loc) · 1.84 KB
/
softlayer_discover.go
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
// Package softlayer provides node discovery for Softlayer.
package softlayer
import (
"fmt"
"io/ioutil"
"log"
"github.com/softlayer/softlayer-go/filter"
"github.com/softlayer/softlayer-go/services"
"github.com/softlayer/softlayer-go/session"
)
type Provider struct{}
func (p *Provider) Help() string {
return `Softlayer:
provider: "softlayer"
datacenter: The SoftLayer datacenter to filter on
tag_value: The tag value to filter on
username: The SoftLayer username to use
api_key: The SoftLayer api key to use
`
}
func (p *Provider) Addrs(args map[string]string, l *log.Logger) ([]string, error) {
if args["provider"] != "softlayer" {
return nil, fmt.Errorf("discover-softlayer: invalid provider " + args["provider"])
}
if l == nil {
l = log.New(ioutil.Discard, "", 0)
}
datacenter := args["datacenter"]
tagValue := args["tag_value"]
username := args["username"]
apiKey := args["api_key"]
l.Printf("[INFO] discover-softlayer: Datacenter is %q", datacenter)
// Create a session and get a service
sess := session.New(username, apiKey)
service := services.GetAccountService(sess)
// Compose the filter
mask := "id,hostname,domain,tagReferences[tag[name]],primaryBackendIpAddress,datacenter"
filterVMs := filter.Build(
filter.Path("virtualGuests.datacenter.name").Eq(datacenter),
filter.Path("virtualGuests.tagReferences.tag.name").Eq(tagValue),
)
// Get the virtual machines that match the filter
vms, err := service.Mask(mask).Filter(filterVMs).GetVirtualGuests()
if err != nil {
return nil, fmt.Errorf("discover-softlayer: %s", err)
}
var addrs []string
for _, vm := range vms {
l.Printf("[INFO] discover-softlayer: Found instance (%d) %s.%s with private IP: %s",
*vm.Id, *vm.Hostname, *vm.Domain, *vm.PrimaryBackendIpAddress)
addrs = append(addrs, *vm.PrimaryBackendIpAddress)
}
return addrs, nil
}