pythoncsvurllib2big-ip

Syntax Error: unexpected EOF while parsing- F5 VIP creation using CSV file


i am able to communicate with f5 box and creating single object called (virtual= VIP=Virtual server) with some basic profiles.

Working code:

from f5.bigip import ManagementRoot
import urllib3
urllib3.disable_warnings()
mgmt = ManagementRoot('13.126.189.103','admin','admin')
ltm = mgmt.tm.ltm
virtuals = mgmt.tm.ltm.virtuals
virtual = mgmt.tm.ltm.virtuals.virtual
virtual1 = mgmt.tm.ltm.virtuals.virtual.create(name='virtual3', description = 'testnew' , destination= '172.31.5.8:80', partition='Common')

my requirement is , i will give input from CSV file , may be 100 VIP's. the above syntax has to run one by one row in imported CSV file.

from f5.bigip import ManagementRoot
import urllib3
import csv
import sys
import os
urllib3.disable_warnings()
mgmt = ManagementRoot('13.126.189.103','admin','admin')
ltm = mgmt.tm.ltm
virtuals = mgmt.tm.ltm.virtuals
virtual = mgmt.tm.ltm.virtuals.virtual


def configureVirtuals(bigip, virtualFile):
  # These are the fields in the file. The purpose of each field is obvious. See. Input from CSV file called 'virtualFile' and each row has config
details
  # the BIG-IP iControl REST API for more information.
  fieldNames = ["name", "description", "ip", "port"]
  virtualReader = csv.DictReader(virtualFile, fieldnames=fieldNames,
                  delimiter=",")


# Create a virtual server, one per line. it will take input from CSV file by row one by one
  try: 
    for row in virtualReader: 
      myvirtual = mgmt.tm.ltm.virtuals.virtual.create(name=row["name"],
                  description=row["description"], 
                  destination="%s:%s" % (row["ip"], row["port"])

I am getting error unexpected EOF while parsing i am f5 engineer and new to python. Building application for bulk changes.

any one please help me?


Solution

  • First of all you forgot a closing bracket. The second problem is a try block, which doesn't have except. If you use try, you have to catch an exception, otherwise you don't need try at all. So either don't use try

    from f5.bigip import ManagementRoot
    import urllib3
    import csv
    import sys
    import os
    urllib3.disable_warnings()
    mgmt = ManagementRoot('13.126.189.103','admin','admin')
    ltm = mgmt.tm.ltm
    virtuals = mgmt.tm.ltm.virtuals
    virtual = mgmt.tm.ltm.virtuals.virtual
    
    
    def configureVirtuals(bigip, virtualFile):
      # These are the fields in the file. The purpose of each field is obvious. See. Input from CSV file called 'virtualFile' and each row has config details
      # the BIG-IP iControl REST API for more information.
      fieldNames = ["name", "description", "ip", "port"]
      virtualReader = csv.DictReader(virtualFile, fieldnames=fieldNames,
                      delimiter=",")
    
    
      # Create a virtual server, one per line. it will take input from CSV file by row one by one
      for row in virtualReader: 
        myvirtual = mgmt.tm.ltm.virtuals.virtual.create(
          name=row["name"],
          description=row["description"], 
          destination="%s:%s" % (row["ip"], row["port"])) # pay attention to brackets
    

    Or catch an exception if you know which one you expect and how to handle it

    from f5.bigip import ManagementRoot
    import urllib3
    import csv
    import sys
    import os
    urllib3.disable_warnings()
    mgmt = ManagementRoot('13.126.189.103','admin','admin')
    ltm = mgmt.tm.ltm
    virtuals = mgmt.tm.ltm.virtuals
    virtual = mgmt.tm.ltm.virtuals.virtual
    
    
    def configureVirtuals(bigip, virtualFile):
      # These are the fields in the file. The purpose of each field is obvious. See. Input from CSV file called 'virtualFile' and each row has config details
      # the BIG-IP iControl REST API for more information.
      fieldNames = ["name", "description", "ip", "port"]
      virtualReader = csv.DictReader(virtualFile, fieldnames=fieldNames,
                      delimiter=",")
    
    
      # Create a virtual server, one per line. it will take input from CSV file by row one by one
    
      try:
        for row in virtualReader: 
          myvirtual = mgmt.tm.ltm.virtuals.virtual.create(
            name=row["name"],
            description=row["description"], 
            destination="%s:%s" % (row["ip"], row["port"])) # pay attention to brackets
      except SpecificExceptionType:
        print('Print some instruction or log an error and continue the flow')