How do people deal with inputs to custom resources...
# python
a
How do people deal with inputs to custom resources that can either be plain values or outputs from other resources? I basically have a small function
Copy code
T = TypeVar("T")


def resolve(v: T | pulumi.Output[T]) -> T:
    if not isinstance(v, pulumi.Output):
        return v
    else:
        return v.apply(lambda x: x)
that I use on all inputs. But that is kinda tedious and at the same time seems like something that should exist in the library as I'm under the impression that this is not an uncommon use case.
l
The code looks a bit off (the apply just returns another Output), but it sounds like you might be looking for something like Output.from_input().
a
> the apply just returns another Output Yeah, you are right. My mistake above Actually I'm doing
Copy code
plain = None

def fn(vv):
    nonlocal plain
    plain = v

v.apply(fn)
return plain
What I'm trying to do is to use the plain value in a custom provider that I have written. I'm not sure what it would help there to cast everything into an
Output
? Isn't that getting me the opposite of what I need?
Maybe I don't fully understand inputs and outputs 🤷
l
I don't think what you're trying is possible. The Output usually resolves into a value at some point in the future, so they real values are not available when the code first runs. You can think of the .apply as registering a callback for when that happens. In your new example I'd expect the final return value to still be
None
, since the callback wont be triggered until the output
v
resolves in the future.
If you need the resolved values, you can wrap your code with Output.all() and it will be called when all the output values are available.
Not the best explanation, but I hope that it makes at least some sense.
a
But it has to work somehow. Assume I do something like this
Copy code
a = FooResource()
b = BarResource(foo_id=a.id)
c = BarResource(foo_id=12345)
Here
BarResource
needs to be able to handle outputs as well as plain values at the same time
And regardless of how it does that,
b
can only be created when
a.id
is resolved