github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/influxdb/provider.go (about)

     1  package influxdb
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/influxdata/influxdb/client"
    11  )
    12  
    13  var quoteReplacer = strings.NewReplacer(`"`, `\"`)
    14  
    15  // Provider returns a terraform.ResourceProvider.
    16  func Provider() terraform.ResourceProvider {
    17  	return &schema.Provider{
    18  		ResourcesMap: map[string]*schema.Resource{
    19  			"influxdb_database":         resourceDatabase(),
    20  			"influxdb_user":             resourceUser(),
    21  			"influxdb_continuous_query": resourceContinuousQuery(),
    22  		},
    23  
    24  		Schema: map[string]*schema.Schema{
    25  			"url": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Optional: true,
    28  				DefaultFunc: schema.EnvDefaultFunc(
    29  					"INFLUXDB_URL", "http://localhost:8086/",
    30  				),
    31  			},
    32  			"username": &schema.Schema{
    33  				Type:        schema.TypeString,
    34  				Optional:    true,
    35  				DefaultFunc: schema.EnvDefaultFunc("INFLUXDB_USERNAME", ""),
    36  			},
    37  			"password": &schema.Schema{
    38  				Type:        schema.TypeString,
    39  				Optional:    true,
    40  				DefaultFunc: schema.EnvDefaultFunc("INFLUXDB_PASSWORD", ""),
    41  			},
    42  		},
    43  
    44  		ConfigureFunc: configure,
    45  	}
    46  }
    47  
    48  func configure(d *schema.ResourceData) (interface{}, error) {
    49  	url, err := url.Parse(d.Get("url").(string))
    50  	if err != nil {
    51  		return nil, fmt.Errorf("invalid InfluxDB URL: %s", err)
    52  	}
    53  
    54  	config := client.Config{
    55  		URL:      *url,
    56  		Username: d.Get("username").(string),
    57  		Password: d.Get("password").(string),
    58  	}
    59  
    60  	conn, err := client.NewClient(config)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  
    65  	_, _, err = conn.Ping()
    66  	if err != nil {
    67  		return nil, fmt.Errorf("error pinging server: %s", err)
    68  	}
    69  
    70  	return conn, nil
    71  }
    72  
    73  func quoteIdentifier(ident string) string {
    74  	return fmt.Sprintf(`%q`, quoteReplacer.Replace(ident))
    75  }