Debian Codegraph @

discover.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // Package discover provides functions to get metadata for different
2 // cloud environments.
3 package discover
4
5 import (
6 "fmt"
7 "log"
8 "sort"
9 "strings"
10 "sync"
11
12 "github.com/hashicorp/go-discover/provider/aliyun"
13 "github.com/hashicorp/go-discover/provider/aws"
14 "github.com/hashicorp/go-discover/provider/gce"
15 "github.com/hashicorp/go-discover/provider/mdns"
16 "github.com/hashicorp/go-discover/provider/os"
17 "github.com/hashicorp/go-discover/provider/packet"
18 )
19
20 // Provider has lookup functions for meta data in a
21 // cloud environment.
22 type Provider interface {
23 // Addrs looks up addresses in the cloud environment according to the
24 // configuration provided in args.
25 Addrs(args map[string]string, l *log.Logger) ([]string, error)
26
27 // Help provides the configuration help for the command line client.
28 Help() string
29 }
30
31 // ProviderWithUserAgent is a provider that declares it's user agent. Not all
32 // providers support this.
33 type ProviderWithUserAgent interface {
34 // SetUserAgent sets the user agent on the provider to the provided string.
35 SetUserAgent(s string)
36 }
37
38 // Providers contains all available providers.
39 var Providers = map[string]Provider{
40 "aliyun": &aliyun.Provider{},
41 "aws": &aws.Provider{},
42 "gce": &gce.Provider{},
43 "mdns": &mdns.Provider{},
44 "os": &os.Provider{},
45 "packet": &packet.Provider{},
46 }
47
48 // Discover looks up metadata in different cloud environments.
49 type Discover struct {
50 // Providers is the list of address lookup providers.
51 // If nil, the default list of providers is used.
52 Providers map[string]Provider
53
54 // userAgent is the string to use for requests, when supported.
55 userAgent string
56
57 // once is used to initialize the actual list of providers.
58 once sync.Once
59 }
60
61 // Option is used as an initialization option/
62 type Option func(*Discover) error
63
64 // New creates a new discover client with the given options.
65 func New(opts ...Option) (*Discover, error) {
66 d := new(Discover)
67
68 for _, opt := range opts {
69 if err := opt(d); err != nil {
70 return nil, err
71 }
72 }
73
74 d.once.Do(d.initProviders)
75
76 return d, nil
77 }
78
79 // WithUserAgent allows specifying a custom user agent option to send with
80 // requests when the underlying client library supports it.
81 func WithUserAgent(agent string) Option {
82 return func(d *Discover) error {
83 d.userAgent = agent
84 return nil
85 }
86 }
87
88 // WithProviders allows specifying your own set of providers.
89 func WithProviders(m map[string]Provider) Option {
90 return func(d *Discover) error {
91 d.Providers = m
92 return nil
93 }
94 }
95
96 // initProviders sets the list of providers to the
97 // default list of providers if none are configured.
98 func (d *Discover) initProviders() {
99 if d.Providers == nil {
100 d.Providers = Providers
101 }
102 }
103
104 // Names returns the names of the configured providers.
105 func (d *Discover) Names() []string {
106 d.once.Do(d.initProviders)
107
108 var names []string
109 for n := range d.Providers {
110 names = append(names, n)
111 }
112 sort.Strings(names)
113 return names
114 }
115
116 var globalHelp = `The options for discovering ip addresses are provided as a
117 single string value in "key=value key=value ..." format where
118 the values are URL encoded.
119
120 provider=aws region=eu-west-1 ...
121
122 The options are provider specific and are listed below.
123 `
124
125 // Help describes the format of the configuration string for address discovery
126 // and the various provider specific options.
127 func (d *Discover) Help() string {
128 d.once.Do(d.initProviders)
129
130 h := []string{globalHelp}
131 for _, name := range d.Names() {
132 h = append(h, d.Providers[name].Help())
133 }
134 return strings.Join(h, "\n")
135 }
136
137 // Addrs discovers ip addresses of nodes that match the given filter criteria.
138 // The config string must have the format 'provider=xxx key=val key=val ...'
139 // where the keys and values are provider specific. The values are URL encoded.
140 func (d *Discover) Addrs(cfg string, l *log.Logger) ([]string, error) {
141 d.once.Do(d.initProviders)
142
143 args, err := Parse(cfg)
144 if err != nil {
145 return nil, fmt.Errorf("discover: %s", err)
146 }
147
148 name := args["provider"]
149 if name == "" {
150 return nil, fmt.Errorf("discover: no provider")
151 }
152
153 providers := d.Providers
154 if providers == nil {
155 providers = Providers
156 }
157
158 p := providers[name]
159 if p == nil {
160 return nil, fmt.Errorf("discover: unknown provider " + name)
161 }
162 l.Printf("[DEBUG] discover: Using provider %q", name)
163
164 if typ, ok := p.(ProviderWithUserAgent); ok {
165 typ.SetUserAgent(d.userAgent)
166 return p.Addrs(args, l)
167 }
168
169 return p.Addrs(args, l)
170 }