Forum Discussion
Upload programatically external data group file?
Good to know that you found the solution.
Here's an alternative solution using F5 Python SDK for iControl REST. While the example below does not give you much advantage over the Jason's pure Python code (uses 'request' module) in this use-case, you may find the SDK useful in some cases.
Just like the curl examples above, the code relies on a http server for uploading the data-group file to BIG-IP, not via a separate scp or POST to the /mgmt/shared/file-transfer/uploads/file.txt endpoint.
BTW, Jason Rahm's another post, "Demystifying iControl REST Part 5: Transferring Files" is a good read on the topic.
Here's the code (Lazy me; I should have refactored it instead of copy-pasting the routines).
from f5.bigip import ManagementRoot
Get all the 'sys file data-group' files
def getDGFiles(top):
try:
dgFiles = top.sys.file.data_groups.get_collection()
print('Got {} file objects.'.format(len(dgFiles)))
for idx, f in enumerate(dgFiles):
print('{}: {}'.format(idx, f.raw))
except Exception as ex:
print('Error: {}'.format(ex))
Upload the external data-group file
def uploadDGFile(top, name, path, dataType):
print('---- Uploading the file {} as {}'.format(path, name))
try:
dgFile = top.sys.file.data_groups.data_group.create(sourcePath=path, name=name, type=dataType)
print(dgFile.raw)
except Exception as ex:
print('Error: {}'.format(ex))
Delete the external data-group file
def deleteDGFile(top, folder, name):
print('---- Deleting file object /{}/{}'.format(folder, name))
try:
dgObject = top.sys.file.data_groups.data_group.load(name=name, partition=folder)
dgObject.delete()
print(dgObject.raw)
except Exception as ex:
print('Error: {}'.format(ex))
Get all the 'ltm data-group external' objects
def getDGObjects(top):
try:
dgObjects = top.ltm.data_group.externals.get_collection()
print('Got {} file objects.'.format(len(dgObjects)))
for idx, o in enumerate(dgObjects):
print('{}: {}'.format(idx, o.raw))
except Exception as ex:
print('Error: {}'.format(ex))
Add the external data-group object
def createDGObject(top, name, file):
print('---- Creating DG object {} from the file {}'.format(name, file))
try:
dgObject = top.ltm.data_group.externals.external.create(name=name, externalFileName=file)
print(dgObject.raw)
except Exception as ex:
print('Error: {}'.format(ex))
Delete the external data-group object
def deleteDGObject(top, folder, name):
print('---- Deleting DG object /{}/{}'.format(folder, name))
try:
dgObject = top.ltm.data_group.externals.external.load(name=name, partition=folder)
dgObject.delete()
print(dgObject.raw)
except Exception as ex:
print('Error: {}'.format(ex))
==== Main routines =====
top = ManagementRoot('192.168.184.30', 'admin', 'admin').tm
getDGFiles(top)
uploadDGFile(top, 'dg-testSw.txt', 'http://172.16.10.10/dg-testSw.txt', 'string')
getDGObjects(top)
createDGObject(top, 'sat2', 'dg-testSw.txt')
deleteDGObject(top, 'Common', 'sat2')
deleteDGFile(top, 'Common', 'dg-testSw.txt')
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com