builder/googlecompute: validate image_family

This commit is contained in:
Peter Schultz 2016-05-06 14:39:55 +02:00 committed by Chris Bednarski
parent 8546aafec5
commit a99a417db9
2 changed files with 19 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package googlecompute
import (
"errors"
"fmt"
"regexp"
"time"
"github.com/mitchellh/packer/common"
@ -94,6 +95,19 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
}
}
if len(c.ImageFamily) > 63 {
errs = packer.MultiErrorAppend(errs,
errors.New("Invalid image family: Must not be longer than 63 characters"))
}
if c.ImageFamily != "" {
if !regexp.MustCompile(`^[a-z]([-a-z0-9]{0,61}[a-z0-9])?$`).MatchString(c.ImageFamily) {
errs = packer.MultiErrorAppend(errs,
errors.New("Invalid image family: The first character must be a lowercase letter, and all following characters must be a dash, lowercase letter, or digit, except the last character, which cannot be a dash"))
}
}
if c.InstanceName == "" {
c.InstanceName = fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID())
}

View File

@ -123,6 +123,11 @@ func TestConfigPrepare(t *testing.T) {
"foo-bar",
false,
},
{
"image_family",
"foo bar",
true,
},
}
for _, tc := range cases {