From e5e44373517112641a3b2a613d7ebe19497f4388 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 17 Aug 2014 14:45:43 -0700 Subject: [PATCH] helper/schema: start Provider --- helper/schema/provider.go | 40 +++++++++++++++++++++++++++++ helper/schema/provider_test.go | 47 ++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 helper/schema/provider.go create mode 100644 helper/schema/provider_test.go diff --git a/helper/schema/provider.go b/helper/schema/provider.go new file mode 100644 index 000000000..d6f1c1a0b --- /dev/null +++ b/helper/schema/provider.go @@ -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) +} diff --git a/helper/schema/provider_test.go b/helper/schema/provider_test.go new file mode 100644 index 000000000..aebffa81e --- /dev/null +++ b/helper/schema/provider_test.go @@ -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) + } + } +}