Move duplicated envDefaultFunc out of each provider and into Schema.

This commit is contained in:
Jeff Mitchell 2015-01-16 17:22:09 +00:00
parent 8cba4a40f5
commit f2bd1f45ab
10 changed files with 36 additions and 128 deletions

View File

@ -1,8 +1,6 @@
package atlas
import (
"os"
"github.com/hashicorp/atlas-go/v1"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
@ -21,14 +19,14 @@ func Provider() terraform.ResourceProvider {
"token": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: envDefaultFunc("ATLAS_TOKEN", nil),
DefaultFunc: schema.EnvDefaultFunc("ATLAS_TOKEN", nil),
Description: descriptions["token"],
},
"address": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: envDefaultFunc("ATLAS_ADDRESS", defaultAtlasServer),
DefaultFunc: schema.EnvDefaultFunc("ATLAS_ADDRESS", defaultAtlasServer),
Description: descriptions["address"],
},
},
@ -55,16 +53,6 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
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
func init() {

View File

@ -1,8 +1,6 @@
package aws
import (
"os"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
@ -17,21 +15,21 @@ func Provider() terraform.ResourceProvider {
"access_key": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: envDefaultFunc("AWS_ACCESS_KEY"),
DefaultFunc: schema.EnvDefaultFunc("AWS_ACCESS_KEY", nil),
Description: descriptions["access_key"],
},
"secret_key": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: envDefaultFunc("AWS_SECRET_KEY"),
DefaultFunc: schema.EnvDefaultFunc("AWS_SECRET_KEY", nil),
Description: descriptions["secret_key"],
},
"region": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: envDefaultFunc("AWS_REGION"),
DefaultFunc: schema.EnvDefaultFunc("AWS_REGION", nil),
Description: descriptions["region"],
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) {
config := Config{
AccessKey: d.Get("access_key").(string),

View File

@ -1,8 +1,6 @@
package cloudflare
import (
"os"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
@ -14,14 +12,14 @@ func Provider() terraform.ResourceProvider {
"email": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: envDefaultFunc("CLOUDFLARE_EMAIL"),
DefaultFunc: schema.EnvDefaultFunc("CLOUDFLARE_EMAIL", nil),
Description: "A registered CloudFlare email address.",
},
"token": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: envDefaultFunc("CLOUDFLARE_TOKEN"),
DefaultFunc: schema.EnvDefaultFunc("CLOUDFLARE_TOKEN", nil),
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) {
config := Config{
Email: d.Get("email").(string),

View File

@ -1,8 +1,6 @@
package cloudstack
import (
"os"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
@ -14,25 +12,25 @@ func Provider() terraform.ResourceProvider {
"api_url": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: envDefaultFunc("CLOUDSTACK_API_URL", nil),
DefaultFunc: schema.EnvDefaultFunc("CLOUDSTACK_API_URL", nil),
},
"api_key": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: envDefaultFunc("CLOUDSTACK_API_KEY", nil),
DefaultFunc: schema.EnvDefaultFunc("CLOUDSTACK_API_KEY", nil),
},
"secret_key": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: envDefaultFunc("CLOUDSTACK_SECRET_KEY", nil),
DefaultFunc: schema.EnvDefaultFunc("CLOUDSTACK_SECRET_KEY", nil),
},
"timeout": &schema.Schema{
Type: schema.TypeInt,
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()
}
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
import (
"os"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
@ -14,7 +12,7 @@ func Provider() terraform.ResourceProvider {
"token": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: envDefaultFunc("DIGITALOCEAN_TOKEN"),
DefaultFunc: schema.EnvDefaultFunc("DIGITALOCEAN_TOKEN", nil),
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) {
config := Config{
Token: d.Get("token").(string),

View File

@ -1,8 +1,6 @@
package dnsimple
import (
"os"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
@ -14,14 +12,14 @@ func Provider() terraform.ResourceProvider {
"email": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: envDefaultFunc("DNSIMPLE_EMAIL"),
DefaultFunc: schema.EnvDefaultFunc("DNSIMPLE_EMAIL", nil),
Description: "A registered DNSimple email address.",
},
"token": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: envDefaultFunc("DNSIMPLE_TOKEN"),
DefaultFunc: schema.EnvDefaultFunc("DNSIMPLE_TOKEN", nil),
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) {
config := Config{
Email: d.Get("email").(string),

View File

@ -1,8 +1,6 @@
package google
import (
"os"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
@ -14,25 +12,25 @@ func Provider() terraform.ResourceProvider {
"account_file": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: envDefaultFunc("GOOGLE_ACCOUNT_FILE"),
DefaultFunc: schema.EnvDefaultFunc("GOOGLE_ACCOUNT_FILE", nil),
},
"client_secrets_file": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: envDefaultFunc("GOOGLE_CLIENT_FILE"),
DefaultFunc: schema.EnvDefaultFunc("GOOGLE_CLIENT_FILE", nil),
},
"project": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: envDefaultFunc("GOOGLE_PROJECT"),
DefaultFunc: schema.EnvDefaultFunc("GOOGLE_PROJECT", nil),
},
"region": &schema.Schema{
Type: schema.TypeString,
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) {
config := Config{
AccountFile: d.Get("account_file").(string),

View File

@ -2,7 +2,6 @@ package heroku
import (
"log"
"os"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
@ -15,13 +14,13 @@ func Provider() terraform.ResourceProvider {
"email": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: envDefaultFunc("HEROKU_EMAIL"),
DefaultFunc: schema.EnvDefaultFunc("HEROKU_EMAIL", nil),
},
"api_key": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: envDefaultFunc("HEROKU_API_KEY"),
DefaultFunc: schema.EnvDefaultFunc("HEROKU_API_KEY", nil),
},
},
@ -37,16 +36,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) {
config := Config{
Email: d.Get("email").(string),

View File

@ -2,7 +2,6 @@ package mailgun
import (
"log"
"os"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
@ -15,7 +14,7 @@ func Provider() terraform.ResourceProvider {
"api_key": &schema.Schema{
Type: schema.TypeString,
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) {
config := Config{
APIKey: d.Get("api_key").(string),

View File

@ -15,6 +15,7 @@ package schema
import (
"fmt"
"os"
"reflect"
"sort"
"strconv"
@ -160,6 +161,19 @@ type Schema struct {
// a field.
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
// element. This unique ID is used to store the element in a hash.
type SchemaSetFunc func(interface{}) int