Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Smith committed Jul 18, 2017
1 parent 563d46d commit 2cf4767
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 76 deletions.
4 changes: 2 additions & 2 deletions samples/aci-demo-contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ def main():
# Login to APIC and push the config
session = Session(args.url, args.login, args.password)
session.login()

# Cleanup (uncomment the next line to delete the config)
#tenant.mark_as_deleted()
# tenant.mark_as_deleted()
resp = tenant.push_to_apic(session)

if resp.ok:
Expand Down
27 changes: 11 additions & 16 deletions samples/aci-epg-reports-in-yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import socket
import yaml
import sys
from pprint import pprint
import acitoolkit.acitoolkit as aci
from acitoolkit.acitoolkit import Tenant, AppProfile, EPG, Endpoint
from acitoolkit import Credentials, Session, Tenant, AppProfile, EPG, Endpoint


def main():
"""
Expand All @@ -19,9 +18,9 @@ def main():
# Login to APIC
description = ('Simple application that logs on to the APIC'
' and displays all of the EPGs.')
creds = aci.Credentials('apic', description)
creds = Credentials('apic', description)
args = creds.get()
session = aci.Session(args.url, args.login, args.password)
session = Session(args.url, args.login, args.password)
resp = session.login()
if not resp.ok:
print('%% Could not login to APIC')
Expand All @@ -40,23 +39,19 @@ def main():

tenants_dict['app-profiles'] = []
for app in tenant.get_children(AppProfile):
app_profiles = {}
app_profiles['name'] =app.name
app_profiles = {'name': app.name}
if app.descr:
app_profiles['description'] = app.descr
app_profiles['epgs'] = []


for epg in app.get_children(EPG):
epgs_info = {}
epgs_info['name'] = epg.name
epgs_info = {'name': epg.name}
if epg.descr:
epgs_info['description'] = epg.descr
epgs_info['description'] = epg.descr
epgs_info['endpoints'] = []

for endpoint in epg.get_children(Endpoint):
endpoint_info = {}
endpoint_info['name'] = endpoint.name
endpoint_info = {'name': endpoint.name}
if endpoint.ip != '0.0.0.0':
endpoint_info['ip'] = endpoint.ip
try:
Expand All @@ -73,9 +68,9 @@ def main():
tenants_dict['app-profiles'].append(app_profiles)
tenants_list.append(tenants_dict)

tenants_info = {}
tenants_info['tenants'] = tenants_list
print(yaml.safe_dump(tenants_info,sys.stdout, indent= 4,default_flow_style=False))
tenants_info = {'tenants': tenants_list}
print(yaml.safe_dump(tenants_info, sys.stdout,
indent=4, default_flow_style=False))

if __name__ == '__main__':
try:
Expand Down
22 changes: 12 additions & 10 deletions samples/aci-find-ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"""
import sys
from ipaddr import IPNetwork
import acitoolkit.acitoolkit as aci
from acitoolkit import (Credentials, Session, Tenant, AppProfile, BridgeDomain,
Subnet, OutsideL3, OutsideEPG, OutsideNetwork)


def main():
"""
Expand All @@ -16,11 +18,11 @@ def main():
# 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 = Credentials('apic', description)
creds.add_argument('-f', '--find_ip', help='IP address to search for')
args = creds.get()

session = aci.Session(args.url, args.login, args.password)
session = Session(args.url, args.login, args.password)
resp = session.login()
if not resp.ok:
print('%% Could not login to APIC')
Expand All @@ -35,30 +37,30 @@ def main():
priv = []
publ = []
ip = args.find_ip
tenants = aci.Tenant.get_deep(session, limit_to=['fvTenant',
tenants = Tenant.get_deep(session, limit_to=['fvTenant',
'fvSubnet',
'l3extOut',
'l3extInstP',
'l3extSubnet'])

for tenant in tenants:
apps = aci.AppProfile.get(session, tenant)
apps = AppProfile.get(session, tenant)
for app in apps:
bds = aci.BridgeDomain.get(session, tenant)
bds = BridgeDomain.get(session, tenant)
for bd in bds:
subnets = aci.Subnet.get(session, bd, tenant)
subnets = Subnet.get(session, bd, tenant)
for subnet in subnets:
net = IPNetwork(subnet.addr)
if net.Contains(IPNetwork(ip)):
priv.append((tenant.name, app.name, bd.name,
subnet.addr, subnet.get_scope()))

for tenant in tenants:
outside_l3s = tenant.get_children(only_class=aci.OutsideL3)
outside_l3s = tenant.get_children(only_class=OutsideL3)
for outside_l3 in outside_l3s:
outside_epgs = outside_l3.get_children(only_class=aci.OutsideEPG)
outside_epgs = outside_l3.get_children(only_class=OutsideEPG)
for outside_epg in outside_epgs:
outside_networks = outside_epg.get_children(only_class=aci.OutsideNetwork)
outside_networks = outside_epg.get_children(only_class=OutsideNetwork)
for outside_network in outside_networks:
net = IPNetwork(outside_network.addr)
if net.Contains(IPNetwork(ip)):
Expand Down
30 changes: 22 additions & 8 deletions samples/aci-show-epgs.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#!/usr/bin/env python

"""
Simple application that logs on to the APIC and displays all
EPGs.
"""
import acitoolkit.acitoolkit as aci
from acitoolkit import Credentials, Session, Tenant, AppProfile, EPG

data = []
longest_names = {'Tenant': len('Tenant'),
'Application Profile': len('Application Profile'),
'EPG': len('EPG')}


def main():
"""
Main show EPGs routine
Expand All @@ -18,18 +19,18 @@ def main():
# Login to APIC
description = ('Simple application that logs on to the APIC'
' and displays all of the EPGs.')
creds = aci.Credentials('apic', description)
creds = Credentials('apic', description)
creds.add_argument('--tenant', help='The name of Tenant')
args = creds.get()

session = aci.Session(args.url, args.login, args.password)
session = 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
tenants = aci.Tenant.get(session)
tenants = Tenant.get(session)
for tenant in tenants:
check_longest_name(tenant.name, "Tenant")
if args.tenant is None:
Expand All @@ -42,26 +43,39 @@ def main():
template = '{0:' + str(longest_names["Tenant"]) + '} ' \
'{1:' + str(longest_names["Application Profile"]) + '} ' \
'{2:' + str(longest_names["EPG"]) + '}'
print(template.format("Tenant", "Application Profile","EPG"))
print(template.format("Tenant", "Application Profile", "EPG"))
print(template.format('-' * longest_names["Tenant"],
'-' * longest_names["Application Profile"],
'-' * longest_names["EPG"]))
for rec in sorted(data):
print(template.format(*rec))


def get_epg(session, tenant):
apps = aci.AppProfile.get(session, tenant)
"""
Get the EPG
:param session: Session class instance
:param tenant: String containing the Tenant name
"""
apps = AppProfile.get(session, tenant)
for app in apps:
check_longest_name(app.name, "Application Profile")
epgs = aci.EPG.get(session, app, tenant)
epgs = EPG.get(session, app, tenant)
for epg in epgs:
check_longest_name(epg.name, "EPG")
data.append((tenant.name, app.name, epg.name))


def check_longest_name(item, title):
"""
Check the longest name
:param item: String containing the name
:param title: String containing the column title
"""
if len(item) > longest_names[title]:
longest_names[title] = len(item)


if __name__ == '__main__':
try:
main()
Expand Down
35 changes: 25 additions & 10 deletions samples/aci-show-external-networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
Simple application that logs on to the APIC and displays all of
the External Networks.
"""
import acitoolkit.acitoolkit as aci
from acitoolkit import *

data = []
longest_names = {'Tenant': len('Tenant'),
'L3Out': len('L3Out'),
'External EPG': len('External EPG'),
'Subnet': len('Subnet'),
'Scope': len('Scope')}


def main():
"""
Main routine
Expand All @@ -20,21 +22,21 @@ def main():
# 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 = Credentials('apic', description)
creds.add_argument('--tenant', help='The name of Tenant')
args = creds.get()

session = aci.Session(args.url, args.login, args.password)
session = 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'])
tenants = 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:
Expand All @@ -58,14 +60,20 @@ def main():
for rec in sorted(data):
print(template.format(*rec))


def get_external_epg(session, tenant):
outside_l3s = tenant.get_children(only_class=aci.OutsideL3)
"""
Get the external EPGs
:param session: Session class instance
:param tenant: String containing the Tenant name
"""
outside_l3s = tenant.get_children(only_class=OutsideL3)
for outside_l3 in outside_l3s:
check_longest_name(outside_l3.name, "L3Out")
outside_epgs = outside_l3.get_children(only_class=aci.OutsideEPG)
outside_epgs = outside_l3.get_children(only_class=OutsideEPG)
for outside_epg in outside_epgs:
check_longest_name(outside_epg.name, "External EPG")
outside_networks = outside_epg.get_children(only_class=aci.OutsideNetwork)
outside_networks = outside_epg.get_children(only_class=OutsideNetwork)
if len(outside_networks) == 0:
data.append((tenant.name, outside_l3.name, outside_epg.name, "", ""))
else:
Expand All @@ -78,10 +86,17 @@ def get_external_epg(session, tenant):
outside_network.addr,
outside_network.get_scope()))


def check_longest_name(item, title):
"""
Check the longest name
:param item: String containing the name
:param title: String containing the column title
"""
if len(item) > longest_names[title]:
longest_names[title] = len(item)


if __name__ == '__main__':
try:
main()
Expand Down
37 changes: 21 additions & 16 deletions samples/aci-show-imported-contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,53 @@
"""
Find out where a contract has been imported and consumed on an EPG.
"""
import acitoolkit.acitoolkit as aci
from acitoolkit.acitoolkit import *
from acitoolkit import (Credentials, Session, Tenant, ContractInterface, AppProfile,
EPG)
from tabulate import tabulate

data = []


def main():
"""
Main execution routine
"""
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 = 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)
session = Session(args.url, args.login, args.password)
resp = session.login()
if not resp.ok:
print('%% Could not login to APIC')

tenants = aci.Tenant.get_deep(session)
tenants = 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:
for contract_interface in contracts_interfaces:
imported_contract = contract_interface.get_import_contract()
if imported_contract 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)
if (imported_contract.name == args.contract_name) and (imported_contract.get_parent().name == args.tenant_name):
apps = AppProfile.get(session, tenant)
for app in apps:
epgs = aci.EPG.get(session, app, tenant)
epgs = EPG.get(session, app, tenant)
for epg in epgs:
data.append((importedContract.name,tenant.name, app.name, epg.name))
data.append((imported_contract.name, tenant.name, app.name, epg.name))
else:
apps = aci.AppProfile.get(session, tenant)
apps = AppProfile.get(session, tenant)
for app in apps:
epgs = aci.EPG.get(session, app, tenant)
epgs = 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"])
data.append((imported_contract.name, tenant.name, app.name, epg.name))
print tabulate(data, headers=["IMPORTED_CONTRACT", "TENANT", "APP_PROFILE", "EPG"])


if __name__ == '__main__':
main()

0 comments on commit 2cf4767

Please sign in to comment.