Multi Network Object POST

Answered Question
Feb 7th, 2017
User Badges:

Dear Support,


am trying to use API to push a bulk of configuration to Firepower 6.1 or 6.2 but without success, i tried to POST multi Network Object as shown below :


under network i POST the below configuration:

 

https://xxx.xxx.xxx.xxx/api/fmc_config/v1/domain/e233abec-e0f2-11e3-2321-6d9ed49b625f/object/networks


  Body:

-----------------------

{
"name":"testing",
"type":"Network",
"description": "testing",
"value": "2.2.90.0/24"
},

{
"name":"testing2",
"type":"Network",
"description": "testing2",
"value": "2.2.91.0/24"
}

----------------


it return with Syntax errors.


can you please provide me with right way to POST multi network Object or any kind of configuration through REST API.


BR,

Hamoud Hamdan 


Correct Answer by neipatel about 3 months 1 day ago

Hamoud,

An high level pseudo code example would be as follows: 


JSON of the Network Objects:

networks = [{name: objecta,

  value: 10.0.0.0/24,

  type: Network},

{name: objectb,

  value: 20.0.0.0/24,

  type: Network},

{name: objectc,

  value: 30.0.0.0/24,

  type: Network}]


Code to iterate and make posts:

for object in networks:

*post object to fmc

this way the loop will go through each object and post them one at a time.


-Neil


  • 1
  • 2
  • 3
  • 4
  • 5
Overall Rating: 5 (3 ratings)
Loading.
neipatel Tue, 02/07/2017 - 07:14
User Badges:
  • Cisco Employee,

Hamoud,

Multi import will not be possible until the next release. Currently if you want to create multiple objects you need to make individual calls with a single object in each JSON payload.

The best way to do multiple objects, today,  would be to build a large JSON file with all your objects and script iteration over the entire file and make individual calls object in the JSON.

If you haven't already spend some time in the API explorer @ https://<<FMC IP>>/api/api-explorer it is a great place to see the calls you can make as well as gather example scripts/JSON for all the calls.

-Neil

hamoudud1 Wed, 02/08/2017 - 00:48
User Badges:

Dear Neipatel,


Thank you for your assist.


Can you share with me small example or more information about building large JSON file with all your objects and script iteration over the entire file and make individual calls object in the JSON. 


BR,

Hamoud Hamdan


ssambourg Fri, 03/24/2017 - 07:04
User Badges:

Hello,


COuld you post your sample for one POST network ?


After that, I think the idea is to adapt something like this :


for i in file["...""..."]:
 yourPostMethod(i[name];i[value])


Correct Answer
neipatel Fri, 03/24/2017 - 07:54
User Badges:
  • Cisco Employee,

Hamoud,

An high level pseudo code example would be as follows: 


JSON of the Network Objects:

networks = [{name: objecta,

  value: 10.0.0.0/24,

  type: Network},

{name: objectb,

  value: 20.0.0.0/24,

  type: Network},

{name: objectc,

  value: 30.0.0.0/24,

  type: Network}]


Code to iterate and make posts:

for object in networks:

*post object to fmc

this way the loop will go through each object and post them one at a time.


-Neil


ssambourg Fri, 03/24/2017 - 08:05
User Badges:

Is there a way to post the body argument in request :


url = "https://%s/api/fmc_platform/v1/auth/generatetoken" % ipaddr

results=[]

headers = {
'cache-control': "no-cache",
'postman-token': "ff30c506-4739-9d4d-2e53-0dc7efc2036a"
}

response = requests.request("POST", url, headers=headers, auth=(user1,pass1), verify=False)

# Authenicates token used in addiotnal HTTPS CRUD request
auth = response.headers['X-auth-access-token']

body= {
"name":"TEST",
"type":"Network",
"description": "TEST",
"value": "1.2.3.4"
}

headers = {
'x-auth-access-token': auth,
'cache-control': "no-cache",
'postman-token': "ff30c506-4739-9d4d-2e53-0dc7efc2036a"
}

url = "https://%s/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/hosts" % ipaddr

querystring = {"limit":"1000"}


print(headers['x-auth-access-token'])


response = requests.request("POST", url, headers=headers, auth=(user1,pass1), body=body, verify=False)


My single network add doens't work.

ssambourg Sat, 03/25/2017 - 10:27
User Badges:

Thanks that's better.


I didn't find the way to load in json format correctly this file :


My code :

#Retrieve JSON file passed in execution argument
try:
inputNetworkObjFile = open(sys.argv[1], "rb")
readedFile = inputNetworkObjFile.read()
i = 1


#Troubleshoot test printing readed input file : OK
print(readedFile)



# if logFile :
# for netObj in readedFile
# networkPOST(headers,uuid,server,netObj)
# logFile.write("Network Obj : %d",i)
# i++

# logFile.write("NAME : %s",netObj[0])
# logFile.write("VALUE : %s",netObj[1])
# logFile.write("TYPE : %s",netObj[2])
# logFile.write("DESCRIPTION : %s",netObj[2])

# if inputNetworkObjFile: inputNetworkObjFile.close()
# else


My output :


b'networkObjects = \r\n[\r\n\t{"name": "network_A",\r\n\t"value": "10.0.0.0/8",\
r\n\t"type": "Network",\r\n\t"description":"objecta"},\r\n\r\n\t{"name": "networ
k_B",\r\n\t"value": "172.16.0.0/12",\r\n\t"type": "Network",\r\n\t"description":
"objecta"},\r\n\t\r\n\t{"name": "network_C",\r\n\t"value": "192.168.0/16",\r\n\t
"type": "Network",\r\n\t"description":"objecta"}\r\n]'

I also try to get this way for my code :

#Retrieve JSON file passed in execution argument
try:
inputNetworkObjFile = open(sys.argv[1], "rb")
readedFile = inputNetworkObjFile.read()

datas = json.dumps(readedFile)
i = 1


#Troubleshoot test printing readed input file : OK
print(readedFile)

then mak a call to the datas variable in the for loop but always this kind of error :

File "test3.py", line 102
for netObj in readedFile
                                    ^
SyntaxError: invalid syntax

Do you have an example parsing JSON file code ?

neipatel Wed, 03/29/2017 - 06:26
User Badges:
  • Cisco Employee,

It looks like you are using some sort of log file write and populating it with tabs/new lines to create your JSON. It is much easier and recommended to used the built in Python JSON handling libraries to ensure there are not formatting errors in it.

https://docs.python.org/2/library/json.html

This way when you read the json back again it will be index-able as it should like a dictionary.


Actions

This Discussion