Advertisement
Guest User

Untitled

a guest
Nov 4th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 32.01 KB | None | 0 0
  1. #!/usr/bin/python
  2. # edge redeploy tool v 0.3 grb June 18 2016
  3. #
  4. # v 0.1 milestones:
  5. #   > fetch XML > COMPLETE
  6. #   > build XML API calls to rebuild > COMPLETE
  7. #   > send XML api calls to build > COMPLETE
  8. #  
  9. #   testing:
  10. #   > 15 attempts and much tweaking successful redeploy of original edge-20.xml with minimal firewall dhcp routing ha nat config
  11. #
  12. # v 0.2 milestones:
  13. #   > expand and configure everything in test edge and redeploy
  14. #   > tested OSPF, IPSEC, DNS to minimal config > COMPLETE
  15. #   > IPSEC PSK is LOST needs manual reset or edit of xml by hand before -R step edit at your own peril
  16. #   > tested minimal load balancer, syslog, LARGE size config > COMPLETE
  17. #   > added howto/example
  18. #   > ssl_vpn works but any localusers are lost / need to be recreated
  19. #   > either ther api is broken for adding/creating all users
  20. #   > or i can't figure it out :(
  21. #   > minimal config tested shortlist: ipsec,firewall,routing,Syslog,dhcp,lb,ha,nat,dns,subinterfaces,l2vpn
  22. #       > sub interfaces may havebroken HA
  23. #   > they did... would need a rewrite to force rebuild order to be HA > subinterfaces > l2Vpn as rebuild order atm
  24. #   > added exclude subInterface option for -B
  25. #  
  26. # v 0.3 milestones: June 19 2016
  27. #   > removed exclude subInterface option
  28. #   > rewrite rebuild order > COMPLETE > pending handler for subInterface > COMPETE
  29. #   > rewrite subInterface to secondary file posted after HA > COMPLETE
  30. #   > expand testing to large firewall rule sets (200/500/1000) see if it needs to be broken up into multiple puts # tested with up to 328 rules
  31. #   # see if we can fix ssl_vpn users? > WONTFIX
  32. #  
  33. #
  34. # howto:  Get > Rebuild API calls > Build;   The Rebuild, and Build step will state wether a component is enabled or successfully configured (204)
  35. #       Get XML Config of existing edge that needs clean redeploy
  36. #   ./edge_rd_tool.py -G edge-20 -n nsxmanager -u admin -p default > edge-20.xml
  37. #
  38. #   Rebuild/chop the configs up into smaller API POST/PUTs; creates/overwrites rebuild_<name>.xml* files.
  39. #   ./edge_rd_tool.py -B edge-20.xml
  40. #
  41. #   Build the edge with rebuild_<name>.xml and rebuild_<name>.xml.<feature> files
  42. #   ./edge_rd_tool.py -R rebuild_edge-20.xml -n nsxmanager -u admin -p default
  43. #  
  44. # example:
  45. #  gavin@ssh:~/edge$ ./edge_rd_tool.py -G edge-20 -n valscar -u admin -p default > edge-20.xml
  46. #
  47. #  gavin@ssh:~/edge$ ./edge_rd_tool.py -B edge-20.xml
  48. #  rebuilding: edge-20
  49. #  named: TestyMcEdge
  50. #  features:
  51. #  l2Vpn not enabled
  52. #  firewall enabled
  53. #  sslvpnConfig not enabled
  54. #  dns enabled
  55. #  routing enabled
  56. #  highAvailability enabled
  57. #  syslog enabled
  58. #  loadBalancer enabled
  59. #  gslb not enabled
  60. #  ipsec enabled
  61. #  dhcp enabled
  62. #  nat enabled
  63. #  bridges not enabled
  64. #
  65. #  gavin@ssh:~/edge$ ls rebuild_* edge-20.xml
  66. #  edge-20.xml               rebuild_edge-20.xml.dns               rebuild_edge-20.xml.ipsec         rebuild_edge-20.xml.routing
  67. #  rebuild_edge-20.xml       rebuild_edge-20.xml.firewall          rebuild_edge-20.xml.loadBalancer  rebuild_edge-20.xml.syslog
  68. #  rebuild_edge-20.xml.dhcp  rebuild_edge-20.xml.highAvailability  rebuild_edge-20.xml.nat
  69. #
  70. #  gavin@ssh:~/edge$ ./edge_rd_tool.py -R rebuild_edge-20.xml -n nsxmanager -u admin -p default
  71. #  Got new Edge API Path: /api/4.0/edges/edge-38
  72. #  Sending ipsec config
  73. #  204
  74. #  Sending Firewall config
  75. #  204
  76. #  Sending routing config
  77. #  204
  78. #  Sending dhcp config
  79. #  204
  80. #  Sending LB config
  81. #  204
  82. #  Sending HA config
  83. #  204
  84. #  Sending nat config
  85. #  204
  86. #  Sending Dns config
  87. #  204
  88. #
  89. #
  90. #
  91. # BEWARE - THERE BE DRAGONS!!
  92.  
  93. import sys, requests, getopt, glob
  94. import xml.etree.ElementTree as ET
  95.  
  96. from requests.packages.urllib3.exceptions import InsecureRequestWarning
  97.  
  98. requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
  99.  
  100. # Define functions used by the script
  101.  
  102.  
  103. def nsxgetrest ( uri, username, passwd ):
  104.     "This function makes an NSX REST call with the GET method"
  105.     r = requests.get(uri, auth=(username,passwd), headers={'Content-Type': 'application/xml','Accept': "application/xml"}, verify=False)
  106.     return r
  107.  
  108. def nsxpmrest ( uri, username, passwd, payload, method ):
  109.     "This function makes an NSX REST call with a payload using the specified method"
  110.     if method == "post" or method == "POST":
  111.         r = requests.post(uri, auth=(username,passwd), headers={'Content-Type': 'application/xml','Accept': "application/xml"}, data = payload, verify=False)
  112.     elif method == "put" or method == "PUT":
  113.         r = requests.put(uri, auth=(username,passwd), headers={'Content-Type': 'application/xml','Accept': "application/xml"}, data = payload, verify=False)
  114.     elif method =="delete" or method == "DELETE":
  115.         r = requests.delete(uri, auth=(username,passwd), headers={'Content-Type': 'application/xml','Accept': "application/xml"}, verify=False)
  116.     else:
  117.         return false
  118.     return r
  119. def loadfeaturefile (feature):
  120.     "This function loads a file"
  121.     rbef_feature_file = open (feature,'r')
  122.     payload_data = rbef_feature_file.read()
  123.     rbef_feature_file.close()
  124.     return payload_data
  125.  
  126. def print_my_help ():
  127.     "This function prints my help message"
  128.     print "edge_rd_tool.py -hGBRDs -n <nsx_fqdn> -u <username> -p <password>"
  129.     print
  130.     print " -h : this help "
  131.     print " -G <edgeid>         | --edge <edgeid> "
  132.     print "     gets the Edge XML from NSX Manager"
  133.     print "     redirect output to a file to process with the -B option"
  134.     print
  135.     print " -B <edgexmlfile>    | --edgexml <edgexmlfile> "
  136.     print "     creates a Rebuild XML, will emit messages indicating each section that"
  137.     print "     needs an additional REST call "
  138.     print "     ::warning the Edge name will be changed from <name> to <name>_rebuild"
  139.     print "     ::warning the Edge password will be changed to Default12!Default12!"
  140.     print "     ::warning to change either of these values manually edit the xml files are your own peril"
  141.     print "     ::warning IPSEC / SSL VPN PSKs will be lost and literally changed to *'s"
  142.     print "     ::warning SSL VPN local users are lost due to an API issue"
  143.     print
  144.     print "     a new xml file will be created named rebuild_<xmlfilename>"
  145.     print "     other files may be generated/overwritten depending if features are enabled or not"
  146.     print "     naming format will be rebuild_<xmlfilename>.<featurename>"
  147.     print "     Example if you specify 'edge-20.xml' as the <edgexmlfile>"
  148.     print "     the following files will appear:"
  149.     print "         rebuild_edge-20.xml"
  150.     print "         rebuild_edge-20.xml.firewall"
  151.     print "         rebuild_edge-20.xml.dns"
  152.     print
  153.     print " -R <rebuildxmlfile> | --rebuild <rebuildxmlfile> "
  154.     print "     sends NSX REST calls to NSX Manager to recreate the Edge"
  155.     print
  156.     print " -D <edgeapipath>"
  157.     print "     deletes an Edge"
  158.     print
  159.     print "-n <nsx_fqdn>"
  160.     print "     NSX manager to communicate with"
  161.     print
  162.     print "-u <username>"
  163.     print "     Username to use for NSX Manager communication"
  164.     print
  165.     print "-p <password>"
  166.     print "     Password to use for NSX Manager communication"
  167.     print "Options n, u, p are only required for G, R, and D"
  168.    
  169.  
  170. #Parse options / Print Help Message
  171. try:
  172.     opts, args = getopt.getopt(sys.argv[1:],"hG:B:R:D:n:u:p:",['edge=','edgexml=','rebuild='])
  173. except getopt.GetoptError:
  174.     print_my_help()
  175.     sys.exit(2)
  176.  
  177. if len(opts) == 0:
  178.     print_my_help()
  179.     sys.exit(2)
  180.  
  181. for opt, arg in opts:
  182.     if opt in ("-G","--edge"):
  183.         my_edge = arg
  184.         my_action = 1
  185.     elif opt in ("-B","--edgexml"):
  186.         my_edge_xml_file = arg
  187.         my_action = 2
  188.     elif opt in ("-R","--rebuild"):
  189.         my_edge_rebuild_file_name = arg
  190.         my_action = 3
  191.     elif opt == "-D":
  192.         my_edge_api_path = arg
  193.         my_action = 4
  194.     elif opt == '-u':
  195.         my_username = arg
  196.     elif opt == '-n':
  197.         my_nsxmanager = arg
  198.     elif opt == '-p':
  199.         my_password = arg
  200.     elif opt == '-h':
  201.         print_my_help()
  202.         sys.exit(2)
  203.  
  204. if my_action == 1 or my_action == 3 or my_action == 4:
  205.     paramTest = 0
  206.     if 'my_username' not in locals():
  207.         print "Error: Missing Username (-u <username>)"
  208.         paramTest = 1
  209.     if 'my_nsxmanager' not in locals():
  210.         print "Error: Missing NSX Manager (-n <nsxmanager_fqdn>)"
  211.         paramTest = 1
  212.     if 'my_password' not in locals():
  213.         print "Error: Missing Password (-p <password>)"
  214.         paramTest = 1
  215.     if paramTest == 1:
  216.         print_my_help()
  217.         sys.exit(2)
  218.  
  219. # Simply get and output the Edge XML data from NSX Manager
  220. if my_action == 1:
  221.     uri = "https://" + my_nsxmanager + "/api/4.0/edges/" + my_edge
  222.     edge_request =  nsxgetrest(uri,my_username,my_password)
  223.     print edge_request.content
  224.  
  225. if my_action == 4:
  226.     if my_edge_api_path.find("edge-") == -1:
  227.         print "edge-id not found"
  228.         print_my_help()
  229.         sys.exit(2)
  230.  
  231.     uri = "https://" + my_nsxmanager + my_edge_api_path
  232.     edge_request = nsxpmrest(uri,my_username,my_password,"","DELETE")
  233.     print edge_request.status_code
  234.     if edge_request.content is not None:
  235.         print edge_request.content
  236.    
  237. # Parse an existing Edge definition and recreate seperate XML files
  238. # to rebuild the edge, and reconfigure it's features
  239. # if a tag is not processed
  240. # it either caused problems on rebuild (such as id/version/names of objects)
  241. # or is not part of the API doc nsx_62_api.pdf
  242. # to rebuild an edge you must first post the base information, I've encountered issues when sending PUTs for "ALL" edge config
  243. # either due to size of request or certain features erroring our due to "interface" configuration not being in place
  244. # and then make PUT statements to update the <features> sections returned by the GET above
  245. #
  246.  
  247.  
  248. elif my_action == 2:
  249.     rbef = ET.Element("edge")
  250.     edge_xml_tree = ET.parse(my_edge_xml_file)
  251.     exroot = edge_xml_tree.getroot()
  252.     for child in exroot:
  253.         if child.tag == 'id':
  254.             print "rebuilding: " + child.text
  255.         if child.tag == 'datacenterMoid':
  256.             xdcMoid = ET.SubElement(rbef,'datacenterMoid')
  257.             xdcMoid.text = child.text
  258.  
  259.         if child.tag == 'tenant':
  260.             xtenant = ET.SubElement(rbef,'tenant')
  261.             xtenant.text = child.text
  262.  
  263.         if child.tag == 'name':
  264.             xname = ET.SubElement(rbef,'name')
  265.             xname.text = child.text+"_rebuild"
  266.             print "named: " + child.text
  267.  
  268.         if child.tag == 'fqdn':
  269.             xfqdn = ET.SubElement(rbef,'fqdn')
  270.             xfqdn.text = child.text
  271.  
  272.         if child.tag == 'enableAesni':
  273.             xaes = ET.SubElement(rbef,'enableAesni')
  274.             xaes.text = child.text
  275.  
  276.         if child.tag == 'enableFips':
  277.             xfips = ET.SubElement(rbef,'enableFips')
  278.             xfips.text = child.text
  279.  
  280.         if child.tag == 'vseLogLevel':
  281.             xlogl = ET.SubElement(rbef,'vseLogLevel')
  282.             xlogl.text = child.text
  283.  
  284.         if child.tag == 'vnics':
  285.             xvnics = ET.SubElement(rbef,"vnics")
  286.             for vnic in child:
  287.                 if vnic.tag == "vnic" and vnic.find("./subInterfaces") is None:
  288.                     xvnic = ET.SubElement(xvnics,"vnic")
  289.                     for vChild in vnic:
  290.                         if vChild.tag == 'label':
  291.                             xvnicl = ET.SubElement(xvnic,"label")
  292.                             xvnicl.text = vChild.text
  293.                         if vChild.tag == 'name':
  294.                             xvnicname = ET.SubElement(xvnic,"name")
  295.                             xvnicname.text = vChild.text
  296.                         if vChild.tag == 'mtu':
  297.                             xvnicmtu = ET.SubElement(xvnic,"mtu")
  298.                             xvnicmtu.text = vChild.text
  299.                         if vChild.tag == 'type':
  300.                             xvnictype = ET.SubElement(xvnic,"type")
  301.                             xvnictype.text = vChild.text
  302.                         if vChild.tag == "isConnected":
  303.                             xvniciscon = ET.SubElement(xvnic,"isConnected")
  304.                             xvniciscon.text = vChild.text
  305.                         if vChild.tag == "index":
  306.                             xvnicindex = ET.SubElement(xvnic,"index")
  307.                             xvnicindex.text = vChild.text
  308.                         if vChild.tag == "portgroupId":
  309.                             xvnicpg = ET.SubElement(xvnic,"portgroupId")
  310.                             xvnicpg.text = vChild.text
  311.                         if vChild.tag == "enableProxyArp":
  312.                             xvnicepa = ET.SubElement(xvnic,"enableProxyArp")
  313.                             xvnicepa.text = vChild.text
  314.                         if vChild.tag == "enableSendRedirects":
  315.                             xvnicesr = ET.SubElement(xvnic,"enableSendRedirects")
  316.                             xvnicesr.text = vChild.text
  317.                         if vChild.tag == "addressGroups":
  318.                             xvnicags = ET.SubElement(xvnic,"addressGroups")
  319.                             for ags in vChild:
  320.                                 xvnicag = ET.SubElement(xvnicags,"addressGroup")
  321.                                 for ag in ags:
  322.                                     if ag.tag == "primaryAddress":
  323.                                         xvnicagpa = ET.SubElement(xvnicag,"primaryAddress")
  324.                                         xvnicagpa.text = ag.text
  325.                                     if ag.tag == "subnetMask":
  326.                                         xvnicagsm = ET.SubElement(xvnicag,"subnetMask")
  327.                                         xvnicagsm.text = ag.text
  328.                                     if ag.tag == "subnetPrefixLength":
  329.                                         xvnicagpl = ET.SubElement(xvnicag,"subnetPrefixLength")
  330.                                         xvnicagpl.text = ag.text
  331.                                     if ag.tag == 'secondaryAddresses':
  332.                                         xvnicagsa = ET.SubElement(xvnicag,"secondaryAddresses")
  333.                                         for ipaddr in ag:
  334.                                             if ipaddr.tag == 'ipAddress':
  335.                                                 xvnicagsaip = ET.SubElement(xvnicagsa,"ipAddress")
  336.                                                 xvnicagsaip.text = ipaddr.text
  337.                 if vnic.tag == "vnic" and vnic.find("./subInterfaces") is not None:
  338.                     xvnic = ET.Element("vnic")
  339.                     for vChild in vnic:
  340.                         if vChild.tag == 'label':
  341.                             xvnicl = ET.SubElement(xvnic,"label")
  342.                             xvnicl.text = vChild.text
  343.                         if vChild.tag == 'name':
  344.                             xvnicname = ET.SubElement(xvnic,"name")
  345.                             xvnicname.text = vChild.text
  346.                         if vChild.tag == 'mtu':
  347.                             xvnicmtu = ET.SubElement(xvnic,"mtu")
  348.                             xvnicmtu.text = vChild.text
  349.                         if vChild.tag == 'type':
  350.                             xvnictype = ET.SubElement(xvnic,"type")
  351.                             xvnictype.text = vChild.text
  352.                         if vChild.tag == "isConnected":
  353.                             xvniciscon = ET.SubElement(xvnic,"isConnected")
  354.                             xvniciscon.text = vChild.text
  355.                         if vChild.tag == "index":
  356.                             xvnicindex = ET.SubElement(xvnic,"index")
  357.                             xvnicindex.text = vChild.text
  358.                         if vChild.tag == "portgroupId":
  359.                             xvnicpg = ET.SubElement(xvnic,"portgroupId")
  360.                             xvnicpg.text = vChild.text
  361.                         if vChild.tag == "enableProxyArp":
  362.                             xvnicepa = ET.SubElement(xvnic,"enableProxyArp")
  363.                             xvnicepa.text = vChild.text
  364.                         if vChild.tag == "enableSendRedirects":
  365.                             xvnicesr = ET.SubElement(xvnic,"enableSendRedirects")
  366.                             xvnicesr.text = vChild.text
  367.                         if vChild.tag == "subInterfaces":
  368.                             xvnicsis = ET.SubElement(xvnic,"subInterfaces")
  369.                             for vnicsis in vChild:
  370.                                 if vnicsis.tag == "subInterface":
  371.                                     xvnicsi = ET.SubElement(xvnicsis,"subInterface")
  372.                                     for vnicsi in vnicsis:
  373.                                         if vnicsi.tag == "isConnected":
  374.                                             xvnicsicon = ET.SubElement(xvnicsi,"isConnected")
  375.                                             xvnicsicon.text = vnicsi.text
  376.                                         if vnicsi.tag =="label":
  377.                                             xvnicsilab = ET.SubElement(xvnicsi,"label")
  378.                                             xvnicsilab.text = vnicsi.text
  379.                                         if vnicsi.tag =="name":
  380.                                             xvnicsinam = ET.SubElement(xvnicsi,"name")
  381.                                             xvnicsinam.text = vnicsi.text
  382.                                         if vnicsi.tag =="tunnelId":
  383.                                             xvnicsitun = ET.SubElement(xvnicsi,"tunnelId")
  384.                                             xvnicsitun.text = vnicsi.text
  385.                                         if vnicsi.tag =="mtu":
  386.                                             xvnicsimtu = ET.SubElement(xvnicsi,"mtu")
  387.                                             xvnicsimtu.text = vnicsi.text
  388.                                         if vnicsi.tag =="vlanId":
  389.                                             xvnicsivln = ET.SubElement(xvnicsi,"vlanId")
  390.                                             xvnicsivln.text = vnicsi.text
  391.                                         if vnicsi.tag =="enableSendRedirects":
  392.                                             xvnicsiesr = ET.SubElement(xvnicsi,"enableSendRedirects")
  393.                                             xvnicsiesr.text = vnicsi.text
  394.                                         if vnicsi.tag =="addressGroups":
  395.                                                                 xvnicsiags = ET.SubElement(xvnicsi,"addressGroups")
  396.                                                                 for ags in vnicsi:
  397.                                                                     xvnicsiag = ET.SubElement(xvnicsiags,"addressGroup")
  398.                                                                         for ag in ags:
  399.                                                                                     if ag.tag == "primaryAddress":
  400.                                                                                             xvnicsiagpa = ET.SubElement(xvnicsiag,"primaryAddress")
  401.                                                                                             xvnicsiagpa.text = ag.text
  402.                                                                                     if ag.tag == "subnetMask":
  403.                                                                                             xvnicsiagsm = ET.SubElement(xvnicsiag,"subnetMask")
  404.                                                                                             xvnicsiagsm.text = ag.text
  405.                                                                                     if ag.tag == "subnetPrefixLength":
  406.                                                                                             xvnicsiagpl = ET.SubElement(xvnicsiag,"subnetPrefixLength")
  407.                                                                                             xvnicsiagpl.text = ag.text
  408.                                                                                     if ag.tag == 'secondaryAddresses':
  409.                                                                                             xvnicsiagsa = ET.SubElement(xvnicsiag,"secondaryAddresses")
  410.                                                                                             for ipaddr in ag:
  411.                                                                                                     if ipaddr.tag == 'ipAddress':
  412.                                                                                                         xvnicsiagsaip = ET.SubElement(xvnicsiagsa,"ipAddress")
  413.                                                                                                             xvnicsiagsaip.text = ipaddr.text
  414.                         if vChild.tag == "addressGroups":
  415.                             xvnicags = ET.SubElement(xvnic,"addressGroups")
  416.                             for ags in vChild:
  417.                                 xvnicag = ET.SubElement(xvnicags,"addressGroup")
  418.                                 for ag in ags:
  419.                                     if ag.tag == "primaryAddress":
  420.                                         xvnicagpa = ET.SubElement(xvnicag,"primaryAddress")
  421.                                         xvnicagpa.text = ag.text
  422.                                     if ag.tag == "subnetMask":
  423.                                         xvnicagsm = ET.SubElement(xvnicag,"subnetMask")
  424.                                         xvnicagsm.text = ag.text
  425.                                     if ag.tag == "subnetPrefixLength":
  426.                                         xvnicagpl = ET.SubElement(xvnicag,"subnetPrefixLength")
  427.                                         xvnicagpl.text = ag.text
  428.                                     if ag.tag == 'secondaryAddresses':
  429.                                         xvnicagsa = ET.SubElement(xvnicag,"secondaryAddresses")
  430.                                         for ipaddr in ag:
  431.                                             if ipaddr.tag == 'ipAddress':
  432.                                                 xvnicagsaip = ET.SubElement(xvnicagsa,"ipAddress")
  433.                                                 xvnicagsaip.text = ipaddr.text
  434.                     #
  435.                     rebuild_xmlstring = ET.tostring(xvnic)
  436.                     rebuild_file_name = "rebuild_" + my_edge_xml_file + ".subInterface." + xvnicindex.text
  437.                     rebuild_file = open(rebuild_file_name,"w")
  438.                     rebuild_file.write(rebuild_xmlstring)
  439.                     rebuild_file.close()
  440.         if child.tag == 'appliances':
  441.             xapps = ET.SubElement(rbef,"appliances")
  442.             for appChild in child:
  443.                 if appChild.tag == 'applianceSize':
  444.                     xappSize = ET.SubElement(xapps,"applianceSize")
  445.                     xappSize.text = appChild.text
  446.                 if appChild.tag =='appliance':
  447.                     xapp = ET.SubElement(xapps,"appliance")
  448.                     for vappChild in appChild:
  449.                         if vappChild.tag == "resourcePoolId":
  450.                             xapprpid = ET.SubElement(xapp,"resourcePoolId")
  451.                             xapprpid.text = vappChild.text
  452.                         if vappChild.tag == "datastoreId":
  453.                             xappdsid = ET.SubElement(xapp,"datastoreId")
  454.                             xappdsid.text = vappChild.text
  455.                         if vappChild.tag == "vmFolderId":
  456.                             xappfldid = ET.SubElement(xapp,"vmFolderId")
  457.                             xappfldid.text = vappChild.text
  458.         if child.tag == 'cliSettings':
  459.             xcli = ET.SubElement(rbef,'cliSettings')
  460.             for cliChild in child:
  461.                 if cliChild.tag == 'remoteAccess':
  462.                     xclira = ET.SubElement(xcli,"remoteAccess")
  463.                     xclira.text = cliChild.text
  464.                 if cliChild.tag == 'userName':
  465.                     xusername = ET.SubElement(xcli,"userName")
  466.                     xusername.text = cliChild.text
  467.                 if cliChild.tag == 'sshLoginBannerText':
  468.                     xsshbanner = ET.SubElement(xcli,"sshLoginBannerText")
  469.                     xsshbanner.text = cliChild.text
  470.                 if cliChild.tag == 'passwordExpiry':
  471.                     xpwexp = ET.SubElement(xcli,"passwordExpiry")
  472.                     xpwexp.text = cliChild.text
  473.  
  474.             xpassword = ET.SubElement(xcli,"password")
  475.             xpassword.text = "Default12!Default12!"
  476.         if child.tag == 'features':
  477.             print "features:"
  478.             # here we can almost cartblanche use the xml contexts returned
  479.             # so we dump the XML string content and rebuild a new xml entity
  480.             # and strip out the versions tag
  481.             # firewall does not like this # now working with own handler
  482.             # nat does not like this # now working with own handler
  483.             # no docs for gslb  
  484.             # l2vpn requires password blankin
  485.             for features in child:
  486.                 clip_xmlstring = ET.tostring(features)
  487.                 rb_feat = ET.fromstring(clip_xmlstring)
  488.                 for ver in rb_feat.findall("version"):
  489.                     rb_feat.remove(ver)
  490.                 en = rb_feat.find("./enabled")
  491.                 if en is not None:
  492.                     if en.text == "true":
  493.                         print features.tag + " enabled"
  494.                         if features.tag == "nat" or features.tag == "firewall":
  495.                             rb_feat.remove(en)
  496.                             if features.tag == "firewall":
  497.                                 rb_feat = ET.Element("firewall")
  498.                                 for fwconfig in features:
  499.                                     if fwconfig.tag == "globalConfig":
  500.                                         fwgc = ET.SubElement(rb_feat,"globalConfig")
  501.                                         for gcparams in fwconfig:
  502.                                             gcparam = ET.SubElement(fwgc,gcparams.tag)
  503.                                             gcparam.text = gcparams.text
  504.                                     if fwconfig.tag == "defaultPolicy":
  505.                                         dfp = ET.SubElement(rb_feat,"defaultPolicy")
  506.                                         for dfparams in fwconfig:
  507.                                             dfparam = ET.SubElement(dfp,dfparams.tag)
  508.                                             dfparam.text = dfparams.text
  509.                                     if fwconfig.tag == "firewallRules":
  510.                                         xfwrs = ET.SubElement(rb_feat,"firewallRules")
  511.                                         for fwrs in fwconfig:
  512.                                             if fwrs.tag == "firewallRule":
  513.                                                 rt = fwrs.find("./ruleType")
  514.                                                 if not rt.text == "default_policy" and not rt.text == "internal_high" and not rt.text == "internal_low":
  515.                                                     xfwr = ET.SubElement(xfwrs,"firewallRule")
  516.                                                     for fwr in fwrs: ## ruleTags from UI created are outside of the 1-65536 for "user" specified ruleTags ## TODO write test for ruleTag in user range
  517.                                                         if not fwr.tag == "ruleTag" and not fwr.tag == "id" and not fwr.tag == "ruleType" and not fwr.tag == "source" and not fwr.tag == "destination" and fwr.text is not None:
  518.                                                             xxtag = ET.SubElement(xfwr,fwr.tag)
  519.                                                             xxtag.text = fwr.text
  520.                                                         if fwr.tag == "source" or fwr.tag == "destination":
  521.                                                             xxtag = ET.SubElement(xfwr,fwr.tag)
  522.                                                             for sd in fwr:
  523.                                                                 xxxtag = ET.SubElement(xxtag,sd.tag)
  524.                                                                 xxxtag.text = sd.text
  525.                                                
  526.                                 rb_feat_xmlstring = ET.tostring(rb_feat)
  527.                                                             rb_feat_file_name = "rebuild_" + my_edge_xml_file + "." + features.tag
  528.                                                             rb_feat_file = open(rb_feat_file_name,'w')
  529.                                                             rb_feat_file.write(rb_feat_xmlstring)
  530.                                                             rb_feat_file.close()
  531.                             if features.tag == "nat":
  532.                                 xnat = ET.Element("nat")
  533.                                 rb_feat = ET.SubElement(xnat,"natRules")
  534.                                 for natrules in features:
  535.                                     if natrules.tag == "natRules":
  536.                                             for natrule in natrules: ## TODO same as above for user range ruleTag
  537.                                                 if natrule.tag == "natRule":
  538.                                                     rt = natrule.find("./ruleType")
  539.                                                     if rt.text == "user":
  540.                                                         xnatrule = ET.SubElement(rb_feat,"natRule")
  541.                                                         for natruleopt in natrule:
  542.                                                             if not natruleopt.tag == "ruleId" and not natruleopt.tag == "ruleType" and not natruleopt.tag =="ruleTag" and natruleopt.text is not None:
  543.                                                                 xxtag = ET.SubElement(xnatrule,natruleopt.tag)
  544.                                                                 xxtag.text = natruleopt.text
  545.                                 rb_feat_xmlstring = ET.tostring(xnat)
  546.                                 rb_feat_file_name = "rebuild_" + my_edge_xml_file + "." + features.tag
  547.                                 rb_feat_file = open(rb_feat_file_name,'w')
  548.                                 rb_feat_file.write(rb_feat_xmlstring)
  549.                                 rb_feat_file.close()
  550.                                                        
  551.                                                        
  552.                                                
  553.                         else:              
  554.                             rb_feat_xmlstring = ET.tostring(rb_feat)
  555.                             rb_feat_file_name = "rebuild_" + my_edge_xml_file + "." + features.tag
  556.                             rb_feat_file = open(rb_feat_file_name,'w')
  557.                             rb_feat_file.write(rb_feat_xmlstring)
  558.                             rb_feat_file.close()
  559.                     else:
  560.                         print features.tag + " not enabled"
  561.         if child.tag == 'autoConfiguration':
  562.             xautoconfig = ET.SubElement(rbef,'autoConfiguration')
  563.             for acChild in child:
  564.                 if acChild.tag == 'enabled':
  565.                     xacenabled = ET.SubElement(xautoconfig,"enabled")
  566.                     xacenabled.text = acChild.text
  567.  
  568.                 if acChild.tag == 'rulePriority':
  569.                     xacrp = ET.SubElement(xautoconfig,"rulePriority")
  570.                     xacrp.text = acChild.text
  571.     rebuild_xmlstring = ET.tostring(rbef)
  572.     rebuild_file_name = "rebuild_" + my_edge_xml_file
  573.     rebuild_file = open(rebuild_file_name,"w")
  574.     rebuild_file.write(rebuild_xmlstring)
  575.     rebuild_file.close()
  576.  
  577. elif my_action == 3:
  578.     rbef_file = open(my_edge_rebuild_file_name,'r')
  579.     rbef_payload_data = rbef_file.read()
  580.     rbef_file.close()
  581.     uri = "https://" + my_nsxmanager + "/api/4.0/edges"
  582.     edge_request = nsxpmrest(uri,my_username,my_password,rbef_payload_data,"POST")
  583.     edge_api_path = edge_request.headers.get("Location")
  584.     if edge_api_path is None:
  585.         print "failed to get edge api path, duplicate edge name may exist"
  586.         sys.exit(2)
  587.  
  588.     print "Got new Edge API Path: " + edge_api_path
  589.     feature_files = glob.glob(my_edge_rebuild_file_name + ".*")
  590.     stripchr = len(my_edge_rebuild_file_name) + 1
  591.    
  592.     features = [f[stripchr:] for f in feature_files]
  593.  
  594.     if "routing" in features:
  595.         payload_data = loadfeaturefile(my_edge_rebuild_file_name + ".routing")
  596.         uri = "https://" + my_nsxmanager + edge_api_path + "/routing/config"
  597.         print "Sending routing config"
  598.         edge_request = nsxpmrest(uri,my_username,my_password,payload_data,"PUT")
  599.         print edge_request.status_code
  600.     if "firewall" in features:
  601.         payload_data = loadfeaturefile(my_edge_rebuild_file_name + ".firewall")
  602.         uri = "https://" + my_nsxmanager + edge_api_path + "/firewall/config"
  603.         print "Sending Firewall config"
  604.         edge_request = nsxpmrest(uri,my_username,my_password,payload_data,"PUT")
  605.         print edge_request.status_code
  606.     if "nat" in features:
  607.         payload_data = loadfeaturefile(my_edge_rebuild_file_name + ".nat")
  608.         uri = "https://" + my_nsxmanager + edge_api_path + "/nat/config"
  609.         print "Sending nat config"
  610.         edge_request = nsxpmrest(uri,my_username,my_password,payload_data,"PUT")
  611.         print edge_request.status_code
  612.     if "dhcp" in features:
  613.         payload_data = loadfeaturefile(my_edge_rebuild_file_name + ".dhcp")
  614.         uri = "https://" + my_nsxmanager + edge_api_path + "/dhcp/config"
  615.         print "Sending dhcp config"
  616.         edge_request = nsxpmrest(uri,my_username,my_password,payload_data,"PUT")
  617.         print edge_request.status_code
  618.     if "dns" in features:
  619.         payload_data = loadfeaturefile(my_edge_rebuild_file_name + ".dns")
  620.         uri = "https://" + my_nsxmanager + edge_api_path + "/dns/config"
  621.         print "Sending Dns config"
  622.         edge_request = nsxpmrest(uri,my_username,my_password,payload_data,"PUT")
  623.         print edge_request.status_code
  624.     if "syslog" in features:
  625.         payload_data = loadfeaturefile(my_edge_rebuild_file_name + ".syslog")
  626.         uri = "https://" + my_nsxmanager + edge_api_path + "/syslog/config"
  627.         print "Sending Syslog config"
  628.         edge_request = nsxpmrest(uri,my_username,my_password,payload_data,"PUT")
  629.         print edge_request.status_code 
  630.     if "loadBalancer" in features:
  631.         payload_data = loadfeaturefile(my_edge_rebuild_file_name + ".loadBalancer")
  632.         uri = "https://" + my_nsxmanager + edge_api_path + "/loadbalancer/config"
  633.         print "Sending LB config"
  634.         edge_request = nsxpmrest(uri,my_username,my_password,payload_data,"PUT")
  635.         print edge_request.status_code
  636.     if "ipsec" in features:
  637.         payload_data = loadfeaturefile(my_edge_rebuild_file_name + ".ipsec")
  638.         uri = "https://" + my_nsxmanager + edge_api_path + "/ipsec/config"
  639.         print "Sending ipsec config"
  640.         edge_request = nsxpmrest(uri,my_username,my_password,payload_data,"PUT")
  641.         print edge_request.status_code
  642.     if "highAvailability" in features:
  643.         payload_data = loadfeaturefile(my_edge_rebuild_file_name + ".highAvailability")
  644.         uri = "https://" + my_nsxmanager + edge_api_path + "/highavailability/config"
  645.         print "Sending HA config"
  646.         edge_request = nsxpmrest(uri,my_username,my_password,payload_data,"PUT")
  647.         print edge_request.status_code
  648.     for subInt in range(0,10):
  649.         if "subInterface." + str(subInt) in features:
  650.             uri = "https://" + my_nsxmanager + edge_api_path + "/vnics/" + str(subInt)
  651.             payload_data = loadfeaturefile(my_edge_rebuild_file_name + ".subInterface."+str(subInt))
  652.             print "Sending vNic_"+str(subInt)+" with subInterface(s)"
  653.             edge_request = nsxpmrest(uri,my_username,my_password,payload_data,"PUT")
  654.             print edge_request.status_code
  655.  
  656.     if "l2Vpn" in features:
  657.         payload_data = loadfeaturefile(my_edge_rebuild_file_name + ".l2Vpn")
  658.         uri = "https://" + my_nsxmanager + edge_api_path + "/l2vpn/config/"
  659.                 print "Sending l2Vpn config"
  660.         # password *must* be posted
  661.         if payload_data.find("<password>") == -1:
  662.             payload_data = payload_data.replace("</userId>","</userId><password>Default12!Default12!</password>")
  663.         edge_request = nsxpmrest(uri,my_username,my_password,payload_data,"PUT")
  664.         print edge_request.status_code
  665.     if "bridges" in features:
  666.         payload_data = loadfeaturefile(my_edge_rebuild_file_name + ".bridges")
  667.         uri = "https://" + my_nsxmanager + edge_api_path + "/bridging/config"
  668.         print "Sending bridging config"
  669.         edge_request = nsxpmrest(uri,my_username,my_password,payload_data,"PUT")
  670.         print edge_request.status_code
  671.     if "sslvpnConfig" in features:
  672.         payload_data = loadfeaturefile(my_edge_rebuild_file_name + ".sslvpnConfig")
  673.         print "Sending sslvpn config"
  674.         xsslvpnConfig = ET.fromstring(payload_data)
  675.         xservsett = xsslvpnConfig.find("./serverSettings")
  676.         if xservsett is not None:
  677.             payload_data = ET.tostring(xservsett)
  678.             uri = "https://" + my_nsxmanager + edge_api_path + "/sslvpn/config/server/"
  679.             print "     serverSettings"
  680.             edge_request = nsxpmrest(uri,my_username,my_password,payload_data,"PUT")
  681.             print edge_request.status_code
  682.         xprivnet = xsslvpnConfig.find("./privateNetwork")
  683.         if xprivnet is not None:
  684.             payload_data = ET.tostring(xprivnet)
  685.             uri = "https://" + my_nsxmanager + edge_api_path + "/sslvpn/config/client/networkextension/privatenetworks/"
  686.             print "     privateNetwork"
  687.             edge_request = nsxpmrest(uri,my_username,my_password,payload_data,"PUT")
  688.             print edge_request.status_code
  689.         xwebresource = xsslvpnConfig.find("./webResource")
  690.         if xwebresource is not None:
  691.             payload_data = ET.tostring(xwebresource)
  692.             uri = "https://" + my_nsxmanager + edge_api_path + "/sslvpn/config/webresources/"
  693.             print "     webResource"
  694.             edge_request = nsxpmrest(uri,my_username,my_password,payload_data,"PUT")
  695.             print edge_request.status_code
  696.         xusers = xsslvpnConfig.find("./users") ## TODO FIX ME?
  697.         if xusers is not None:
  698.             payload_data = ET.tostring(xusers)
  699.             uri = "https://" + my_nsxmanager + edge_api_path + "/sslvpn/config/auth/localserver/users/"
  700.             print "     users - BROKEN"
  701.             edge_request = nsxpmrest(uri,my_username,my_password,payload_data,"PUT")
  702.             print edge_request.status_code
  703.         xippool = xsslvpnConfig.find("./ipAddressPools")
  704.         if xippool is not None:
  705.             payload_data = ET.tostring(xippool)
  706.             uri = "https://" + my_nsxmanager + edge_api_path + "/sslvpn/config/client/networkextension/ippools"
  707.             print "     ippool"
  708.             edge_request = nsxpmrest(uri,my_username,my_password,payload_data,"PUT")
  709.             print edge_request.status_code
  710.         xcconf = xsslvpnConfig.find("./clientConfiguration")
  711.         if xcconf is not None:
  712.             payload_data = ET.tostring(xcconf)
  713.             uri = "https://" + my_nsxmanager + edge_api_path + "/sslvpn/config/client/networkextension/clientconfig/"
  714.             print "     clientConfiguration"
  715.             edge_request = nsxpmrest(uri,my_username,my_password,payload_data,"PUT")
  716.             print edge_request.status_code
  717.         xcip = xsslvpnConfig.find("./clientInstallPackage")
  718.         if xcip is not None:
  719.             payload_data = ET.tostring(xcip)
  720.             uri = "https://" + my_nsxmanager + edge_api_path + "/sslvpn/config/client/networkextension/installpackages"
  721.             print "         clientInstallPackage"
  722.             edge_request = nsxpmrest(uri,my_username,my_password,payload_data,"PUT")
  723.             print edge_request.status_code
  724.         xlayout = xsslvpnConfig.find("./layoutConfiguration")
  725.         if xlayout is not None:
  726.             xml_string = ET.tostring(xlayout)
  727.             payload_data = xml_string.replace("layoutConfiguration","layout")
  728.             uri = "https://" + my_nsxmanager + edge_api_path + "/sslvpn/config/layout/portal"
  729.             print "     layout"
  730.             edge_request = nsxpmrest(uri,my_username,my_password,payload_data,"PUT")
  731. print edge_request.status_code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement