forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
continuous_query.go
120 lines (99 loc) · 2.56 KB
/
continuous_query.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package influxdb
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/influxdata/influxdb/client"
)
func resourceContinuousQuery() *schema.Resource {
return &schema.Resource{
Create: createContinuousQuery,
Read: readContinuousQuery,
Delete: deleteContinuousQuery,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"database": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"query": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func createContinuousQuery(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*client.Client)
name := d.Get("name").(string)
database := d.Get("database").(string)
queryStr := fmt.Sprintf("CREATE CONTINUOUS QUERY %s ON %s BEGIN %s END", name, quoteIdentifier(database), d.Get("query").(string))
query := client.Query{
Command: queryStr,
}
resp, err := conn.Query(query)
if err != nil {
return err
}
if resp.Err != nil {
return resp.Err
}
d.Set("name", name)
d.Set("database", database)
d.Set("query", d.Get("query").(string))
d.SetId(fmt.Sprintf("influxdb-cq:%s", name))
return readContinuousQuery(d, meta)
}
func readContinuousQuery(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*client.Client)
name := d.Get("name").(string)
database := d.Get("database").(string)
// InfluxDB doesn't have a command to check the existence of a single
// ContinuousQuery, so we instead must read the list of all ContinuousQuerys and see
// if ours is present in it.
query := client.Query{
Command: "SHOW CONTINUOUS QUERIES",
}
resp, err := conn.Query(query)
if err != nil {
return err
}
if resp.Err != nil {
return resp.Err
}
for _, series := range resp.Results[0].Series {
if series.Name == database {
for _, result := range series.Values {
if result[0].(string) == name {
return nil
}
}
}
}
// If we fell out here then we didn't find our ContinuousQuery in the list.
d.SetId("")
return nil
}
func deleteContinuousQuery(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*client.Client)
name := d.Get("name").(string)
database := d.Get("database").(string)
queryStr := fmt.Sprintf("DROP CONTINUOUS QUERY %s ON %s", name, quoteIdentifier(database))
query := client.Query{
Command: queryStr,
}
resp, err := conn.Query(query)
if err != nil {
return err
}
if resp.Err != nil {
return resp.Err
}
d.SetId("")
return nil
}