Guest User

Untitled

a guest
Jul 11th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. from bcf_methods import BigCloudFabric
  2. import pprint
  3. import csv
  4. import yaml
  5.  
  6. def main():
  7.  
  8. segment_info = []
  9. with open('/home/nhanlon/Downloads/Kayak-SomervilleNetwork-Discovery - Segments.csv', 'rb') as f:
  10. data = csv.DictReader(
  11. f,
  12. fieldnames=['Tenant', 'VlanID', 'VlanDescription', 'Subnet', 'Routers', 'RouterIPs', 'FirewallIPs', 'SwitchIPs', 'OSPFArea', 'Comments'],
  13. delimiter=',',
  14. quotechar='"',
  15. )
  16. next(data, None) # Skip over headers
  17. for i in data:
  18. segment_info.append(i)
  19.  
  20. api = BigCloudFabric(controller_ip="<redacted>", username="admin", password='<redacted>')
  21.  
  22.  
  23. for segment in segment_info:
  24. print(segment)
  25. tenant_name = segment['Tenant']
  26. vlan_id = segment['VlanID']
  27. segment_name = segment['VlanDescription']
  28. subnet = segment['Subnet']
  29.  
  30. res = api.create_tenant(tenant_name)
  31. if not res:
  32. print("Tenant {} already exists".format(tenant_name))
  33. else:
  34. print("Created Tenant: {}".format(tenant_name))
  35.  
  36. description = yaml.safe_dump(segment, default_flow_style=False)
  37.  
  38. # Segments can't contain spaces. Make them underscores?
  39. segment_name = segment_name.replace(" ", "_")
  40.  
  41. # Segment names can't contain Slashes, take everything after the last /
  42. segment_name = segment_name[(segment_name.rfind('/')+1):len(segment_name)]
  43.  
  44. res = api.create_segment(tenant_name, segment_name, description)
  45. if not res:
  46. print("Segment '{}' already exists in Tenant '{}'".format(segment_name, tenant_name))
  47. else:
  48. print("Created Segment '{}' in Tenant '{}'".format(segment_name, tenant_name))
  49.  
  50. print len(segment_info)
  51.  
  52. production_segments = api.get_segments('Production')
  53.  
  54. pprint.pprint(production_segments[0])
  55.  
  56. pprint.pprint(yaml.load(production_segments[0]['description']))
  57.  
  58.  
  59.  
  60.  
  61. if __name__ == '__main__':
  62. main()
Add Comment
Please, Sign In to add comment