github.com/erriapo/terraform@v0.6.12-0.20160203182612-0340ea72354f/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  		},
    39  
    40  		ResourcesMap: map[string]*schema.Resource{
    41  			"postgresql_database": resourcePostgresqlDatabase(),
    42  			"postgresql_role":     resourcePostgresqlRole(),
    43  		},
    44  
    45  		ConfigureFunc: providerConfigure,
    46  	}
    47  }
    48  
    49  func providerConfigure(d *schema.ResourceData) (interface{}, error) {
    50  	config := Config{
    51  		Host:     d.Get("host").(string),
    52  		Port:     d.Get("port").(int),
    53  		Username: d.Get("username").(string),
    54  		Password: d.Get("password").(string),
    55  	}
    56  
    57  	client, err := config.NewClient()
    58  	if err != nil {
    59  		return nil, fmt.Errorf("Error initializing Postgresql client: %s", err)
    60  	}
    61  
    62  	return client, nil
    63  }