github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/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  		},
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"url": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Optional: true,
    26  				DefaultFunc: schema.EnvDefaultFunc(
    27  					"INFLUXDB_URL", "http://localhost:8086/",
    28  				),
    29  			},
    30  			"username": &schema.Schema{
    31  				Type:        schema.TypeString,
    32  				Optional:    true,
    33  				DefaultFunc: schema.EnvDefaultFunc("INFLUXDB_USERNAME", ""),
    34  			},
    35  			"password": &schema.Schema{
    36  				Type:        schema.TypeString,
    37  				Optional:    true,
    38  				DefaultFunc: schema.EnvDefaultFunc("INFLUXDB_PASSWORD", ""),
    39  			},
    40  		},
    41  
    42  		ConfigureFunc: Configure,
    43  	}
    44  }
    45  
    46  func Configure(d *schema.ResourceData) (interface{}, error) {
    47  	url, err := url.Parse(d.Get("url").(string))
    48  	if err != nil {
    49  		return nil, fmt.Errorf("invalid InfluxDB URL: %s", err)
    50  	}
    51  
    52  	config := client.Config{
    53  		URL:      *url,
    54  		Username: d.Get("username").(string),
    55  		Password: d.Get("password").(string),
    56  	}
    57  
    58  	conn, err := client.NewClient(config)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	_, _, err = conn.Ping()
    64  	if err != nil {
    65  		return nil, fmt.Errorf("error pinging server: %s", err)
    66  	}
    67  
    68  	return conn, nil
    69  }
    70  
    71  func quoteIdentifier(ident string) string {
    72  	return fmt.Sprintf(`"%s"`, quoteReplacer.Replace(ident))
    73  }