diff --git a/examples/main.tf b/examples/main.tf index 8d08aea..45420e6 100644 --- a/examples/main.tf +++ b/examples/main.tf @@ -26,20 +26,21 @@ resource "packer_image" "image1" { triggers = { packer_version = data.packer_version.version.version - files_hash = data.packer_files.files1.files_hash + files_hash = data.packer_files.files1.files_hash } } resource "packer_image" "image2" { directory = data.packer_files.files2.directory - force = true + force = true variables = { test_var3 = "test 3" } + keep_environment = true triggers = { packer_version = data.packer_version.version.version - files_hash = data.packer_files.files2.files_hash + files_hash = data.packer_files.files2.files_hash } } diff --git a/packer_interop/const.go b/packer_interop/const.go new file mode 100644 index 0000000..355a192 --- /dev/null +++ b/packer_interop/const.go @@ -0,0 +1,3 @@ +package packer_interop + +const TPPRunPacker = "TPP_RUN_PACKER" diff --git a/packer_interop/env.go b/packer_interop/env.go new file mode 100644 index 0000000..da6c10e --- /dev/null +++ b/packer_interop/env.go @@ -0,0 +1,24 @@ +package packer_interop + +import ( + "os" + "strings" +) + +func EnvVars(additionalEnvVars map[string]string, passThroughCurrent bool) map[string]string { + envVars := map[string]string{} + if passThroughCurrent { + for _, envVarStr := range os.Environ() { + split := strings.SplitN(envVarStr, "=", 2) + if len(split) != 2 { + continue + } + envVars[split[0]] = split[1] + } + } + for key, value := range additionalEnvVars { + envVars[key] = value + } + envVars[TPPRunPacker] = "true" + return envVars +} diff --git a/provider/data_source_packer_version.go b/provider/data_source_packer_version.go index abdac33..e5ea352 100644 --- a/provider/data_source_packer_version.go +++ b/provider/data_source_packer_version.go @@ -5,6 +5,8 @@ import ( "os" "strings" + "terraform-provider-packer/packer_interop" + "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/tfsdk" "github.com/hashicorp/terraform-plugin-framework/types" @@ -42,7 +44,7 @@ func (r dataSourceVersion) Read(ctx context.Context, req tfsdk.ReadDataSourceReq exe, _ := os.Executable() output, err := cmds.RunCommandWithEnvReturnOutput( exe, - map[string]string{tppRunPacker: "true"}, + map[string]string{packer_interop.TPPRunPacker: "true"}, "version") if err != nil { resp.Diagnostics.AddError("Failed to run packer", err.Error()) diff --git a/provider/provider.go b/provider/provider.go index 0db76de..0cd2304 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -4,6 +4,8 @@ import ( "context" "os" + "terraform-provider-packer/packer_interop" + "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/tfsdk" "github.com/hashicorp/terraform-plugin-framework/types" @@ -11,8 +13,6 @@ import ( "github.com/toowoxx/go-lib-userspace-common/cmds" ) -const tppRunPacker = "TPP_RUN_PACKER" - func New() tfsdk.Provider { return &provider{} } @@ -34,7 +34,7 @@ func (p *provider) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) func (p *provider) Configure(ctx context.Context, req tfsdk.ConfigureProviderRequest, resp *tfsdk.ConfigureProviderResponse) { exe, _ := os.Executable() - err := cmds.RunCommandWithEnv(exe, map[string]string{tppRunPacker: "true"}, "version") + err := cmds.RunCommandWithEnv(exe, map[string]string{packer_interop.TPPRunPacker: "true"}, "version") if err != nil { panic(err) } diff --git a/provider/resource_packer_image.go b/provider/resource_packer_image.go index 1a7f573..1a7b712 100644 --- a/provider/resource_packer_image.go +++ b/provider/resource_packer_image.go @@ -4,6 +4,8 @@ import ( "context" "os" + "terraform-provider-packer/packer_interop" + "github.com/pkg/errors" "github.com/toowoxx/go-lib-userspace-common/cmds" @@ -21,6 +23,7 @@ type resourceImageType struct { Directory types.String `tfsdk:"directory"` File types.String `tfsdk:"file"` Environment map[string]string `tfsdk:"environment"` + KeepEnvironment types.Bool `tfsdk:"keep_environment"` Triggers map[string]string `tfsdk:"triggers"` Force types.Bool `tfsdk:"force"` BuildUUID types.String `tfsdk:"build_uuid"` @@ -63,6 +66,11 @@ func (r resourceImageType) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diag Type: types.MapType{ElemType: types.StringType}, Optional: true, }, + "keep_environment": { + Description: "Passes all environment variables of the provider through to Packer", + Type: types.BoolType, + Optional: true, + }, "triggers": { Description: "Values that, when changed, trigger an update of this resource", Type: types.MapType{ElemType: types.StringType}, @@ -108,11 +116,7 @@ func (r resourceImage) getFileParam(resourceState *resourceImageType) string { } func (r resourceImage) packerInit(resourceState *resourceImageType) error { - envVars := map[string]string{} - for key, value := range resourceState.Environment { - envVars[key] = value - } - envVars[tppRunPacker] = "true" + envVars := packer_interop.EnvVars(resourceState.Environment, resourceState.KeepEnvironment.Value) params := []string{"init"} params = append(params, r.getFileParam(resourceState)) @@ -128,11 +132,7 @@ func (r resourceImage) packerInit(resourceState *resourceImageType) error { } func (r resourceImage) packerBuild(resourceState *resourceImageType) error { - envVars := map[string]string{} - for key, value := range resourceState.Environment { - envVars[key] = value - } - envVars[tppRunPacker] = "true" + envVars := packer_interop.EnvVars(resourceState.Environment, resourceState.KeepEnvironment.Value) params := []string{"build"} for key, value := range resourceState.Variables {