Skip to content

Commit

Permalink
Add artifact support for HCP Packer registry (#190)
Browse files Browse the repository at this point in the history
* Add artifact support for HCP Packer registry

Working off of the official documentation
(https://www.packer.io/docs/plugins/hcp-support).  This should now allow
utilization of the hcp_packer_registry block
(https://www.packer.io/docs/templates/hcl_templates/blocks/build/hcp_packer_registry)
and support adding Vultr snapshots to the HCP Packer registry.

* Make artifact tests pass
  • Loading branch information
optik-aper authored Jun 7, 2022
1 parent ce81a6a commit f946843
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 4 deletions.
25 changes: 23 additions & 2 deletions builder/vultr/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"log"

registryimage "github.com/hashicorp/packer-plugin-sdk/packer/registry/image"

"github.com/vultr/govultr/v2"
)

Expand All @@ -15,8 +17,14 @@ type Artifact struct {
// The Description of the snapshot
Description string

// The client for making
// The client for making changes
client *govultr.Client

// config definition from the builder
config *Config

// State data used by HCP container registry
StateData map[string]interface{}
}

func (a *Artifact) BuilderId() string {
Expand All @@ -36,7 +44,20 @@ func (a *Artifact) String() string {
}

func (a *Artifact) State(name string) interface{} {
return nil
if name == registryimage.ArtifactStateURI {
img, err := registryimage.FromArtifact(a,
registryimage.WithID(a.SnapshotID),
registryimage.WithProvider("Vultr"),
registryimage.WithRegion(a.config.RegionID),
)

if err != nil {
log.Printf("[DEBUG] error encountered when creating a registry image %v", err)
return nil
}
return img
}
return a.StateData[name]
}

func (a *Artifact) Destroy() error {
Expand Down
4 changes: 2 additions & 2 deletions builder/vultr/artifact_test.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestArtifact_Impl(t *testing.T) {
}

func TestArtifactId(t *testing.T) {
a := &Artifact{"d455d0246e8e6", "packer-test", nil}
a := &Artifact{"d455d0246e8e6", "packer-test", nil, nil, nil}
expected := "d455d0246e8e6"

if a.Id() != expected {
Expand All @@ -24,7 +24,7 @@ func TestArtifactId(t *testing.T) {
}

func TestArtifactString(t *testing.T) {
a := &Artifact{"d455d0246e8e6", "packer-test", nil}
a := &Artifact{"d455d0246e8e6", "packer-test", nil, nil, nil}
expected := "Vultr Snapshot: packer-test (d455d0246e8e6)"

if a.String() != expected {
Expand Down
1 change: 1 addition & 0 deletions builder/vultr/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (ret
artifact := &Artifact{
SnapshotID: snapshot.ID,
Description: snapshot.Description,
config: &b.config,
client: client,
}

Expand Down

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

1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ github.com/hashicorp/packer-plugin-sdk/multistep
github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps
github.com/hashicorp/packer-plugin-sdk/net
github.com/hashicorp/packer-plugin-sdk/packer
github.com/hashicorp/packer-plugin-sdk/packer/registry/image
github.com/hashicorp/packer-plugin-sdk/packerbuilderdata
github.com/hashicorp/packer-plugin-sdk/pathing
github.com/hashicorp/packer-plugin-sdk/plugin
Expand Down

0 comments on commit f946843

Please sign in to comment.