diff --git a/builder/digitalocean/config.go b/builder/digitalocean/config.go index 6e58bc759..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" @@ -35,6 +36,7 @@ type Config struct { DropletName string `mapstructure:"droplet_name"` UserData string `mapstructure:"user_data"` UserDataFile string `mapstructure:"user_data_file"` + Tags []string `mapstructure:"tags"` ctx interpolate.Context } @@ -121,6 +123,17 @@ 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 } diff --git a/builder/digitalocean/step_create_droplet.go b/builder/digitalocean/step_create_droplet.go index d9c8976bf..9c6c5712a 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.Tags, }) if err != nil { err := fmt.Errorf("Error creating droplet: %s", err) 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