def test_node_type(self): """ Test node type :return: None """ node = Node('1', '2', 'Leaf1', role='leaf') self.assertEqual(node.get_type(), 'node')
def test_node_type(self): """ Test node type :return: None """ node = Node("1", "2", "Leaf1", role="leaf") self.assertEqual(node.get_type(), "node")
def test_node_parent(self): """ Test node parent :return: None """ pod_id = '1' pod1 = Pod(pod_id) node = Node('1', '2', 'Spine1', role='leaf', parent=pod1) self.assertEqual(pod1, node.get_parent())
def test_node_parent(self): """ Test node parent :return: None """ pod_id = "1" pod1 = Pod(pod_id) node = Node("1", "2", "Spine1", role="leaf", parent=pod1) self.assertEqual(pod1, node.get_parent())
def test_node_id(self): """ Test node id :return: None """ node = Node("1", "2", "Leaf1", role="leaf") self.assertEqual(node.get_pod(), "1") self.assertEqual(node.get_node(), "2") self.assertEqual(node.get_name(), "Leaf1")
def test_node_id(self): """ Test node id :return: None """ node = Node('1', '2', 'Leaf1', role='leaf') self.assertEqual(node.get_pod(), '1') self.assertEqual(node.get_node(), '2') self.assertEqual(node.get_name(), 'Leaf1')
def test_node_model(self): """ Test node model :return: None """ node = Node("1", "2", "Leaf1", role="leaf") self.assertEqual(node.get_chassis_type(), None) node.model = "N9K-C9396PX" self.assertEqual(node.model, "N9K-C9396PX") self.assertEqual(node.get_chassis_type(), "n9k")
def test_node_model(self): """ Test node model :return: None """ node = Node('1', '2', 'Leaf1', role='leaf') self.assertEqual(node.get_chassis_type(), None) node.model = 'N9K-C9396PX' self.assertEqual(node.model, 'N9K-C9396PX') self.assertEqual(node.get_chassis_type(), 'n9k')
def test_node_get_parent_pod_id(self): session = self.login_to_apic() pod = '1' nodes = Node.get(session, parent=pod) self.assertTrue(len(nodes) > 0) self.assertEqual(nodes[0].get_parent(), None)
def test_node_get_parent_pod(self): session = self.login_to_apic() pods = Pod.get(session) pod = pods[0] nodes = Node.get(session, parent=pod) self.assertEqual(len(nodes), len(pod.get_children())) self.assertEqual(nodes[0].get_parent(), pod)
def show_basic_node(): from acitoolkit.aciphysobject import Node if not connected(): if not collect_login(): return data = [] # Get and print all of the items from the APIC items = Node.get(session) for item in items: if item.role == 'controller': item.role = 'APIC' data.append((item.serial, item.role, item.node, item.health, item.model, item.oob_mgmt_ip, item.tep_ip)) newdata = sorted(data, key=lambda x: x[1]) # Display the data downloaded template = "{0:15} {1:7} {2:6} {3:8} {4:17} {5:15} {6:15}" print template.format("Serial Number", "Role", "Node", "Health", "Model", "OOB Address", "TEP Address") print template.format("-------------", "-----", "----", "------", "---------------", "-------------", "---------------") for rec in newdata: print template.format(*rec)
def show_basic_node(): from acitoolkit.aciphysobject import Node if not connected(): if not collect_login(): return data = [] # Get and print all of the items from the APIC items = Node.get(session) for item in items: if item.role == 'controller': item.role = 'APIC' data.append((item.serial, item.role, item.node, item.health, item.model, item.oob_mgmt_ip, item.tep_ip)) newdata = sorted(data,key=lambda x: x[1]) # Display the data downloaded template = "{0:15} {1:7} {2:6} {3:8} {4:17} {5:15} {6:15}" print template.format("Serial Number", "Role", "Node", "Health", "Model", "OOB Address", "TEP Address") print template.format("-------------", "-----", "----", "------", "---------------", "-------------", "---------------") for rec in newdata: print template.format(*rec)
def test_pod_invalid_parent(self): """ Tests that the parent class of the pod :return: """ session = self.login_to_apic() parent = Node('1', '101', 'Switch') self.assertRaises(TypeError, Pod.get, session, parent)
def test_invalid_session_populate_children(self): """ Test node invalid populate_children :return: None """ pod1 = Pod('1') node = Node('1', '2', 'Spine1', 'spine', pod1) self.assertRaises(TypeError, node.populate_children)
def test_node_role(self): """ Test node role :return: None """ node_name = 'Leaf1' node_pod = '1' node_node = '3' node_role = 'leaf' node = Node(node_pod, node_node, node_name, role=node_role) self.assertEqual(node.role, node_role) node_role = 'controller' node = Node(node_pod, node_node, node_name, role=node_role) self.assertEqual(node.role, node_role) node_role = 'bogus' self.assertRaises(ValueError, Node, node_pod, node_node, node_name, node_role)
def test_get_endpoint_objects(self): pod_id = '1' node1_id = '2' node2_id = '3' slot1_id = '4' slot2_id = '5' port1_id = '6' port2_id = '7' link_id = '101' pod = Pod(pod_id) node1 = Node(pod_id, node1_id, 'Spine', 'spine', pod) node2 = Node(pod_id, node2_id, 'Leaf', 'leaf', pod) linecard1 = Linecard(slot1_id, node1) linecard2 = Linecard(slot2_id, node2) interface1 = Interface(interface_type='eth', pod=pod_id, node=node1_id, module=slot1_id, port=port1_id, parent=linecard1) interface2 = Interface(interface_type='eth', pod=pod_id, node=node2_id, module=slot2_id, port=port2_id, parent=linecard2) link1 = Link(pod) link1.pod = pod_id link1.link = link_id link1.node1 = node1_id link1.slot1 = slot1_id link1.port1 = port1_id link1.node2 = node2_id link1.slot2 = slot2_id link1.port2 = port2_id self.assertEqual(node1, link1.get_node1()) self.assertEqual(node2, link1.get_node2()) self.assertEqual(linecard1, link1.get_slot1()) self.assertEqual(linecard2, link1.get_slot2()) self.assertEqual(interface1, link1.get_port1()) self.assertEqual(interface2, link1.get_port2())
def get_leafs(session): from acitoolkit.aciphysobject import Node data = [] items = Node.get(session) for item in items: if item.role == 'leaf': data.append(item.node) return data
def test_node_get_fabric_state(self): """ Test node get fabric state :return: None """ node_name = 'Leaf1' node_pod = '1' node_node = '3' node_role = 'leaf' node1 = Node(node_pod, node_node, node_name, node_role)
def test_node_equal(self): """ Test node equal :return: None """ node_name = 'Leaf1' node_pod = '1' node_node = '3' node_role = 'leaf' node1 = Node(node_pod, node_node, node_name, node_role) node_name = 'Leaf2' node2 = Node(node_pod, node_node, node_name, node_role) self.assertNotEqual(node1, node2) node_name = 'Leaf1' node_pod = '2' node2 = Node(node_pod, node_node, node_name, node_role) self.assertNotEqual(node1, node2) node_pod = '1' node_node = '4' node2 = Node(node_pod, node_node, node_name, node_role) self.assertNotEqual(node1, node2) node_node = '3' node_role = 'controller' node2 = Node(node_pod, node_node, node_name, node_role) self.assertNotEqual(node1, node2) node_role = 'leaf' node2 = Node(node_pod, node_node, node_name, node_role) self.assertEqual(node1, node2) node2 = Pod(node_pod) self.assertNotEqual(node1, node2)
def check_mod_parent(self, mod_class): """ :param self: :param mod_class: """ mod_pod = "1" mod_node = "5" mod_slot = "2" node = Node(mod_pod, mod_node, "Spine2", "spine") mod1 = mod_class(mod_pod, mod_node, mod_slot, node) self.assertEqual(mod1.get_parent(), node) self.assertEqual(node.get_children(), [mod1]) mod2 = mod_class(mod_pod, mod_node, mod_slot, node) self.assertEqual(node.get_children(), [mod2]) mod_slot = "3" mod3 = mod_class(mod_pod, mod_node, mod_slot, node) self.assertEqual(node.get_children(), [mod2, mod3]) self.assertEqual(node.get_children(mod_class), [mod2, mod3]) # test illegal parent type self.assertRaises(TypeError, mod_class, mod_pod, mod_node, mod_slot, mod1)
def test_eNode_parent(self): pod = Pod('1') node = ExternalSwitch(parent=pod) self.assertEqual(node.role, 'external_switch') self.assertEqual(node._parent, pod) children = pod.get_children() self.assertEqual(len(children), 1) for child in children: self.assertEqual(child, node) pod = Node('1', '101', 'Spine2', 'leaf') self.assertRaises(TypeError, ExternalSwitch, pod)
def check_mod_parent(self, mod_class): """ :param self: :param mod_class: """ mod_pod = '1' mod_node = '5' mod_slot = '2' node = Node(mod_pod, mod_node, 'Spine2', 'spine') mod1 = mod_class(mod_pod, mod_node, mod_slot, node) self.assertEqual(mod1.get_parent(), node) self.assertEqual(node.get_children(), [mod1]) mod2 = mod_class(mod_pod, mod_node, mod_slot, node) self.assertEqual(node.get_children(), [mod2]) mod_slot = '3' mod3 = mod_class(mod_pod, mod_node, mod_slot, node) self.assertEqual(node.get_children(), [mod2, mod3]) self.assertEqual(node.get_children(mod_class), [mod2, mod3]) # test illegal parent type self.assertRaises(TypeError, mod_class, mod_pod, mod_node, mod_slot, mod1)
def test_find(self): pod_id = '1' node1_id = '2' node2_id = '3' slot1_id = '4' slot2_id = '5' port2_id = '7' pod = Pod(pod_id) node1 = Node(pod_id, node1_id, 'Spine', 'spine', pod) node2 = Node(pod_id, node2_id, 'Leaf', 'leaf', pod) linecard1 = Linecard(slot1_id, node1) linecard2 = Linecard(slot2_id, node2) linecard1.serial = 'SerialNumber1' linecard2.serial = 'SerialNumber2' interface2 = Interface(interface_type='eth', pod=pod_id, node=node2_id, module=slot2_id, port=port2_id, parent=linecard2) so = Search() so.node = node2_id results = pod.find(so) self.assertIn(node2, results) self.assertIn(linecard2, results) self.assertIn(interface2, results) self.assertEqual(len(results), 3) for result in results: self.assertEqual(result.node, node2_id) so = Search() so.serial = 'SerialNumber1' results = pod.find(so) self.assertIn(linecard1, results) self.assertEqual(len(results), 1) self.assertEqual(results[0].serial, 'SerialNumber1')
def __init__(self, session, txtformat='text', verbose=0): """ Initialize a statistics session. :param self: :param session: :param txtformat: Output format, 'text' or 'csv' :param verbose: Verbosity level of debug output """ print 'Getting inital info from APIC....' self.session = session self.nodes = Node.get_deep(session) self.txtformat = txtformat self.verbose = verbose try: locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') except locale.Error: pass
def test_node(self): session = self.login_to_apic() nodes = Node.get(session) for node in nodes: self.assertIsInstance(node.get_name(), str) self.assertTrue(len(node.get_name()) > 0) self.assertEqual(node.get_type(), "node") self.assertIsInstance(node.get_pod(), str) self.assertTrue(len(node.get_pod()) > 0) self.assertIsInstance(node.get_node(), str) self.assertTrue(len(node.get_node()) > 0) self.assertIn(node.get_role(), ["controller", "leaf", "spine"]) self.assertIn(node.getFabricSt(), ["active", "inactive", "unknown"])
def __init__(self, session, txtformat='text', verbose=0): """ Initialize a statistics session. :param self: :param session: :param txtformat: Output format, 'text' or 'csv' :param verbose: Verbosity level of debug output """ print('Getting inital info from APIC....') self.session = session self.nodes = Node.get_deep(session) self.txtformat = txtformat self.verbose = verbose try: locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') except locale.Error: pass
def test_node(self): session = self.login_to_apic() nodes = Node.get(session) for node in nodes: self.assertIsInstance(node.get_name(), str) self.assertTrue(len(node.get_name()) > 0) self.assertEqual(node.get_type(), 'node') self.assertIsInstance(node.get_pod(), str) self.assertTrue(len(node.get_pod()) > 0) self.assertIsInstance(node.get_node(), str) self.assertTrue(len(node.get_node()) > 0) self.assertIn(node.get_role(), ['controller', 'leaf', 'spine']) self.assertIn(node.getFabricSt(), ['active', 'inactive', 'unknown'])
def test_link_get_for_node(self): session = self.login_to_apic() pod = Pod.get(session)[0] links = Link.get(session) total_links = len(links) self.assertTrue(total_links > 0) self.assertRaises(TypeError, links[0].get_node1) self.assertRaises(TypeError, links[0].get_node2) self.assertRaises(TypeError, links[0].get_slot1) self.assertRaises(TypeError, links[0].get_slot2) self.assertRaises(TypeError, links[0].get_port1) self.assertRaises(TypeError, links[0].get_port2) links = Link.get(session, pod) self.assertEqual(len(links), total_links) switches = [] for link in links: self.assertEqual(link.get_node1(), None) self.assertEqual(link.get_slot1(), None) self.assertEqual(link.get_port1(), None) self.assertEqual(link.get_node2(), None) self.assertEqual(link.get_slot2(), None) self.assertEqual(link.get_port2(), None) if link.node1 not in switches: switches.append(link.node1) self.assertTrue(len(switches) > 1) nodes = Node.get(session) spine = None for node in nodes: if node.get_role() == 'spine' and node.fabricSt == 'active': spine = node break if spine: links = Link.get(session, pod, spine.node) spine_links = len(links) self.assertTrue(spine_links > 0) self.assertTrue(spine_links < total_links) for link in links: self.assertEqual(link.node1, spine.node) self.assertIsInstance(str(link), str) links = Link.get(session, '1', spine.node) self.assertEqual(len(links), spine_links) self.assertNotEqual(links[0], links[1])
def main(): with open(CONFFILE, 'r') as r: conf = json.loads(r.read()) #login to apic session = Session(conf['url'], conf['login'], conf['password']) resp = session.login() if not resp.ok: print('%% Could not login to APIC') return sys.exit(0) nodes = Node.get(session) for node in nodes: print('=' * 50) print('Pod: {}'.format(node.pod)) print('Node: {}'.format(node.node)) print('Mode: {}'.format(node.mode)) print('Model: {}'.format(node.model)) print('Vendor: {}'.format(node.vendor)) print('Serial: {}'.format(node.serial))
def test_pod_equal(self): """ Test pods are equal :return: None """ pod_id = '1' pod1 = Pod(pod_id) # check different IDs pod_id = '2' pod2 = Pod(pod_id) self.assertNotEqual(pod1, pod2) # check same pod_id = '1' pod2 = Pod(pod_id) self.assertEqual(pod1, pod2) # check different types pod2 = Node(pod_id, '2', 'Leaf1', role='leaf') self.assertNotEqual(pod1, pod2)
def check_module(self, mod_class, mod_name_prefix, mod_type): """ :param self: :param mod_class: :param mod_name_prefix: :param mod_type: """ mod_pod = '1' mod_node = '5' mod_slot = '2' node = Node(mod_pod, mod_node, 'Spine2', 'leaf') mod = mod_class(mod_pod, mod_node, mod_slot) name = mod_name_prefix + '-' + '/'.join([mod_pod, mod_node, mod_slot]) self.assertNotEqual(mod, node) self.assertEqual(mod.get_pod(), mod_pod) self.assertEqual(mod.get_node(), mod_node) self.assertEqual(mod.get_slot(), mod_slot) self.assertEqual(mod.get_parent(), None) self.assertEqual(mod.get_type(), mod_type) self.assertEqual(mod.get_name(), name) self.assertEqual(str(mod), name) self.assertEqual(mod._session, None)
def get_spine(self): session = self.login_to_apic() nodes = Node.get(session) for node in nodes: if node.get_role() == "spine" and node.fabricSt == "active": return node, session
def collect_add_server_EPG(): from acitoolkit.aciphysobject import Node if not collect_login(): return False tenants_list = [] apps_list = [] epgs_list = [] print '\n\n' print '=====================================' print '== Add physcial server to an EPG ==' print '=====================================\n' tenants = ACI.Tenant.get(session) for tenant in tenants: tenants_list.append((tenant.name)) for a in range(len(tenants_list)): print str(a) + ': ' + tenants_list[a] input = raw_input('\nEnter the Tenant #: ') tenant_in = 99 try: tenant_in = int(input) except: pass print '\n' apps = ACI.AppProfile.get(session, tenants[tenant_in]) for app in apps: apps_list.append((app.name)) if len(apps) <= 0: error_message([2,'There are no applications in this Tenant yet', 2]) return for a in range(len(apps_list)): print str(a) + ': ' + apps_list[a] input = raw_input('\nEnter the Application Profile #: ') app_in = 99 try: app_in = int(input) except: pass print '\n' epgs = ACI.EPG.get(session, apps[app_in], tenants[tenant_in]) if len(epgs) <= 0: error_message([3,'There are no EPGs in this Application yet', 2]) return for epg in epgs: epgs_list.append((epg.name)) for a in range(len(epgs_list)): print str(a) + ': ' + epgs_list[a] input = raw_input('\nEnter the End Point Group #: ') epg_in = 99 try: epg_in = int(input) except: pass # Select the leaf node print '\n' leafs = Node.get(session) if len(leafs) <= 0: error_message([3,'You have no Leafs!', 2]) return leafs_list = [] for leaf in leafs: if leaf.model == 'APIC': continue if leaf.role == 'spine': continue leafs_list.append((leaf.node)) for a in range(len(leafs_list)): print str(a) + ': ' + leafs_list[a] input = raw_input('\nEnter the leaf number where the new server is located #: ') leaf_in = 99 try: leaf_in = int(input) except: pass # Enter the interface print '\n' input = raw_input('\nEnter the interface number the new server is attached too #: ') if_in = 999 try: if_in = int(input) except: pass # Enter the interface print '\n' input = raw_input('\nEnter the VLAN number (this interface will be a tagged interface but can be changed in the APIC GUI later): ') vlan_in = 999 try: vlan_in = int(input) except: pass interface = {'type': 'eth', 'pod': '1', 'node': str(leafs_list[leaf_in]), 'module': '1', 'port': str(if_in)} vlan = {'name': 'vlan' + str(vlan_in), 'encap_type': 'vlan', 'encap_id': str(vlan_in)} input = raw_input('\nPress Enter to continue.') create_add_server_EPG(tenants[tenant_in],apps[app_in],epgs[epg_in],interface,vlan)
def test_get_all_nodes(self): pods, session = self.get_all_pods() for pod in pods: nodes = Node.get(session) self.assertTrue(len(nodes) > 0) self.assertTrue(pod.get_serial() is None)
def test_exists_node(self): session = self.login_to_apic() nodes = Node.get(session) self.assertTrue(Node.exists(session, nodes[0])) nodes[0].node = '999' self.assertFalse(Node.exists(session, nodes[0]))
def get_leaf(self): session = self.login_to_apic() nodes = Node.get(session) for node in nodes: if node.get_role() == 'leaf' and node.fabricSt == 'active': return node, session
def collect_add_server_EPG(): from acitoolkit.aciphysobject import Node if not collect_login(): return False tenants_list = [] apps_list = [] epgs_list = [] print '\n\n' print '=====================================' print '== Add physcial server to an EPG ==' print '=====================================\n' tenants = ACI.Tenant.get(session) for tenant in tenants: tenants_list.append((tenant.name)) for a in range(len(tenants_list)): print str(a) + ': ' + tenants_list[a] input = raw_input('\nEnter the Tenant #: ') tenant_in = 99 try: tenant_in = int(input) except: pass print '\n' apps = ACI.AppProfile.get(session, tenants[tenant_in]) for app in apps: apps_list.append((app.name)) if len(apps) <= 0: error_message([2, 'There are no applications in this Tenant yet', 2]) return for a in range(len(apps_list)): print str(a) + ': ' + apps_list[a] input = raw_input('\nEnter the Application Profile #: ') app_in = 99 try: app_in = int(input) except: pass print '\n' epgs = ACI.EPG.get(session, apps[app_in], tenants[tenant_in]) if len(epgs) <= 0: error_message([3, 'There are no EPGs in this Application yet', 2]) return for epg in epgs: epgs_list.append((epg.name)) for a in range(len(epgs_list)): print str(a) + ': ' + epgs_list[a] input = raw_input('\nEnter the End Point Group #: ') epg_in = 99 try: epg_in = int(input) except: pass # Select the leaf node print '\n' leafs = Node.get(session) if len(leafs) <= 0: error_message([3, 'You have no Leafs!', 2]) return leafs_list = [] for leaf in leafs: if leaf.model == 'APIC': continue if leaf.role == 'spine': continue leafs_list.append((leaf.node)) for a in range(len(leafs_list)): print str(a) + ': ' + leafs_list[a] input = raw_input( '\nEnter the leaf number where the new server is located #: ') leaf_in = 99 try: leaf_in = int(input) except: pass # Enter the interface print '\n' input = raw_input( '\nEnter the interface number the new server is attached too #: ') if_in = 999 try: if_in = int(input) except: pass # Enter the interface print '\n' input = raw_input( '\nEnter the VLAN number (this interface will be a tagged interface but can be changed in the APIC GUI later): ' ) vlan_in = 999 try: vlan_in = int(input) except: pass interface = { 'type': 'eth', 'pod': '1', 'node': str(leafs_list[leaf_in]), 'module': '1', 'port': str(if_in) } vlan = { 'name': 'vlan' + str(vlan_in), 'encap_type': 'vlan', 'encap_id': str(vlan_in) } input = raw_input('\nPress Enter to continue.') create_add_server_EPG(tenants[tenant_in], apps[app_in], epgs[epg_in], interface, vlan)
def get_controller(self): session = self.login_to_apic() nodes = Node.get(session) for node in nodes: if node.get_role() == 'controller' and node.fabricSt != 'inactive': return node, session
Phy_links=(Link) Phy_pwrs=(Powersupply) # Get and print all of the items from the APIC items = phy_class.get(session) for item in items: print item.info() links=Phy_links.get(session) for link in links: print link.info() pwrs=Phy_pwrs.get(session) for pwr in pwrs: print pwr.info() for node in Node.get(session): print node.info() tenant = Tenant('Alex_Tenant') res=session.get(tenant.get_url()) print res.text if __name__ == '__main__': pass