pulumi / pulumi-terraform-bridge

A library allowing Terraform providers to be bridged into Pulumi.
Apache License 2.0
194 stars 43 forks source link

Configuration via Nested Environment Variables #423

Open muhlba91 opened 2 years ago

muhlba91 commented 2 years ago

Hello!

Issue details

Moving from https://github.com/muhlba91/pulumi-proxmoxve/issues/2 to here, as requested.

The Proxmox Terraform provider expects nested configuration or environment variables. However, setting environment variables does not work and we get a configuration error from the Terraform provider (https://github.com/bpg/terraform-provider-proxmox/blob/main/proxmoxtf/provider.go#L210).

Hence, we need to work-around like this:

const provider = new proxmox.Provider('proxmoxve', {
  virtualEnvironment: {
    endpoint: process.env.PROXMOX_VE_ENDPOINT,
    insecure: process.env.PROXMOX_VE_INSECURE,
    username: process.env.PROXMOX_VE_USERNAME,
    password: process.env.PROXMOX_VE_PASSWORD
  }
});

It would be great if we can pass through environment variables configuration to the provider.

Steps to reproduce

First, the terraform provider defines it's configuration schema here: https://github.com/bpg/terraform-provider-proxmox/blob/main/proxmoxtf/provider.go#L71 - as you can see it generates a nested schema with - for example - those environment variables:

Those I define in my environment variables like:

export PROXMOX_VE_USERNAME="root@pam"
export PROXMOX_VE_PASSWORD="<password>"
export PROXMOX_VE_ENDPOINT="https://<ip_address>:8006/"
export PROXMOX_VE_INSECURE="true"

Based on the schema generated by the terraform bridge, the configuration is picked up correctly. See https://github.com/muhlba91/pulumi-proxmoxve/blob/main/provider/cmd/pulumi-resource-proxmoxve/schema.json#L1474 for the nesting, and https://github.com/muhlba91/pulumi-proxmoxve/blob/main/provider/cmd/pulumi-resource-proxmoxve/schema.json#L1395 for the nested schema.

If I now execute Pulumi using this provider, I receive error: You must specify the virtual environment details in the provider configuration (defined here: https://github.com/bpg/terraform-provider-proxmox/blob/main/proxmoxtf/provider.go#L210).

Hence, my conclusion that the environment variables are not passed through to the provider configuration properly. To me it looks like the nested configuration for the provider is initialised (empty) and passed, leading to the terraform provider not calling the initialisation functions (default functions for each variable).

Then I thought I can simply define the configuration in the resources.go file like:

Config: map[string]*tfbridge.SchemaInfo{
            "virtualEnvironment": {
                Type: makeType("virtualEnvironment", "VirtualEnvironment"),
                Elem: &tfbridge.SchemaInfo{
                    Fields: map[string]*tfbridge.SchemaInfo{
                        "endpoint": &tfbridge.SchemaInfo{
                            Type: makeType("endpoint", "Endpoint"),
                            Default: &tfbridge.DefaultInfo{
                                EnvVars: []string{"PROXMOX_VE_ENDPOINT", "PM_VE_ENDPOINT"},
                            },
                        },
                        "insecure": &tfbridge.SchemaInfo{
                            Type: makeType("insecure", "Insecure"),
                            Default: &tfbridge.DefaultInfo{
                                EnvVars: []string{"PROXMOX_VE_INSECURE", "PM_VE_INSECURE"},
                            },
                        },
                        "otp": &tfbridge.SchemaInfo{
                            Type: makeType("otp", "OTP"),
                            Default: &tfbridge.DefaultInfo{
                                EnvVars: []string{"PROXMOX_VE_OTP", "PM_VE_OTP"},
                            },
                        },
                        "password": &tfbridge.SchemaInfo{
                            Type: makeType("password", "Password"),
                            Default: &tfbridge.DefaultInfo{
                                EnvVars: []string{"PROXMOX_VE_PASSWORD", "PM_VE_PASSWORD"},
                            },
                        },
                        "username": &tfbridge.SchemaInfo{
                            Type: makeType("username", "Username"),
                            Default: &tfbridge.DefaultInfo{
                                EnvVars: []string{"PROXMOX_VE_USERNAME", "PM_VE_USERNAME"},
                            },
                        },
                    },
                },
            },
        },

Apart from the fact that I would expect it to work without such addition, I think I did something wrong here since I receive the following error when using the plugin now error: could not load plugin for proxmoxve provider 'urn:pulumi:dev::pve-test::pulumi:providers:proxmoxve::default_0_0_1_alpha_1637516154_bcc93c62_dirty': could not read plugin [/Users/daniel/go/bin/pulumi-resource-proxmoxve] stdout: EOF.

Expected: The bridge takes the environment variables and maps them correctly. Actual: The bridge ignores the environment variables and/or doesn't map them to the nested configuration.

stack72 commented 2 years ago

Hi @muhlba91

So my apologies for this - we should be picking the Env Vars up automatically but if that isn't working then in your provider/resources.go, you should be able to take advantage of the Config block to be able to do this:

Config: map[string]*tfbridge.SchemaInfo{
            "region": {
                Default: &tfbridge.DefaultInfo{
                    EnvVars: []string{"AWS_REGION", "AWS_DEFAULT_REGION"},
                },
            },
            "profile": {
                Default: &tfbridge.DefaultInfo{
                    EnvVars: []string{"AWS_PROFILE"},
                },
            },
        },

That will allow you to be able to define the mapping between the TF schema for the provider and the Pulumi schema

Please let me know how that works out. I will continue trying to figure out why Env Vars are not working for nested config as this should be the case

Paul

muhlba91 commented 2 years ago

Hi,

sorry for getting back so late. I posted my excerpt of the Config property above already, which ended up throwing a could not read plugin [/Users/daniel/go/bin/pulumi-resource-proxmoxve] stdout: EOF error. Did I miss anything in my definition?