helper/schema: start Provider
This commit is contained in:
parent
a33e4bcdf0
commit
e5e4437351
|
@ -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)
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue