https://pulumi.com logo
#general
Title
# general
g

great-sunset-355

07/28/2021, 3:42 PM
Is it possible to deserialize
stackReference
into
pydantic
object?
stack core
Copy code
import pulumi
import pulumi_aws.route53 as route53
my_zone = route53.Zone(
    "my-zone",
    comment="",
    force_destroy=False,
    name="<http://my-zone.domain.com|my-zone.domain.com>",
    opts=pulumi.ResourceOptions(protect=True),
)

pulumi.export("my_zone", my_zone)
stack app/dev
Copy code
import pulumi
from pydantic import BaseModel


class MyZone(BaseModel):
    zone_id: str
    force_destroy: bool


ref_zone = pulumi.StackReference('jan/proj/core')

my_zone_pd = MyZone(**ref_zone.require_output('my_zone'))  # this is a problem
is there any trick on how to assign variables during
Output.apply()
?
Is this the right way to go about it?
Copy code
stack_name = 'jan/proj/core'
class MyZone(BaseModel):
    zone_id: str
    force_destroy: bool


async def fun():
    ref_zone = pulumi.StackReference(stack_name)
    print('lol')
    result = await ref_zone.outputs.future()
    print(result)
    my_zone = MyZone.parse_obj(result['my_zone'])
    print(my_zone)

   # ... contiune creating my resources here

loop = asyncio.get_running_loop()
loop.create_task(fun())
@billowy-army-68599 maybe you'd know?
b

billowy-army-68599

07/28/2021, 4:46 PM
no I don't think it's going to work because it's not known at runtime
r

red-match-15116

07/28/2021, 4:46 PM
Does something like this work?
Copy code
def create_ref_zone_pd(ref_zone):
    return MyZone(**ref_zone)

my_zone_pd = ref_zone.require_output.apply(create_ref_zone_pd)
g

great-sunset-355

07/28/2021, 4:55 PM
@red-match-15116 Hmm this would basically put me in the bucket of creating resources inside
apply
because anything coming out of
apply
is an Output so I cannot use
my_zone_pd.zone_id
r

red-match-15116

07/28/2021, 5:00 PM
Do you mean you can’t use
my_zone_pd.zone_id
as an input to another resource? Because you can use an
Output
as an
Input
(since
Input
is the union of
Output[T]
and
T
)
g

great-sunset-355

07/28/2021, 5:02 PM
The problem with that is that I do get an Output not a convenient object I created
@billowy-army-68599 This worked, but I do not know enough about async to say why it worked
Copy code
async def fun():
    ref_zone = pulumi.StackReference(stack_name)
    print('lol')
    result = await ref_zone.outputs.future()
    print(result)
    my_zone = MyZone.parse_obj(result['my_zone'])
    print(my_zone)
    ec2.SecurityGroup(
        f"mhh{my_zone.zone_id}"
    )

loop = asyncio.get_running_loop()
loop.create_task(fun())
3 Views