Skip to content

Commit

Permalink
Add feature to pass through env vars from provider
Browse files Browse the repository at this point in the history
  • Loading branch information
simaotwx committed Dec 3, 2021
1 parent 5902ba3 commit 6bcc266
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 17 deletions.
7 changes: 4 additions & 3 deletions examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
3 changes: 3 additions & 0 deletions packer_interop/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package packer_interop

const TPPRunPacker = "TPP_RUN_PACKER"
24 changes: 24 additions & 0 deletions packer_interop/env.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 3 additions & 1 deletion provider/data_source_packer_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
Expand Down
6 changes: 3 additions & 3 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ 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"

"github.com/toowoxx/go-lib-userspace-common/cmds"
)

const tppRunPacker = "TPP_RUN_PACKER"

func New() tfsdk.Provider {
return &provider{}
}
Expand All @@ -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)
}
Expand Down
20 changes: 10 additions & 10 deletions provider/resource_packer_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"os"

"terraform-provider-packer/packer_interop"

"github.com/pkg/errors"
"github.com/toowoxx/go-lib-userspace-common/cmds"

Expand All @@ -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"`
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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))
Expand All @@ -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 {
Expand Down

0 comments on commit 6bcc266

Please sign in to comment.