Skip to content

Commit de86c98

Browse files
authored
feat(builder): HCP Packer image metadata support (#33)
1 parent 53b557a commit de86c98

File tree

8 files changed

+110
-107
lines changed

8 files changed

+110
-107
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ See updating [Changelog example here](https://keepachangelog.com/en/1.0.0/)
66
## [Unreleased]
77

88
### Added
9+
- HCP Packer image metadata support
910
- enviroment variables `UPCLOUD_USERNAME` and `UPCLOUD_PASSWORD` for authentication
1011

1112
### Deprecated

builder/upcloud/artifact.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package upcloud
22

33
import (
44
"fmt"
5+
"log"
56
"strings"
67

78
"github.com/UpCloudLtd/packer-plugin-upcloud/internal/driver"
89
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud"
10+
"github.com/hashicorp/packer-plugin-sdk/packer/registry/image"
911
)
1012

1113
// packersdk.Artifact implementation
@@ -40,6 +42,14 @@ func (a *Artifact) String() string {
4042
}
4143

4244
func (a *Artifact) State(name string) interface{} {
45+
if name == image.ArtifactStateURI {
46+
images, err := a.buildHCPPackerRegistryMetadata()
47+
if err != nil {
48+
log.Printf("[DEBUG] error encountered when creating a registry image %v", err)
49+
return nil
50+
}
51+
return images
52+
}
4353
return a.StateData[name]
4454
}
4555

@@ -52,3 +62,35 @@ func (a *Artifact) Destroy() error {
5262
}
5363
return nil
5464
}
65+
66+
func (a *Artifact) buildHCPPackerRegistryMetadata() ([]*image.Image, error) {
67+
var sourceTemplateUUID, sourceTemplateTitle string
68+
if v, ok := a.StateData["source_template_uuid"]; ok {
69+
sourceTemplateUUID = v.(string)
70+
}
71+
72+
if v, ok := a.StateData["source_template_title"]; ok {
73+
sourceTemplateTitle = v.(string)
74+
}
75+
76+
images := make([]*image.Image, 0)
77+
for _, template := range a.Templates {
78+
img, err := image.FromArtifact(a,
79+
image.WithID(template.UUID),
80+
image.WithRegion(template.Zone),
81+
image.WithProvider("upcloud"),
82+
)
83+
if err != nil {
84+
return images, err
85+
}
86+
87+
img.SourceImageID = sourceTemplateUUID
88+
img.Labels["source_id"] = sourceTemplateUUID
89+
img.Labels["source"] = sourceTemplateTitle
90+
img.Labels["name"] = template.Title
91+
img.Labels["name_prefix"] = a.config.TemplatePrefix
92+
img.Labels["size"] = fmt.Sprint(template.Size)
93+
images = append(images, img)
94+
}
95+
return images, nil
96+
}

builder/upcloud/artifact_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66

77
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud"
88
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
9+
"github.com/hashicorp/packer-plugin-sdk/packer/registry/image"
10+
"github.com/stretchr/testify/assert"
911
)
1012

1113
func TestArtifact_impl(t *testing.T) {
@@ -42,3 +44,49 @@ func TestArtifact_String(t *testing.T) {
4244
t.Errorf("Expected: %q, got: %q", expected, result)
4345
}
4446
}
47+
48+
func TestArtifact_Metadata(t *testing.T) {
49+
templates := []*upcloud.Storage{}
50+
templates = append(templates,
51+
&upcloud.Storage{
52+
UUID: "some-uuid",
53+
Size: 10,
54+
Title: "some-title",
55+
Zone: "fi-hel1",
56+
},
57+
&upcloud.Storage{
58+
UUID: "some-other-uuid",
59+
Size: 10,
60+
Title: "some-title",
61+
Zone: "fi-hel2",
62+
},
63+
)
64+
65+
a := &Artifact{
66+
Templates: templates,
67+
config: &Config{
68+
Zone: "fi-hel1",
69+
CloneZones: []string{"fi-hel2"},
70+
TemplatePrefix: "prefix",
71+
},
72+
StateData: map[string]interface{}{
73+
"source_template_title": "source-title",
74+
"source_template_uuid": "source-uuid",
75+
},
76+
}
77+
got := a.State(image.ArtifactStateURI).([]*image.Image)
78+
want := &image.Image{
79+
ImageID: "some-uuid",
80+
ProviderName: "upcloud",
81+
ProviderRegion: "fi-hel1",
82+
Labels: map[string]string{
83+
"source": "source-title",
84+
"source_id": "source-uuid",
85+
"name": "some-title",
86+
"name_prefix": "prefix",
87+
"size": "10",
88+
},
89+
SourceImageID: "source-uuid",
90+
}
91+
assert.Equal(t, want, got[0])
92+
}

builder/upcloud/builder.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
9999
config: &b.config,
100100
driver: b.driver,
101101
StateData: map[string]interface{}{
102-
"generated_data": state.Get("generated_data"),
103-
"template_prefix": b.config.TemplatePrefix,
104-
"template_name": b.config.TemplateName,
102+
"generated_data": state.Get("generated_data"),
103+
"template_prefix": b.config.TemplatePrefix,
104+
"template_name": b.config.TemplateName,
105+
"source_template_uuid": state.Get("source_template_uuid"),
106+
"source_template_title": state.Get("source_template_title"),
105107
},
106108
}
107109

builder/upcloud/step_create_server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ func (s *StepCreateServer) Run(_ context.Context, state multistep.StateBag) mult
7272
ui.Say(fmt.Sprintf("Auto-selecting ip '%s' as Server IP", addr.Address))
7373
}
7474

75+
state.Put("source_template_uuid", storage.UUID)
76+
state.Put("source_template_title", storage.Title)
7577
state.Put("server_ip_address", addr)
7678
state.Put("server_uuid", response.UUID)
7779
state.Put("server_title", response.Title)

builder/upcloud/step_create_template.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (s *StepCreateTemplate) Run(_ context.Context, state multistep.StateBag) mu
3030
return stepHaltWithError(state, err)
3131
}
3232

33-
// clonning to zones
33+
// cloning to zones
3434
cleanupStorageUuid := []string{}
3535
storageUuids := []string{}
3636
storageUuids = append(storageUuids, storage.UUID)
@@ -45,7 +45,7 @@ func (s *StepCreateTemplate) Run(_ context.Context, state multistep.StateBag) mu
4545
storageUuids = append(storageUuids, clonedStorage.UUID)
4646
cleanupStorageUuid = append(cleanupStorageUuid, clonedStorage.UUID)
4747
}
48-
ui.Say("Clonning completed...")
48+
ui.Say("Cloning completed...")
4949

5050
// creating template
5151
templates := []*upcloud.Storage{}

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ module github.com/UpCloudLtd/packer-plugin-upcloud
33
go 1.17
44

55
require (
6-
github.com/UpCloudLtd/upcloud-go-api/v4 v4.5.2
6+
github.com/UpCloudLtd/upcloud-go-api/v4 v4.6.0
77
github.com/hashicorp/hcl/v2 v2.12.0
88
github.com/hashicorp/packer-plugin-sdk v0.2.12
9-
github.com/stretchr/testify v1.7.0
9+
github.com/stretchr/testify v1.7.2
1010
github.com/zclconf/go-cty v1.10.0
1111
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f
1212
)
@@ -105,5 +105,5 @@ require (
105105
google.golang.org/grpc v1.42.0 // indirect
106106
google.golang.org/protobuf v1.27.1 // indirect
107107
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
108-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
108+
gopkg.in/yaml.v3 v3.0.1 // indirect
109109
)

0 commit comments

Comments
 (0)