https://pulumi.com logo
#python
Title
# python
l

little-river-49422

04/02/2019, 12:21 PM
if that is true, how do I create stuff that's outside of pulumi from resource outputs?
i

incalculable-sundown-82514

04/02/2019, 5:19 PM
You can use
output.apply
anywhere, as long as the promise that gets returned is awaited by something (a resource or by exporting using
pulumi.export
)
l

little-river-49422

04/02/2019, 8:49 PM
so, ugh, i'd need to explicitly export this resource output? else I wont be able to use it in my custom code?
i

incalculable-sundown-82514

04/02/2019, 8:53 PM
what do you mean “custom code”? in another process, or within the same Pulumi process?
l

little-river-49422

04/02/2019, 9:06 PM
in the same process. but my own code, not pulumi resource
just call my own function, right now I'm just doing async\await
and it fails on the first time, because parent resource is not there
i

incalculable-sundown-82514

04/02/2019, 9:07 PM
can you post your code?
l

little-river-49422

04/02/2019, 9:07 PM
Copy code
if not pulumi.runtime.is_dry_run():
            asyncio.ensure_future(create_sql_database_from_existing())
Copy code
async def create_sql_database_from_existing():
    sql_client = client_factory('SqlManagementClient')
    async_db_create = await sql_client.databases.create_or_update(
        gen_name(BRANCH),
        gen_name(BRANCH),
        'TslTest',
        {
            'location': LOCATION,
            'create_mode': 'Copy',
            'sourceDatabaseId': DBID
        }
    )
    async_db_create.result()
so i'd need to reference sql_server.name in this function
to await for sql server created in the same config (ideally)
i

incalculable-sundown-82514

04/02/2019, 9:08 PM
you would, yeah; you can use
apply
for this. does that not work?
l

little-river-49422

04/02/2019, 9:09 PM
no, it says cannot find server <pulumi.Output.output at xxxxxx>
hm, let me try again
i

incalculable-sundown-82514

04/02/2019, 9:09 PM
can I see your code that does the apply?
l

little-river-49422

04/02/2019, 9:14 PM
Copy code
sql_server.name.apply(lambda name: name),
replaced one of the gen_name(BRANCH) with it
here's the error:
Copy code
Can not perform requested operation on nested resource. Parent resource '<pulumi.output.Output object at 0x7f3aac273860>' not found.
i

incalculable-sundown-82514

04/02/2019, 9:14 PM
right -
apply
returns something of type
Output
if you want to do something with the actual value of name, you need to do it inside the lambda
with the “name” parameter
l

little-river-49422

04/02/2019, 9:15 PM
ah, like
"{}".format(name)
?
i

incalculable-sundown-82514

04/02/2019, 9:15 PM
or just
name
, it’s type is “string”
but yeah
you have to do your operations on
name
inside the lambda
l

little-river-49422

04/02/2019, 9:16 PM
ok, i dont seem to understand
that didnt work
sql_server.name.apply(lambda name: "{0}".format(name)),
i

incalculable-sundown-82514

04/02/2019, 9:16 PM
you need to call sql_client.databases.create_or_update inside the lambda
l

little-river-49422

04/02/2019, 9:16 PM
oh, the whole thing?
i

incalculable-sundown-82514

04/02/2019, 9:17 PM
yeah
that is the only time you are allowed to observe the actual value of
name
l

little-river-49422

04/02/2019, 9:17 PM
and it** can be not async?
i

incalculable-sundown-82514

04/02/2019, 9:17 PM
it can be, if you want
l

little-river-49422

04/02/2019, 9:17 PM
but doesnt have to?
i

incalculable-sundown-82514

04/02/2019, 9:17 PM
you can call
apply
with an async function
you can also call it with a non-async function
both work fine
l

little-river-49422

04/02/2019, 9:17 PM
or wait, it will block, so i still need async
or wait. will it work in parallel like all the other resources?
i

incalculable-sundown-82514

04/02/2019, 9:18 PM
it will not
it will block the whole program
l

little-river-49422

04/02/2019, 9:18 PM
ok, i'll leave it as async
i

incalculable-sundown-82514

04/02/2019, 9:18 PM
if you do it asynchronously, it will proceed in parallel with the rest of the program
l

little-river-49422

04/02/2019, 9:18 PM
let me try, thank you
ok, that works flawlessly, thanks
i

incalculable-sundown-82514

04/02/2019, 9:32 PM
no problem!
l

little-river-49422

04/02/2019, 9:32 PM
Copy code
await sql_server.name.apply(lambda name: sql_client.databases.create_or_update(
            gen_name(BRANCH),
            name,
            'TslTest',
            {
                'location': LOCATION,
                'create_mode': 'Copy',
                'sourceDatabaseId': DBID
            }
        )
    )
p

proud-artist-4864

04/03/2019, 6:10 AM
ahhh! The light dawns… OK, so I can call one of my functions within the async apply 🙂
partypus 1
so I’m exploring this. My main has the following code: if name == “__main__“:
oops, try again: async def main(): # do pulumi stuff if name = “__main__“: loop = asyncio.get_event_loop() asyncio.ensure_future(main(),loop=loop)
but main() doesn’t appear to be called and there’s no error from pulumi and only the Stack is created
So I’m back to being confused 😄
l

little-river-49422

04/03/2019, 8:05 AM
i dont think you need async around pulumi stuff?
for me, i'm just calling a function using asyncio.ensure_future
without creating a loop before hand
and I'm using await inside the function i'm calling
p

proud-artist-4864

04/03/2019, 8:09 AM
hmm, will try that
hmm, my first await inside the function works (it’s a call to get_vpc), but subsequent awaits don’t and the processing just ends, I’m wondering whetrher I need to gather all the awaitables and wait for all of them 🙂
I guess I’m confused about how the ensure_future which is run inside the Stack just seems to stop executing at a particular point…
l

little-river-49422

04/03/2019, 8:52 AM
yeah, i'm not a python developer, have basically zero understanding about async in python
p

proud-artist-4864

04/03/2019, 10:03 AM
same 🙂