feat(autoapi): add workspace scoped envvars to LocalWorkspace and Stack
This commit is contained in:
parent
9ef5c72f72
commit
46da25492e
|
@ -4,6 +4,8 @@ CHANGELOG
|
|||
## HEAD (Unreleased)
|
||||
_(none)_
|
||||
|
||||
- feat(autoapi): add workspace scoped envvars to LocalWorkspace and Stack
|
||||
[#5275](https://github.com/pulumi/pulumi/pull/5275)
|
||||
- refactor(autoapi-gitrepo): use Workspace in SetupFn callback
|
||||
[#5279](https://github.com/pulumi/pulumi/pull/5279)
|
||||
|
||||
|
|
|
@ -226,6 +226,47 @@ func ExampleLocalWorkspace_GetConfig() {
|
|||
fmt.Println(cfgVal.Secret)
|
||||
}
|
||||
|
||||
func ExampleLocalWorkspace_GetEnvVars() {
|
||||
ctx := context.Background()
|
||||
// create a workspace from a local project
|
||||
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
|
||||
// get all environment values from the workspace for the stack
|
||||
e := w.GetEnvVars()
|
||||
for k, v := range e {
|
||||
fmt.Println(k)
|
||||
fmt.Println(v)
|
||||
}
|
||||
}
|
||||
|
||||
func ExampleLocalWorkspace_SetEnvVars() {
|
||||
ctx := context.Background()
|
||||
// create a workspace from a local project
|
||||
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
|
||||
// set the environment values on the workspace for the stack
|
||||
e := map[string]string{
|
||||
"FOO": "bar",
|
||||
}
|
||||
w.SetEnvVars(e)
|
||||
}
|
||||
|
||||
func ExampleLocalWorkspace_SetEnvVar() {
|
||||
ctx := context.Background()
|
||||
// create a workspace from a local project
|
||||
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
|
||||
// set the environment value on the workspace for the stack
|
||||
w.SetEnvVar("FOO", "bar")
|
||||
}
|
||||
|
||||
func ExampleLocalWorkspace_UnsetEnvVar() {
|
||||
ctx := context.Background()
|
||||
// create a workspace from a local project
|
||||
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
|
||||
// set the environment value on the workspace for the stack
|
||||
w.SetEnvVar("FOO", "bar")
|
||||
// unset the environment value on the workspace for the stack
|
||||
w.UnsetEnvVar("FOO")
|
||||
}
|
||||
|
||||
func ExampleLocalWorkspace_InstallPlugin() {
|
||||
ctx := context.Background()
|
||||
// create a workspace from a local project
|
||||
|
|
|
@ -42,6 +42,7 @@ type LocalWorkspace struct {
|
|||
workDir string
|
||||
pulumiHome string
|
||||
program pulumi.RunFunc
|
||||
envvars map[string]string
|
||||
}
|
||||
|
||||
var settingsExtensions = []string{".yaml", ".yml", ".json"}
|
||||
|
@ -244,6 +245,47 @@ func (l *LocalWorkspace) RefreshConfig(ctx context.Context, fqsn string) (Config
|
|||
return cfg, nil
|
||||
}
|
||||
|
||||
// GetEnvVars returns the environment values scoped to the current workspace.
|
||||
func (l *LocalWorkspace) GetEnvVars() map[string]string {
|
||||
if l.envvars == nil {
|
||||
return nil
|
||||
}
|
||||
return l.envvars
|
||||
}
|
||||
|
||||
// SetEnvVars sets the specified map of environment values scoped to the current workspace.
|
||||
// These values will be passed to all Workspace and Stack level commands.
|
||||
func (l *LocalWorkspace) SetEnvVars(envvars map[string]string) error {
|
||||
if envvars == nil {
|
||||
return errors.New("unable to set nil environment values")
|
||||
}
|
||||
if l.envvars == nil {
|
||||
l.envvars = map[string]string{}
|
||||
}
|
||||
for k, v := range envvars {
|
||||
l.envvars[k] = v
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetEnvVar sets the specified environment value scoped to the current workspace.
|
||||
// This value will be passed to all Workspace and Stack level commands.
|
||||
func (l *LocalWorkspace) SetEnvVar(key, value string) {
|
||||
if l.envvars == nil {
|
||||
l.envvars = map[string]string{}
|
||||
}
|
||||
l.envvars[key] = value
|
||||
}
|
||||
|
||||
// UnsetEnvVar unsets the specified environment value scoped to the current workspace.
|
||||
// This value will be removed from all Workspace and Stack level commands.
|
||||
func (l *LocalWorkspace) UnsetEnvVar(key string) {
|
||||
if l.envvars == nil {
|
||||
return
|
||||
}
|
||||
delete(l.envvars, key)
|
||||
}
|
||||
|
||||
// WorkDir returns the working directory to run Pulumi CLI commands.
|
||||
// LocalWorkspace expects that this directory contains a Pulumi.yaml file.
|
||||
// For "Inline" Pulumi programs created from NewStackInlineSource, a Pulumi.yaml
|
||||
|
@ -410,6 +452,12 @@ func (l *LocalWorkspace) runPulumiCmdSync(
|
|||
homeEnv := fmt.Sprintf("%s=%s", pulumiHomeEnv, l.PulumiHome())
|
||||
env = append(env, homeEnv)
|
||||
}
|
||||
if envvars := l.GetEnvVars(); envvars != nil {
|
||||
for k, v := range envvars {
|
||||
e := []string{k, v}
|
||||
env = append(env, strings.Join(e, "="))
|
||||
}
|
||||
}
|
||||
return runPulumiCommandSync(ctx, l.WorkDir(), env, args...)
|
||||
}
|
||||
|
||||
|
|
|
@ -67,6 +67,24 @@ func TestNewStackLocalSource(t *testing.T) {
|
|||
t.FailNow()
|
||||
}
|
||||
|
||||
// Set environment variables scoped to the workspace.
|
||||
envvars := map[string]string{
|
||||
"foo": "bar",
|
||||
"barfoo": "foobar",
|
||||
}
|
||||
err = s.Workspace().SetEnvVars(envvars)
|
||||
assert.Nil(t, err, "failed to set environment values")
|
||||
envvars = s.Workspace().GetEnvVars()
|
||||
assert.NotNil(t, envvars, "failed to get environment values after setting many")
|
||||
|
||||
s.Workspace().SetEnvVar("bar", "buzz")
|
||||
envvars = s.Workspace().GetEnvVars()
|
||||
assert.NotNil(t, envvars, "failed to get environment value after setting")
|
||||
|
||||
s.Workspace().UnsetEnvVar("bar")
|
||||
envvars = s.Workspace().GetEnvVars()
|
||||
assert.NotNil(t, envvars, "failed to get environment values after unsetting.")
|
||||
|
||||
// -- pulumi up --
|
||||
res, err := s.Up(ctx)
|
||||
if err != nil {
|
||||
|
|
|
@ -634,6 +634,12 @@ func (s *Stack) runPulumiCmdSync(ctx context.Context, args ...string) (string, s
|
|||
homeEnv := fmt.Sprintf("%s=%s", pulumiHomeEnv, s.Workspace().PulumiHome())
|
||||
env = append(env, homeEnv)
|
||||
}
|
||||
if envvars := s.Workspace().GetEnvVars(); envvars != nil {
|
||||
for k, v := range envvars {
|
||||
e := []string{k, v}
|
||||
env = append(env, strings.Join(e, "="))
|
||||
}
|
||||
}
|
||||
additionalArgs, err := s.Workspace().SerializeArgsForOp(ctx, s.Name())
|
||||
if err != nil {
|
||||
return "", "", -1, errors.Wrap(err, "failed to exec command, error getting additional args")
|
||||
|
|
|
@ -56,6 +56,17 @@ type Workspace interface {
|
|||
RemoveAllConfig(context.Context, string, []string) error
|
||||
// RefreshConfig gets and sets the config map used with the last Update for Stack matching fullyQualifiedStackName.
|
||||
RefreshConfig(context.Context, string) (ConfigMap, error)
|
||||
// GetEnvVars returns the environment values scoped to the current workspace.
|
||||
GetEnvVars() map[string]string
|
||||
// SetEnvVars sets the specified map of environment values scoped to the current workspace.
|
||||
// These values will be passed to all Workspace and Stack level commands.
|
||||
SetEnvVars(map[string]string) error
|
||||
// SetEnvVar sets the specified environment value scoped to the current workspace.
|
||||
// This value will be passed to all Workspace and Stack level commands.
|
||||
SetEnvVar(string, string)
|
||||
// UnsetEnvVar unsets the specified environment value scoped to the current workspace.
|
||||
// This value will be removed from all Workspace and Stack level commands.
|
||||
UnsetEnvVar(string)
|
||||
// WorkDir returns the working directory to run Pulumi CLI commands.
|
||||
WorkDir() string
|
||||
// PulumiHome returns the directory override for CLI metadata if set.
|
||||
|
|
Loading…
Reference in a new issue