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

Automatically unmarshal complex configuration types #171

Open
blampe opened this issue Dec 20, 2023 · 0 comments · May be fixed by pulumi/pulumi#15032
Open

Automatically unmarshal complex configuration types #171

blampe opened this issue Dec 20, 2023 · 0 comments · May be fixed by pulumi/pulumi#15032
Labels
kind/enhancement Improvements or new features

Comments

@blampe
Copy link
Contributor

blampe commented Dec 20, 2023

Non-primitive config fields are automatically JSON.Stringify-d when sent to the server:

https://github.com/pulumi/pulumi/blob/f500bef2fa732aab23c644dc07ed9b101a9cfaaf/pkg/codegen/nodejs/gen.go#L794-L797

				// provider properties must be marshaled as JSON strings.
				if r.IsProvider && !isStringType(prop.Type) {
					arg = fmt.Sprintf("pulumi.output(%s).apply(JSON.stringify)", arg)
				}

However we aren't unmarshaling them on the receiving end. A type like this results in an error:

package foo

type Config struct {
   Foo []string `pulumi:"foo"`
}
error: ... provider returned an unexpected error from Configure: unknown mapper error: Field 'foo' on 'foo.Config' must be a '[]string'; got 'string' instead

The bridge seems to handle this here:

https://github.com/pulumi/pulumi-terraform-bridge/blob/0f7ccfdef31d3fddd4eb46c9770d460bbfbe81e3/pkg/tfbridge/config_encoding.go#L150C11-L174

    var jsonString string
    var jsonStringDetected, jsonStringSecret bool


    if pv.IsString() {
        jsonString = pv.StringValue()
        jsonStringDetected = true
    }


    if opts.KeepSecrets && pv.IsSecret() && pv.SecretValue().Element.IsString() {
        jsonString = pv.SecretValue().Element.StringValue()
        jsonStringDetected = true
        jsonStringSecret = true
    }


    if jsonStringDetected {
        v, err := enc.convertStringToPropertyValue(jsonString, shimType)
        if err != nil {
            return nil, fmt.Errorf("error unmarshalling property %q: %w", key, err)
        }
        if jsonStringSecret {
            s := resource.MakeSecret(v)
            return &s, nil
        }
        return &v, nil
    }

I think this is essentially the issue:

func (p *provider) Configure(ctx context.Context, req *rpc.ConfigureRequest) (*rpc.ConfigureResponse, error) {
argMap, err := p.getMap(req.GetArgs())

We unmarshal the provider's config the same way we unmarshal properties, but this JSON behavior is unique to config.

@blampe blampe added kind/enhancement Improvements or new features needs-triage Needs attention from the triage team labels Dec 20, 2023
@blampe blampe changed the title Automatically unmarshal complex types Automatically unmarshal complex configuration types Dec 20, 2023
@mjeffryes mjeffryes removed the needs-triage Needs attention from the triage team label Dec 21, 2023
@blampe blampe linked a pull request Jan 12, 2024 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement Improvements or new features
Projects
None yet
2 participants