Skip to content

Commit 39748c0

Browse files
akwintxamwhaley
authored andcommitted
Adding sample code for Coding Skills Learning Labs
1 parent 420a9e2 commit 39748c0

32 files changed

+1948
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# coding-skills-sample-code
22
Sample code for the Cisco DevNet Coding Skills Learning Labs
3+
4+
You can find step-by-step tutorials that walk through this sample code on [Cisco DevNet](http://learninglabs.cisco.com).
5+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Getting started with APIC-EM APIs
2+
# First Call to APIC-EM REST API - "Hello World"
3+
4+
# * THIS SAMPLE APPLICATION AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
5+
# * OF ANY KIND BY CISCO, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
6+
# * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY FITNESS FOR A PARTICULAR
7+
# * PURPOSE, NONINFRINGEMENT, SATISFACTORY QUALITY OR ARISING FROM A COURSE OF
8+
# * DEALING, LAW, USAGE, OR TRADE PRACTICE. CISCO TAKES NO RESPONSIBILITY
9+
# * REGARDING ITS USAGE IN AN APPLICATION, AND IT IS PRESENTED ONLY AS AN
10+
# * EXAMPLE. THE SAMPLE CODE HAS NOT BEEN THOROUGHLY TESTED AND IS PROVIDED AS AN
11+
# * EXAMPLE ONLY, THEREFORE CISCO DOES NOT GUARANTEE OR MAKE ANY REPRESENTATIONS
12+
# * REGARDING ITS RELIABILITY, SERVICEABILITY, OR FUNCTION. IN NO EVENT DOES
13+
# * CISCO WARRANT THAT THE SOFTWARE IS ERROR FREE OR THAT CUSTOMER WILL BE ABLE
14+
# * TO OPERATE THE SOFTWARE WITHOUT PROBLEMS OR INTERRUPTIONS. NOR DOES CISCO
15+
# * WARRANT THAT THE SOFTWARE OR ANY EQUIPMENT ON WHICH THE SOFTWARE IS USED WILL
16+
# * BE FREE OF VULNERABILITY TO INTRUSION OR ATTACK. THIS SAMPLE APPLICATION IS
17+
# * NOT SUPPORTED BY CISCO IN ANY MANNER. CISCO DOES NOT ASSUME ANY LIABILITY
18+
# * ARISING FROM THE USE OF THE APPLICATION. FURTHERMORE, IN NO EVENT SHALL CISCO
19+
# * OR ITS SUPPLIERS BE LIABLE FOR ANY INCIDENTAL OR CONSEQUENTIAL DAMAGES, LOST
20+
# * PROFITS, OR LOST DATA, OR ANY OTHER INDIRECT DAMAGES EVEN IF CISCO OR ITS
21+
# * SUPPLIERS HAVE BEEN INFORMED OF THE POSSIBILITY THEREOF.-->
22+
23+
# import the requests library so we can use it to make REST calls (http://docs.python-requests.org/en/latest/index.html)
24+
import requests
25+
26+
# All of our REST calls will use the url for the APIC EM Controller as the base URL
27+
# So lets define a variable for the controller IP or DNS so we don't have to keep typing it
28+
controller = "https://sandboxapic.cisco.com"
29+
30+
# Get Devices
31+
# This function allows you to view a list of all the devices in the network(routers and switches).
32+
# Use our controller address plus the url for this API to construct the URL we need to call
33+
get_devices_url = controller + '/api/v0/network-device/1/3'
34+
35+
# Send Request using GET Method and specify URL
36+
# An object containing the response codes and json is returned.
37+
get_devices_response = requests.get(get_devices_url, verify=False)
38+
39+
# Print the response so we can see it.
40+
print ("Devices = ")
41+
print (get_devices_response.text)
42+

coding102-REST-python/apic-em1.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Simple Example of calling REST API from Python
2+
# This is all that is required to call a REST API from python
3+
4+
# * THIS SAMPLE APPLICATION AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
5+
# * OF ANY KIND BY CISCO, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
6+
# * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY FITNESS FOR A PARTICULAR
7+
# * PURPOSE, NONINFRINGEMENT, SATISFACTORY QUALITY OR ARISING FROM A COURSE OF
8+
# * DEALING, LAW, USAGE, OR TRADE PRACTICE. CISCO TAKES NO RESPONSIBILITY
9+
# * REGARDING ITS USAGE IN AN APPLICATION, AND IT IS PRESENTED ONLY AS AN
10+
# * EXAMPLE. THE SAMPLE CODE HAS NOT BEEN THOROUGHLY TESTED AND IS PROVIDED AS AN
11+
# * EXAMPLE ONLY, THEREFORE CISCO DOES NOT GUARANTEE OR MAKE ANY REPRESENTATIONS
12+
# * REGARDING ITS RELIABILITY, SERVICEABILITY, OR FUNCTION. IN NO EVENT DOES
13+
# * CISCO WARRANT THAT THE SOFTWARE IS ERROR FREE OR THAT CUSTOMER WILL BE ABLE
14+
# * TO OPERATE THE SOFTWARE WITHOUT PROBLEMS OR INTERRUPTIONS. NOR DOES CISCO
15+
# * WARRANT THAT THE SOFTWARE OR ANY EQUIPMENT ON WHICH THE SOFTWARE IS USED WILL
16+
# * BE FREE OF VULNERABILITY TO INTRUSION OR ATTACK. THIS SAMPLE APPLICATION IS
17+
# * NOT SUPPORTED BY CISCO IN ANY MANNER. CISCO DOES NOT ASSUME ANY LIABILITY
18+
# * ARISING FROM THE USE OF THE APPLICATION. FURTHERMORE, IN NO EVENT SHALL CISCO
19+
# * OR ITS SUPPLIERS BE LIABLE FOR ANY INCIDENTAL OR CONSEQUENTIAL DAMAGES, LOST
20+
# * PROFITS, OR LOST DATA, OR ANY OTHER INDIRECT DAMAGES EVEN IF CISCO OR ITS
21+
# * SUPPLIERS HAVE BEEN INFORMED OF THE POSSIBILITY THEREOF.-->
22+
23+
24+
import requests
25+
26+
# put the ip address or dns of your apic-em controller in this url
27+
url = 'https://sandboxapic.cisco.com/api/v0/host/1/3'
28+
29+
# this statement performs a GET on the specified url
30+
response = requests.get(url, verify=False)
31+
32+
# print the json that is returned
33+
print(response.text)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Getting started with APIC-EM APIs
2+
# Follows APIC-EM Basics Learning Lab Step 2
3+
# Get Devices and Print networkDeviceId values
4+
5+
# * THIS SAMPLE APPLICATION AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
6+
# * OF ANY KIND BY CISCO, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
7+
# * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY FITNESS FOR A PARTICULAR
8+
# * PURPOSE, NONINFRINGEMENT, SATISFACTORY QUALITY OR ARISING FROM A COURSE OF
9+
# * DEALING, LAW, USAGE, OR TRADE PRACTICE. CISCO TAKES NO RESPONSIBILITY
10+
# * REGARDING ITS USAGE IN AN APPLICATION, AND IT IS PRESENTED ONLY AS AN
11+
# * EXAMPLE. THE SAMPLE CODE HAS NOT BEEN THOROUGHLY TESTED AND IS PROVIDED AS AN
12+
# * EXAMPLE ONLY, THEREFORE CISCO DOES NOT GUARANTEE OR MAKE ANY REPRESENTATIONS
13+
# * REGARDING ITS RELIABILITY, SERVICEABILITY, OR FUNCTION. IN NO EVENT DOES
14+
# * CISCO WARRANT THAT THE SOFTWARE IS ERROR FREE OR THAT CUSTOMER WILL BE ABLE
15+
# * TO OPERATE THE SOFTWARE WITHOUT PROBLEMS OR INTERRUPTIONS. NOR DOES CISCO
16+
# * WARRANT THAT THE SOFTWARE OR ANY EQUIPMENT ON WHICH THE SOFTWARE IS USED WILL
17+
# * BE FREE OF VULNERABILITY TO INTRUSION OR ATTACK. THIS SAMPLE APPLICATION IS
18+
# * NOT SUPPORTED BY CISCO IN ANY MANNER. CISCO DOES NOT ASSUME ANY LIABILITY
19+
# * ARISING FROM THE USE OF THE APPLICATION. FURTHERMORE, IN NO EVENT SHALL CISCO
20+
# * OR ITS SUPPLIERS BE LIABLE FOR ANY INCIDENTAL OR CONSEQUENTIAL DAMAGES, LOST
21+
# * PROFITS, OR LOST DATA, OR ANY OTHER INDIRECT DAMAGES EVEN IF CISCO OR ITS
22+
# * SUPPLIERS HAVE BEEN INFORMED OF THE POSSIBILITY THEREOF.-->
23+
24+
# import the requests library so we can use it to make REST calls (http://docs.python-requests.org/en/latest/index.html)
25+
import requests
26+
27+
# import the json library. This library gives us many handy features for formatting, displaying
28+
# and manipulating json.
29+
import json
30+
31+
# All of our REST calls will use the url for the APIC EM Controller as the base URL
32+
# So lets define a variable for the controller IP or DNS so we don't have to keep typing it
33+
controller_url = "https://sandboxapic.cisco.com"
34+
35+
# Get Devices
36+
# This function allows you to view a list of all the devices in the network(routers and switches).
37+
get_devices_url = controller_url + '/api/v0/network-device/1/3'
38+
39+
#Perform GET on get_devices_url
40+
get_devices_response = requests.get(get_devices_url, verify=False)
41+
42+
# The json method of the response object returned by requests.get returns the request body in json format
43+
get_devices_json = get_devices_response.json()
44+
45+
#Now let's read and display some specific information from the json
46+
47+
# set our parent as the top level response object
48+
parent = get_devices_json["response"]
49+
50+
print ("Devices = ")
51+
52+
# for each device returned, print the networkDeviceId
53+
for item in parent:
54+
print ("id = " + item["id"] + " type = " + item["type"])
55+
56+
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Getting started with APIC-EM APIs
2+
# Follows APIC-EM Basics Learning Lab
3+
# Basics Learning Lab Full example for Get Devices, Get Hosts, Get Policies, Get Applications
4+
5+
# * THIS SAMPLE APPLICATION AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
6+
# * OF ANY KIND BY CISCO, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
7+
# * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY FITNESS FOR A PARTICULAR
8+
# * PURPOSE, NONINFRINGEMENT, SATISFACTORY QUALITY OR ARISING FROM A COURSE OF
9+
# * DEALING, LAW, USAGE, OR TRADE PRACTICE. CISCO TAKES NO RESPONSIBILITY
10+
# * REGARDING ITS USAGE IN AN APPLICATION, AND IT IS PRESENTED ONLY AS AN
11+
# * EXAMPLE. THE SAMPLE CODE HAS NOT BEEN THOROUGHLY TESTED AND IS PROVIDED AS AN
12+
# * EXAMPLE ONLY, THEREFORE CISCO DOES NOT GUARANTEE OR MAKE ANY REPRESENTATIONS
13+
# * REGARDING ITS RELIABILITY, SERVICEABILITY, OR FUNCTION. IN NO EVENT DOES
14+
# * CISCO WARRANT THAT THE SOFTWARE IS ERROR FREE OR THAT CUSTOMER WILL BE ABLE
15+
# * TO OPERATE THE SOFTWARE WITHOUT PROBLEMS OR INTERRUPTIONS. NOR DOES CISCO
16+
# * WARRANT THAT THE SOFTWARE OR ANY EQUIPMENT ON WHICH THE SOFTWARE IS USED WILL
17+
# * BE FREE OF VULNERABILITY TO INTRUSION OR ATTACK. THIS SAMPLE APPLICATION IS
18+
# * NOT SUPPORTED BY CISCO IN ANY MANNER. CISCO DOES NOT ASSUME ANY LIABILITY
19+
# * ARISING FROM THE USE OF THE APPLICATION. FURTHERMORE, IN NO EVENT SHALL CISCO
20+
# * OR ITS SUPPLIERS BE LIABLE FOR ANY INCIDENTAL OR CONSEQUENTIAL DAMAGES, LOST
21+
# * PROFITS, OR LOST DATA, OR ANY OTHER INDIRECT DAMAGES EVEN IF CISCO OR ITS
22+
# * SUPPLIERS HAVE BEEN INFORMED OF THE POSSIBILITY THEREOF.-->
23+
24+
# import the requests library so we can use it to make REST calls (http://docs.python-requests.org/en/latest/index.html)
25+
import requests
26+
27+
# import the json library. This library provides handy features for formatting, displaying
28+
# and manipulating json.
29+
import json
30+
31+
# All of our REST calls will use the url for the APIC EM Controller as the base URL
32+
# So lets define a variable for the controller IP or DNS so we don't have to keep typing it
33+
controller_url = "http://64.103.26.59:8081/"
34+
35+
# Get Devices
36+
# This function allows you to view a list of all the devices in the network(routers and switches).
37+
get_devices_url = controller_url + 'api/v0/network-device'
38+
39+
#Perform GET on get_devices_url and load response into a json object
40+
get_devices_response = requests.get(get_devices_url)
41+
42+
get_devices_json = get_devices_response.json()
43+
44+
45+
#Now let's read and display some specific information from the json
46+
47+
# set our parent as the top level response object
48+
parent = get_devices_json["response"]
49+
50+
print "\nDevices = "
51+
52+
# for each device returned, print the networkDeviceId
53+
for item in parent:
54+
print item["networkDeviceId"]
55+
56+
57+
# Get Hosts
58+
# This function allows you to view a list of all the hosts in the network.
59+
get_hosts_url = controller_url + 'api/v0/host'
60+
61+
#Perform GET on get_hosts_url and load response into a json object
62+
get_hosts_response = requests.get(get_hosts_url)
63+
64+
get_hosts_json = get_hosts_response.json()
65+
66+
#Now let's read and display some specific information from the json
67+
68+
# set our parent as the top level response object
69+
hosts_parent = get_hosts_json["response"]
70+
71+
print "\nHosts= "
72+
73+
# for each device returned, print the networkDeviceId
74+
for item in hosts_parent:
75+
print item["hostIp"]
76+
77+
78+
# Get Policies
79+
# This function allows you to view a list of all the policies in the network.
80+
get_policies_url = controller_url + 'api/v0/policy'
81+
82+
#Perform GET on get_hosts_url and load response into a json object
83+
get_policies_response = requests.get(get_policies_url)
84+
85+
get_policies_json = get_policies_response.json()
86+
87+
#Now let's read and display some specific information from the json
88+
89+
# set our parent as the top level response object
90+
policies_parent = get_policies_json["response"]
91+
92+
print "\nPolicies= "
93+
94+
# for each device returned, print the networkDeviceId
95+
for item in policies_parent:
96+
print item["policyId"]
97+
98+
99+
100+
# Get Applications
101+
# This function allows you to view a list of all the applications in the network.
102+
get_apps_url = controller_url + 'api/v0/configured-application'
103+
104+
#Perform GET on get_hosts_url and load response into a json object
105+
get_apps_response = requests.get(get_apps_url)
106+
107+
get_apps_json = get_apps_response.json()
108+
109+
#Now let's read and display some specific information from the json
110+
111+
# set our parent as the top level response object
112+
apps_parent = get_apps_json["response"]
113+
114+
print "\nApplications= "
115+
116+
# for each device returned, print the networkDeviceId
117+
for item in apps_parent:
118+
print item["applicationName"]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Getting started with APIC-EM APIs
2+
# Follows APIC-EM Basics Learning Lab
3+
# Hello World with JSON and pretty printing
4+
5+
# * THIS SAMPLE APPLICATION AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
6+
# * OF ANY KIND BY CISCO, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
7+
# * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY FITNESS FOR A PARTICULAR
8+
# * PURPOSE, NONINFRINGEMENT, SATISFACTORY QUALITY OR ARISING FROM A COURSE OF
9+
# * DEALING, LAW, USAGE, OR TRADE PRACTICE. CISCO TAKES NO RESPONSIBILITY
10+
# * REGARDING ITS USAGE IN AN APPLICATION, AND IT IS PRESENTED ONLY AS AN
11+
# * EXAMPLE. THE SAMPLE CODE HAS NOT BEEN THOROUGHLY TESTED AND IS PROVIDED AS AN
12+
# * EXAMPLE ONLY, THEREFORE CISCO DOES NOT GUARANTEE OR MAKE ANY REPRESENTATIONS
13+
# * REGARDING ITS RELIABILITY, SERVICEABILITY, OR FUNCTION. IN NO EVENT DOES
14+
# * CISCO WARRANT THAT THE SOFTWARE IS ERROR FREE OR THAT CUSTOMER WILL BE ABLE
15+
# * TO OPERATE THE SOFTWARE WITHOUT PROBLEMS OR INTERRUPTIONS. NOR DOES CISCO
16+
# * WARRANT THAT THE SOFTWARE OR ANY EQUIPMENT ON WHICH THE SOFTWARE IS USED WILL
17+
# * BE FREE OF VULNERABILITY TO INTRUSION OR ATTACK. THIS SAMPLE APPLICATION IS
18+
# * NOT SUPPORTED BY CISCO IN ANY MANNER. CISCO DOES NOT ASSUME ANY LIABILITY
19+
# * ARISING FROM THE USE OF THE APPLICATION. FURTHERMORE, IN NO EVENT SHALL CISCO
20+
# * OR ITS SUPPLIERS BE LIABLE FOR ANY INCIDENTAL OR CONSEQUENTIAL DAMAGES, LOST
21+
# * PROFITS, OR LOST DATA, OR ANY OTHER INDIRECT DAMAGES EVEN IF CISCO OR ITS
22+
# * SUPPLIERS HAVE BEEN INFORMED OF THE POSSIBILITY THEREOF.-->
23+
24+
# import the requests library so we can use it to make REST calls (http://docs.python-requests.org/en/latest/index.html)
25+
import requests
26+
27+
# import the json library. This provides many handy features for formatting, displaying
28+
# and manipulating json. https://docs.python.org/2/library/json.html
29+
import json
30+
31+
# All of our REST calls will use the url for the APIC EM Controller as the base URL
32+
# So lets define a variable for the controller IP or DNS so we don't have to keep typing it
33+
controller_url = "https://sandboxapic.cisco.com"
34+
35+
# Get Devices
36+
# This function allows you to view a list of all the devices in the network(routers and switches).
37+
get_devices_url = controller_url + '/api/v0/network-device/1/3'
38+
39+
#Perform GET on get_devices_url
40+
get_devices_response = requests.get(get_devices_url, verify=False)
41+
42+
# The json method of the response object returned by requests.get returns the request body in json format
43+
get_devices_json = get_devices_response.json()
44+
45+
# json.dumps serializes the json into a string and allows us to
46+
# print the response in a 'pretty' format with indentation etc.
47+
print ("Devices = ")
48+
print (json.dumps(get_devices_json, indent=4, separators=(',', ': ')))
49+

0 commit comments

Comments
 (0)