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 "\n Devices = "
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 "\n Hosts= "
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 "\n Policies= "
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 "\n Applications= "
115+
116+ # for each device returned, print the networkDeviceId
117+ for item in apps_parent :
118+ print item ["applicationName" ]
0 commit comments