Bulk update of service groups via API

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Please sign in to see details of an important advisory in our Customer Advisories area.

Bulk update of service groups via API

L1 Bithead

I have a requirement to do a bulk update of a custom service group in Panorama (11.0.1) via the API. This is reading in a list of services from a CSV file. However it only ever applies the last entry in the CSV. What am i doing wrong?  I've tried PUT & POST. Post simply errors with a duplicate entry. A PUT to an existing group only inserts the last entry overwriting everything else.

 

location = {'location': 'shared', 'name': 'TEST-GRP' }

with open ('groups.csv') as f:
reader = csv.DictReader(f)
for row in reader:
member = row['members']

body = json.dumps(
{
"entry":
{
"@name": "TEST-GRP",
"members": { "member" : [ member ]
}
}
}
)

r = requests.put(api_url, params=location, verify=False, headers=headers, data=body)
print(r.text)

 

Contents of groups.csv

members
service-https,
service-http,

 

4 REPLIES 4

L2 Linker

give this a go:

 

import json
from pandas import *
data = read_csv("groups.csv")
member_list = data['members'].tolist()
member_set = set(member_list)
unique_member_list = list(member_set)
body = json.dumps(
{
"entry":
{
"@name": "TEST-GRP",
"members": { "member" : unique_member_list
}
}
}
)

print(body)

 

Outputs:

{"entry": {"@name": "TEST-GRP", "members": {"member": ["service-http", "service-https"]}}}

 

tip: conversion from list to set and back is  to remove any duplicates

Just got back to this.

Unfortunately it does not work. I've taken the CSV import out to test also and i still get errors. Tried it with addresses also rather than services:

 

"code":3,"message":"Invalid Object","details":[{"@type":"CauseInfo","causes":[{"code":12,"module":"panui_mgmt","description":"Invalid Object: TEST-GRP -> members unexpected here."}]}]}
{"entry": {"@name": "TEST-GRP", "members": {"member": ["gav-1", "gav-2"]}}}

 

Okay finally got this working so i thought i'd share:

import requests
import json
import csv

 

with open('test.csv', 'r') as read_obj:
csv_reader = csv.reader(read_obj)
MyList = list(csv_reader)

location = {'location': 'shared', 'name': 'TEST-GRP' }

body = json.dumps(
{
"entry": [
{
"@name": "TEST-GRP",
"@location": "Shared",
"static": {
"member": (MyList)
}
}
]
}
)

r = requests.put(api_url, params=location, verify=False, headers=headers, data=body)
print(r.text)
print (body)

Didn't have pandas available to me but got it to work based off of your input. Thanks

  • 1092 Views
  • 4 replies
  • 0 Likes
Like what you see?

Show your appreciation!

Click Like if a post is helpful to you or if you just want to show your support.

Click Accept as Solution to acknowledge that the answer to your question has been provided.

The button appears next to the replies on topics you’ve started. The member who gave the solution and all future visitors to this topic will appreciate it!

These simple actions take just seconds of your time, but go a long way in showing appreciation for community members and the LIVEcommunity as a whole!

The LIVEcommunity thanks you for your participation!