github.com/gabrielperezs/terraform@v0.7.0-rc2.0.20160715084931-f7da2612946f/builtin/providers/postgresql/provider.go (about)

     1  package postgresql
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform/helper/schema"
     7  	"github.com/hashicorp/terraform/terraform"
     8  )
     9  
    10  // Provider returns a terraform.ResourceProvider.
    11  func Provider() terraform.ResourceProvider {
    12  	return &schema.Provider{
    13  		Schema: map[string]*schema.Schema{
    14  			"host": &schema.Schema{
    15  				Type:        schema.TypeString,
    16  				Required:    true,
    17  				DefaultFunc: schema.EnvDefaultFunc("POSTGRESQL_HOST", nil),
    18  				Description: "The postgresql server address",
    19  			},
    20  			"port": &schema.Schema{
    21  				Type:        schema.TypeInt,
    22  				Optional:    true,
    23  				Default:     5432,
    24  				Description: "The postgresql server port",
    25  			},
    26  			"username": &schema.Schema{
    27  				Type:        schema.TypeString,
    28  				Required:    true,
    29  				DefaultFunc: schema.EnvDefaultFunc("POSTGRESQL_USERNAME", nil),
    30  				Description: "Username for postgresql server connection",
    31  			},
    32  			"password": &schema.Schema{
    33  				Type:        schema.TypeString,
    34  				Required:    true,
    35  				DefaultFunc: schema.EnvDefaultFunc("POSTGRESQL_PASSWORD", nil),
    36  				Description: "Password for postgresql server connection",
    37  			},
    38  			"ssl_mode": &schema.Schema{
    39  				Type:        schema.TypeString,
    40  				Optional:    true,
    41  				Default:     "prefer",
    42  				Description: "Connection mode for postgresql server",
    43  			},
    44  		},
    45  
    46  		ResourcesMap: map[string]*schema.Resource{
    47  			"postgresql_database": resourcePostgresqlDatabase(),
    48  			"postgresql_role":     resourcePostgresqlRole(),
    49  		},
    50  
    51  		ConfigureFunc: providerConfigure,
    52  	}
    53  }
    54  
    55  func providerConfigure(d *schema.ResourceData) (interface{}, error) {
    56  	config := Config{
    57  		Host:     d.Get("host").(string),
    58  		Port:     d.Get("port").(int),
    59  		Username: d.Get("username").(string),
    60  		Password: d.Get("password").(string),
    61  		SslMode:  d.Get("ssl_mode").(string),
    62  	}
    63  
    64  	client, err := config.NewClient()
    65  	if err != nil {
    66  		return nil, fmt.Errorf("Error initializing Postgresql client: %s", err)
    67  	}
    68  
    69  	return client, nil
    70  }