Skip to content

Commit

Permalink
Merge pull request #151 from Bryan-Lester-Dolby/lester/tag-snapshots
Browse files Browse the repository at this point in the history
Issue #33 Add snapshot_tags
  • Loading branch information
andrewsomething authored Jul 12, 2024
2 parents 1a7fa3f + 69435ff commit a4b42c4
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .web-docs/components/builder/digitalocean/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
7 changes: 5 additions & 2 deletions builder/digitalocean/builder_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ const (
"region": "nyc2",
"size": "s-1vcpu-1gb",
"image": "%v",
"ssh_username": "root"
"ssh_username": "root",
"tags": ["tag1","tag2"]
}]
}
`
Expand All @@ -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"]
}]
}
`
Expand All @@ -193,6 +195,7 @@ const (
"image": "%v",
"ssh_username": "root",
"snapshot_regions": ["nyc2", "nyc3"],
"tags": ["tag1","tag2"],
"wait_snapshot_transfer": false
}]
}
Expand Down
2 changes: 2 additions & 0 deletions builder/digitalocean/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
2 changes: 2 additions & 0 deletions builder/digitalocean/config.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions builder/digitalocean/step_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"log"
"strconv"
"time"

"github.com/digitalocean/godo"
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions docs-partials/builder/digitalocean/Config-not-required.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit a4b42c4

Please sign in to comment.