You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: labs/coding-103-python-json/1.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ Completion Time: 45 minutes
14
14
## Prerequisites
15
15
16
16
**Background**
17
-
* We recommend that you complete the [Coding 101 Rest Basics Learning Lab](/#/lab/coding-101-rest-basics-ga/step/1) before you start this lab.
17
+
* We recommend that you complete the [Coding 101 Rest Basics Learning Lab](/#/lab/coding-101-rest-basics-ga/step/1)and [Coding 102 Rest Python Learning Lab](/#/lab/coding-102-rest-python-ga/step/1)before you start this lab.
18
18
19
19
**Access to an APIC-EM Controller**
20
20
* To run these code samples, you will need access to an APIC-EM controller.
@@ -91,4 +91,4 @@ Here's an example of using the Python interpretor<br/><br/>
91
91
**On Mac OS or Linux type*: **python3 hello.py**
92
92
4. The program should execute or display an error message.
93
93
94
-
**Next Step:** Learn about Python Data Types
94
+
**Next Step:** Learn about Python Scope, Variables, Operators and Simple Conditional Statements
In this step we're going to get the topology then parse the data to determine and display how devices link to one another and their link status.
3
+
A function is a block of code that is run only when it's explicitly called. For example, print() is a function written in Python that you've called many times. Functions are written to modularize code to make it easy to read, easier to debug because it's located in one place and to reuse. Essentially, you don't want to write code over and over again that does the same thing. Instead you would put it into a function and then call that function whenever you need it.
4
4
5
+
Let's look at the structure of a function, then we'll look at a simple example. In Python a function is defined in the follow manner shown below. The keyword **def** specifies that a function is defined which is then followed by the name of the function and optional arguments that are passed into it.
5
6
6
-
#### build-topology.py
7
-
This sample code uses the application REST function call to retrieve a list of devices called nodes and links which are the interfaces that connect them. Our goal is to find the devices and display information about them and how they connect to other devices.
Let's look at some simple examples of functions. The first function named **my_function** simply prints a statement. The second function **brett** takes an argument called **val** which it passes to the function **range** and uses for looping.
Now let's look at these simple functions in a script to see how they're called. When this script is run, starting from the top of the script, the Python interpretor looks at what it should run now. It sees the call to print and executes that. It then sees the next two defined functions, makes note of them, but does not run them because they are not explicitly called. Continuing down the script it then sees the call to **function my_function** and executes it. Finally, it sees the call to **function brett** with the argument of 5 passed in and executes it.
You ran the sample code below in the [Coding 102 Rest Python Learning Lab](/#/lab/coding-102-rest-python-ga/step/1), but that was before you knew how to write Python. Now you've learned the basics about variables, operators, scope, parsing JSON, loops and functions. Review the functions and how they are called. Check out the dictionaries that are created and assigned to variables along with how the JSON data is parsed.
34
+
35
+
#### get-network-devices.py
36
+
This sample code uses the network-device REST function call to retrieve a list of the network devices. A network device is a component such as a router, switch or hub used to connect workstations or devices to a network so that they can share data and other resources. Our goal is to find the network devices and display information about them.
8
37
9
38
10
39
```python
@@ -14,13 +43,8 @@ import requests
14
43
#import json library
15
44
import json
16
45
17
-
# Disable warnings
18
-
requests.packages.urllib3.disable_warnings()
19
-
20
-
#controller='sandboxapic.cisco.com'
21
46
controller='devnetapi.cisco.com/sandbox/apic_em'
22
47
23
-
24
48
defgetTicket():
25
49
# put the ip address or dns of your apic-em controller in this url
26
50
url ="https://"+ controller +"/api/v1/ticket"
@@ -34,8 +58,6 @@ def getTicket():
34
58
#Performs a POST on the specified url to get the service ticket
#Iterate through network device data and print the id and series name of each device
89
+
for i in r_json["response"]:
90
+
print(i["id"] +""+ i["series"])
110
91
92
+
#call the functions
111
93
theTicket=getTicket()
112
-
getTopology(theTicket)
94
+
getNetworkDevices(theTicket)
113
95
```
114
96
115
-
Let's look at what the code is doing. We'll focus on the key code changes.
116
-
**def getTopology(theTicket):*
117
-
* We define the function named getTopology. This function reads in the topology data. It parses the nodes which are devices on the network and displays some information about them. It also parses the link data which are interfaces that connect the nodes and displays information about how they connect the devices and their status. The passed in parameter 'theTicket' is used for authorization purposes.
118
-
**for n in r_json["response"]["nodes"]:*
119
-
* We read the dictionary data for each node into n from which we will parse the data.
120
-
**if "platformId" in n:*
121
-
* Checking that the node data in n contains a 'platformId' key. Some records don't have this key so different field data would need to be accessed.
122
-
**if "startPortName" in i:*
123
-
* Checking that the interface data in i contains a 'startPortName' key. Some records don't have this key so different field data would need to be accessed.
124
-
125
97
126
98
To run this code sample:
127
99
1. Go to directory **coding102-REST-python-ga**. In the terminal type:
2. Assign the APIC-EM Controller IP address or DNS to the **controller** variable.
130
-
* Open the file **build-topology.py**. For example, in Windows type: **notepad build-topology.py**
131
-
**If you are not using your own APIC-EM Controller*, use the [DevNet Sandbox](https://developer.cisco.com/site/devnet/sandbox/) Always-On APIC-EM Lab: [sandboxapic.cisco.com](https://sandboxapic.cisco.com)
132
-
* controller='sandboxapic.cisco.com'
102
+
* Open the file **get-network-devices.py**. For example, in Windows type: **get-network-devices.py**
103
+
**If you are not using your own APIC-EM Controller*, use the [DevNet Sandbox](https://developer.cisco.com/site/devnet/sandbox/) Always-On APIC-EM Lab: https:[]()//devnetapi.cisco.com/sandbox/apic_em
* After running the script review the node data printed. Modify the source code by replacing the label key with another key such as role or nodeType. Run the script again and determine the change of the data displayed.
146
-
* Review the printed topology data and determine how many host devices exist and to which devices they connect.
147
-
* Starting from the cloud node, use the topology data provided to draw a picture of the first three layers of the topology. Hint: look for matching devices specified by the Label attribute. For example, the cloud node connects to four devices with one device being Branch-Router1. Determine which devices Branch-Router1 connects to by finding it in the Label field of the topology data. Then do the same steps for the other three devices.
114
+
## Give it a Try
115
+
* Open file **json_parse-1.py** . Create and call two functions that parse the JSON data. You may use the code you wrote in Step 5 to complete the functions. Run the code.
116
+
* Run the file \DevNetCode\<your-name>\coding-skills-sample-code\coding102-REST-python-ga\get-network-devices.py . Review the JSON data printed. Then open the file and modify it to print out other data. Run the code.
148
117
149
118
150
-
In the next section, we will learn how to use the NeXt UI toolkit and Flask to build a network topology and display it graphically
0 commit comments