forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
75 lines (63 loc) · 1.73 KB
/
provider.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
package influxdb
import (
"fmt"
"net/url"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/influxdata/influxdb/client"
)
var quoteReplacer = strings.NewReplacer(`"`, `\"`)
// Provider returns a terraform.ResourceProvider.
func Provider() terraform.ResourceProvider {
return &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
"influxdb_database": resourceDatabase(),
"influxdb_user": resourceUser(),
"influxdb_continuous_query": resourceContinuousQuery(),
},
Schema: map[string]*schema.Schema{
"url": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc(
"INFLUXDB_URL", "http://localhost:8086/",
),
},
"username": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("INFLUXDB_USERNAME", ""),
},
"password": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("INFLUXDB_PASSWORD", ""),
},
},
ConfigureFunc: configure,
}
}
func configure(d *schema.ResourceData) (interface{}, error) {
url, err := url.Parse(d.Get("url").(string))
if err != nil {
return nil, fmt.Errorf("invalid InfluxDB URL: %s", err)
}
config := client.Config{
URL: *url,
Username: d.Get("username").(string),
Password: d.Get("password").(string),
}
conn, err := client.NewClient(config)
if err != nil {
return nil, err
}
_, _, err = conn.Ping()
if err != nil {
return nil, fmt.Errorf("error pinging server: %s", err)
}
return conn, nil
}
func quoteIdentifier(ident string) string {
return fmt.Sprintf(`%q`, quoteReplacer.Replace(ident))
}