Python Dynamic Providers (#2900)
Dynamic providers in Python.
This PR uses [dill](https://pypi.org/project/dill/) for code serialization, along with a customization to help ensure deterministic serialization results.
One notable limitation - which I believe is a general requirement of Python - is that any serialization of Python functions must serialize byte code, and byte code is not safely versioned across Python versions. So any resource created with Python `3.x.y` can only be updated by exactly the same version of Python. This is very constraining, but it's not clear there is any other option within the realm of what "dynamic providers" are as a feature. It is plausible that we could ensure that updates which only update the serialized provider can avoid calling the dynamic provider operations, so that version updates could still be accomplished. We can explore this separately.
```py
from pulumi import ComponentResource, export, Input, Output
from pulumi.dynamic import Resource, ResourceProvider, CreateResult, UpdateResult
from typing import Optional
from github import Github, GithubObject
auth = "<auth token>"
g = Github(auth)
class GithubLabelArgs(object):
owner: Input[str]
repo: Input[str]
name: Input[str]
color: Input[str]
description: Optional[Input[str]]
def __init__(self, owner, repo, name, color, description=None):
self.owner = owner
self.repo = repo
self.name = name
self.color = color
self.description = description
class GithubLabelProvider(ResourceProvider):
def create(self, props):
l = g.get_user(props["owner"]).get_repo(props["repo"]).create_label(
name=props["name"],
color=props["color"],
description=props.get("description", GithubObject.NotSet))
return CreateResult(l.name, {**props, **l.raw_data})
def update(self, id, _olds, props):
l = g.get_user(props["owner"]).get_repo(props["repo"]).get_label(id)
l.edit(name=props["name"],
color=props["color"],
description=props.get("description", GithubObject.NotSet))
return UpdateResult({**props, **l.raw_data})
def delete(self, id, props):
l = g.get_user(props["owner"]).get_repo(props["repo"]).get_label(id)
l.delete()
class GithubLabel(Resource):
name: Output[str]
color: Output[str]
url: Output[str]
description: Output[str]
def __init__(self, name, args: GithubLabelArgs, opts = None):
full_args = {'url':None, 'description':None, 'name':None, 'color':None, **vars(args)}
super().__init__(GithubLabelProvider(), name, full_args, opts)
label = GithubLabel("foo", GithubLabelArgs("lukehoban", "todo", "mylabel", "d94f0b"))
export("label_color", label.color)
export("label_url", label.url)
```
Fixes https://github.com/pulumi/pulumi/issues/2902.
2019-07-19 19:18:25 +02:00
|
|
|
#!/bin/sh
|
2020-06-10 01:42:53 +02:00
|
|
|
|
|
|
|
if [ -n "${PULUMI_RUNTIME_VIRTUALENV:-}" ] ; then
|
|
|
|
# Remove trailing slash.
|
|
|
|
PULUMI_RUNTIME_VIRTUALENV=${PULUMI_RUNTIME_VIRTUALENV%/}
|
|
|
|
|
|
|
|
# Make the path absolute (if not already).
|
|
|
|
case $PULUMI_RUNTIME_VIRTUALENV in
|
|
|
|
/*) : ;;
|
|
|
|
*) PULUMI_RUNTIME_VIRTUALENV=$PWD/$PULUMI_RUNTIME_VIRTUALENV;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
# If python exists in the virtual environment, set PATH and run it.
|
|
|
|
if [ -f "$PULUMI_RUNTIME_VIRTUALENV/bin/python" ]; then
|
|
|
|
# Update PATH and unset PYTHONHOME.
|
|
|
|
PATH="$PULUMI_RUNTIME_VIRTUALENV/bin:$PATH"
|
|
|
|
export PATH
|
|
|
|
if [ -n "${PYTHONHOME:-}" ] ; then
|
|
|
|
unset PYTHONHOME
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Run python from the virtual environment.
|
|
|
|
"$PULUMI_RUNTIME_VIRTUALENV/bin/python" -u -m pulumi.dynamic $@
|
|
|
|
else
|
|
|
|
echo "\"$PULUMI_RUNTIME_VIRTUALENV\" doesn't appear to be a virtual environment"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
# Otherwise, just run python3.
|
|
|
|
python3 -u -m pulumi.dynamic $@
|
|
|
|
fi
|