Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate current and root env name to providers #264

Merged
merged 4 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
### Improvements

- Propagate current and root env name to providers.
[#264](https://github.com/pulumi/esc/pull/264)

### Bug Fixes
2 changes: 1 addition & 1 deletion analysis/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/esc/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
76 changes: 49 additions & 27 deletions environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,56 @@ import (

const AnonymousEnvironmentName = "<yaml>"

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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading