Forum Discussion
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 + the new record)
Is this the only way to achieve this?
I am searching the way to only add the new record(value) in one datagroup instead of replace all old records with new records
- JRahmAdmin
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'])
- Robert_47833Altostratus
hi, Jason
thanks so much
it is very helpful
I am almost done with it.
but I have another question:
how to sync it to file just like the tmsh command :save sys config current-partition
- JRahmAdmin
POST /mgmt/tm/sys/config
With json body:
{"command": "save", "partition": ["partition1", "partition2"] }
There is no current partition context for iControl REST.
All iControl REST users are administrators as of 11.6, but you can limit their methods and trees, check out the user guide for details.
- Robert_47833Altostratus
hmm, but if the user only manage partition xyz, what to do? do we need to specify the exact partition in url? the user doesn't have authority to save common partition
- JRahmAdminlet me dig in and ask, didn't consider that use case.
- JRahmAdminI updated my answer above with a working example. Had to be on the admin (or a similar account with admin access) to make the save command work.
- Robert_47833AltostratusI also met an authority issue. the user as manager role in partition1 can't call the icontrol rest to update the datagroup in partition1 any idea how to fix it?
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