Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Pass provider credentials as environment variables
Browse files Browse the repository at this point in the history
- Fixes #99

Signed-off-by: Alper Rifat Ulucinar <ulucinar@users.noreply.github.com>
  • Loading branch information
ulucinar committed Oct 13, 2021
1 parent 734cd94 commit 57f7df5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 2 additions & 0 deletions pkg/terraform/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Setup struct {
Version string
Requirement ProviderRequirement
Configuration ProviderConfiguration
Env []string
}

// WorkspaceStoreOption lets you configure the workspace store.
Expand Down Expand Up @@ -127,6 +128,7 @@ func (ws *WorkspaceStore) Workspace(ctx context.Context, c resource.SecretClient
if xpresource.Ignore(os.IsNotExist, err) != nil {
return nil, errors.Wrap(err, "cannot stat init lock file")
}
w.env = ts.Env
// We need to initialize only if the workspace hasn't been initialized yet.
if !os.IsNotExist(err) {
return w, nil
Expand Down
21 changes: 15 additions & 6 deletions pkg/terraform/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type Workspace struct {
LastOperation *Operation

dir string
env []string
logger logging.Logger
}

Expand All @@ -86,7 +87,7 @@ func (w *Workspace) ApplyAsync(callback CallbackFn) error {
go func() {
defer cancel()
cmd := exec.CommandContext(ctx, "terraform", "apply", "-auto-approve", "-input=false", "-lock=false", "-json")
cmd.Dir = w.dir
w.configureCmd(cmd)
out, err := cmd.CombinedOutput()
w.LastOperation.MarkEnd()
w.logger.Debug("apply async ended", "out", string(out))
Expand Down Expand Up @@ -131,7 +132,7 @@ func (w *Workspace) Apply(ctx context.Context) (ApplyResult, error) {
return ApplyResult{}, errors.Errorf("%s operation that started at %s is still running", w.LastOperation.Type, w.LastOperation.StartTime().String())
}
cmd := exec.CommandContext(ctx, "terraform", "apply", "-auto-approve", "-input=false", "-lock=false", "-json")
cmd.Dir = w.dir
w.configureCmd(cmd)
out, err := cmd.CombinedOutput()
w.logger.Debug("apply ended", "out", string(out))
if err != nil {
Expand Down Expand Up @@ -167,7 +168,7 @@ func (w *Workspace) DestroyAsync() error {
go func() {
defer cancel()
cmd := exec.CommandContext(ctx, "terraform", "destroy", "-auto-approve", "-input=false", "-lock=false", "-json")
cmd.Dir = w.dir
w.configureCmd(cmd)
out, err := cmd.CombinedOutput()
w.LastOperation.MarkEnd()
w.logger.Debug("destroy async ended", "out", string(out))
Expand All @@ -188,7 +189,7 @@ func (w *Workspace) Destroy(ctx context.Context) error {
return errors.Errorf("%s operation that started at %s is still running", w.LastOperation.Type, w.LastOperation.StartTime().String())
}
cmd := exec.CommandContext(ctx, "terraform", "destroy", "-auto-approve", "-input=false", "-lock=false", "-json")
cmd.Dir = w.dir
w.configureCmd(cmd)
out, err := cmd.CombinedOutput()
w.logger.Debug("destroy ended", "out", string(out))
return errors.Wrapf(err, "cannot destroy: %s", string(out))
Expand Down Expand Up @@ -221,7 +222,7 @@ func (w *Workspace) Refresh(ctx context.Context) (RefreshResult, error) {
}
}
cmd := exec.CommandContext(ctx, "terraform", "apply", "-refresh-only", "-auto-approve", "-input=false", "-lock=false", "-json")
cmd.Dir = w.dir
w.configureCmd(cmd)
out, err := cmd.CombinedOutput()
w.logger.Debug("refresh ended", "out", string(out))
if err != nil {
Expand Down Expand Up @@ -256,7 +257,7 @@ func (w *Workspace) Plan(ctx context.Context) (PlanResult, error) {
return PlanResult{}, errors.Errorf("%s operation that started at %s is still running", w.LastOperation.Type, w.LastOperation.StartTime().String())
}
cmd := exec.CommandContext(ctx, "terraform", "plan", "-refresh=false", "-input=false", "-lock=false", "-json")
cmd.Dir = w.dir
w.configureCmd(cmd)
out, err := cmd.CombinedOutput()
w.logger.Debug("plan ended", "out", string(out))
if err != nil {
Expand Down Expand Up @@ -287,3 +288,11 @@ func (w *Workspace) Plan(ctx context.Context) (PlanResult, error) {
UpToDate: p.Changes.Change == 0,
}, nil
}

func (w *Workspace) configureCmd(cmd *exec.Cmd) {
if cmd.Env == nil {
cmd.Env = os.Environ()
}
cmd.Env = append(cmd.Env, w.env...)
cmd.Dir = w.dir
}

0 comments on commit 57f7df5

Please sign in to comment.