Learn F5 Technologies, Get Answers & Share Community Solutions Join DevCentral

Filter by:
  • Solution
  • Technology
code share

Python script for updating data group

Problem this snippet solves:

Updating data group via Python. From the article: Routing HTTP by request headers

Currently when you need to update a data group via iControl REST it requires you to retrieve a copy of all of the records, make your changes, and then re-submit all the records to make any changes. The following Python script takes care of the process of extracting all of the records, turning them into a dictionary, and then allowing the data group to be updated by passing a dictionary.

How to use this snippet:

This code can be imported as a class or run from the command-line. From the command-line:

% ./cgp.py add www.mycompany.example
added: www.mycompany.example
% ./cgp.py get www.mycompany.example
% ./cgp.py update www.mycompany.example
updated: www.mycompany.example
% ./cgp.py del www.mycompany.example
deleted: www.mycompany.example

From code

from cgp import CGP
BIGIP_USER = '[admin user]'
BIGIP_PASS = '[admin password]'

DATA_GROUP = '~Common~[data-group name]'
# get record
data =  cgp.get_data_group(DATA_GROUP)

# add record
data['www.mycompany.example'] = ''
cgp.set_data_group(DATA_GROUP, data)

# update record
data =  cgp.get_data_group(DATA_GROUP)
data['www.mycompany.example'] = ''
cgp.set_data_group(DATA_GROUP, data)

# delete record
data =  cgp.get_data_group(DATA_GROUP)
del data['www.mycompany.example']
cgp.set_data_group(DATA_GROUP, data)
Tested on Version:
Comments on this Snippet
Comment made 14-Sep-2015 by Robert 534
it doesn't work teach me hehe
Comment made 14-Sep-2015 by Robert 534
it needs to replace all values in the datagroup when add/delete/update? any other better way?
Comment made 15-Sep-2015 by Eric Chen
Robert, I'm not aware of a method to perform an incremental update of a datagroup at this time. There was a similar question on DevCentral: https://devcentral.f5.com/questions/modify-a-data-group-with-icontrol-rest
Comment made 21-Sep-2015 by Robert 534
thanks,Eric I have completed my python version yeah, there is no way to perform an incremental update of a datagroup
Comment made 1 month ago by Frank Ren 0

why i got a problem which said "keyerror:'records'"?how can i resolve the question? tks