diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index 8eb91073..91a75fa4 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -1,4 +1,6 @@ ### Improvements +- Propagate current and root env name to providers. + [#264](https://github.com/pulumi/esc/pull/264) ### Bug Fixes diff --git a/analysis/common_test.go b/analysis/common_test.go index fe294bf6..527b80b7 100644 --- a/analysis/common_test.go +++ b/analysis/common_test.go @@ -57,7 +57,7 @@ func (testProvider) Schema() (*schema.Schema, *schema.Schema) { return testProviderSchema, schema.Always() } -func (testProvider) Open(ctx context.Context, inputs map[string]esc.Value, context map[string]esc.Value) (esc.Value, error) { +func (testProvider) Open(ctx context.Context, inputs map[string]esc.Value, context esc.EnvExecContext) (esc.Value, error) { return esc.NewValue(inputs), nil } diff --git a/cmd/esc/cli/cli_test.go b/cmd/esc/cli/cli_test.go index 166a367e..3a327689 100644 --- a/cmd/esc/cli/cli_test.go +++ b/cmd/esc/cli/cli_test.go @@ -198,7 +198,7 @@ func (testProvider) Schema() (*schema.Schema, *schema.Schema) { return schema.Always(), schema.Always() } -func (testProvider) Open(ctx context.Context, inputs map[string]esc.Value, context map[string]esc.Value) (esc.Value, error) { +func (testProvider) Open(ctx context.Context, inputs map[string]esc.Value, context esc.EnvExecContext) (esc.Value, error) { return esc.NewValue(inputs), nil } diff --git a/environment.go b/environment.go index 5482ad69..0c24bc81 100644 --- a/environment.go +++ b/environment.go @@ -24,9 +24,56 @@ import ( const AnonymousEnvironmentName = "" +type EnvExecContext interface { + // Returns the current execution context values + Values() map[string]Value + + // Returns the root evaluated environment. + // For anonymous environments, it resolves to the "rootest" non anonymous environment. + GetRootEnvironmentName() string + + // Returns the current environment being evaluated. + GetCurrentEnvironmentName() string +} + type ExecContext struct { - rootEnvironment string - values map[string]Value + rootEnvironment string + currentEnvironment string + values map[string]Value +} + +func (ec *ExecContext) CopyForEnv(envName string) *ExecContext { + values := copyContext(ec.values) + values["currentEnvironment"] = NewValue(map[string]Value{ + "name": NewValue(envName), + }) + + root := ec.rootEnvironment + if ec.rootEnvironment == AnonymousEnvironmentName || ec.rootEnvironment == "" { + root = envName + } + + values["rootEnvironment"] = NewValue(map[string]Value{ + "name": NewValue(root), + }) + + return &ExecContext{ + values: values, + rootEnvironment: root, + currentEnvironment: envName, + } +} + +func (ec *ExecContext) Values() map[string]Value { + return ec.values +} + +func (ec *ExecContext) GetRootEnvironmentName() string { + return ec.rootEnvironment +} + +func (ec *ExecContext) GetCurrentEnvironmentName() string { + return ec.currentEnvironment } type copier struct { @@ -83,31 +130,6 @@ func copyContext(context map[string]Value) map[string]Value { return newContext } -func (ec *ExecContext) CopyForEnv(envName string) *ExecContext { - values := copyContext(ec.values) - values["currentEnvironment"] = NewValue(map[string]Value{ - "name": NewValue(envName), - }) - - root := ec.rootEnvironment - if ec.rootEnvironment == AnonymousEnvironmentName || ec.rootEnvironment == "" { - root = envName - } - - values["rootEnvironment"] = NewValue(map[string]Value{ - "name": NewValue(root), - }) - - return &ExecContext{ - values: values, - rootEnvironment: root, - } -} - -func (ec *ExecContext) Values() map[string]Value { - return ec.values -} - var ErrReservedContextkey = errors.New("reserved context key") func validateContextVariable(context map[string]Value, key string) error { diff --git a/eval/eval.go b/eval/eval.go index 8d262740..4117c7b8 100644 --- a/eval/eval.go +++ b/eval/eval.go @@ -903,7 +903,7 @@ func (e *evalContext) evaluateBuiltinOpen(x *expr, repr *openExpr) *value { return v } - output, err := provider.Open(e.ctx, inputs.export("").Value.(map[string]esc.Value), e.execContext.Values()) + output, err := provider.Open(e.ctx, inputs.export("").Value.(map[string]esc.Value), e.execContext) if err != nil { e.errorf(repr.syntax(), err.Error()) v.unknown = true diff --git a/eval/eval_test.go b/eval/eval_test.go index f70db477..8d403661 100644 --- a/eval/eval_test.go +++ b/eval/eval_test.go @@ -43,7 +43,7 @@ func (errorProvider) Schema() (*schema.Schema, *schema.Schema) { return schema.Record(map[string]schema.Builder{"why": schema.String()}).Schema(), schema.Always() } -func (errorProvider) Open(ctx context.Context, inputs map[string]esc.Value, context map[string]esc.Value) (esc.Value, error) { +func (errorProvider) Open(ctx context.Context, inputs map[string]esc.Value, context esc.EnvExecContext) (esc.Value, error) { return esc.Value{}, errors.New(inputs["why"].Value.(string)) } @@ -106,7 +106,7 @@ func (testSchemaProvider) Schema() (*schema.Schema, *schema.Schema) { return s, s } -func (testSchemaProvider) Open(ctx context.Context, inputs map[string]esc.Value, context map[string]esc.Value) (esc.Value, error) { +func (testSchemaProvider) Open(ctx context.Context, inputs map[string]esc.Value, context esc.EnvExecContext) (esc.Value, error) { return esc.NewValue(inputs), nil } @@ -116,7 +116,7 @@ func (testProvider) Schema() (*schema.Schema, *schema.Schema) { return schema.Always(), schema.Always() } -func (testProvider) Open(ctx context.Context, inputs map[string]esc.Value, context map[string]esc.Value) (esc.Value, error) { +func (testProvider) Open(ctx context.Context, inputs map[string]esc.Value, context esc.EnvExecContext) (esc.Value, error) { return esc.NewValue(inputs), nil } diff --git a/provider.go b/provider.go index 3b626b66..153cbaa2 100644 --- a/provider.go +++ b/provider.go @@ -27,5 +27,5 @@ type Provider interface { Schema() (inputs, outputs *schema.Schema) // Open retrieves the provider's secrets. - Open(ctx context.Context, inputs map[string]Value, executionContext map[string]Value) (Value, error) + Open(ctx context.Context, inputs map[string]Value, executionContext EnvExecContext) (Value, error) }