Merge pull request #825 from jefferai/envdefault

Move duplicated envDefaultFunc out of each provider and into Schema.
This commit is contained in:
Mitchell Hashimoto 2015-01-16 10:50:43 -08:00
commit b3e77ef244
10 changed files with 36 additions and 128 deletions

View File

@ -1,8 +1,6 @@
package atlas package atlas
import ( import (
"os"
"github.com/hashicorp/atlas-go/v1" "github.com/hashicorp/atlas-go/v1"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
@ -21,14 +19,14 @@ func Provider() terraform.ResourceProvider {
"token": &schema.Schema{ "token": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: envDefaultFunc("ATLAS_TOKEN", nil), DefaultFunc: schema.EnvDefaultFunc("ATLAS_TOKEN", nil),
Description: descriptions["token"], Description: descriptions["token"],
}, },
"address": &schema.Schema{ "address": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
DefaultFunc: envDefaultFunc("ATLAS_ADDRESS", defaultAtlasServer), DefaultFunc: schema.EnvDefaultFunc("ATLAS_ADDRESS", defaultAtlasServer),
Description: descriptions["address"], Description: descriptions["address"],
}, },
}, },
@ -55,16 +53,6 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
return client, nil return client, nil
} }
func envDefaultFunc(k string, alt interface{}) schema.SchemaDefaultFunc {
return func() (interface{}, error) {
if v := os.Getenv(k); v != "" {
return v, nil
}
return alt, nil
}
}
var descriptions map[string]string var descriptions map[string]string
func init() { func init() {

View File

@ -1,8 +1,6 @@
package aws package aws
import ( import (
"os"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
@ -17,21 +15,21 @@ func Provider() terraform.ResourceProvider {
"access_key": &schema.Schema{ "access_key": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: envDefaultFunc("AWS_ACCESS_KEY"), DefaultFunc: schema.EnvDefaultFunc("AWS_ACCESS_KEY", nil),
Description: descriptions["access_key"], Description: descriptions["access_key"],
}, },
"secret_key": &schema.Schema{ "secret_key": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: envDefaultFunc("AWS_SECRET_KEY"), DefaultFunc: schema.EnvDefaultFunc("AWS_SECRET_KEY", nil),
Description: descriptions["secret_key"], Description: descriptions["secret_key"],
}, },
"region": &schema.Schema{ "region": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: envDefaultFunc("AWS_REGION"), DefaultFunc: schema.EnvDefaultFunc("AWS_REGION", nil),
Description: descriptions["region"], Description: descriptions["region"],
InputDefault: "us-east-1", InputDefault: "us-east-1",
}, },
@ -79,16 +77,6 @@ func init() {
} }
} }
func envDefaultFunc(k string) schema.SchemaDefaultFunc {
return func() (interface{}, error) {
if v := os.Getenv(k); v != "" {
return v, nil
}
return nil, nil
}
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) { func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := Config{ config := Config{
AccessKey: d.Get("access_key").(string), AccessKey: d.Get("access_key").(string),

View File

@ -1,8 +1,6 @@
package cloudflare package cloudflare
import ( import (
"os"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
@ -14,14 +12,14 @@ func Provider() terraform.ResourceProvider {
"email": &schema.Schema{ "email": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: envDefaultFunc("CLOUDFLARE_EMAIL"), DefaultFunc: schema.EnvDefaultFunc("CLOUDFLARE_EMAIL", nil),
Description: "A registered CloudFlare email address.", Description: "A registered CloudFlare email address.",
}, },
"token": &schema.Schema{ "token": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: envDefaultFunc("CLOUDFLARE_TOKEN"), DefaultFunc: schema.EnvDefaultFunc("CLOUDFLARE_TOKEN", nil),
Description: "The token key for API operations.", Description: "The token key for API operations.",
}, },
}, },
@ -34,16 +32,6 @@ func Provider() terraform.ResourceProvider {
} }
} }
func envDefaultFunc(k string) schema.SchemaDefaultFunc {
return func() (interface{}, error) {
if v := os.Getenv(k); v != "" {
return v, nil
}
return nil, nil
}
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) { func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := Config{ config := Config{
Email: d.Get("email").(string), Email: d.Get("email").(string),

View File

@ -1,8 +1,6 @@
package cloudstack package cloudstack
import ( import (
"os"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
@ -14,25 +12,25 @@ func Provider() terraform.ResourceProvider {
"api_url": &schema.Schema{ "api_url": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: envDefaultFunc("CLOUDSTACK_API_URL", nil), DefaultFunc: schema.EnvDefaultFunc("CLOUDSTACK_API_URL", nil),
}, },
"api_key": &schema.Schema{ "api_key": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: envDefaultFunc("CLOUDSTACK_API_KEY", nil), DefaultFunc: schema.EnvDefaultFunc("CLOUDSTACK_API_KEY", nil),
}, },
"secret_key": &schema.Schema{ "secret_key": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: envDefaultFunc("CLOUDSTACK_SECRET_KEY", nil), DefaultFunc: schema.EnvDefaultFunc("CLOUDSTACK_SECRET_KEY", nil),
}, },
"timeout": &schema.Schema{ "timeout": &schema.Schema{
Type: schema.TypeInt, Type: schema.TypeInt,
Required: true, Required: true,
DefaultFunc: envDefaultFunc("CLOUDSTACK_TIMEOUT", 180), DefaultFunc: schema.EnvDefaultFunc("CLOUDSTACK_TIMEOUT", 180),
}, },
}, },
@ -64,13 +62,3 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
return config.NewClient() return config.NewClient()
} }
func envDefaultFunc(k string, dv interface{}) schema.SchemaDefaultFunc {
return func() (interface{}, error) {
if v := os.Getenv(k); v != "" {
return v, nil
}
return dv, nil
}
}

View File

@ -1,8 +1,6 @@
package digitalocean package digitalocean
import ( import (
"os"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
@ -14,7 +12,7 @@ func Provider() terraform.ResourceProvider {
"token": &schema.Schema{ "token": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: envDefaultFunc("DIGITALOCEAN_TOKEN"), DefaultFunc: schema.EnvDefaultFunc("DIGITALOCEAN_TOKEN", nil),
Description: "The token key for API operations.", Description: "The token key for API operations.",
}, },
}, },
@ -29,16 +27,6 @@ func Provider() terraform.ResourceProvider {
} }
} }
func envDefaultFunc(k string) schema.SchemaDefaultFunc {
return func() (interface{}, error) {
if v := os.Getenv(k); v != "" {
return v, nil
}
return nil, nil
}
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) { func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := Config{ config := Config{
Token: d.Get("token").(string), Token: d.Get("token").(string),

View File

@ -1,8 +1,6 @@
package dnsimple package dnsimple
import ( import (
"os"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
@ -14,14 +12,14 @@ func Provider() terraform.ResourceProvider {
"email": &schema.Schema{ "email": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: envDefaultFunc("DNSIMPLE_EMAIL"), DefaultFunc: schema.EnvDefaultFunc("DNSIMPLE_EMAIL", nil),
Description: "A registered DNSimple email address.", Description: "A registered DNSimple email address.",
}, },
"token": &schema.Schema{ "token": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: envDefaultFunc("DNSIMPLE_TOKEN"), DefaultFunc: schema.EnvDefaultFunc("DNSIMPLE_TOKEN", nil),
Description: "The token key for API operations.", Description: "The token key for API operations.",
}, },
}, },
@ -34,16 +32,6 @@ func Provider() terraform.ResourceProvider {
} }
} }
func envDefaultFunc(k string) schema.SchemaDefaultFunc {
return func() (interface{}, error) {
if v := os.Getenv(k); v != "" {
return v, nil
}
return nil, nil
}
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) { func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := Config{ config := Config{
Email: d.Get("email").(string), Email: d.Get("email").(string),

View File

@ -1,8 +1,6 @@
package google package google
import ( import (
"os"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
@ -14,25 +12,25 @@ func Provider() terraform.ResourceProvider {
"account_file": &schema.Schema{ "account_file": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: envDefaultFunc("GOOGLE_ACCOUNT_FILE"), DefaultFunc: schema.EnvDefaultFunc("GOOGLE_ACCOUNT_FILE", nil),
}, },
"client_secrets_file": &schema.Schema{ "client_secrets_file": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: envDefaultFunc("GOOGLE_CLIENT_FILE"), DefaultFunc: schema.EnvDefaultFunc("GOOGLE_CLIENT_FILE", nil),
}, },
"project": &schema.Schema{ "project": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: envDefaultFunc("GOOGLE_PROJECT"), DefaultFunc: schema.EnvDefaultFunc("GOOGLE_PROJECT", nil),
}, },
"region": &schema.Schema{ "region": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: envDefaultFunc("GOOGLE_REGION"), DefaultFunc: schema.EnvDefaultFunc("GOOGLE_REGION", nil),
}, },
}, },
@ -49,16 +47,6 @@ func Provider() terraform.ResourceProvider {
} }
} }
func envDefaultFunc(k string) schema.SchemaDefaultFunc {
return func() (interface{}, error) {
if v := os.Getenv(k); v != "" {
return v, nil
}
return nil, nil
}
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) { func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := Config{ config := Config{
AccountFile: d.Get("account_file").(string), AccountFile: d.Get("account_file").(string),

View File

@ -2,7 +2,6 @@ package heroku
import ( import (
"log" "log"
"os"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
@ -15,13 +14,13 @@ func Provider() terraform.ResourceProvider {
"email": &schema.Schema{ "email": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
DefaultFunc: envDefaultFunc("HEROKU_EMAIL"), DefaultFunc: schema.EnvDefaultFunc("HEROKU_EMAIL", nil),
}, },
"api_key": &schema.Schema{ "api_key": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
DefaultFunc: envDefaultFunc("HEROKU_API_KEY"), DefaultFunc: schema.EnvDefaultFunc("HEROKU_API_KEY", nil),
}, },
}, },
@ -30,23 +29,13 @@ func Provider() terraform.ResourceProvider {
"heroku_addon": resourceHerokuAddon(), "heroku_addon": resourceHerokuAddon(),
"heroku_domain": resourceHerokuDomain(), "heroku_domain": resourceHerokuDomain(),
"heroku_drain": resourceHerokuDrain(), "heroku_drain": resourceHerokuDrain(),
"heroku_cert": resourceHerokuCert(), "heroku_cert": resourceHerokuCert(),
}, },
ConfigureFunc: providerConfigure, ConfigureFunc: providerConfigure,
} }
} }
func envDefaultFunc(k string) schema.SchemaDefaultFunc {
return func() (interface{}, error) {
if v := os.Getenv(k); v != "" {
return v, nil
}
return nil, nil
}
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) { func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := Config{ config := Config{
Email: d.Get("email").(string), Email: d.Get("email").(string),

View File

@ -2,7 +2,6 @@ package mailgun
import ( import (
"log" "log"
"os"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
@ -15,7 +14,7 @@ func Provider() terraform.ResourceProvider {
"api_key": &schema.Schema{ "api_key": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: envDefaultFunc("MAILGUN_API_KEY"), DefaultFunc: schema.EnvDefaultFunc("MAILGUN_API_KEY", nil),
}, },
}, },
@ -27,16 +26,6 @@ func Provider() terraform.ResourceProvider {
} }
} }
func envDefaultFunc(k string) schema.SchemaDefaultFunc {
return func() (interface{}, error) {
if v := os.Getenv(k); v != "" {
return v, nil
}
return nil, nil
}
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) { func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := Config{ config := Config{
APIKey: d.Get("api_key").(string), APIKey: d.Get("api_key").(string),

View File

@ -15,6 +15,7 @@ package schema
import ( import (
"fmt" "fmt"
"os"
"reflect" "reflect"
"sort" "sort"
"strconv" "strconv"
@ -160,6 +161,19 @@ type Schema struct {
// a field. // a field.
type SchemaDefaultFunc func() (interface{}, error) type SchemaDefaultFunc func() (interface{}, error)
// EnvDefaultFunc is a helper function that returns the value of the
// given environment variable, if one exists, or the default value
// otherwise.
func EnvDefaultFunc(k string, dv interface{}) SchemaDefaultFunc {
return func() (interface{}, error) {
if v := os.Getenv(k); v != "" {
return v, nil
}
return dv, nil
}
}
// SchemaSetFunc is a function that must return a unique ID for the given // SchemaSetFunc is a function that must return a unique ID for the given
// element. This unique ID is used to store the element in a hash. // element. This unique ID is used to store the element in a hash.
type SchemaSetFunc func(interface{}) int type SchemaSetFunc func(interface{}) int