https://pulumi.com logo
#python
Title
# python
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
Copy code
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:
Copy code
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
Copy code
subnet = gcp.compute.get_subnetwork(
    name='some-subnet-that-exists',
    project=my-proejct-id,
    region=gcp_region
)
if subnet exists i can verify with
Copy code
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
Copy code
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