forked from russellcardullo/terraform-provider-pingdom
-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.go
85 lines (73 loc) · 2.06 KB
/
config.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package pingdom
import (
"errors"
"log"
"os"
"github.com/DrFaust92/go-pingdom/pingdom"
"github.com/DrFaust92/go-pingdom/pingdomext"
)
// Config respresents the client configuration
type Config struct {
APIToken string `mapstructure:"api_token"`
SolarwindsUser string `mapstructure:"solarwinds_user"`
SolarwindsPassword string `mapstructure:"solarwinds_passwd"`
SolarwindsOrgID string `mapstructure:"solarwinds_org_id"`
}
type Clients struct {
Pingdom *pingdom.Client
PingdomExt *pingdomext.Client
// Solarwinds *solarwinds.Client
}
func (c *Config) Client() (*Clients, error) {
pingdomClient, err := c.pingdomClient()
if err != nil {
return nil, err
}
if user := os.Getenv("SOLARWINDS_USER"); user != "" {
if password := os.Getenv("SOLARWINDS_PASSWD"); password != "" {
c.SolarwindsUser = user
c.SolarwindsPassword = password
} else {
return nil, errors.New("user and password must be present together")
}
}
if orgID := os.Getenv("SOLARWINDS_ORG_ID"); orgID != "" {
c.SolarwindsOrgID = orgID
}
// solarwindsClient, err := solarwinds.NewClient(solarwinds.ClientConfig{
// Username: c.SolarwindsUser,
// Password: c.SolarwindsPassword,
// })
// if err != nil {
// return nil, err
// }
// err = solarwindsClient.Init()
// if err != nil {
// return nil, err
// }
var pingdomClientExt *pingdomext.Client
if c.SolarwindsUser != "" {
pingdomClientExt, err = pingdomext.NewClientWithConfig(pingdomext.ClientConfig{
Username: c.SolarwindsUser,
Password: c.SolarwindsPassword,
OrgID: c.SolarwindsOrgID,
})
if err != nil {
return nil, err
}
}
return &Clients{
Pingdom: pingdomClient,
PingdomExt: pingdomClientExt,
// Solarwinds: solarwindsClient,
}, nil
}
// Client returns a new client for accessing pingdom.
func (c *Config) pingdomClient() (*pingdom.Client, error) {
if v := os.Getenv("PINGDOM_API_TOKEN"); v != "" {
c.APIToken = v
}
client, _ := pingdom.NewClientWithConfig(pingdom.ClientConfig{APIToken: c.APIToken})
log.Printf("[INFO] Pingdom Client configured.")
return client, nil
}