cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
5470
Views
20
Helpful
20
Replies

404 error in Python script to DELETE objects from FMC using a CSV file

Maneck_UK
Level 1
Level 1

I have a fully working python script that can POST objects to the FMC (6.5.0.4) API using a .csv file for the data.

I want to use this same approach/method to DELETE objects, but after many days I have been unable to get the script to work.

Initially I was adding the object to the script as data rather than in the URL, I fixed this using a single object, if I add the object into the script using:

del_data = ['005056A6-6972-0ed3-0000-090194313992']

the URL works and the objects deletes.

But this defeats the purpose of the script as I can delete a single object in simpler ways.

 

I believe I now have a script that adds the host object ID to the url but it continues to fail with a url 404 error:

 

Connection Error --> 404 Client Error: Not Found for url: https://<FMC-NAME>/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/hosts/005056A6-6972-0ed3-0000-090194314504

 

I'm new to this Python scripting business and I would be grateful if anyone has any pointers.

The relevant parts to my script are below:

Thanks in advance

 

URL build script component

f = open("delid.csv")
objectsfile = csv.DictReader(f)

for object in objectsfile:
    del_data = {
        object["objectid"]
    }


for object in del_data:
        del_url = server + api_path + object

try:
    r = requests.delete(del_url, headers=headers, verify=False)
    status_code = r.status_code
    resp = r.text
    log = open('delete_objects.log', 'a')
    print(" Status Code: "+str(status_code))
    json_resp = json.loads(resp)
    log.write(json.dumps(json_resp,sort_keys=True,indent=4, separators=(',', ': ')))

 

    if status_code == 201 or status_code == 202:
        print(" SUCCESS ")
    elif status_code == 400:
        print((" Message: ")+ resp + ('\n'))
    else:
        r.raise_for_status()
        print((" Message: ")+ resp + ('\n'))

except requests.exceptions.HTTPError as err:
    print("Connection Error --> "+str(err))
finally:
    if r: r.close()

 

 

 

20 Replies 20

Weird, my reply got discarded I think?

 

I'll repost it:

 

 

f = open("del-hostid.csv")
elementsfile = csv.DictReader(f)

for element in elementsfile:
    del_data = {
        element["objectid"],
    }

for element in del_data:
    del_url = server + api_path + element
    try: 
        print(del_url)
        r = requests.delete(del_url, headers=headers, verify=False)
        status_code = r.status_code
        resp = r.text
        log = open('delete_objects.log', 'a')
        print(" Status code: "+str(status_code))
        json_resp = r.json()
        log.write('\n=====\n')
        log.write(json.dumps(json_resp,sort_keys=True,indent=4, separators=(',', ': ')))
        if status_code == 201 or status_code == 202:
            print(" SUCCESS ")
        elif status_code == 400 or status_code == 404:
            print(" Message: " + resp + '\n')
        else:
            r.raise_for_status()
            print(" Message: " + resp + '\n')

    except requests.exceptions.HTTPError as err:
        print("Connection error -> "+str(err))

You had a double for-loop which wasn't needed.

 

 

# Old : elif status_code == 400:
# Old :     print((" Message: ")+ resp + ('\n'))

# New : elif status_code == 400 or status_code == 404:
# New : print(" Message: " + resp + '\n')

 

 

I've changed these two things:

 

# Old : json_resp = json.loads(resp)
# New : json_resp = r.json()

 

@JonasKs, damned annoying your earlier post disappeared, the post you wrote about how the script worked through the loops allowed me to work it out and understand it.
I just spent several hours re-writing all of my scripts cleanly so that I could re-test the POST objects, GET host object ID and create .csv and then the all important DELETE objects.
I completed testing using my script prior to your final update from Hotfix and whilst it wasn't as clean as your script I had everything working :-)
In order to test the new changes I will need to recreate the all the objects again and then run the extractions, remove an object to test the error aspect. All of which I will do, however it will be tomorrow now.
I understand the clean up of the messaging but what does the following achieve please?
# Old : json_resp = json.loads(resp)
# New : json_resp = r.json()

First time really replying in here, might've been me messing up something. 

So you do this:

 

resp = r.text
json_resp = json.loads(resp)

 What this does is that it converts the JSON  into a Python dictionary. How ever, the response object already has this built in. In other words, you can change those two lines into this 

json_resp = r.json()

This works since `.json()` is a function that takes the response.text and converts it into a Python dictionary. So it’s doing the exact same thing, it’s just often easier to do, so I wanted to show you :) sorry if it confused you. 

 

 

 

 

Thanks,
I will try that version tomorrow. I was confused at the start of this post but thanks to your help I have a significantly improved understanding :-).
I also had a post in my more normal reading location in the networking section of the forums, I've tried to link that back to you too, so you get the Kudos from that post also.
Thanks for your time and effort. It is very much appreciated.

You’re welcome. :) I appreciate the helpful votes! 

Good luck! 

And I really appreciate the help :-)