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
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(),

View File

@ -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(