github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/postgresql/provider.go (about)

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