cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1126
Views
2
Helpful
12
Replies

acibaseobject issue

JamesDean87
Level 1
Level 1

Hi,
I found one problem in acitoolkit acibaseobject currently working on cable plan application, and noticed that target node is not same format as node in for loop. List self._parent.get_children(Node) have nodes names rather then nodes numbers so each time it throws None and nothing appears not sure how to change this list to include list o nodes number, will you be so kind and help me to change list to be nodes number representation?
Below part of code:

 

 

 

 

 def get_node1(self):

        """Returns the Node object that corresponds to the first

        node of the link.  The Node must be a child of

        the Pod that this link is a member of, i.e. it

        must already have been read from the APIC.  This can

        most easily be done by populating the entire

        physical heirarchy from the Pod down.

 

        :returns: Node object at first end of link

        """

        return self._get_node(1)

def get_node2(self):

    """Returns the Node object that corresponds to the

    second node of the link.  The Node must be a child of

    the Pod that this link is a member of, i.e. it must

    already have been read from the APIC.  This can

    most easily be done by populating the entire physical

    heirarchy from the Pod down.



    :returns: Node object at second end of link

    """

    return self._get_node(2)


def _get_node(self, node_number):

    """Common implementation of get_node1() and get_node2()"""

    if not self._parent:

        raise TypeError("Parent pod must be specified in order to get node")

    target_node = {1: self.node1, 2: self.node2}[node_number]

    matching_nodes = (

        node for node in self._parent.get_children(Node)

        if node == target_node

    )


    return next(matching_nodes, None)`

 

 

 

 

 

1 Accepted Solution

Accepted Solutions

Marcel Zehnder
Spotlight
Spotlight

Hi you should get the node ID's by calling get_node(), try this for your "matching_nodes":

 

matching_nodes = (node.get_node() for node in self._parent.get_children(Node) if node.get_node() == target_node)

 

The following works for me:

 

from acitoolkit.acitoolkit import Session
from acitoolkit.aciphysobject import Pod, Node

url = "xxx"
login = "admin"
password = "xxx"

session = Session(url, login, password)
session.login()

pods = Pod.get(session)
for pod in pods:
    pod.populate_children(deep=False)
    node_generator = (node.get_node() for node in pod.get_children(Node))
    for x in node_generator:
        print(x)

""" OUTPUT:
101
201
1
102
"""

 

 

 

 

View solution in original post

12 Replies 12

Marcel Zehnder
Spotlight
Spotlight

Hi you should get the node ID's by calling get_node(), try this for your "matching_nodes":

 

matching_nodes = (node.get_node() for node in self._parent.get_children(Node) if node.get_node() == target_node)

 

The following works for me:

 

from acitoolkit.acitoolkit import Session
from acitoolkit.aciphysobject import Pod, Node

url = "xxx"
login = "admin"
password = "xxx"

session = Session(url, login, password)
session.login()

pods = Pod.get(session)
for pod in pods:
    pod.populate_children(deep=False)
    node_generator = (node.get_node() for node in pod.get_children(Node))
    for x in node_generator:
        print(x)

""" OUTPUT:
101
201
1
102
"""

 

 

 

 

Hi Marcel,

thank you very much for yours help, indeed it started working after one amendment added get_node() to if - node.get_node()  as yours first response (seems right now you also added it). 

After that I am struggle next problem related with getFabricSt, wondering how to call it to get proper data, anyway i am also nto sure what this returns ? State of what ? Any ideas ?

AttributeError: 'str' object has no attribute 'getFabricSt'

 

def _build_apic(self, pod):

        """This will build the cable plan using the configuration of the pod.

 

        :param pod: Pod

 

        :returns: None

        """

        nodes = pod.get_children(ACI.Node)

        for node in nodes:

            if node.getFabricSt() == 'active':

                if node.get_role() == 'spine':

                    self.add_switch(CpSwitch(node.get_name(), node.get_chassis_type(), spine=True))

                if node.get_role() == 'leaf':

                    self.add_switch(CpSwitch(node.get_name(), node.get_chassis_type()))

        links = pod.get_children(ACI.Link)

        for link in links:

            # node1_id = ACI.Link._get_target_node(link, 1)

            # print(node1_id)

            switch1 = link.get_node1()

            switch2 = link.get_node2()

            if switch1 and switch2:

                if link.get_node1().getFabricSt() == 'active' and link.get_node2().getFabricSt() == 'active':
                    if link.get_node1().get_role() != 'controller' and link.get_node2().get_role() != 'controller':

                        source_chassis = self._get_switch_from_apic_link(link, 1)

                        dest_chassis = self._get_switch_from_apic_link(link, 2)

                        source_interface = link.get_port1()

                        dest_interface = link.get_port2()

                        source_port = '{0:s}{1:s}/{2:s}'.format(source_interface.interface_type,

                                                                source_interface.module, source_interface.port)

                        dest_port = '{0:s}{1:s}/{2:s}'.format(dest_interface.interface_type,

                                                              dest_interface.module,

                                                              dest_interface.port)

 

                        self.add_link(

                            CpLink(source_chassis=source_chassis, source_port=source_port, dest_chassis=dest_chassis,

                                   dest_port=dest_port))

 

 

 

I think I found the problem it is triggered now by node name not number, will try to fix it. 

Good to hear, if you stuck again, let me know.

Regarding the other question: getFabricSt() returns the state of the node in question (active, inactivedecommissioned, disabled, discovering...etc) - it's the same state as you see in the UI under Fabric > Inventory > Fabric Membership.

For sure will do  

def _get_node(self, node_number):

    """Common implementation of get_node1() and get_node2()"""

    if not self._parent:

        raise TypeError("Parent pod must be specified in order to get node")

    target_node = {1: self.node1, 2: self.node2}[node_number]

    matching_nodes = (

        node for node in self._parent.get_children(Node)

        if node.get_node() == target_node

    )


    return next(matching_nodes, None)`

 Generally easiest way in my case was replacing 

 if node == target_node  to  if node.get_node() == target_node

rather rewriting whole functions.  

Seems have next one, self.other in cableplan it is getting None and it is crashing here again, generally it seems it prompt switches names, but why i gets None ?

 

return self.name == other.name
AttributeError: 'NoneType' object has no attribute 'name'

def __eq__(self, other):

        print(other)

        return self.name == other.name

 

Fixed, finally program started working  

Solution:

 

 

def __eq__(self, other):

        print(other)

        return self.name == other

 

 

Can you post the complete script?

Never mind, I did not see your last post. Happy it works now.

sorry for adding it few times... not sure what happened.

same problem here 

sure attached below, also acibaseobject you need to amend as below: 

 if node == target_node  to  if node.get_node() == target_node