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

quaint-tailor-52411

09/15/2022, 8:50 PM
for ComponentResources, what does
self.register_outputs()
actually do? when I register some child resources there and then later try to access them as a property of component object i get
object no attribute my_outputted_resource
s

stocky-restaurant-98004

09/15/2022, 8:53 PM
Are you calling it with no params?
I believe you have to specify the outputs - it won't automatically walk your component.
q

quaint-tailor-52411

09/15/2022, 8:54 PM
Copy code
# We also need to register all the expected outputs for this component resource that will get returned by default.
        self.register_outputs({
            "virtual_network": virtual_network,
            "subnet": subnet,
        })
Copy code
Traceback (most recent call last):
      File "/Users/james/repos/cyclecloud/vm/./__main__.py", line 96, in <module>
        virtual_network_components[f"{SERVICE}-{location}"] = virtual_network.VirtualNetworkComponent(
      File "/Users/james/repos/cyclecloud/vm/./virtual_network.py", line 28, in __init__
        first_address_prefix = self.virtual_network.address_space.address_prefixes[0]
    AttributeError: 'VirtualNetworkComponent' object has no attribute 'virtual_network'
s

stocky-restaurant-98004

09/15/2022, 8:55 PM
And it's saying
your_component.virtual_network
is not defined?
NM - I see above.
Can you give me a few lines of code around where the stack trace is?
The
self
is a little confusing to me, but I am probably missing something
q

quaint-tailor-52411

09/15/2022, 8:57 PM
sorry i ended up added self so I could access it as a member since
register_outputs
was not working
some of pulumi examples does this also
Copy code
import pulumi

from pulumi_azure_native import network

class MyComponent(pulumi.ComponentResource):
    def __init__(self, name, props=None, opts=None):
        super().__init__("pkg:network:MyComponent", name=name, props=None, opts=opts)

        child_opts = pulumi.ResourceOptions(parent=self)

        virtual_network = network.VirtualNetwork(
            "name",
            address_space=network.AddressSpaceArgs(
                address_prefixes=[
                    "10.0.0.0/8",
                ],
            ),
            location="westus",
            resource_group_name="myRG",
            virtual_network_name="vnetName",
            opts=pulumi.ResourceOptions(
                parent=self,
            ),
        )

        self.register_outputs({
            "virtual_network": virtual_network,
        })
Copy code
import component

mycomponent = component.MyComponent("mycomponent")
mycomponent.virtual_network
Copy code
error: Program failed with an unhandled exception:
    Traceback (most recent call last):
      File "/Users/james/repos/cyclecloud/vm/./__main__.py", line 22, in <module>
        mycomponent.virtual_network
    AttributeError: 'MyComponent' object has no attribute 'virtual_network'
s

stocky-restaurant-98004

09/15/2022, 9:09 PM
Giving this code a hard stare...
Let me try to repro this real quick
What version of pulumi are you using?
q

quaint-tailor-52411

09/15/2022, 9:15 PM
Copy code
(.venv) jamess-MacBook-Pro:vm james$ pulumi version
v3.39.3
s

stocky-restaurant-98004

09/15/2022, 9:16 PM
Can you verify that virtual network creates successfully by copy/pasting outside of the component?
q

quaint-tailor-52411

09/15/2022, 9:18 PM
this was a dummy example but i was able to create vnets with it yes
s

stocky-restaurant-98004

09/15/2022, 9:22 PM
I'm getting the same. Let me double-check what the correct way is.
It should be
self.virtual_network = ...
, and you do not need to call
register_outputs
. I'll submit a PR to update the docs, and my apologies for hitting this. Our docs should be clearer on this subject.
q

quaint-tailor-52411

09/15/2022, 9:30 PM
ok so this is just standard python then
since you're going to update the docs
i think this does nothing also?
child_opts = pulumi.ResourceOptions(parent=self)
s

stocky-restaurant-98004

09/15/2022, 9:41 PM
You do need to set
parent=self
for each resource in the component because that's how you inherit the provider.
q

quaint-tailor-52411

09/15/2022, 9:44 PM
yeah but this was just in the contructor with no resource
2 Views