Skip to content

Commit 45638bc

Browse files
committed
feat: add required hcp metadata
By declaring these metadata, the builds informations can be sent to HCP
1 parent a7e79ff commit 45638bc

File tree

4 files changed

+69
-5
lines changed

4 files changed

+69
-5
lines changed

.web-docs/components/builder/openstack/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,13 @@ builder.
453453
useful if, for example, packer hangs on a connection after a reboot.
454454
Example: `5m`. Disabled by default.
455455

456-
- `ssh_remote_tunnels` ([]string) -
456+
- `ssh_remote_tunnels` ([]string) - Remote tunnels forward a port from your local machine to the instance.
457+
Format: ["REMOTE_PORT:LOCAL_HOST:LOCAL_PORT"]
458+
Example: "9090:localhost:80" forwards localhost:9090 on your machine to port 80 on the instance.
457459

458-
- `ssh_local_tunnels` ([]string) -
460+
- `ssh_local_tunnels` ([]string) - Local tunnels forward a port from the instance to your local machine.
461+
Format: ["LOCAL_PORT:REMOTE_HOST:REMOTE_PORT"]
462+
Example: "8080:localhost:3000" allows the instance to access your local machine’s port 3000 via localhost:8080.
459463

460464
<!-- End of code generated from the comments of the SSH struct in communicator/config.go; -->
461465

builder/openstack/artifact.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99

1010
"github.com/gophercloud/gophercloud"
1111
"github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
12+
13+
registryimage "github.com/hashicorp/packer-plugin-sdk/packer/registry/image"
1214
)
1315

1416
// Artifact is an artifact implementation that contains built images.
@@ -22,9 +24,16 @@ type Artifact struct {
2224
// OpenStack connection for performing API stuff.
2325
Client *gophercloud.ServiceClient
2426

27+
// SourceImage ID of the created image this is actually resolved
28+
// based on a few configured config attributes
29+
SourceImage string
30+
31+
// The region is read from the env OS_REGION_NAME if not provided
32+
Region string
33+
2534
// StateData should store data such as GeneratedData
2635
// to be shared with post-processors
27-
StateData map[string]interface{}
36+
StateData map[string]any
2837
}
2938

3039
func (a *Artifact) BuilderId() string {
@@ -44,7 +53,21 @@ func (a *Artifact) String() string {
4453
return fmt.Sprintf("An image was created: %v", a.ImageId)
4554
}
4655

47-
func (a *Artifact) State(name string) interface{} {
56+
func (a *Artifact) State(name string) any {
57+
58+
if name == registryimage.ArtifactStateURI {
59+
img, err := registryimage.FromArtifact(a,
60+
registryimage.WithRegion(a.Region),
61+
registryimage.WithSourceID(a.SourceImage),
62+
)
63+
64+
if err != nil {
65+
log.Printf("[DEBUG] error encountered when creating a registry image %v", err)
66+
return nil
67+
}
68+
return img
69+
70+
}
4871
return a.StateData[name]
4972
}
5073

builder/openstack/artifact_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
package openstack
55

66
import (
7+
"reflect"
78
"testing"
89

910
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
11+
registryimage "github.com/hashicorp/packer-plugin-sdk/packer/registry/image"
12+
"github.com/mitchellh/mapstructure"
1013
)
1114

1215
func TestArtifact_Impl(t *testing.T) {
@@ -63,3 +66,35 @@ func TestArtifactState_StateData(t *testing.T) {
6366
t.Fatalf("Bad: State should be nil for nil StateData")
6467
}
6568
}
69+
70+
func TestArtifactState_hcpPackerRegistryMetadata(t *testing.T) {
71+
artifact := &Artifact{
72+
ImageId: "foo",
73+
BuilderIdValue: "openstack",
74+
SourceImage: "bar",
75+
Region: "mordor-7",
76+
}
77+
78+
result := artifact.State(registryimage.ArtifactStateURI)
79+
if result == nil {
80+
t.Fatalf("Bad: HCP Packer registry image data was nil")
81+
}
82+
83+
var image registryimage.Image
84+
err := mapstructure.Decode(result, &image)
85+
if err != nil {
86+
t.Errorf("Bad: unexpected error when trying to decode state into registryimage.Image %v", err)
87+
}
88+
89+
expected := registryimage.Image{
90+
ImageID: "foo",
91+
ProviderName: "openstack",
92+
ProviderRegion: "mordor-7",
93+
SourceImageID: "bar",
94+
Labels: map[string]string{},
95+
}
96+
97+
if !reflect.DeepEqual(image, expected) {
98+
t.Fatalf("bad: \ngot:\n%#v,\nexpected: \n%#v", image, expected)
99+
}
100+
}

builder/openstack/builder.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
200200
ImageId: state.Get("image").(string),
201201
BuilderIdValue: BuilderId,
202202
Client: imageClient,
203-
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
203+
SourceImage: state.Get("source_image").(string),
204+
Region: b.config.AccessConfig.Region,
205+
StateData: map[string]any{"generated_data": state.Get("generated_data")},
204206
}
205207

206208
return artifact, nil

0 commit comments

Comments
 (0)