Skip to content

Commit

Permalink
Upgrade Packer to 1.9.4, Go to 1.20, dependencies
Browse files Browse the repository at this point in the history
And do the necessary migration
  • Loading branch information
simaotwx committed Oct 24, 2023
1 parent d6aa52d commit 7fddce7
Show file tree
Hide file tree
Showing 8 changed files with 514 additions and 1,310 deletions.
231 changes: 91 additions & 140 deletions go.mod

Large diffs are not rendered by default.

1,221 changes: 212 additions & 1,009 deletions go.sum

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions packer_interop/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package packer_interop
import (
"os"
"strings"

"github.com/google/uuid"
)

func EnvVars(additionalEnvVars map[string]string, passThroughCurrent bool) map[string]string {
Expand All @@ -20,5 +22,6 @@ func EnvVars(additionalEnvVars map[string]string, passThroughCurrent bool) map[s
envVars[key] = value
}
envVars[TPPRunPacker] = "true"
envVars["PACKER_RUN_UUID"] = uuid.Must(uuid.NewRandom()).String()
return envVars
}
66 changes: 36 additions & 30 deletions provider/data_source_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (

"terraform-provider-packer/crypto_util"

"github.com/hashicorp/terraform-plugin-framework/datasource/schema"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/provider"

"github.com/pkg/errors"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)

Expand All @@ -25,9 +26,9 @@ type dataSourceFilesType struct {

func (d dataSourceFiles) updateAutoComputed(resourceState *dataSourceFilesType) error {
deps := resourceState.FileDependencies
if resourceState.File.Null || len(resourceState.File.Value) == 0 {
dir := resourceState.Directory.Value
if resourceState.Directory.Unknown || len(dir) == 0 {
if resourceState.File.IsNull() || len(resourceState.File.ValueString()) == 0 {
dir := resourceState.Directory.ValueString()
if resourceState.Directory.IsUnknown() || len(dir) == 0 {
dir = "."
}
hclFiles, err := filepath.Glob(dir + "/*.pkr.hcl")
Expand All @@ -40,44 +41,43 @@ func (d dataSourceFiles) updateAutoComputed(resourceState *dataSourceFilesType)
}
deps = append(append(deps, hclFiles...), jsonFiles...)
} else {
deps = append([]string{resourceState.File.Value}, deps...)
deps = append([]string{resourceState.File.ValueString()}, deps...)
}

depFilesHash, err := crypto_util.FilesSHA256(deps...)
if err != nil {
return err
}
resourceState.FilesHash = types.String{Value: depFilesHash}
resourceState.FilesHash = types.StringValue(depFilesHash)

return nil
}

func (d dataSourceFilesType) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Description: "Specify files to detect changes. By default, the current directory will be used.",
Attributes: map[string]tfsdk.Attribute{
"file": {
Description: "Packer file to use for building",
Type: types.StringType,
Optional: true,
},
"files_hash": {
Description: "Hash of the files provided. Used for updates.",
Type: types.StringType,
Computed: true,
},
"file_dependencies": {
Description: "Files that should be depended on so that the resource is updated when these files change",
Type: types.SetType{ElemType: types.StringType},
Optional: true,
},
"directory": {
Description: "Directory to run packer in. Defaults to cwd.",
Type: types.StringType,
Optional: true,
func (d dataSourceFiles) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
*resp = datasource.SchemaResponse{
Schema: schema.Schema{
Description: "Specify files to detect changes. By default, the current directory will be used.",
Attributes: map[string]schema.Attribute{
"file": schema.StringAttribute{
Description: "Packer file to use for building",
Optional: true,
},
"files_hash": schema.StringAttribute{
Description: "Hash of the files provided. Used for updates.",
Computed: true,
},
"file_dependencies": schema.SetAttribute{
Description: "Files that should be depended on so that the resource is updated when these files change",
ElementType: types.StringType,
Optional: true,
},
"directory": schema.StringAttribute{
Description: "Directory to run packer in. Defaults to cwd.",
Optional: true,
},
},
},
}, nil
}
}

func (d dataSourceFilesType) NewDataSource(_ context.Context, p provider.Provider) (datasource.DataSource, diag.Diagnostics) {
Expand All @@ -90,6 +90,12 @@ type dataSourceFiles struct {
p tfProvider
}

func (d dataSourceFiles) Metadata(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) {
*resp = datasource.MetadataResponse{
TypeName: "packer_files",
}
}

func (d dataSourceFiles) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
resourceState := dataSourceFilesType{}
diags := req.Config.Get(ctx, &resourceState)
Expand Down
27 changes: 17 additions & 10 deletions provider/data_source_packer_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,30 @@ import (

"terraform-provider-packer/packer_interop"

"github.com/hashicorp/terraform-plugin-framework/datasource/schema"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/provider"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/toowoxx/go-lib-userspace-common/cmds"
)

type dataSourceVersionType struct {
Version string `tfsdk:"version"`
}

func (r dataSourceVersionType) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"version": {
Description: "Version of embedded Packer",
Computed: true,
Type: types.StringType,
func (r dataSourceVersion) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
*resp = datasource.SchemaResponse{
Schema: schema.Schema{
Attributes: map[string]schema.Attribute{
"version": schema.StringAttribute{
Description: "Version of embedded Packer",
Computed: true,
},
},
},
}, nil
}
}

func (r dataSourceVersionType) NewDataSource(_ context.Context, p provider.Provider) (datasource.DataSource, diag.Diagnostics) {
Expand All @@ -42,6 +43,12 @@ type dataSourceVersion struct {
p tfProvider
}

func (r dataSourceVersion) Metadata(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) {
*resp = datasource.MetadataResponse{
TypeName: "packer_version",
}
}

func (r dataSourceVersion) Read(ctx context.Context, _ datasource.ReadRequest, resp *datasource.ReadResponse) {
resourceState := dataSourceVersionType{}
exe, _ := os.Executable()
Expand Down
56 changes: 28 additions & 28 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (

"terraform-provider-packer/packer_interop"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/datasource"
provider_schema "github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"

"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/toowoxx/go-lib-userspace-common/cmds"
)
Expand All @@ -21,17 +22,31 @@ func New() provider.Provider {
type tfProvider struct {
}

func (p *tfProvider) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"dummy": {
Type: types.StringType,
Optional: true,
Computed: true,
Description: "Used as a placeholder. Do not use.",
},
func (p *tfProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
*resp = provider.MetadataResponse{
TypeName: "packer",
}
}

func (p *tfProvider) Schema(_ context.Context, _ provider.SchemaRequest, response *provider.SchemaResponse) {
*response = provider.SchemaResponse{
Schema: provider_schema.Schema{
Attributes: map[string]provider_schema.Attribute{},
},
}, nil
}
}

func (p *tfProvider) DataSources(_ context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
func() datasource.DataSource { return dataSourceVersion{p: *p} },
func() datasource.DataSource { return dataSourceFiles{p: *p} },
}
}

func (p *tfProvider) Resources(_ context.Context) []func() resource.Resource {
return []func() resource.Resource{
func() resource.Resource { return resourceImage{p: *p} },
}
}

func (p *tfProvider) Configure(_ context.Context, _ provider.ConfigureRequest, _ *provider.ConfigureResponse) {
Expand All @@ -41,18 +56,3 @@ func (p *tfProvider) Configure(_ context.Context, _ provider.ConfigureRequest, _
panic(err)
}
}

// GetResources - Defines provider resources
func (p *tfProvider) GetResources(_ context.Context) (map[string]provider.ResourceType, diag.Diagnostics) {
return map[string]provider.ResourceType{
"packer_image": resourceImageType{},
}, nil
}

// GetDataSources - Defines provider data sources
func (p *tfProvider) GetDataSources(_ context.Context) (map[string]provider.DataSourceType, diag.Diagnostics) {
return map[string]provider.DataSourceType{
"packer_version": dataSourceVersionType{},
"packer_files": dataSourceFilesType{},
}, nil
}
Loading

0 comments on commit 7fddce7

Please sign in to comment.