From 7081fe990b8b0ae9bc49b557a4769f97846ec9fb Mon Sep 17 00:00:00 2001 From: Mike Zupan Date: Mon, 30 Jul 2018 07:52:40 -0600 Subject: [PATCH 1/4] Adding in droplet tags on creation --- builder/digitalocean/config.go | 5 +++++ builder/digitalocean/step_create_droplet.go | 1 + 2 files changed, 6 insertions(+) diff --git a/builder/digitalocean/config.go b/builder/digitalocean/config.go index 6e58bc759..f2f389b7a 100644 --- a/builder/digitalocean/config.go +++ b/builder/digitalocean/config.go @@ -35,6 +35,7 @@ type Config struct { DropletName string `mapstructure:"droplet_name"` UserData string `mapstructure:"user_data"` UserDataFile string `mapstructure:"user_data_file"` + RunTags []string `mapstructure:"run_tags"` ctx interpolate.Context } @@ -121,6 +122,10 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { } } + if c.RunTags == nil { + c.RunTags = make([]string, 0) + } + if errs != nil && len(errs.Errors) > 0 { return nil, nil, errs } diff --git a/builder/digitalocean/step_create_droplet.go b/builder/digitalocean/step_create_droplet.go index d9c8976bf..a67e28e79 100644 --- a/builder/digitalocean/step_create_droplet.go +++ b/builder/digitalocean/step_create_droplet.go @@ -49,6 +49,7 @@ func (s *stepCreateDroplet) Run(_ context.Context, state multistep.StateBag) mul Monitoring: c.Monitoring, IPv6: c.IPv6, UserData: userData, + Tags: c.RunTags, }) if err != nil { err := fmt.Errorf("Error creating droplet: %s", err) From 11271ead5925fea25e829d47298946b3aa0c7a42 Mon Sep 17 00:00:00 2001 From: Mike Zupan Date: Mon, 30 Jul 2018 07:55:06 -0600 Subject: [PATCH 2/4] Change name to tags --- builder/digitalocean/config.go | 6 +++--- builder/digitalocean/step_create_droplet.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/builder/digitalocean/config.go b/builder/digitalocean/config.go index f2f389b7a..bb4175972 100644 --- a/builder/digitalocean/config.go +++ b/builder/digitalocean/config.go @@ -35,7 +35,7 @@ type Config struct { DropletName string `mapstructure:"droplet_name"` UserData string `mapstructure:"user_data"` UserDataFile string `mapstructure:"user_data_file"` - RunTags []string `mapstructure:"run_tags"` + Tags []string `mapstructure:"tags"` ctx interpolate.Context } @@ -122,8 +122,8 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { } } - if c.RunTags == nil { - c.RunTags = make([]string, 0) + if c.Tags == nil { + c.Tags = make([]string, 0) } if errs != nil && len(errs.Errors) > 0 { diff --git a/builder/digitalocean/step_create_droplet.go b/builder/digitalocean/step_create_droplet.go index a67e28e79..9c6c5712a 100644 --- a/builder/digitalocean/step_create_droplet.go +++ b/builder/digitalocean/step_create_droplet.go @@ -49,7 +49,7 @@ func (s *stepCreateDroplet) Run(_ context.Context, state multistep.StateBag) mul Monitoring: c.Monitoring, IPv6: c.IPv6, UserData: userData, - Tags: c.RunTags, + Tags: c.Tags, }) if err != nil { err := fmt.Errorf("Error creating droplet: %s", err) From 9247a3c56479ee9f871416e1e80db39a2ac2ade5 Mon Sep 17 00:00:00 2001 From: Mike Zupan Date: Mon, 30 Jul 2018 11:27:57 -0600 Subject: [PATCH 3/4] adding in th docs --- website/source/docs/builders/digitalocean.html.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/website/source/docs/builders/digitalocean.html.md b/website/source/docs/builders/digitalocean.html.md index 5683491df..6d5be6fb7 100644 --- a/website/source/docs/builders/digitalocean.html.md +++ b/website/source/docs/builders/digitalocean.html.md @@ -88,6 +88,8 @@ builder. - `user_data_file` (string) - Path to a file that will be used for the user data when launching the Droplet. +- `tags` (list) - Tags to apply to the droplet when it is created + ## Basic Example Here is a basic example. It is completely valid as soon as you enter your own From 889c89ec79fc4ee1eaff0aba597cff40119e96b9 Mon Sep 17 00:00:00 2001 From: Rickard von Essen Date: Wed, 15 Aug 2018 15:27:00 +0200 Subject: [PATCH 4/4] Validate tags --- builder/digitalocean/config.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/builder/digitalocean/config.go b/builder/digitalocean/config.go index bb4175972..19c802c4b 100644 --- a/builder/digitalocean/config.go +++ b/builder/digitalocean/config.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "os" + "regexp" "time" "github.com/hashicorp/packer/common" @@ -125,6 +126,13 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { if c.Tags == nil { c.Tags = make([]string, 0) } + tagRe := regexp.MustCompile("^[[:alnum:]:_-]{1,255}$") + + for _, t := range c.Tags { + if !tagRe.MatchString(t) { + errs = packer.MultiErrorAppend(errs, errors.New(fmt.Sprintf("invalid tag: %s", t))) + } + } if errs != nil && len(errs.Errors) > 0 { return nil, nil, errs