1

Like most i am attempting to learn python to progress my career etc... as part of that we are looking at deploying Cisco Meraki at work. Due to the large number of networks that we will have it would be great to be able to deploy information from a csv file via the API.

I have managed to get the below code working to an extent. it will run successfully but only runs the last line of the CSV rather than each row until no rows left.

The CSV is approx 130 rows of subnets, ip addresses and network ID's

I'm so close but i just cant seem to figure this last bit.

    # actionBatch-VlanUpdate.py

import requests
import csv

# Environment Variables
API_KEY = "xxxxxx"
org_id = xxxxx
url = f"https://api.meraki.com/api/v0/organizations/{org_id}/actionBatches"

with open('Subnets.csv', newline="\n") as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
     net_id = row[3]
     V5NET = row[4]
     V5GW = row[5]
     V10NET = row[6]
     V10GW = row[7]
     V11NET = row[8]
     V11GW = row[9]



payload = {
    "confirmed": True,
    "synchronous": True,
    "actions": [
        {
            "resource": f"/networks/{net_id}/vlans",
            "operation": "create",
            "body": {
                "id": "5",
                "name": "Data",
                "applianceIp": f"{V5GW}",
                "subnet": f"{V5NET}/24",
            },
        },
        {
            "resource": f"/networks/{net_id}/vlans",
            "operation": "create",
            "body": {
                "id": 10,
                "name": "Voice",
                "applianceIp": f"{V10GW}",
                "subnet": f"{V10NET}/24",
            },
        },
        {
            "resource": f"/networks/{net_id}/vlans",
            "operation": "create",
            "body": {
                "id": 11,
                "name": "Property",
                "applianceIp": f"{V11GW}",
                "subnet": f"{V11NET}/24",
            },
        },
    ],
}

headers = {
    "X-Cisco-Meraki-API-Key": API_KEY,
    "Content-Type": "application/json",
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

Edited version based on feedback

# actionBatch-VlanUpdate.py

import requests
import csv

# Environment Variables
API_KEY = "XXXX"
org_id = xxxx
url = f"https://api.meraki.com/api/v0/organizations/{org_id}/actionBatches"

with open('Subnets.csv', newline="\n") as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
     net_id = row[3]
     V5NET = row[4]
     V5GW = row[5]
     V10NET = row[6]
     V10GW = row[7]
     V11NET = row[8]
     V11GW = row[9]
        payload = {
            "confirmed": True,
            "synchronous": True,
            "actions": [
                {
                    "resource": f"/networks/{net_id}/vlans",
                    "operation": "create",
                    "body": {
                        "id": "5",
                        "name": "Data",
                        "applianceIp": f"{V5GW}",
                    "subnet": f"{V5NET}/24",
                    },
                },
                {
                    "resource": f"/networks/{net_id}/vlans",
                    "operation": "create",
                    "body": {
                        "id": 10,
                        "name": "Voice",
                        "applianceIp": f"{V10GW}",
                        "subnet": f"{V10NET}/24",
                    },
                },
                {
                    "resource": f"/networks/{net_id}/vlans",
                    "operation": "create",
                    "body": {
                        "id": 11,
                        "name": "Property",
                        "applianceIp": f"{V11GW}",
                        "subnet": f"{V11NET}/24",
                    },
                },
            ],
        }

headers = {
    "X-Cisco-Meraki-API-Key": API_KEY,
    "Content-Type": "application/json",
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)
Ryujin
  • 23
  • 3

1 Answers1

0

In your for loop, you're overwriting the results of each previous iteration, so it will always be the last line in the file. You could use the .append() array method to insert the result of each loop at the end of your various arrays instead.

Edit - looks like you took the declaration of the arrays before the for loop out. If you want to just call requests 130 times, you just need to change the indentation so your requests call is inside your for loop.

Edit 2 - You want to get the requests call in the for loop as well.

    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        net_id = row[3]
        V5NET = row[4]
        V5GW = row[5]
        V10NET = row[6]
        V10GW = row[7]
        V11NET = row[8]
        V11GW = row[9]
        payload = {
            "confirmed": True,
            "synchronous": True,
            "actions": [
                ...
            ],
        }

        headers = {
            "X-Cisco-Meraki-API-Key": API_KEY,
            "Content-Type": "application/json",
        }

        response = requests.post(url, json=payload, headers=headers)
        print(response.text) 
  • Thanks for a quick response, i moved the payload information under the for loop (at least im sure i have ) It doesnt seem to do it however, im sure im just not doing it right though, further thoughts are appreciated. – Ryujin Nov 08 '19 at 15:30
  • Added a comment about spacing & requests to the above - also, as a matter of style, try to keep your whitespace indents the same length, it can be helpful for debugging stuff like this. – dogversioning Nov 08 '19 at 15:52
  • Thanks so much dogsversioning, I know there are probably much more elagantly but your help has solved my issue by moving it under completely it ran fine and the changes were successful... Thanks for your patience – Ryujin Nov 08 '19 at 17:00