From a99a417db92a1253ddc23cb784c5c2310c8da7fc Mon Sep 17 00:00:00 2001 From: Peter Schultz Date: Fri, 6 May 2016 14:39:55 +0200 Subject: [PATCH] builder/googlecompute: validate image_family --- builder/googlecompute/config.go | 14 ++++++++++++++ builder/googlecompute/config_test.go | 5 +++++ 2 files changed, 19 insertions(+) diff --git a/builder/googlecompute/config.go b/builder/googlecompute/config.go index 871176396..981a60a98 100644 --- a/builder/googlecompute/config.go +++ b/builder/googlecompute/config.go @@ -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()) } diff --git a/builder/googlecompute/config_test.go b/builder/googlecompute/config_test.go index 5124eadc9..f54ba92e8 100644 --- a/builder/googlecompute/config_test.go +++ b/builder/googlecompute/config_test.go @@ -123,6 +123,11 @@ func TestConfigPrepare(t *testing.T) { "foo-bar", false, }, + { + "image_family", + "foo bar", + true, + }, } for _, tc := range cases {