providers/aws: convert validation to helper/schema

This commit is contained in:
Mitchell Hashimoto 2014-09-09 21:44:31 -07:00
parent 0250c17d6e
commit 6a275d7bd0
2 changed files with 29 additions and 26 deletions

View File

@ -1,6 +1,8 @@
package aws package aws
import ( import (
"os"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
) )
@ -15,6 +17,32 @@ func Provider() *schema.Provider {
// TODO: Move the configuration to this, requires validation // TODO: Move the configuration to this, requires validation
return &schema.Provider{ 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{ ResourcesMap: map[string]*schema.Resource{
"aws_eip": resourceAwsEip(), "aws_eip": resourceAwsEip(),
"aws_instance": resourceAwsInstance(), "aws_instance": resourceAwsInstance(),

View File

@ -2,7 +2,6 @@ package aws
import ( import (
"log" "log"
"os"
"github.com/hashicorp/terraform/helper/config" "github.com/hashicorp/terraform/helper/config"
"github.com/hashicorp/terraform/helper/multierror" "github.com/hashicorp/terraform/helper/multierror"
@ -32,31 +31,7 @@ type ResourceProvider struct {
} }
func (p *ResourceProvider) Validate(c *terraform.ResourceConfig) ([]string, []error) { func (p *ResourceProvider) Validate(c *terraform.ResourceConfig) ([]string, []error) {
type param struct { return Provider().Validate(c)
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)
} }
func (p *ResourceProvider) ValidateResource( func (p *ResourceProvider) ValidateResource(