helper/schema: start Provider

This commit is contained in:
Mitchell Hashimoto 2014-08-17 14:45:43 -07:00
parent a33e4bcdf0
commit e5e4437351
2 changed files with 87 additions and 0 deletions

40
helper/schema/provider.go Normal file
View File

@ -0,0 +1,40 @@
package schema
import (
"fmt"
"github.com/hashicorp/terraform/terraform"
)
// Provider represents a Resource provider in Terraform, and properly
// implements all of the ResourceProvider API.
//
// This is a friendlier API than the core Terraform ResourceProvider API,
// and is recommended to be used over that.
type Provider struct {
Schema map[string]*Schema
Resources map[string]*Resource
Configure ConfigureFunc
}
// ConfigureFunc is the function used to configure a Provider.
type ConfigureFunc func(*ResourceData) error
// Validate validates the provider configuration against the schema.
func (p *Provider) Validate(c *terraform.ResourceConfig) ([]string, []error) {
return schemaMap(p.Schema).Validate(c)
}
// ValidateResource validates the resource configuration against the
// proper schema.
func (p *Provider) ValidateResource(
t string, c *terraform.ResourceConfig) ([]string, []error) {
r, ok := p.Resources[t]
if !ok {
return nil, []error{fmt.Errorf(
"Provider doesn't support resource: %s", t)}
}
return r.Validate(c)
}

View File

@ -0,0 +1,47 @@
package schema
import (
"testing"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/terraform"
)
func TestProviderValidateResource(t *testing.T) {
cases := []struct {
P *Provider
Type string
Config map[string]interface{}
Err bool
}{
{
P: &Provider{},
Type: "foo",
Config: nil,
Err: true,
},
{
P: &Provider{
Resources: map[string]*Resource{
"foo": &Resource{},
},
},
Type: "foo",
Config: nil,
Err: false,
},
}
for i, tc := range cases {
c, err := config.NewRawConfig(tc.Config)
if err != nil {
t.Fatalf("err: %s", err)
}
_, es := tc.P.ValidateResource(tc.Type, terraform.NewResourceConfig(c))
if (len(es) > 0) != tc.Err {
t.Fatalf("%d: %#v", i, es)
}
}
}