opentofu/builtin/providers/influxdb/continuous_query.go
Raphael Randschau 7630a585a2 Improve influxdb provider (#7333)
* Improve influxdb provider

- reduce public funcs. We should not make things public that don't need to be public
- improve tests by verifying remote state
- add influxdb_user resource

    allows you to manage influxdb users:

    ```
    resource "influxdb_user" "admin" {
      name = "administrator"
      password = "super-secret"
      admin = true
    }
    ```

    and also database specific grants:

    ```
    resource "influxdb_user" "ro" {
      name = "read-only"
      password = "read-only"

      grant {
        database = "a"
        privilege = "read"
      }
    }
    ```

* Grant/ revoke admin access properly

* Add continuous_query resource

see
https://docs.influxdata.com/influxdb/v0.13/query_language/continuous_queries/
for the details about continuous queries:

```
resource "influxdb_database" "test" {
    name = "terraform-test"
}

resource "influxdb_continuous_query" "minnie" {
    name = "minnie"
    database = "${influxdb_database.test.name}"
    query = "SELECT min(mouse) INTO min_mouse FROM zoo GROUP BY time(30m)"
}
```
2016-08-05 16:27:03 +10:00

121 lines
2.6 KiB
Go

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
}