Advertisement
Guest User

Untitled

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