diff --git a/.web-docs/components/builder/digitalocean/README.md b/.web-docs/components/builder/digitalocean/README.md index cfc0293..0be8ccb 100644 --- a/.web-docs/components/builder/digitalocean/README.md +++ b/.web-docs/components/builder/digitalocean/README.md @@ -106,6 +106,8 @@ each category, the available configuration keys are alphabetized. - `tags` ([]string) - Tags to apply to the droplet when it is created +- `snapshot_tags` ([]string) - Tags to apply to the snapshot after it is created + - `vpc_uuid` (string) - UUID of the VPC which the droplet will be created in. Before using this, private_networking should be enabled. diff --git a/builder/digitalocean/builder_acc_test.go b/builder/digitalocean/builder_acc_test.go index b779932..c285438 100644 --- a/builder/digitalocean/builder_acc_test.go +++ b/builder/digitalocean/builder_acc_test.go @@ -166,7 +166,8 @@ const ( "region": "nyc2", "size": "s-1vcpu-1gb", "image": "%v", - "ssh_username": "root" + "ssh_username": "root", + "tags": ["tag1","tag2"] }] } ` @@ -179,7 +180,8 @@ const ( "size": "s-1vcpu-1gb", "image": "%v", "ssh_username": "root", - "snapshot_regions": ["nyc1", "nyc2", "nyc3"] + "snapshot_regions": ["nyc1", "nyc2", "nyc3"], + "tags": ["tag1","tag2"] }] } ` @@ -193,6 +195,7 @@ const ( "image": "%v", "ssh_username": "root", "snapshot_regions": ["nyc2", "nyc3"], + "tags": ["tag1","tag2"], "wait_snapshot_transfer": false }] } diff --git a/builder/digitalocean/config.go b/builder/digitalocean/config.go index 06813a1..7dec065 100644 --- a/builder/digitalocean/config.go +++ b/builder/digitalocean/config.go @@ -103,6 +103,8 @@ type Config struct { UserDataFile string `mapstructure:"user_data_file" required:"false"` // Tags to apply to the droplet when it is created Tags []string `mapstructure:"tags" required:"false"` + // Tags to apply to the snapshot after it is created + SnapshotTags []string `mapstructure:"snapshot_tags" required:"false"` // UUID of the VPC which the droplet will be created in. Before using this, // private_networking should be enabled. VPCUUID string `mapstructure:"vpc_uuid" required:"false"` diff --git a/builder/digitalocean/config.hcl2spec.go b/builder/digitalocean/config.hcl2spec.go index abb1871..2338de7 100644 --- a/builder/digitalocean/config.hcl2spec.go +++ b/builder/digitalocean/config.hcl2spec.go @@ -89,6 +89,7 @@ type FlatConfig struct { UserData *string `mapstructure:"user_data" required:"false" cty:"user_data" hcl:"user_data"` UserDataFile *string `mapstructure:"user_data_file" required:"false" cty:"user_data_file" hcl:"user_data_file"` Tags []string `mapstructure:"tags" required:"false" cty:"tags" hcl:"tags"` + SnapshotTags []string `mapstructure:"snapshot_tags" required:"false" cty:"snapshot_tags" hcl:"snapshot_tags"` VPCUUID *string `mapstructure:"vpc_uuid" required:"false" cty:"vpc_uuid" hcl:"vpc_uuid"` ConnectWithPrivateIP *bool `mapstructure:"connect_with_private_ip" required:"false" cty:"connect_with_private_ip" hcl:"connect_with_private_ip"` SSHKeyID *int `mapstructure:"ssh_key_id" required:"false" cty:"ssh_key_id" hcl:"ssh_key_id"` @@ -185,6 +186,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "user_data": &hcldec.AttrSpec{Name: "user_data", Type: cty.String, Required: false}, "user_data_file": &hcldec.AttrSpec{Name: "user_data_file", Type: cty.String, Required: false}, "tags": &hcldec.AttrSpec{Name: "tags", Type: cty.List(cty.String), Required: false}, + "snapshot_tags": &hcldec.AttrSpec{Name: "snapshot_tags", Type: cty.List(cty.String), Required: false}, "vpc_uuid": &hcldec.AttrSpec{Name: "vpc_uuid", Type: cty.String, Required: false}, "connect_with_private_ip": &hcldec.AttrSpec{Name: "connect_with_private_ip", Type: cty.Bool, Required: false}, "ssh_key_id": &hcldec.AttrSpec{Name: "ssh_key_id", Type: cty.Number, Required: false}, diff --git a/builder/digitalocean/step_snapshot.go b/builder/digitalocean/step_snapshot.go index ee6259e..1eca4fa 100644 --- a/builder/digitalocean/step_snapshot.go +++ b/builder/digitalocean/step_snapshot.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "log" + "strconv" "time" "github.com/digitalocean/godo" @@ -79,6 +80,19 @@ func (s *stepSnapshot) Run(ctx context.Context, state multistep.StateBag) multis return multistep.ActionHalt } + if len(c.SnapshotTags) > 0 { + for _, tag := range c.SnapshotTags { + _, err = client.Tags.TagResources(context.TODO(), tag, &godo.TagResourcesRequest{Resources: []godo.Resource{{ID: strconv.Itoa(imageId), Type: "image"}}}) + if err != nil { + err := fmt.Errorf("Error Tagging Image: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + ui.Say(fmt.Sprintf("Added snapshot tag: %s...", tag)) + } + } + if len(c.SnapshotRegions) > 0 { regionSet := make(map[string]bool) regions := make([]string, 0, len(c.SnapshotRegions)) diff --git a/docs-partials/builder/digitalocean/Config-not-required.mdx b/docs-partials/builder/digitalocean/Config-not-required.mdx index db1195b..4a461f1 100644 --- a/docs-partials/builder/digitalocean/Config-not-required.mdx +++ b/docs-partials/builder/digitalocean/Config-not-required.mdx @@ -60,6 +60,8 @@ - `tags` ([]string) - Tags to apply to the droplet when it is created +- `snapshot_tags` ([]string) - Tags to apply to the snapshot after it is created + - `vpc_uuid` (string) - UUID of the VPC which the droplet will be created in. Before using this, private_networking should be enabled.