pulumi/tests/integration/partial_values/python/__main__.py

37 lines
1,013 B
Python
Raw Normal View History

Propagate inputs to outputs during preview. (#3327) These changes restore a more-correct version of the behavior that was disabled with #3014. The original implementation of this behavior was done in the SDKs, which do not have access to the complete inputs for a resource (in particular, default values filled in by the provider during `Check` are not exposed to the SDK). This lack of information meant that the resolved output values could disagree with the typings present in a provider SDK. Exacerbating this problem was the fact that unknown values were dropped entirely, causing `undefined` values to appear in unexpected places. By doing this in the engine and allowing unknown values to be represented in a first-class manner in the SDK, we can attack both of these issues. Although this behavior is not _strictly_ consistent with respect to the resource model--in an update, a resource's output properties will come from its provider and may differ from its input properties--this behavior was present in the product for a fairly long time without significant issues. In the future, we may be able to improve the accuracy of resource outputs during a preview by allowing the provider to dry-run CRUD operations and return partially-known values where possible. These changes also introduce new APIs in the Node and Python SDKs that work with unknown values in a first-class fashion: - A new parameter to the `apply` function that indicates that the callback should be run even if the result of the apply contains unknown values - `containsUnknowns` and `isUnknown`, which return true if a value either contains nested unknown values or is exactly an unknown value - The `Unknown` type, which represents unknown values The primary use case for these APIs is to allow nested, properties with known values to be accessed via the lifted property accessor even when the containing property is not fully know. A common example of this pattern is the `metadata.name` property of a Kubernetes `Namespace` object: while other properties of the `metadata` bag may be unknown, `name` is often known. These APIs allow `ns.metadata.name` to return a known value in this case. In order to avoid exposing downlevel SDKs to unknown values--a change which could break user code by exposing it to unexpected values--a language SDK must indicate whether or not it supports first-class unknown values as part of each `RegisterResourceRequest`. These changes also allow us to avoid breaking user code with the new behavior introduced by the prior commit. Fixes #3190.
2019-11-11 21:09:34 +01:00
# Copyright 2016-2018, Pulumi Corporation. All rights reserved.
import asyncio
from pulumi import Output, export, UNKNOWN
from pulumi.dynamic import Resource, ResourceProvider, CreateResult
from pulumi.runtime import is_dry_run
class MyProvider(ResourceProvider):
def create(self, props):
return CreateResult("0", props)
class MyResource(Resource):
foo: Output
bar: Output
baz: Output
def __init__(self, name, props, opts = None):
super().__init__(MyProvider(), name, props, opts)
unknown = Output.from_input(UNKNOWN if is_dry_run() else "foo")
a = MyResource("a", {
"foo": "foo",
"bar": { "value": "foo", "unknown": unknown },
"baz": [ "foo", unknown ],
})
async def check_knowns():
assert await a.foo.is_known()
assert await a.bar["value"].is_known()
assert await a.bar["unknown"].is_known() != is_dry_run()
assert await a.baz[0].is_known()
assert await a.baz[1].is_known() != is_dry_run()
print("ok")
export("o", check_knowns())