def main(): """ Main show EPGs routine :return: None """ # Login to APIC description = ('Simple application that logs on to the APIC' ' and displays all of the EPGs.') creds = aci.Credentials('apic', description) args = creds.get() session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') # Download all of the tenants, app profiles, and EPGs # and store the names as tuples in a list data = [] tenants = aci.Tenant.get(session) for tenant in tenants: apps = aci.AppProfile.get(session, tenant) for app in apps: epgs = aci.EPG.get(session, app, tenant) for epg in epgs: data.append((tenant.name, app.name, epg.name)) # Display the data downloaded template = "{0:19} {1:20} {2:15}" print(template.format("TENANT", "APP_PROFILE", "EPG")) print(template.format("------", "-----------", "---")) for rec in data: print(template.format(*rec))
def main(): description = ( 'Simple application that logs on to the APIC and displays the' ' response of a given query') creds = aci.Credentials('apic', description) creds.add_argument('-c', '--class_name', help='The class which is to be queried', required=True) creds.add_argument( '-q', '--query_target', default='self', help=('This restricts the scope of the query,query_target takes self' '| children | subtree. ex: -q self. The default is self.')) args = creds.get() if not args.class_name: args.class_name = raw_input("Class Name: ") # Login to APIC session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') sys.exit(0) class_url = '/api/node/class/' + args.class_name + '.json?query-target=' + args.query_target print("class_url is " + class_url) ret = session.get(class_url) response = ret.json() imdata = response['imdata'] pprint(imdata)
def main(): """ Main create tenant routine :return: None """ # Get all the arguments description = 'It logs in to the APIC and will create the vlan pool.' creds = aci.Credentials('apic', description) creds.add_argument('-v', '--vlan', help='The name of vlan pool', default=DEFAULT_VLAN_NAME) args = creds.get() # Login to the APIC session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') # Create the VLAN pool vlan_pool = aci.NetworkPool(args.vlan, 'vlan', '222', '223', 'dynamic') # Push the VLAN pool to the APIC resp = session.push_to_apic(vlan_pool.get_url(), vlan_pool.get_json()) if not resp.ok: print('%% Error: Could not push configuration to APIC') print(resp.text)
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = 'Simple application that logs on to the APIC and displays all of the Tenants.' creds = ACI.Credentials('apic', description) args = creds.get() # Login to APIC session = ACI.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') sys.exit(0) # Download all of the tenants print("TENANT") print("------") tenants = ACI.Tenant.get(session) for tenant in tenants: print(tenant.name)
def main(): """ Main create tenant routine :return: None """ # Get all the arguments description = 'It logs in to the APIC and will create the tenant.' creds = aci.Credentials('apic', description) creds.add_argument('-t', '--tenant', help='The name of tenant', default=DEFAULT_TENANT_NAME) args = creds.get() # Login to the APIC session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') # Create the Tenant tenant = aci.Tenant(args.tenant) # Push the tenant to the APIC resp = session.push_to_apic(tenant.get_url(), tenant.get_json()) if not resp.ok: print('%% Error: Could not push configuration to APIC') print(resp.text)
def main(): """ Main Show Endpoints Routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = ('Simple application that logs on to the APIC' ' and displays all of the Endpoints.') creds = aci.Credentials('apic', description) args = creds.get() # Login to APIC session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') return # Download all of the interfaces # and store the data as tuples in a list data = [] endpoints = aci.Endpoint.get(session) for ep in endpoints: epg = ep.get_parent() app_profile = epg.get_parent() tenant = app_profile.get_parent() data.append((ep.if_name, tenant.name)) # Display the data downloaded print(tabulate(data, headers=["INTERFACE", "TENANT"]))
def main(): """ Main show EPGs routine :return: None """ # Login to APIC description = ('Simple application that logs on to the APIC' ' and add static-binding-leaves.') creds = aci.Credentials('apic', description) creds.add_argument('-t', '--tenant', help='Tenant name', default=DEFAULT_TENANT) creds.add_argument('-a', '--app', help='Application profile name', default=DEFAULT_APP) creds.add_argument('-e', '--epg', help='EPG name', default=DEFAULT_EPG) creds.add_argument('-n', '--node', help='Node ID (e.g. 101)', default=DEFAULT_NODE_ID) creds.add_argument('-y', '--type', help='Encapsulation type (vlan | vxlan | nvgre)', default=DEFAULT_ENCAP_TYPE) creds.add_argument('-i', '--id', help='Specific identifier representing the virtual L2 network (e.g. 100)', default=DEFAULT_ENCAP_ID) creds.add_argument('-m', '--mode', help='Encapsulation mode (regular | untagged | native)', default=DEFAULT_ENCAP_MODE) creds.add_argument('-d', '--deploy', help='Deployment immediacy (immediate | lazy)', default=DEFAULT_IMMEDIACY) creds.add_argument('-o', '--pod', help='Pod number (e.g. 1)', default=DEFAULT_POD) args = creds.get() session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') tenant = aci.Tenant(args.tenant) app = aci.AppProfile(args.app, tenant) epg = aci.EPG(args.epg, app) epg.add_static_leaf_binding(args.node, args.type, args.id, args.mode, args.deploy, args.pod) # Push it all to the APIC resp = session.push_to_apic(tenant.get_url(), tenant.get_json())
def main(): """ Main create tenant routine :return: None """ # Get all the arguments description = 'It logs in to APIC and will create the physical domain.' creds = aci.Credentials('apic', description) creds.add_argument('-p', '--phy_domain', help='The name of physical domain', default=DEFAULT_PHY_DOMAIN_NAME) args = creds.get() # Login to the APIC session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') # Create the physical Domain phy_dmn = aci.PhysDomain(args.phy_domain) # Push the physical domain to the APIC resp = session.push_to_apic(phy_dmn.get_url(), phy_dmn.get_json()) if not resp.ok: print('%% Error: Could not push configuration to APIC') print(resp.text)
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = 'Simple application that logs on to the APIC and displays \ all of the Bridge domains.' creds = ACI.Credentials('apic', description) args = creds.get() # Login to APIC session = ACI.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') sys.exit(0) # Download all of the tenants template = '{0:20} {1:20}' print template.format("TENANT", "BRIDGE DOMAIN") print template.format("------", "-------------") tenants = ACI.Tenant.get(session) for tenant in tenants: bds = ACI.BridgeDomain.get(session, tenant) for bd in bds: print template.format(tenant.name, bd.name)
def main(): """ Main subscribe tenants routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = ('Simple application using event subscription for the' 'Tenant class. When run, this application will log ' 'into the APIC and subscribe to events on the Tenant ' 'class. If a new tenant is created, the event will be' 'printed on the screen. Likewise, if an existing tenant' 'is deleted.') creds = aci.Credentials('apic', description) args = creds.get() # Login to APIC session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') sys.exit(0) aci.Tenant.subscribe(session) while True: if aci.Tenant.has_events(session): tenant = aci.Tenant.get_event(session) if tenant.is_deleted(): print('Tenant', tenant.name, 'has been deleted.') else: print('Tenant', tenant.name, 'has been created or modified.')
def main(): # Get the APIC login credentials description = 'testing tags' creds = aci.Credentials('apic', description) args = creds.get() # Login to APIC and push the config session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') return # get all physical domains phydms = aci.PhysDomain.get(session) template = "{0:30} {1:30} {2:25}" if len(phydms) > 0: print(template.format("Physical Domain", "Network", "encap_type")) for p in phydms: pool_name = "" encap_type = "" if p.has_network(): net = p.get_network() pool_name = net.name encap_type = net.encap_type print(template.format(p.name, pool_name, encap_type))
def main(): """ Main Endpoint Tracker routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = ('Application that logs on to the APIC and tracks' ' all of the Endpoints in a MySQL database.') creds = aci.Credentials(qualifier=('apic', 'mysql', 'daemon'), description=description) args = creds.get() if args.daemon or args.kill or args.restart: args.daemon = True pid = '/var/run/endpointtracker.pid' daemon = Daemonize(args, pid) if args.kill: daemon.stop() elif args.restart: daemon.restart() elif args.daemon: daemon.start() else: tracker(args)
def main(): """ Main common routine for show interface description :return: None """ # Set up the command line options description = ('Simple application that logs in to the APIC' 'and displays the Interface Optics and Neighbors') creds = ACI.Credentials('apic', description) args = creds.get() # Login to APIC apic = ACI.Session(args.url, args.login, args.password) resp = apic.login() if not resp.ok: print('%% Could not login to APIC') return # Show interface Optic & LLDP Neighbor #print('starting node_ids') switch_attributes = get_switch_attributes(apic, args) #print('starting intf_ids') intf_ids = get_intf_ids(apic, args, switch_attributes) #print('starting Section for Optics and Neighbors') interface_detail(apic, args, switch_attributes, intf_ids)
def main(): time.sleep(70) description = ('Simple application that logs on to the APIC' ' and displays all of the Endpoints.') creds = aci.Credentials('apic', description) args = creds.get() session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') sys.exit(0) data = [] endpoints = aci.IPEndpoint.get(session) for ep in endpoints: epg = ep.get_parent() app_profile = epg.get_parent() tenant = app_profile.get_parent() if (tenant.name == 'Devops' and ((epg.name == 'web'))): data.append((ep.mac, ep.ip, epg.name)) #print "Test here\n" y = len(data) print "[WEB]" print data[y - 1][1]
def main(): """ Main Show Physical Domains Routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = ('Simple application that logs on to the APIC' ' and displays all of the Physical Domains.') creds = aci.Credentials('apic', description) args = creds.get() # Login to APIC session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') sys.exit(0) domains = aci.PhysDomain.get(session) if len(domains) > 0: print ('---------------') print ('Physical Domain') print ('---------------') for domain in domains: print domain.name if len(domains) > 0: print '\n' domains = aci.VmmDomain.get(session)
def main(): # Get the APIC login credentials description = 'testing tags' creds = aci.Credentials('apic', description) creds.add_argument('--tag', help='Add tag to all objects in this configuration') creds.add_argument('--tenant', help='Tenant name to be created') args = creds.get() #Create the Tenant if args.tenant: tenant = aci.Tenant(args.tenant) else: tenant = aci.Tenant('tutorial-tag') # Create the Application Profile app = aci.AppProfile('myapp', tenant) # Create the EPG epg1 = aci.EPG('myepg1', app) epg2 = aci.EPG('myepg2', app) # Create a Context and BridgeDomain context = aci.Context('myvrf', tenant) bd = aci.BridgeDomain('mybd', tenant) bd.add_context(context) # Place the EPG in the BD epg1.add_bd(bd) epg2.add_bd(bd) # Add Tag to the EPGs epg1.add_tag("web server") epg2.add_tag("database") # test app2 = aci.AppProfile('myapp2', tenant) epg21 = aci.EPG('myepg21', app2) epg22 = aci.EPG('myepg22', app2) # Add Tag to all objects in this configuration if args.tag: tenant.add_tag(args.tag) context.add_tag(args.tag) bd.add_tag(args.tag) epg1.add_tag(args.tag) epg2.add_tag(args.tag) # Login to APIC and push the config session = aci.Session(args.url, args.login, args.password) session.login() resp = tenant.push_to_apic(session) if resp.ok: print('Success') # Print what was sent print('Pushed the following JSON to the APIC') print('URL:', tenant.get_url()) print('JSON:', tenant.get_json())
def main(): ''' Main Routine ''' # Take login credentials from the command line if provided description = ('Simple application to display details about faults in an ACI Fabric') creds = aci.Credentials('apic', description) creds.add_argument('--sort', choices=["asc", "desc"], default="desc", help='Specify the sort order within each severity based on time/date. Default is Descending order. i.e. Newest faults at the top of each category') args = creds.get() # Login to APIC session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') return # Available severity codes for ACI Fault Instances fault_severity = ["critical", "major", "minor", "warning", "info", "cleared"] fault_lookup = {} # Dictionary to be used to store the returned MO's, with the key being the severity fault_severity_count = {} # Dictionary to track total faults per severity # Loop through all fault severity types, perform a fault lookup in the system and return the list of MO's. # Assign to the fault_lookup dictionary, with severity as the key for fault_type in fault_severity: resp = query_fault(session, fault_type, args.sort) resp2 = resp['imdata'] faultInst = [] for entry in resp2: faultInst.append((entry["faultInst"]["attributes"]["code"], entry["faultInst"]["attributes"]["descr"], entry["faultInst"]["attributes"]["cause"], entry["faultInst"]["attributes"]["occur"], entry["faultInst"]["attributes"]["lastTransition"])) #print faultInst fault_lookup[fault_type] = faultInst # Find out the length of the inner list, based off the fault severity in the lookup # eg: fault_lookup["critical"] = [x,y,z] fault_severity_count[fault_type] = len(fault_lookup[fault_type]) # Loop through each severity, sort by faultCode and then print it out for item in fault_severity: print_fault(item, fault_lookup[item]) print "=" * 80 print ("Summary of total faults") print "-" * 80 for fault_summary in fault_severity: print (fault_summary + " = " + str(fault_severity_count[fault_summary])) print "=" * 80
def main(): description = 'Simple application that logs on to the APIC and displays stats for all of the Interfaces.' creds = ACI.Credentials('apic', description) # group = creds.add_mutually_exclusive_group() group1 = creds.add_argument_group('Export', 'Export a cable plan') group1.add_argument('-e', '--export_file', default=None, const='export text', dest='export_file', nargs='?', help='Export cableplan from running fabric. If EXPORT_FILE is specified, the ' 'cableplan will be written to EXPORT_FILE') group2 = creds.add_argument_group('Compare', 'Compare cable plans') group2.add_argument('-c1', '--cableplan1', type=str, nargs=1, default=None, help="Name of cableplan xml file. If only CABLEPLAN1 is specified, " "it will be compared to the running fabric. If it is specified with " "CABLEPLAN2 (the -c2 option), then it will compare CABLEPLAN1 with CABLEPLAN2") group2.add_argument('-c2', '--cableplan2', type=str, nargs=1, default=None, help="Name of second cableplan xml file. The second cableplan file. This file will " "be compared to CABLEPLAN1. This option must only be used " "in conjunction with the -c1 option.") args = creds.get() session = ACI.Session(args.url, args.login, args.password) if args.export_file and (args.cableplan1 or args.cableplan2): creds.print_help() print '\nError: export and compare operations are mutually exclusive' exit() if args.cableplan2 and not args.cableplan1: creds.print_help() print '\nError: -c2 option only valid with -c1 option' exit() if not args.export_file and not args.cableplan1: creds.print_help() print '\nError: Either export (-e) or compare (-c1) is required' exit() if args.export_file: if args.export_file == 'export text': export_to_file(session) else: export_to_file(session, args.export_file) if args.cableplan1: if args.cableplan2: compare_cable_plans(session, args.cableplan1[0], args.cableplan2[0]) else: compare_cable_plans(session, args.cableplan1[0])
def main(): #################### CONFIGURATION MENU ################################################################# print("\n ********* Welcome to the LAB-MANAGER configurator! ********* ") print(" Please provide the following information to set up the labs on the Cisco APIC Sandbox. ") # course_partecipants = int(input(' COURSE PARTECIPANTS NUMBER: ')) validate_partecipants(course_partecipants) lab_id = int(input(' RESTORE TO LAB #: ')) validate_lab(lab_id) # #################### DEFINE LAB LIST #################################################################### # lab = [lab1, lab2, lab3, lab4] # #################### GET CREDENTIALS & APIC LOGIN ####################################################### # ## Credentials # description = 'ACI Fabric Setup' creds = aci.Credentials('apic', description) creds.add_argument('--delete', action='store_true', help='Delete the configuration from the APIC') args = creds.get() # ## Login to the APIC # session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') # #################### CREATE & PUSH ACI CONFIG ########################################################### # ## Define ACI config per lab # tenant_list = [] for l in range(lab_id): tenant_list = lab[l](course_partecipants, tenant_list) # ## Push to APIC # for cp in range(course_partecipants): tenant = tenant_list[cp] resp = session.push_to_apic(tenant.get_url(), tenant.get_json()) if not resp.ok: print('%% Error: Could not push {} configuration to APIC'.format(tenant.name)) print(resp.text) print("\n ********* LABS RESTORED! ********* \n")
def main(): """ Main Show Endpoints Routine """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = ('Simple application to display details about endpoints') creds = aci.Credentials('apic', description) args = creds.get() # Login to APIC session = aci.Session(args.url, args.login, args.password) # session = aci.Session(URL1, LOGIN1, PASSWORD1) resp = session.login() if not resp.ok: print('%% Could not login to APIC') return # Start time count at this point, otherwise takes into consideration the amount of time taken to input the password start_time = time.time() # Download all of the interfaces and store the data as tuples in a list data = [] endpoints = aci.Endpoint.get(session) for ep in endpoints: epg = ep.get_parent() app_profile = epg.get_parent() tenant = app_profile.get_parent() # Check each MAC address via API call to macvendors.com to identify hardware vendor url = "http://api.macvendors.com/" + ep.mac response = requests.request("GET", url) mac_vendor = response.text # Store in list as tuple, that will be printed in the tabulate format data.append((ep.mac, mac_vendor, ep.ip, ep.if_name, ep.encap, tenant.name, app_profile.name, epg.name)) # Display the data downloaded print tabulate(data, headers=["MACADDRESS", "MAC VENDOR", "IPADDRESS", "INTERFACE", "ENCAP", "TENANT", "APP PROFILE", "EPG"], tablefmt="simple") print ("#" * 80) finish_time = time.time() print ("Started @ {}".format(time.asctime(time.localtime(start_time)))) print ("Ended @ {}".format(time.asctime(time.localtime(finish_time)))) print("--- Total Execution Time: %s seconds ---" % (finish_time - start_time)) print ("#" * 80)
def main(): """ Main show Subnets routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = ('Simple application that logs on to the APIC' ' and displays all of the Subnets.') creds = aci.Credentials('apic', description) creds.add_argument('--tenant', help='The name of Tenant') args = creds.get() # Login to APIC session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') # Download all of the tenants, app profiles, and Subnets # and store the names as tuples in a list tenants = aci.Tenant.get(session) for tenant in tenants: check_longest_name(tenant.name, "Tenant") if args.tenant is None: get_subnet(session, tenant) else: if tenant.name == args.tenant: get_subnet(session, tenant) # Display the data downloaded template = '{0:' + str(longest_names["Tenant"]) + '} ' \ '{1:' + str(longest_names["Application Profile"]) + '} ' \ '{2:' + str(longest_names["Bridge Domain"]) + '} ' \ '{3:' + str(longest_names["Subnet"]) + '} ' \ '{4:' + str(longest_names["Scope"]) + '}' print( template.format("Tenant", "Application Profile", "Bridge Domain", "Subnet", "Scope")) print( template.format('-' * longest_names["Tenant"], '-' * longest_names["Application Profile"], '-' * longest_names["Bridge Domain"], '-' * longest_names["Subnet"], '-' * longest_names["Scope"])) for rec in sorted(data): print(template.format(*rec))
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = 'Simple application that logs on to the APIC and displays stats for all of the Interfaces.' creds = ACI.Credentials('apic', description) creds.add_argument('-i', '--interface', type=str, help='Specify a particular interface node/pod/module/port e.g. 1/201/1/21') creds.add_argument('-e', '--epoch', type=int, default=0, help='Show a particular epoch (default=0)') creds.add_argument('-g', '--granularity', type=str, default='5min', help='Show a particular granularity (default="5min")') creds.add_argument('-f', '--full', action="store_true", help='Show full statistics - only available if interface is specified') creds.add_argument('-n', '--nonzero', action='store_true', help='Show only interfaces where the counters are not zero. - only available if interface is NOT specified') args = creds.get() # Login to APIC session = ACI.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') sys.exit(0) # Download all of the interfaces and get their stats # and display the stats if args.interface: interface = args.interface if 'eth ' in interface: interface = interface[4:] (pod, node, module, port) = interface.split('/') interfaces = ACI.Interface.get(session, pod, node, module, port) else: interfaces = ACI.Interface.get(session) if not args.full or not args.interface: show_stats_short(args, interfaces) else: show_stats_long(args, interfaces)
def main(): """ Main create tenant routine :return: None """ # Get all the arguments description = 'It logs in to the APIC and will create the tenant.' creds = aci.Credentials('apic', description) creds.add_argument('-t', '--tenant', help='The name of tenant', default=DEFAULT_TENANT_NAME) creds.add_argument('-c', '--contract', help='The name of contract', default=DEFAULT_CONTRACT_NAME) creds.add_argument('-s', '--contract_sub', help='The name of contract \ subject', default=DEFAULT_CONTRACT_SUB_NAME) creds.add_argument('-f', '--filter', help='The name of filter', default=DEFAULT_CONTRACT_SUB_NAME) args = creds.get() # Login to the APIC session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') # Create the Tenant tenant = aci.Tenant(args.tenant) # Create Contract contract = aci.Contract(args.contract, tenant) contract_sub = aci.ContractSubject(args.contract_sub, contract) # Create Filter filter = aci.Filter(args.filter, contract_sub) # Push the Contract to the APIC resp = session.push_to_apic(tenant.get_url(), tenant.get_json()) if not resp.ok: print('%% Error: Could not push configuration to APIC') print(resp.text)
def main(): """ Main create tenant routine :return: None """ # Get all the arguments description = 'It logs in to the APIC and will create the tenant.' creds = aci.Credentials('apic', description) creds.add_argument('-t', '--tenant', help='The name of tenant', default=DEFAULT_TENANT_NAME) creds.add_argument('-f', '--filter', help='The name of contract', default=DEFAULT_FILTER_NAME) args = creds.get() # Login to the APIC session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') # Create the Tenant tenant = aci.Tenant(args.tenant) # Create Filter filter = aci.Filter(args.filter, tenant) entry1 = aci.FilterEntry(args.filter, applyToFrag='no', arpOpc='unspecified', dFromPort='3306', dToPort='3306', etherT='ip', prot='tcp', sFromPort='1', sToPort='65535', tcpRules='unspecified', parent=filter) # Push the Contract to the APIC resp = session.push_to_apic(tenant.get_url(), tenant.get_json()) if not resp.ok: print('%% Error: Could not push configuration to APIC') print(resp.text)
def main(): description = ( 'Simple application that logs on to the APIC' ' and displays all the tenant info of the contract_interface related to the imported contract.' ) creds = aci.Credentials('apic', description) creds.add_argument("-t", "--tenant_name", help="Tenant Name of where the contract is created") creds.add_argument("-i", "--contract_name", help="Imported Contract Name") args = creds.get() if (args.tenant_name is not None) and (args.contract_name is None): args.contract_name = raw_input("Contract Name: ") session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') data = [] tenants = aci.Tenant.get_deep(session) for tenant in tenants: contracts_interfaces = tenant.get_children( only_class=ContractInterface) for contractInterface in contracts_interfaces: importedContract = contractInterface.get_import_contract() if importedContract is not None: if args.tenant_name is not None: if (importedContract.name == args.contract_name) and ( importedContract.get_parent().name == args.tenant_name): apps = aci.AppProfile.get(session, tenant) for app in apps: epgs = aci.EPG.get(session, app, tenant) for epg in epgs: data.append((importedContract.name, tenant.name, app.name, epg.name)) else: apps = aci.AppProfile.get(session, tenant) for app in apps: epgs = aci.EPG.get(session, app, tenant) for epg in epgs: data.append((importedContract.name, tenant.name, app.name, epg.name)) print tabulate( data, headers=["IMPORTED_CONTRACT", "TENANT", "APP_PROFILE", "EPG"])
def main(): # Get the APIC login credentials description = 'testing tags' creds = aci.Credentials('apic', description) creds.add_argument('--tenant', help='show tags of this tenant if specify') args = creds.get() # Login to APIC and push the config session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') return # Get tenants from the APIC if args.tenant: tenants = aci.Tenant.get_deep(session, limit_to=['fvTenant'], names=[args.tenant]) else: tenants = aci.Tenant.get(session) # get all EPGs with their tag data = [] for tenant in tenants: apps = aci.AppProfile.get(session, tenant) for app in apps: epgs = aci.EPG.get(session, app, tenant) for epg in epgs: tag_list = aci.Tag.get(session, parent=epg, tenant=tenant) if len(tag_list): tag_list = [tag.name for tag in tag_list] if len(tag_list): data.append((tenant.name, app.name, epg.name, ",".join(tag_list))) template = "{0:20} {1:20} {2:20} {3:20}" if len(data): print(template.format("Tenant", "App", "EPG", "Tag")) print(template.format("-" * 20, "-" * 20, "-" * 20, "-" * 20)) for d in data: print(template.format(*d))
def main(): # Get the APIC login credentials description = 'testing tags' creds = aci.Credentials('apic', description) creds.add_argument('--tenant', help='delete this tag from epgs of the given tenant') creds.add_argument('--tag', help='the tag to be deleted') args = creds.get() if not args.tag: print("please pass tag argument") return # Login to APIC and push the config session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') return # Get tenants from the APIC if args.tenant: tenants = aci.Tenant.get_deep(session, limit_to=['fvAp', 'fvAEPg', 'tagInst'], names=[args.tenant]) else: # Get all Tenants within APIC tenants = aci.Tenant.get(session) names_tenants = [tenant.name for tenant in tenants] tenants = aci.Tenant.get_deep(session, limit_to=['fvAp', 'fvAEPg', 'tagInst'], names=names_tenants) # get all EPGs with their tag for tenant in tenants: apps = tenant.get_children(only_class=aci.AppProfile) for app in apps: epgs = app.get_children(only_class=aci.EPG) for epg in epgs: epg.delete_tag(args.tag) resp = tenant.push_to_apic(session) if resp.ok: print('Success') # Print what was sent print('Pushed the following JSON to the APIC') print('URL:', tenant.get_url()) print('JSON:', tenant.get_json())
def main(): """ Main show Subnets routine :return: None """ # Login to APIC description = ('Simple application that logs on to the APIC' ' and displays all of the Subnets.') creds = aci.Credentials('apic', description) args = creds.get() session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') # Download all of the tenants, app profiles, and Subnets # and store the names as tuples in a list data = [] tenants = aci.Tenant.get(session) for tenant in tenants: apps = aci.AppProfile.get(session, tenant) for app in apps: bds = aci.BridgeDomain.get(session, tenant) for bd in bds: subnets = aci.Subnet.get(session, bd, tenant) if len(subnets) == 0: data.append((tenant.name, app.name, bd.name, "", "")) else: for subnet in subnets: data.append( (tenant.name, app.name, bd.name, subnet.addr, subnet.get_scope())) # Display the data downloaded template = "{0:20} {1:20} {2:20} {3:18} {4:15}" print( template.format("Tenant ", "AppProfile ", "BridgeDomain ", "Subnet ", "Scope ")) print( template.format("--------------------", "--------------------", "--------------------", "------------------", "---------------")) for rec in data: print(template.format(*rec))
def main(): """ Main routine :return: None """ # Login to APIC description = ('Simple application that logs on to the APIC' ' and displays all of the External Subnets.') creds = aci.Credentials('apic', description) creds.add_argument('--tenant', help='The name of Tenant') args = creds.get() session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') # Download all of the tenants, app profiles, and Subnets # and store the names as tuples in a list tenants = aci.Tenant.get_deep(session, limit_to=['fvTenant', 'l3extOut', 'l3extInstP', 'l3extSubnet']) for tenant in tenants: check_longest_name(tenant.name, "Tenant") if args.tenant is None: get_external_epg(session, tenant) else: if tenant.name == args.tenant: get_external_epg(session, tenant) # Display the data downloaded template = '{0:' + str(longest_names["Tenant"]) + '} ' \ '{1:' + str(longest_names["L3Out"]) + '} ' \ '{2:' + str(longest_names["External EPG"]) + '} ' \ '{3:' + str(longest_names["Subnet"]) + '} ' \ '{4:' + str(longest_names["Scope"]) + '}' print(template.format("Tenant", "L3Out", "External EPG", "Subnet", "Scope")) print(template.format('-' * longest_names["Tenant"], '-' * longest_names["L3Out"], '-' * longest_names["External EPG"], '-' * longest_names["Subnet"], '-' * longest_names["Scope"])) for rec in sorted(data): print(template.format(*rec))
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = 'Simple application that logs on to the APIC and displays all of the Interfaces.' creds = aci.Credentials('apic', description) args = creds.get() # Login to APIC session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') sys.exit(0) resp = session.get('/api/class/ipv4Addr.json') intfs = json.loads(resp.text)['imdata'] data = {} for i in intfs: ip = i['ipv4Addr']['attributes']['addr'] op = i['ipv4Addr']['attributes']['operSt'] cfg = i['ipv4Addr']['attributes']['operStQual'] dn = i['ipv4Addr']['attributes']['dn'] node = dn.split('/')[2] intf = re.split(r'\[|\]', dn)[1] vrf = re.split(r'/|dom-', dn)[7] if vrf not in data.keys(): data[vrf] = [] else: data[vrf].append((node, intf, ip, cfg, op)) for k in data.keys(): header = 'IP Interface Status for VRF "{}"'.format(k) print header template = "{0:15} {1:10} {2:20} {3:8} {4:10}" print( template.format("Node", "Interface", "IP Address ", "Admin Status", "Status")) for rec in sorted(data[k]): print(template.format(*rec))