From 0c812ba9e8605da270fae6495ef2778c069156b9 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 8 Jul 2014 10:17:36 -0700 Subject: [PATCH] helper/resource: automatically validate resources /cc @pearkes - So, just set a ConfigValidator struct up on your resources and it'll now automatically validate. --- helper/resource/map.go | 15 ++++++++++++ helper/resource/map_test.go | 49 +++++++++++++++++++++++++++++++++++++ helper/resource/resource.go | 12 +++++---- 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/helper/resource/map.go b/helper/resource/map.go index 0daa3483e..9ed671236 100644 --- a/helper/resource/map.go +++ b/helper/resource/map.go @@ -13,6 +13,21 @@ type Map struct { Mapping map[string]Resource } +func (m *Map) Validate( + t string, c *terraform.ResourceConfig) ([]string, []error) { + r, ok := m.Mapping[t] + if !ok { + return nil, []error{fmt.Errorf("Unknown resource type: %s", t)} + } + + // If there is no validator set, then it is valid + if r.ConfigValidator == nil { + return nil, nil + } + + return r.ConfigValidator.Validate(c) +} + // Apply performs a create or update depending on the diff, and calls // the proper function on the matching Resource. func (m *Map) Apply( diff --git a/helper/resource/map_test.go b/helper/resource/map_test.go index f188f54e4..6809687fa 100644 --- a/helper/resource/map_test.go +++ b/helper/resource/map_test.go @@ -4,6 +4,8 @@ import ( "reflect" "testing" + tfconfig "github.com/hashicorp/terraform/config" + "github.com/hashicorp/terraform/helper/config" "github.com/hashicorp/terraform/terraform" ) @@ -30,3 +32,50 @@ func TestMapResources(t *testing.T) { t.Fatalf("bad: %#v", rts) } } + +func TestMapValidate(t *testing.T) { + m := &Map{ + Mapping: map[string]Resource{ + "aws_elb": Resource{ + ConfigValidator: &config.Validator{ + Required: []string{"foo"}, + }, + }, + }, + } + + var c *terraform.ResourceConfig + var ws []string + var es []error + + // Valid + c = testConfig(t, map[string]interface{}{"foo": "bar"}) + ws, es = m.Validate("aws_elb", c) + if len(ws) > 0 { + t.Fatalf("bad: %#v", ws) + } + if len(es) > 0 { + t.Fatalf("bad: %#v", es) + } + + // Invalid + c = testConfig(t, map[string]interface{}{}) + ws, es = m.Validate("aws_elb", c) + if len(ws) > 0 { + t.Fatalf("bad: %#v", ws) + } + if len(es) == 0 { + t.Fatalf("bad: %#v", es) + } +} + +func testConfig( + t *testing.T, + c map[string]interface{}) *terraform.ResourceConfig { + r, err := tfconfig.NewRawConfig(c) + if err != nil { + t.Fatalf("bad: %s", err) + } + + return terraform.NewResourceConfig(r) +} diff --git a/helper/resource/resource.go b/helper/resource/resource.go index 0437b34e1..819ad1769 100644 --- a/helper/resource/resource.go +++ b/helper/resource/resource.go @@ -1,15 +1,17 @@ package resource import ( + "github.com/hashicorp/terraform/helper/config" "github.com/hashicorp/terraform/terraform" ) type Resource struct { - Create CreateFunc - Destroy DestroyFunc - Diff DiffFunc - Refresh RefreshFunc - Update UpdateFunc + ConfigValidator *config.Validator + Create CreateFunc + Destroy DestroyFunc + Diff DiffFunc + Refresh RefreshFunc + Update UpdateFunc } // CreateFunc is a function that creates a resource that didn't previously