terraform: validate resource names

This commit is contained in:
Mitchell Hashimoto 2015-02-09 09:50:20 -08:00
parent 8552bb18cf
commit 9cd877a59c
3 changed files with 33 additions and 21 deletions

View File

@ -302,6 +302,25 @@ func TestContext2Validate_resourceConfig_good(t *testing.T) {
}
}
func TestContext2Validate_resourceNameSymbol(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-resource-name-symbol")
c := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
w, e := c.Validate()
if len(w) == 0 {
t.Fatalf("bad: %#v", w)
}
if len(e) > 0 {
t.Fatalf("bad: %s", e)
}
}
func TestContext2Validate_selfRef(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-self-ref")
@ -446,25 +465,6 @@ func TestContextValidate_tainted(t *testing.T) {
}
}
func TestContextValidate_resourceNameSymbol(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-resource-name-symbol")
c := testContext(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
w, e := c.Validate()
if len(w) == 0 {
t.Fatalf("bad: %#v", w)
}
if len(e) > 0 {
t.Fatalf("bad: %#v", e)
}
}
func TestContextValidate_provisionerConfig_bad(t *testing.T) {
m := testModule(t, "validate-bad-prov-conf")
p := testProvider("aws")

View File

@ -96,6 +96,7 @@ func (n *EvalValidateProvider) Type() EvalType {
type EvalValidateResource struct {
Provider EvalNode
Config EvalNode
ResourceName string
ResourceType string
}
@ -109,9 +110,19 @@ func (n *EvalValidateResource) Eval(
// TODO: test
provider := args[0].(ResourceProvider)
config := args[1].(*ResourceConfig)
cfg := args[1].(*ResourceConfig)
warns, errs := provider.ValidateResource(n.ResourceType, cfg)
// If the resouce name doesn't match the name regular
// expression, show a warning.
if !config.NameRegexp.Match([]byte(n.ResourceName)) {
warns = append(warns, fmt.Sprintf(
"%s: resource name can only contain letters, numbers, "+
"dashes, and underscores.\n"+
"This will be an error in Terraform 0.4",
n.ResourceName))
}
warns, errs := provider.ValidateResource(n.ResourceType, config)
if len(warns) == 0 && len(errs) == 0 {
return nil, nil
}

View File

@ -83,6 +83,7 @@ func (n *graphNodeExpandedResource) EvalTree() EvalNode {
&EvalValidateResource{
Provider: &EvalGetProvider{Name: n.ProvidedBy()},
Config: &EvalInterpolate{Config: n.Resource.RawConfig},
ResourceName: n.Resource.Name,
ResourceType: n.Resource.Type,
},
},