If you missed the earlier parts of our series, please make sure you go back to review part one before diving in.
Functions are a very important concept in programming. They allow you to reuse code so you don’t have to write the same code every time.
For example, many operations with the Meraki Dashboard require you to have the organization ID and network ID.
Here is an API endpoint to claim devices into a network which requires the network ID.
Here is an API endpoint to claim your order and add to the organization inventory.
So rather than rewrite the code for getting the network ID and organization ID every time we need those values, we can instead create a function for it and call on the function anytime we need the ID.
Let’s get started on how to create Python functions!
If you have followed all the parts in this series so far, you should now be familiar with retrieving a list of organizations and filtering the list for particular organization parameters. Let’s have a look at the same code but now as a function.
Here is a function for getting the organization ID of DevNet Sandbox:
Let’s analyze the code line by line.
This statement imports the meraki and os python libraries.
This block of code allows the script to read the .env file.
from dotenv import load_dotenv
This statement sets the API key for the meraki library functions imported.
Then we use a for loop, as we have seen before to iterate through the list of organizations stored in response and look for the specific organization name ‘DevNet Sandbox’ and then if the name matches, store its organization ID in a variable org_id.
for org in response:
if org[‘name’] == ‘DevNet Sandbox’:
org_id = org[‘id’]
Then we return the value of the organization ID whenever this function is called using the return statement.
How do you call a function?
Here is the statement which does that.
org_id = get_org_id()
We create a variable called org_id and then call our function get_org_id. Remember that the function returns the value of the organization ID, which gets stored in the variable org_id. Now that we have the value of the organization ID stored in the variable, all we need to do is to print the variable!
Let us look at one more example.
Now that we have the organization ID, what can we do with it? The next step would be to get the network ID of a network within the organization, we can then configure firewall rules, claim devices etc for the network with the network ID. So let’s create a function for that.
Let’s analyze this code block.
First, we create a function called get_net:
Within the braces, we have the parameter org_id. We can pass values to function code blocks using parameters within braces. In this case, we are sending the organization ID, which the function needs in order to retrieve a list of networks in the organization.
Next, we take the org_id parameter passed to the function and then store it in a variable called organization_id.
organization_id = org_id
Then we do a GET request for a list of networks in the organization.