Skip to content

Commit

Permalink
Add source image ID and region attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ajax-ryzhyi-r committed Nov 12, 2023
1 parent a890f1a commit 9dd1cbe
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
38 changes: 32 additions & 6 deletions builder/hcloud/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,42 @@ func (a *Artifact) String() string {

func (a *Artifact) State(name string) interface{} {
if name == registryimage.ArtifactStateURI {
img, err := registryimage.FromArtifact(a, registryimage.WithProvider("hetznercloud"))
if err != nil {
log.Printf("[DEBUG] error encountered when creating a registry image %v", err)
return nil
}
return img
return a.stateHCPPackerRegistryMetadata()
}
return a.StateData[name]
}

func (a *Artifact) stateHCPPackerRegistryMetadata() interface{} {
// create labels map
labels := make(map[string]string)
sourceImage, ok := a.StateData["source_image"].(string)
if ok {
labels["source_image"] = sourceImage
}
// get and set region from stateData into labels
region, ok := a.StateData["region"].(string)
if ok {
labels["region"] = region
}
// get and set server_type from stateData into labels
serverType, ok := a.StateData["server_type"].(string)
if ok {
labels["server_type"] = serverType
}

image, err := registryimage.FromArtifact(a,
registryimage.WithProvider("hetznercloud"),
registryimage.WithID(strconv.FormatInt(a.snapshotId, 10)),
registryimage.WithSourceID(sourceImage),
registryimage.WithRegion(region))
if err != nil {
log.Printf("[DEBUG] error encountered when creating registry image %s", err)
return nil
}
image.Labels = labels
return image
}

func (a *Artifact) Destroy() error {
log.Printf("Destroying image: %d (%s)", a.snapshotId, a.snapshotName)
_, err := a.hcloudClient.Image.Delete(context.TODO(), &hcloud.Image{ID: a.snapshotId})
Expand Down
44 changes: 44 additions & 0 deletions builder/hcloud/artifact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
package hcloud

import (
"reflect"
"testing"

packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
registryimage "github.com/hashicorp/packer-plugin-sdk/packer/registry/image"
"github.com/mitchellh/mapstructure"
)

func TestArtifact_Impl(t *testing.T) {
Expand Down Expand Up @@ -58,3 +61,44 @@ func TestArtifactState_StateData(t *testing.T) {
t.Fatalf("Bad: State should be nil for nil StateData")
}
}

func TestArtifactState_hcpPackerRegistryMetadata(t *testing.T) {
region := "nbg1"
artifact := &Artifact{
snapshotId: 1111111111,
snapshotName: "test-image",
StateData: map[string]interface{}{
"source_image": "ubuntu-22.04",
"region": region,
"server_type": "cx11",
},
}
// result should contain "something"
result := artifact.State(registryimage.ArtifactStateURI)
if result == nil {
t.Fatalf("Bad: HCP Packer registry image data was nil")
}

// check for proper decoding of result into slice of registryimage.Image
var image registryimage.Image
err := mapstructure.Decode(result, &image)
if err != nil {
t.Errorf("Bad: unexpected error when trying to decode state into registryimage.Image %v", err)
}

// check that all properties of the images were set correctly
expected := registryimage.Image{
ImageID: "1111111111",
ProviderName: "hetznercloud",
ProviderRegion: region,
SourceImageID: "ubuntu-22.04",
Labels: map[string]string{
"source_image": "ubuntu-22.04",
"region": region,
"server_type": "cx11",
},
}
if !reflect.DeepEqual(image, expected) {
t.Fatalf("Bad: expected %#v got %#v", expected, image)
}
}
7 changes: 6 additions & 1 deletion builder/hcloud/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
snapshotName: state.Get("snapshot_name").(string),
snapshotId: state.Get("snapshot_id").(int64),
hcloudClient: b.hcloudClient,
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
StateData: map[string]interface{}{
"generated_data": state.Get("generated_data"),
"source_image": b.config.Image,
"region": b.config.Location,
"server_type": b.config.ServerType,
},
}

return artifact, nil
Expand Down

0 comments on commit 9dd1cbe

Please sign in to comment.