Skip to content

Commit

Permalink
builder: create tag if it does not exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsomething committed Nov 7, 2024
1 parent 622f2ea commit 9d72ac0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
8 changes: 5 additions & 3 deletions builder/digitalocean/builder_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/digitalocean/godo"
"github.com/digitalocean/packer-plugin-digitalocean/version"
"github.com/google/uuid"
"github.com/hashicorp/packer-plugin-sdk/acctest"
"github.com/hashicorp/packer-plugin-sdk/useragent"
"golang.org/x/oauth2"
Expand All @@ -22,7 +23,7 @@ func TestBuilderAcc_basic(t *testing.T) {
}
acctest.TestPlugin(t, &acctest.PluginTestCase{
Name: "test-digitalocean-builder-basic",
Template: fmt.Sprintf(testBuilderAccBasic, "ubuntu-20-04-x64"),
Template: fmt.Sprintf(testBuilderAccBasic, "ubuntu-20-04-x64", uuid.New().String()),
Check: func(buildCommand *exec.Cmd, logfile string) error {
if buildCommand.ProcessState != nil {
if buildCommand.ProcessState.ExitCode() != 0 {
Expand Down Expand Up @@ -152,7 +153,7 @@ func makeTemplateWithImageId(t *testing.T) string {
t.Fatalf("failed to retrieve image ID: %s", err)
}

return fmt.Sprintf(testBuilderAccBasic, image.ID)
return fmt.Sprintf(testBuilderAccBasic, image.ID, uuid.New().String())
}

return ""
Expand All @@ -167,7 +168,8 @@ const (
"size": "s-1vcpu-1gb",
"image": "%v",
"ssh_username": "root",
"tags": ["tag1","tag2"]
"tags": ["tag1","tag2"],
"snapshot_tags": ["%s"]
}]
}
`
Expand Down
35 changes: 30 additions & 5 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"
"net/http"
"strconv"
"time"

Expand Down Expand Up @@ -81,16 +82,40 @@ func (s *stepSnapshot) Run(ctx context.Context, state multistep.StateBag) multis
}

if len(c.SnapshotTags) > 0 {
var finalErr error
for _, tag := range c.SnapshotTags {
_, err = client.Tags.TagResources(context.TODO(), tag, &godo.TagResourcesRequest{Resources: []godo.Resource{{ID: strconv.Itoa(imageId), Type: "image"}}})
tagReq := &godo.TagResourcesRequest{
Resources: []godo.Resource{
{ID: strconv.Itoa(imageId), Type: "image"},
},
}

resp, err := client.Tags.TagResources(context.TODO(), tag, tagReq)
if err != nil {
err := fmt.Errorf("Error Tagging Image: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
// If the tag doesn't exist, create it and try again.
if resp != nil && resp.StatusCode == http.StatusNotFound {
log.Printf("Tag '%s' not found; creating it...", tag)
_, _, err = client.Tags.Create(context.TODO(), &godo.TagCreateRequest{Name: tag})
if err != nil {
finalErr = fmt.Errorf("Error creating tag: %s", err)
break
}
_, err = client.Tags.TagResources(context.TODO(), tag, tagReq)
if err != nil {
finalErr = fmt.Errorf("Error tagging image: %s", err)
break
}
}
}

ui.Say(fmt.Sprintf("Added snapshot tag: %s...", tag))
}
if finalErr != nil {
err := fmt.Errorf("Error tagging image: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}

if len(c.SnapshotRegions) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ toolchain go1.22.4
require (
github.com/aws/aws-sdk-go v1.44.114
github.com/digitalocean/godo v1.122.0
github.com/google/uuid v1.4.0
github.com/hashicorp/hcl/v2 v2.19.1
github.com/hashicorp/packer-plugin-sdk v0.5.4
github.com/mitchellh/mapstructure v1.5.0
Expand Down Expand Up @@ -39,7 +40,6 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/hashicorp/consul/api v1.25.1 // indirect
Expand Down

0 comments on commit 9d72ac0

Please sign in to comment.