Skip to content

Commit

Permalink
Use latest version of hcp-sdk-go
Browse files Browse the repository at this point in the history
Remove loggers and move AddMetadataToBuild method to Version
  • Loading branch information
devashish-patel authored and lbajolet-hashicorp committed Apr 11, 2024
1 parent 8e92027 commit edc8a4d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 65 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module github.com/hashicorp/packer

replace github.com/hashicorp/hcp-sdk-go => github.com/hashicorp/hcp-sdk-go-internal v0.0.0-20240325194734-0bb6bd5bbc19

require (
cloud.google.com/go v0.110.8 // indirect
github.com/biogo/hts v1.4.3
Expand Down
6 changes: 3 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/hcl/v2 v2.19.1 h1://i05Jqznmb2EXqa39Nsvyan2o5XyMowW5fnCKW5RPI=
github.com/hashicorp/hcl/v2 v2.19.1/go.mod h1:ThLC89FV4p9MPW804KVbe/cEXoQ8NZEh+JtMeeGErHE=
github.com/hashicorp/hcp-sdk-go-internal v0.0.0-20240325194734-0bb6bd5bbc19 h1:QFacHWwhQbVqA9p/nX5CC7wPVQUuT8T2pN7TKI15BQw=
github.com/hashicorp/hcp-sdk-go-internal v0.0.0-20240325194734-0bb6bd5bbc19/go.mod h1:vsLMHasv6H3Qf7wIlRSzHXIOp81G+ad1FP31JfdTKYc=
github.com/hashicorp/hcp-sdk-go v0.90.0 h1:dYN7PB9bBYKamjMHPBQnK6d8zphyexHGXw4ddynK2ck=
github.com/hashicorp/hcp-sdk-go v0.90.0/go.mod h1:vQ4fzdL1AmhIAbCw+4zmFe5Hbpajj3NvRWkJoVuxmAk=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/mdns v1.0.4/go.mod h1:mtBihi+LeNXGtG8L9dX59gAEa12BDtBQSp4v/YAJqrc=
github.com/hashicorp/memberlist v0.5.0 h1:EtYPN8DpAURiapus508I4n9CzHs2W+8NZGbmmR/prTM=
Expand Down Expand Up @@ -784,8 +784,8 @@ gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
22 changes: 6 additions & 16 deletions internal/hcp/registry/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (h *HCLRegistry) StartBuild(ctx context.Context, build sdkpacker.Build) err
}

metadata := cb.GetMetadata()
err := h.bucket.AddMetadataToBuild(ctx, name, metadata)
err := h.bucket.Version.AddMetadataToBuild(ctx, name, metadata)
if err != nil {
return err
}
Expand All @@ -86,28 +86,18 @@ func (h *HCLRegistry) CompleteBuild(
artifacts []sdkpacker.Artifact,
buildErr error,
) ([]sdkpacker.Artifact, error) {
name := build.Name()
buildName := build.Name()
cb, ok := build.(*packer.CoreBuild)
if ok {
name = cb.Type
buildName = cb.Type
}

metadata := cb.GetMetadata()
log.Printf(
"[TRACE] HCL 'Packer Version' Metadata for build name %q: %q\n",
name, metadata.PackerVersion,
)
for k, pluginDetails := range metadata.Plugins {
log.Printf(
"[TRACE] HCL 'Plugin' Metadata for build name %q: %q -- %q\n",
name, k, pluginDetails.Description.Version,
)
}
err := h.bucket.AddMetadataToBuild(ctx, name, metadata)
buildMetadata := cb.GetMetadata()
err := h.bucket.Version.AddMetadataToBuild(ctx, buildName, buildMetadata)
if err != nil {
return nil, err
}
return h.bucket.completeBuild(ctx, name, artifacts, buildErr)
return h.bucket.completeBuild(ctx, buildName, artifacts, buildErr)
}

// VersionStatusSummary prints a status report in the UI if the version is not yet done
Expand Down
22 changes: 5 additions & 17 deletions internal/hcp/registry/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (h *JSONRegistry) StartBuild(ctx context.Context, build sdkpacker.Build) er
name := build.Name()

metadata := build.(*packer.CoreBuild).GetMetadata()
err := h.bucket.AddMetadataToBuild(ctx, name, metadata)
err := h.bucket.Version.AddMetadataToBuild(ctx, name, metadata)
if err != nil {
return err
}
Expand All @@ -100,25 +100,13 @@ func (h *JSONRegistry) CompleteBuild(
artifacts []sdkpacker.Artifact,
buildErr error,
) ([]sdkpacker.Artifact, error) {
name := build.Name()

metadata := build.(*packer.CoreBuild).GetMetadata()
log.Printf(
"[TRACE] JSON 'Packer Version' Metadata for build name %q: %q\n",
name, metadata.PackerVersion,
)
for k, pluginDetails := range metadata.Plugins {
log.Printf(
"[TRACE] JSON 'Plugin' Metadata for build name %q: %q -- %q\n",
name, k, pluginDetails.Description.Version,
)
}

err := h.bucket.AddMetadataToBuild(ctx, name, metadata)
buildName := build.Name()
buildMetadata := build.(*packer.CoreBuild).GetMetadata()
err := h.bucket.Version.AddMetadataToBuild(ctx, buildName, buildMetadata)
if err != nil {
return nil, err
}
return h.bucket.completeBuild(ctx, name, artifacts, buildErr)
return h.bucket.completeBuild(ctx, buildName, artifacts, buildErr)
}

// VersionStatusSummary prints a status report in the UI if the version is not yet done
Expand Down
27 changes: 0 additions & 27 deletions internal/hcp/registry/types.bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/hashicorp/packer/hcl2template"
hcpPackerAPI "github.com/hashicorp/packer/internal/hcp/api"
"github.com/hashicorp/packer/internal/hcp/env"
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/mapstructure"
"google.golang.org/grpc/codes"
)
Expand Down Expand Up @@ -603,32 +602,6 @@ type NotAHCPArtifactError struct {
error
}

// AddMetadataToBuild adds metadata to a build in the HCP Packer registry.
func (bucket *Bucket) AddMetadataToBuild(
ctx context.Context, buildName string, metadata packer.BuildMetadata,
) error {
buildToUpdate, err := bucket.Version.Build(buildName)
if err != nil {
return err
}

packerMetadata := make(map[string]interface{})
packerMetadata["version"] = metadata.PackerVersion

var pluginsMetadata []map[string]interface{}
for _, plugin := range metadata.Plugins {
pluginMetadata := map[string]interface{}{
"version": plugin.Description.Version,
"name": plugin.Name,
}
pluginsMetadata = append(pluginsMetadata, pluginMetadata)
}
packerMetadata["plugins"] = pluginsMetadata

buildToUpdate.Metadata.Packer = packerMetadata
return nil
}

func (bucket *Bucket) completeBuild(
ctx context.Context,
buildName string,
Expand Down
28 changes: 28 additions & 0 deletions internal/hcp/registry/types.version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package registry

import (
"context"
"errors"
"fmt"
"math/rand"
Expand All @@ -16,6 +17,7 @@ import (
sdkpacker "github.com/hashicorp/packer-plugin-sdk/packer"
packerSDKRegistry "github.com/hashicorp/packer-plugin-sdk/packer/registry/image"
"github.com/hashicorp/packer/internal/hcp/env"
"github.com/hashicorp/packer/packer"
"github.com/oklog/ulid"
)

Expand Down Expand Up @@ -174,3 +176,29 @@ func (version *Version) statusSummary(ui sdkpacker.Ui) {

ui.Say(buf.String())
}

// AddMetadataToBuild adds metadata to a build in the HCP Packer registry.
func (version *Version) AddMetadataToBuild(
ctx context.Context, buildName string, metadata packer.BuildMetadata,
) error {
buildToUpdate, err := version.Build(buildName)
if err != nil {
return err
}

packerMetadata := make(map[string]interface{})
packerMetadata["version"] = metadata.PackerVersion

var pluginsMetadata []map[string]interface{}
for _, plugin := range metadata.Plugins {
pluginMetadata := map[string]interface{}{
"version": plugin.Description.Version,
"name": plugin.Name,
}
pluginsMetadata = append(pluginsMetadata, pluginMetadata)
}
packerMetadata["plugins"] = pluginsMetadata

buildToUpdate.Metadata.Packer = packerMetadata
return nil
}

0 comments on commit edc8a4d

Please sign in to comment.