From 6a275d7bd0442242c6a36a49637623e9846a811b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 9 Sep 2014 21:44:31 -0700 Subject: [PATCH] providers/aws: convert validation to helper/schema --- builtin/providers/aws/provider.go | 28 ++++++++++++++++++++++ builtin/providers/aws/resource_provider.go | 27 +-------------------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/builtin/providers/aws/provider.go b/builtin/providers/aws/provider.go index 0a231fdc9..7d236fd70 100644 --- a/builtin/providers/aws/provider.go +++ b/builtin/providers/aws/provider.go @@ -1,6 +1,8 @@ package aws import ( + "os" + "github.com/hashicorp/terraform/helper/schema" ) @@ -15,6 +17,32 @@ func Provider() *schema.Provider { // TODO: Move the configuration to this, requires validation return &schema.Provider{ + Schema: map[string]*schema.Schema{ + "region": &schema.Schema{ + Type: schema.TypeString, + Required: true, + DefaultFunc: func() (interface{}, error) { + return os.Getenv("AWS_REGION"), nil + }, + }, + + "access_key": &schema.Schema{ + Type: schema.TypeString, + Required: true, + DefaultFunc: func() (interface{}, error) { + return os.Getenv("AWS_ACCESS_KEY"), nil + }, + }, + + "secret_key": &schema.Schema{ + Type: schema.TypeString, + Required: true, + DefaultFunc: func() (interface{}, error) { + return os.Getenv("AWS_SECRET_KEY"), nil + }, + }, + }, + ResourcesMap: map[string]*schema.Resource{ "aws_eip": resourceAwsEip(), "aws_instance": resourceAwsInstance(), diff --git a/builtin/providers/aws/resource_provider.go b/builtin/providers/aws/resource_provider.go index edbe58dc3..5a71b4acc 100644 --- a/builtin/providers/aws/resource_provider.go +++ b/builtin/providers/aws/resource_provider.go @@ -2,7 +2,6 @@ package aws import ( "log" - "os" "github.com/hashicorp/terraform/helper/config" "github.com/hashicorp/terraform/helper/multierror" @@ -32,31 +31,7 @@ type ResourceProvider struct { } func (p *ResourceProvider) Validate(c *terraform.ResourceConfig) ([]string, []error) { - type param struct { - env string - key string - } - params := []param{ - {"AWS_REGION", "region"}, - {"AWS_ACCESS_KEY", "access_key"}, - {"AWS_SECRET_KEY", "secret_key"}, - } - - var optional []string - var required []string - for _, p := range params { - if v := os.Getenv(p.env); v != "" { - optional = append(optional, p.key) - } else { - required = append(required, p.key) - } - } - - v := &config.Validator{ - Required: required, - Optional: optional, - } - return v.Validate(c) + return Provider().Validate(c) } func (p *ResourceProvider) ValidateResource(