opentofu/builtin/providers/influxdb/provider.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

76 lines
1.7 KiB
Go

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))
}