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

[codegen/python] - Implement dynamic config-getters #7447

Merged
merged 11 commits into from
Jul 14, 2021

Conversation

komalali
Copy link
Member

@komalali komalali commented Jul 7, 2021

Description

This PR implements dynamic provider config getters for python. To avoid a breaking change, we define a class that extends the ModuleType type and implements property getters for each config key. We then overwrite the __class__ attribute of the current module as described in the proposal for PEP-549.

To continue to get autocomplete for the config keys, we now emit a __init__.pyi stub file in the config module which declares the variables and associated types.

I have added tests for the codegen side of things, but I'm struggling to figure out how to test the functionality in isolation. I used the following Automation API script to locally validate the result, but it requires a locally built pulumi_aws based on the codegen in this PR.

import pulumi
from pulumi import automation as auto
import pulumi_aws as aws

def pulumi_program():
    pulumi.export("region", aws.config.region)

project_name = "dynamic_config_test"
stack_name = "dev"
stack = auto.create_or_select_stack(stack_name=stack_name,
                                    project_name=project_name,
                                    program=pulumi_program)

stack.workspace.install_plugin("aws", "v4.11.0")

stack.set_config("aws:region", auto.ConfigValue(value="us-west-1"))
up_res = stack.up()
assert up_res.outputs["region"].value == "us-west-1"

stack.set_config("aws:region", auto.ConfigValue(value="us-east-1"))
up_res = stack.up()
assert up_res.outputs["region"].value == "us-east-1"

stack.destroy()

Python part of #5582

Notable Changes

Checklist

  • I have added tests that prove my fix is effective or that my feature works
  • Yes, there are changes in this PR that warrants bumping the Pulumi Service API version

@komalali komalali force-pushed the komalali/python-config-codegen branch 2 times, most recently from af138f5 to 465811f Compare July 7, 2021 19:21
@github-actions
Copy link

github-actions bot commented Jul 7, 2021

Diff for pulumi-random with merge commit 01d5fa1

@github-actions
Copy link

github-actions bot commented Jul 7, 2021

Diff for pulumi-azuread with merge commit 01d5fa1

@github-actions
Copy link

github-actions bot commented Jul 7, 2021

Diff for pulumi-random with merge commit 1c8a51e

@github-actions
Copy link

github-actions bot commented Jul 7, 2021

Diff for pulumi-azuread with merge commit 1c8a51e

@github-actions
Copy link

github-actions bot commented Jul 7, 2021

Diff for pulumi-gcp with merge commit 01d5fa1

@github-actions
Copy link

github-actions bot commented Jul 7, 2021

Diff for pulumi-gcp with merge commit 1c8a51e

@github-actions
Copy link

github-actions bot commented Jul 7, 2021

Diff for pulumi-azure with merge commit 1c8a51e

@github-actions
Copy link

github-actions bot commented Jul 7, 2021

Diff for pulumi-aws with merge commit 01d5fa1

@github-actions
Copy link

github-actions bot commented Jul 7, 2021

Diff for pulumi-aws with merge commit 1c8a51e

@komalali komalali force-pushed the komalali/python-config-codegen branch from 465811f to 0bcd069 Compare July 8, 2021 05:10
@github-actions
Copy link

github-actions bot commented Jul 8, 2021

Diff for pulumi-random with merge commit 2797193

@komalali komalali force-pushed the komalali/python-config-codegen branch from fa80cbb to 9156c3a Compare July 9, 2021 00:43
@github-actions
Copy link

github-actions bot commented Jul 9, 2021

Diff for pulumi-random with merge commit b58eea8

@github-actions
Copy link

github-actions bot commented Jul 9, 2021

Diff for pulumi-random with merge commit a502b1e

@komalali komalali marked this pull request as ready for review July 9, 2021 03:11
@komalali komalali force-pushed the komalali/python-config-codegen branch from df1bd26 to 8dc80bb Compare July 9, 2021 16:49
@github-actions
Copy link

github-actions bot commented Jul 9, 2021

Diff for pulumi-random with merge commit 7b1a923

@github-actions
Copy link

github-actions bot commented Jul 9, 2021

Diff for pulumi-random with merge commit 63887d3

Copy link
Member

@justinvp justinvp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two initial comments after taking a quick look

import sys
from .vars import _ExportableConfig

sys.modules[__name__].__class__ = _ExportableConfig
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤯

@t0yv0
Copy link
Member

t0yv0 commented Jul 9, 2021

I feel a little lost situating what this is for, do we have a ticket list? I'll try to read again next week with a fresh head. By the implementation of it, it sounds like schema can define config variables, types and defaults, and the PR makes these "type-safely" (I'd say statically) available to the autogenerated Python resource provider code. What's tripping me up is "dynamic", use of auto API, and what exactly are the bw-compat concerns.

@komalali
Copy link
Member Author

komalali commented Jul 9, 2021

@t0yv0 this is for the python part of #5582 - sorry, I had that in the PR description but I guess it got dropped at some point. The problem is defined pretty well in the issue, let me know if you need any more information.

The issue that @justinvp brought up about the types is somewhat orthogonal to the specific problem that this PR is solving but it came up because I tried to emit the output types and they are incorrect.

@github-actions
Copy link

Diff for pulumi-random with merge commit f107b64

@komalali komalali force-pushed the komalali/python-config-codegen branch from 6bfb0ec to 7e1a638 Compare July 12, 2021 21:23
@github-actions
Copy link

Diff for pulumi-random with merge commit 622c097

@komalali komalali requested a review from justinvp July 13, 2021 18:08
Copy link
Member

@justinvp justinvp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM otherwise.

It might be worth adding a comment that explains briefly why we're generating the _ExportableConfig and using sys.modules[__name__].__class__ = _ExportableConfig, and why we're generating the stub file.

pkg/codegen/python/gen.go Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants