https://pulumi.com logo
Docs
Join the conversationJoin Slack
Channels
announcements
automation-api
aws
azure
blog-posts
built-with-pulumi
cloudengineering
cloudengineering-support
content-share
contribex
contribute
docs
dotnet
finops
general
getting-started
gitlab
golang
google-cloud
hackathon-03-19-2020
hacktoberfest
install
java
jobs
kubernetes
learn-pulumi-events
linen
localstack
multi-language-hackathon
office-hours
oracle-cloud-infrastructure
plugin-framework
pulumi-cdk
pulumi-crosscode
pulumi-deployments
pulumi-kubernetes-operator
pulumi-service
pulumiverse
python
registry
status
testingtesting123
testingtesting321
typescript
welcome
workshops
yaml
Powered by Linen
python
  • b

    bulky-match-1583

    05/20/2020, 5:39 PM
    So i can't use any of the outputs in the filter parameter right?
  • s

    sparse-state-34229

    05/20/2020, 5:39 PM
    i’m asking if
    type(project.project_id) == Output
  • s

    sparse-state-34229

    05/20/2020, 5:39 PM
    if that is true, i can help you
  • b

    bulky-match-1583

    05/20/2020, 5:39 PM
    sorry 🙂 yes
    😅 1
  • s

    sparse-state-34229

    05/20/2020, 5:39 PM
    okay cool
  • s

    sparse-state-34229

    05/20/2020, 5:39 PM
    you want to use
    Output.apply()
  • s

    sparse-state-34229

    05/20/2020, 5:40 PM
    so something like
  • s

    sparse-state-34229

    05/20/2020, 5:41 PM
    Output.all([project.project_id]).apply(lambda x: gcp.projects.get_project(filter=f"project_id:{x[0][0]} AND …"))
  • s

    sparse-state-34229

    05/20/2020, 5:41 PM
    you may have to play with it and figure out the exact form for what you need
  • s

    sparse-state-34229

    05/20/2020, 5:43 PM
    note
    x[0][0]
    - the argument passed to
    .apply()
    is a list (but you may only need
    x[0]
    )
  • s

    sparse-state-34229

    05/20/2020, 5:44 PM
    can’t remember which
  • s

    sparse-state-34229

    05/20/2020, 5:45 PM
    here is an example I use:
  • s

    sparse-state-34229

    05/20/2020, 5:45 PM
    def user_data(self):
            user_data_tpl = Template(BASE_USERDATA_TPL)
    
            return Output.all([self.vpc.id]).apply(
                lambda x: user_data_tpl.render(
                    dns_domain=self.vpc.dns_domain,
                    vpc_cidr=self.vpc.cidr_block,
                    vpc_id=x[0][0],
                    vpc_name=self.vpc.name,
                ).encode("utf-8")
            )
  • b

    bulky-match-1583

    05/20/2020, 5:45 PM
    thank you very much
    👍 1
  • s

    sparse-state-34229

    05/20/2020, 5:46 PM
    np!
  • s

    sparse-state-34229

    05/20/2020, 5:46 PM
    took me a while to figure it out lol
    🙂 3
  • c

    chilly-hairdresser-56259

    05/21/2020, 4:02 AM
    Does pulumi-docker for python not support image creation and push?
    f
    • 2
    • 10
  • h

    hundreds-dog-95098

    05/21/2020, 4:41 PM
    from pulumi_aws import ec2
    
    vpcs = ec2.get_vpcs(tags={
       "key": "value",
    })
    when trying to get all vpc by tags then getting exception. File “/usr/local/bin/pulumi-language-python-exec”, line 85, in <module> loop.run_until_complete(coro) File “/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py”, line 587, in run_until_complete return future.result() File “/usr/local/lib/python3.7/site-packages/pulumi/runtime/stack.py”, line 81, in run_in_stack await run_pulumi_func(lambda: Stack(func)) File “/usr/local/lib/python3.7/site-packages/pulumi/runtime/stack.py”, line 50, in run_pulumi_func await RPC_MANAGER.rpcs.pop() File “/usr/local/lib/python3.7/site-packages/pulumi/runtime/stack.py”, line 34, in run_pulumi_func func() File “/usr/local/lib/python3.7/site-packages/pulumi/runtime/stack.py”, line 81, in <lambda> await run_pulumi_func(lambda: Stack(func)) File “/usr/local/lib/python3.7/site-packages/pulumi/runtime/stack.py”, line 114, in init func() File “/usr/local/bin/pulumi-language-python-exec”, line 84, in <lambda> coro = pulumi.runtime.run_in_stack(lambda: runpy.run_path(args.PROGRAM, run_name=‘__main__‘)) File “/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py”, line 280, in run_path run_name, mod_spec, pkg_name).copy() File “/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py”, line 85, in _run_code exec(code, run_globals) File “./__main__.py”, line 15, in <module> tags=tags File “./vpc.py”, line 108, in create_network cidr_block = allocate_new_cidr(base_cidr) File “./utils.py”, line 19, in allocate_new_cidr “project”: “cspaas”, File “/usr/local/lib/python3.7/site-packages/pulumi_aws/ec2/get_vpcs.py”, line 88, in get_vpcs ret = pulumi.runtime.invoke(‘aws:ec2/getVpcs:getVpcs’, args, opts=opts).value File “/usr/local/lib/python3.7/site-packages/pulumi/runtime/invoke.py”, line 125, in invoke return InvokeResult(_sync_await(asyncio.ensure_future(do_rpc()))) File “/usr/local/lib/python3.7/site-packages/pulumi/runtime/sync_await.py”, line 95, in _sync_await return fut.result() File “/usr/local/lib/python3.7/site-packages/pulumi/runtime/invoke.py”, line 122, in do_rpc raise exn File “/usr/local/lib/python3.7/site-packages/pulumi/runtime/rpc_manager.py”, line 67, in rpc_wrapper result = await rpc File “/usr/local/lib/python3.7/site-packages/pulumi/runtime/invoke.py”, line 106, in do_invoke resp = await asyncio.get_event_loop().run_in_executor(None, do_invoke) File “/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/thread.py”, line 57, in run result = self.fn(*self.args, **self.kwargs) File “/usr/local/lib/python3.7/site-packages/pulumi/runtime/invoke.py”, line 104, in do_invoke raise Exception(details) Exception: invocation of aws:ec2/getVpcs:getVpcs returned an error: invoking aws:ec2/getVpcs:getVpcs: no matching VPC found
  • s

    sparse-state-34229

    05/21/2020, 4:42 PM
    are you using actual tags?
  • s

    sparse-state-34229

    05/21/2020, 4:42 PM
    also might i suggest backtics for pasting code (one or three, same as github)
  • h

    hundreds-dog-95098

    05/21/2020, 4:46 PM
    yes but at the start there is no vpc with this tags the pulumi need to craete many vpc with the tags but when there is no vpcs its failed insted of return empty array of ids
  • s

    sparse-state-34229

    05/21/2020, 4:47 PM
    catch the exception then?
  • s

    sparse-state-34229

    05/21/2020, 4:48 PM
    what are you trying to do with the return value?
  • h

    hundreds-dog-95098

    05/21/2020, 4:50 PM
    getting the cidr_block of each vpc. ok, if there is no some solution to resolve it i will catch the exception. Thanks
  • s

    sparse-state-34229

    05/21/2020, 9:19 PM
    if the VPC doesn’t exist how can you get its CIDR block?
  • s

    sparse-state-34229

    05/21/2020, 9:19 PM
    sorry for late response, busy day 😞
  • g

    gentle-diamond-70147

    05/21/2020, 9:22 PM
    @hundreds-dog-95098 Unfortunately, these "not found" exceptions from lookup functions and and
    <resource>.get()
    calls cannot be caught and handled with a try/catch. There is an issue open regarding this at https://github.com/pulumi/pulumi/issues/3364 if you want to subscribe to it or add additional use case information.
  • s

    sparse-state-34229

    05/21/2020, 9:28 PM
    d’oh
  • s

    sparse-state-34229

    05/21/2020, 9:31 PM
    if these are pulumi-managed VPCs, you should have the CIDR block data available without querying AWS, tho
  • b

    bulky-match-1583

    05/24/2020, 1:14 PM
    Any of the python guru's knows how to get the string value out of random.RandomString
    random_id = random.RandomString(
        resource_name='project-unique-id',
        length=8,
        lower=True,
        upper=False,
        special=False,
        min_numeric=4
    )
    is there a way to extract the string value from random_id.result?
    s
    • 2
    • 30
Powered by Linen
Title
b

bulky-match-1583

05/24/2020, 1:14 PM
Any of the python guru's knows how to get the string value out of random.RandomString
random_id = random.RandomString(
    resource_name='project-unique-id',
    length=8,
    lower=True,
    upper=False,
    special=False,
    min_numeric=4
)
is there a way to extract the string value from random_id.result?
s

sparse-state-34229

05/24/2020, 3:56 PM
what is the type currently?
is it
Output
?
b

bulky-match-1583

05/24/2020, 3:59 PM
yes
s

sparse-state-34229

05/24/2020, 4:05 PM
you need .apply possibly with
Output.all()
what are you doing with the value?
b

bulky-match-1583

05/24/2020, 4:08 PM
what i need to do is to get the subnet back later, i know it sounds problematic , but its required subnet = gcp.compute.get_subnetwork( name=subnet_name, project=project_id, region=gcp_region ) if subnet.self_link: return subnet.ip_cidr_range else: return allocate_new_cidr(base_cidr_range)
name accepts only string i tried passing another output but no success
name of the subnet contains the random id i generated before
s

sparse-state-34229

05/24/2020, 4:11 PM
here is an example of how I do it:
return Output.all([self.vpc.id]).apply(
            lambda x: user_data_tpl.render(
                dns_domain=self.vpc.dns_domain,
                vpc_cidr=self.vpc.cidr_block,
                vpc_id=x[0][0],
                vpc_name=self.vpc.name,
            ).encode("utf-8")
        )
note the
x[0][0]
b

bulky-match-1583

05/24/2020, 4:12 PM
yes i tried
the problem that is returns another output and thre is no way to check if the subnet really exists 🙂
s

sparse-state-34229

05/24/2020, 4:15 PM
that’s fine, you just can’t print it
b

bulky-match-1583

05/24/2020, 4:17 PM
its and an empty output object , but seems i don't have a way to verify if something was found
s

sparse-state-34229

05/24/2020, 4:17 PM
what do you mean by verify?
b

bulky-match-1583

05/24/2020, 4:19 PM
if i pass just a string to subnet name
subnet = gcp.compute.get_subnetwork(
    name='some-subnet-that-exists',
    project=my-proejct-id,
    region=gcp_region
)
if subnet exists i can verify with
if subnet.self_link:
    return subnet.ip_cidr_range
doesn't seem to work with apply
s

sparse-state-34229

05/24/2020, 4:22 PM
in what way does it not work?
b

bulky-match-1583

05/24/2020, 4:23 PM
subnet = pulumi.Output.all([config.project_id]).apply(
    lambda x: gcp.compute.get_subnetwork(
        name=f'{x[0][0]}-subnet',
        project=x[0][0],
        region=config.gcp_region
    )
)
if subnet.self_link:
    return subnet.ip_cidr_range
if subnet.self_link always true even if it doesn't exist
i'm missing someting
s

sparse-state-34229

05/24/2020, 4:23 PM
subnet.self_link
is bool?
hmm
b

bulky-match-1583

05/24/2020, 4:24 PM
it jus tests that its not None
without apply with string in name works like a charm
s

sparse-state-34229

05/24/2020, 4:24 PM
well, there are ways that can be true without it being
None
might be prudent to test explicitly, but i’m not sure that will help here
b

bulky-match-1583

05/24/2020, 4:25 PM
thank very much anyways!
s

sparse-state-34229

05/24/2020, 4:25 PM
are you doing other things with
subnet
?
b

bulky-match-1583

05/24/2020, 4:26 PM
i'm creating it but i need to assign a specific cidr if it doesn't exist
View count: 1