Any of the python guru's knows how to get the stri...
# python
b
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
what is the type currently?
is it
Output
?
b
yes
s
you need .apply possibly with
Output.all()
what are you doing with the value?
b
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
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
yes i tried
the problem that is returns another output and thre is no way to check if the subnet really exists 🙂
s
that’s fine, you just can’t print it
b
its and an empty output object , but seems i don't have a way to verify if something was found
s
what do you mean by verify?
b
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
in what way does it not work?
b
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
subnet.self_link
is bool?
hmm
b
it jus tests that its not None
without apply with string in name works like a charm
s
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
thank very much anyways!
s
are you doing other things with
subnet
?
b
i'm creating it but i need to assign a specific cidr if it doesn't exist