config: validate that count is an int

This commit is contained in:
Mitchell Hashimoto 2014-10-02 16:51:20 -07:00
parent 581d1dee8c
commit dd14303022
5 changed files with 55 additions and 17 deletions

View File

@ -275,6 +275,18 @@ func (c *Config) Validate() error {
}
}
// Interpolate with a fixed number to verify that its a number
r.RawCount.interpolate(func(Interpolation) (string, error) {
return "5", nil
})
_, err := strconv.ParseInt(r.RawCount.Value().(string), 0, 0)
if err != nil {
errs = append(errs, fmt.Errorf(
"%s: resource count must be an integer",
n))
}
r.RawCount.init()
for _, d := range r.DependsOn {
if _, ok := resources[d]; !ok {
errs = append(errs, fmt.Errorf(

View File

@ -53,6 +53,13 @@ func TestConfigValidate_badDependsOn(t *testing.T) {
}
}
func TestConfigValidate_countInt(t *testing.T) {
c := testConfig(t, "validate-count-int")
if err := c.Validate(); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestConfigValidate_countModuleVar(t *testing.T) {
c := testConfig(t, "validate-count-module-var")
if err := c.Validate(); err == nil {
@ -60,6 +67,13 @@ func TestConfigValidate_countModuleVar(t *testing.T) {
}
}
func TestConfigValidate_countNotInt(t *testing.T) {
c := testConfig(t, "validate-count-not-int")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
func TestConfigValidate_countResourceVar(t *testing.T) {
c := testConfig(t, "validate-count-resource-var")
if err := c.Validate(); err == nil {

View File

@ -79,24 +79,9 @@ func (r *RawConfig) Config() map[string]interface{} {
//
// If a variable key is missing, this will panic.
func (r *RawConfig) Interpolate(vs map[string]string) error {
config, err := copystructure.Copy(r.Raw)
if err != nil {
return err
}
r.config = config.(map[string]interface{})
fn := func(i Interpolation) (string, error) {
return r.interpolate(func(i Interpolation) (string, error) {
return i.Interpolate(vs)
}
w := &interpolationWalker{F: fn, Replace: true}
err = reflectwalk.Walk(r.config, w)
if err != nil {
return err
}
r.unknownKeys = w.unknownKeys
return nil
})
}
func (r *RawConfig) init() error {
@ -126,6 +111,23 @@ func (r *RawConfig) init() error {
return nil
}
func (r *RawConfig) interpolate(fn interpolationWalkerFunc) error {
config, err := copystructure.Copy(r.Raw)
if err != nil {
return err
}
r.config = config.(map[string]interface{})
w := &interpolationWalker{F: fn, Replace: true}
err = reflectwalk.Walk(r.config, w)
if err != nil {
return err
}
r.unknownKeys = w.unknownKeys
return nil
}
func (r *RawConfig) merge(r2 *RawConfig) *RawConfig {
rawRaw, err := copystructure.Copy(r.Raw)
if err != nil {

View File

@ -0,0 +1,5 @@
variable "foo" {}
resource "aws_instance" "web" {
count = "${var.foo}"
}

View File

@ -0,0 +1,5 @@
variable "foo" {}
resource "aws_instance" "web" {
count = "nope${var.foo}"
}