Forum Discussion
Robert_47833
Sep 14, 2015Altostratus
add value in data group via REST API
Hi, F5
I tried to add value in data group via REST API
what I have known is:
In order to add one record in the datagroup, I need to replace the old records with new records(all old records ...
JRahm
Sep 14, 2015Admin
It is the only way. Data-group records are not subcollections, so in order to replace one you have to replace them all. Here's some code I wrote for expanding and contracting data groups a while back in python:
def get_dg(rq, url, dg_details):
dg = rq.get('%s/ltm/data-group/%s/%s' % (url, dg_details[0], dg_details[1])).json()
return dg
def extend_dg(rq, url, dg_details, additional_records):
dg = rq.get('%s/ltm/data-group/%s/%s' % (url, dg_details[0], dg_details[1])).json()
current_records = dg['records']
new_records = []
for record in current_records:
nr = [ {'name': record['name']}]
new_records.extend(nr)
for record in additional_records:
nr = [ {'name': record}]
new_records.extend(nr)
payload = {}
payload['records'] = new_records
rq.put('%s/ltm/data-group/%s/%s' % (url, dg_details[0], dg_details[1]), json.dumps(payload))
def contract_dg(rq, url, dg_details, removal_records):
dg = rq.get('%s/ltm/data-group/%s/%s' % (url, dg_details[0], dg_details[1])).json()
new_records = []
for record in removal_records:
nr = [ {'name': record}]
new_records.extend(nr)
current_records = dg['records']
new_records = [x for x in current_records if x not in new_records]
payload = {}
payload['records'] = new_records
rq.put('%s/ltm/data-group/%s/%s' % (url, dg_details[0], dg_details[1]), json.dumps(payload))
if __name__ == "__main__":
import requests, json
b = requests.session()
b.auth = ('admin', 'admin')
b.verify = False
b.headers.update({'Content-Type' : 'application/json'})
b_url_base = 'https://172.16.44.5/mgmt/tm'
dg_details = ['internal', 'myNetworks']
net_changes = ['3.0.0.0/8', '4.0.0.0/8']
print "\nExisting Records for %s Data-Group:\n\t%s" % (dg_details[1], get_dg(b, b_url_base, dg_details)['records'])
extend_dg(b, b_url_base, dg_details, net_changes)
print "\nUpdated Records for %s Data-Group:\n\t%s" % (dg_details[1], get_dg(b, b_url_base, dg_details)['records'])
contract_dg(b, b_url_base, dg_details, net_changes)
print "\nUpdated Records for %s Data-Group:\n\t%s" % (dg_details[1], get_dg(b, b_url_base, dg_details)['records'])
Recent Discussions
Related Content
DevCentral Quicklinks
* 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
Discover DevCentral Connects