Skip to content

Commit

Permalink
feat: Pass PULUMI_STACK, PULUMI_PROJECT, PULUMI_ORGANIZATION an…
Browse files Browse the repository at this point in the history
…d `PULUMI_CONFIG` as environment variable to compiler process
  • Loading branch information
phramz committed Jun 3, 2024
1 parent 67e7225 commit 1408c11
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
### Improvements

- Pass `PULUMI_STACK`, `PULUMI_ORGANIZATION`, `PULUMI_PROJECT` and `PULUMI_CONFIG` as environment variable to compiler process.

### Bug Fixes
2 changes: 1 addition & 1 deletion pkg/pulumiyaml/codegen/eject.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func LoadTemplate(dir string) (*workspace.Project, *ast.TemplateDecl, hcl.Diagno
if !ok {
return nil, nil, nil, fmt.Errorf("compiler option must be a string, got %v", reflect.TypeOf(compilerOpt))
}
t, diags, err = pulumiyaml.LoadFromCompiler(compiler, main)
t, diags, err = pulumiyaml.LoadFromCompiler(compiler, main, nil)
} else {
t, diags, err = pulumiyaml.LoadDir(main)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/pulumiyaml/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Load() (*ast.TemplateDecl, syntax.Diagnostics, error) {
return LoadDir(".")
}

func LoadFromCompiler(compiler string, workingDirectory string) (*ast.TemplateDecl, syntax.Diagnostics, error) {
func LoadFromCompiler(compiler string, workingDirectory string, env []string) (*ast.TemplateDecl, syntax.Diagnostics, error) {
var diags syntax.Diagnostics
var stdout bytes.Buffer
var stderr bytes.Buffer
Expand All @@ -60,6 +60,7 @@ func LoadFromCompiler(compiler string, workingDirectory string) (*ast.TemplateDe
cmd.Dir = workingDirectory
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Env = append(os.Environ(), env...)
err = cmd.Run()
if err != nil {
return nil, nil, fmt.Errorf("error running compiler %v: %v, stderr follows: %v", name, err, stderr.String())
Expand Down
24 changes: 19 additions & 5 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package server

import (
"context"
"encoding/json"
"fmt"
"os"

pbempty "github.com/golang/protobuf/ptypes/empty"
Expand Down Expand Up @@ -53,18 +55,17 @@ func NewLanguageHost(engineAddress, tracing string, compiler string) pulumirpc.L
}
}

func (host *yamlLanguageHost) loadTemplate() (*ast.TemplateDecl, syntax.Diagnostics, error) {
func (host *yamlLanguageHost) loadTemplate(compilerEnv []string) (*ast.TemplateDecl, syntax.Diagnostics, error) {
if host.template != nil {
return host.template, host.diags, nil
}

var template *ast.TemplateDecl
var diags syntax.Diagnostics
var err error
if host.compiler == "" {
template, diags, err = pulumiyaml.Load()
} else {
template, diags, err = pulumiyaml.LoadFromCompiler(host.compiler, "")
template, diags, err = pulumiyaml.LoadFromCompiler(host.compiler, "", compilerEnv)
}
if err != nil {
return nil, diags, err
Expand All @@ -82,7 +83,7 @@ func (host *yamlLanguageHost) loadTemplate() (*ast.TemplateDecl, syntax.Diagnost
func (host *yamlLanguageHost) GetRequiredPlugins(ctx context.Context,
req *pulumirpc.GetRequiredPluginsRequest,
) (*pulumirpc.GetRequiredPluginsResponse, error) {
template, diags, err := host.loadTemplate()
template, diags, err := host.loadTemplate(nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -121,7 +122,20 @@ func (host *yamlLanguageHost) Run(ctx context.Context, req *pulumirpc.RunRequest
}
}

template, diags, err := host.loadTemplate()
configValue := req.GetConfig()
jsonConfigValue, err := json.Marshal(configValue)
if err != nil {
return nil, err
}

compilerEnv := []string{
fmt.Sprintf(`PULUMI_STACK=%s`, req.GetStack()),
fmt.Sprintf(`PULUMI_ORGANIZATION=%s`, req.GetOrganization()),
fmt.Sprintf(`PULUMI_PROJECT=%s`, req.GetProject()),
fmt.Sprintf(`PULUMI_CONFIG=%s`, jsonConfigValue),
}

template, diags, err := host.loadTemplate(compilerEnv)
if err != nil {
return &pulumirpc.RunResponse{Error: err.Error()}, nil
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,23 @@ func TestProjectConfigWithSecretDecrypted(t *testing.T) {
}
integration.ProgramTest(t, &testOptions)
}

//nolint:paralleltest // uses parallel programtest
func TestEnvVarsPassedToExecCommand(t *testing.T) {
testOptions := integration.ProgramTestOptions{
Dir: filepath.Join(getCwd(t), "testdata", "env-vars"),
Env: []string{"TEST_ENV_VAR=foobar"},
PrepareProject: prepareYamlProject,
StackName: "dev",
SecretsProvider: "default",
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
assert.Equal(t, "foo", stack.Outputs["TEST_FOO"])
assert.Equal(t, "foobar", stack.Outputs["TEST_ENV_VAR"])
assert.Equal(t, "dev", stack.Outputs["PULUMI_STACK"])
assert.Equal(t, "project-env-vars", stack.Outputs["PULUMI_PROJECT"])
assert.Equal(t, "TODO", stack.Outputs["PULUMI_ORGANIZATION"])
assert.Equal(t, "TODO", stack.Outputs["PULUMI_CONFIG"])
},
}
integration.ProgramTest(t, &testOptions)
}
15 changes: 15 additions & 0 deletions pkg/tests/testdata/env-vars/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: project-env-vars
runtime:
name: yaml
options:
compiler: |
sh -c "cat Pulumi.yaml;
echo \"\";
echo \"outputs:\";
echo \" TEST_FOO: foo\";
echo \" TEST_ENV_VAR: $TEST_ENV_VAR\";
echo \" PULUMI_STACK: $PULUMI_STACK\";
echo \" PULUMI_PROJECT: $PULUMI_PROJECT\";
echo \" PULUMI_ORGANIZATION: $PULUMI_ORGANIZATION\";
echo \" PULUMI_CONFIG: $PULUMI_CONFIG\";
echo \"\";"

0 comments on commit 1408c11

Please sign in to comment.