github.com/bradfeehan/terraform@v0.7.0-rc3.0.20170529055808-34b45c5ad841/builtin/providers/postgresql/provider.go (about)

     1  package postgresql
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/errwrap"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  // Provider returns a terraform.ResourceProvider.
    12  func Provider() terraform.ResourceProvider {
    13  	return &schema.Provider{
    14  		Schema: map[string]*schema.Schema{
    15  			"host": {
    16  				Type:        schema.TypeString,
    17  				Optional:    true,
    18  				DefaultFunc: schema.EnvDefaultFunc("PGHOST", nil),
    19  				Description: "Name of PostgreSQL server address to connect to",
    20  			},
    21  			"port": {
    22  				Type:        schema.TypeInt,
    23  				Optional:    true,
    24  				DefaultFunc: schema.EnvDefaultFunc("PGPORT", 5432),
    25  				Description: "The PostgreSQL port number to connect to at the server host, or socket file name extension for Unix-domain connections",
    26  			},
    27  			"database": {
    28  				Type:        schema.TypeString,
    29  				Optional:    true,
    30  				Description: "The name of the database to connect to in order to conenct to (defaults to `postgres`).",
    31  				DefaultFunc: schema.EnvDefaultFunc("PGDATABASE", "postgres"),
    32  			},
    33  			"username": {
    34  				Type:        schema.TypeString,
    35  				Optional:    true,
    36  				DefaultFunc: schema.EnvDefaultFunc("PGUSER", "postgres"),
    37  				Description: "PostgreSQL user name to connect as",
    38  			},
    39  			"password": {
    40  				Type:        schema.TypeString,
    41  				Optional:    true,
    42  				DefaultFunc: schema.EnvDefaultFunc("PGPASSWORD", nil),
    43  				Description: "Password to be used if the PostgreSQL server demands password authentication",
    44  			},
    45  			"sslmode": {
    46  				Type:        schema.TypeString,
    47  				Optional:    true,
    48  				DefaultFunc: schema.EnvDefaultFunc("PGSSLMODE", nil),
    49  				Description: "This option determines whether or with what priority a secure SSL TCP/IP connection will be negotiated with the PostgreSQL server",
    50  			},
    51  			"ssl_mode": {
    52  				Type:       schema.TypeString,
    53  				Optional:   true,
    54  				Deprecated: "Rename PostgreSQL provider `ssl_mode` attribute to `sslmode`",
    55  			},
    56  			"connect_timeout": {
    57  				Type:         schema.TypeInt,
    58  				Optional:     true,
    59  				DefaultFunc:  schema.EnvDefaultFunc("PGCONNECT_TIMEOUT", 180),
    60  				Description:  "Maximum wait for connection, in seconds. Zero or not specified means wait indefinitely.",
    61  				ValidateFunc: validateConnTimeout,
    62  			},
    63  		},
    64  
    65  		ResourcesMap: map[string]*schema.Resource{
    66  			"postgresql_database":  resourcePostgreSQLDatabase(),
    67  			"postgresql_extension": resourcePostgreSQLExtension(),
    68  			"postgresql_schema":    resourcePostgreSQLSchema(),
    69  			"postgresql_role":      resourcePostgreSQLRole(),
    70  		},
    71  
    72  		ConfigureFunc: providerConfigure,
    73  	}
    74  }
    75  
    76  func validateConnTimeout(v interface{}, key string) (warnings []string, errors []error) {
    77  	value := v.(int)
    78  	if value < 0 {
    79  		errors = append(errors, fmt.Errorf("%s can not be less than 0", key))
    80  	}
    81  	return
    82  }
    83  
    84  func providerConfigure(d *schema.ResourceData) (interface{}, error) {
    85  	var sslMode string
    86  	if sslModeRaw, ok := d.GetOk("sslmode"); ok {
    87  		sslMode = sslModeRaw.(string)
    88  	} else {
    89  		sslMode = d.Get("ssl_mode").(string)
    90  	}
    91  	config := Config{
    92  		Host:              d.Get("host").(string),
    93  		Port:              d.Get("port").(int),
    94  		Database:          d.Get("database").(string),
    95  		Username:          d.Get("username").(string),
    96  		Password:          d.Get("password").(string),
    97  		SSLMode:           sslMode,
    98  		ApplicationName:   tfAppName(),
    99  		ConnectTimeoutSec: d.Get("connect_timeout").(int),
   100  	}
   101  
   102  	client, err := config.NewClient()
   103  	if err != nil {
   104  		return nil, errwrap.Wrapf("Error initializing PostgreSQL client: {{err}}", err)
   105  	}
   106  
   107  	return client, nil
   108  }
   109  
   110  func tfAppName() string {
   111  	return fmt.Sprintf("Terraform v%s", terraform.VersionString())
   112  }