1+ # import requests library
2+ import requests
3+
4+ # import json library
5+ import json
6+
7+ # Disable warnings
8+ requests .packages .urllib3 .disable_warnings ()
9+
10+ # controller = IP Address of the controller
11+ controller = ""
12+
13+ # Authentication Token obtained from Spark's Developer page
14+ auth = ""
15+
16+ def getTicket ():
17+ # Provide APIC-EM's username
18+ username = ""
19+
20+ # Provide password for the username defined above
21+ password = ""
22+
23+ # put the ip address or dns of your apic-em controller in this url
24+ url = "https://" + controller + "/api/v1/ticket"
25+
26+ # the username and password to access the APIC-EM Controller
27+ payload = {"username" : username , "password" : password }
28+
29+ # Content type must be included in the header
30+ header = {"content-type" : "application/json" }
31+
32+ # Performs a POST on the specified url to get the service ticket
33+ response = requests .post (url , data = json .dumps (
34+ payload ), headers = header , verify = False )
35+
36+ print (response )
37+
38+ # convert response to json format
39+ r_json = response .json ()
40+
41+ # parse the json to get the service ticket
42+ ticket = r_json ["response" ]["serviceTicket" ]
43+
44+ return ticket
45+
46+
47+ def getTopology (ticket ):
48+
49+ # Final Result
50+ result = []
51+
52+ # API Call to retrieve physical topology
53+ api_call = ""
54+
55+ # URL for topology REST API call to get list of existing devices on the
56+ # network, and build topology
57+ url = "https://" + controller + "/api/v1" + api_call
58+
59+ # Content type as well as the ticket must be included in the header
60+ header = {"content-type" : "application/json" , "X-Auth-Token" : ticket }
61+
62+ # this statement performs a GET on the specified network device url
63+ response = requests .get (url , headers = header , verify = False )
64+
65+ # convert data to json format.
66+ r_json = response .json ()
67+
68+ # Iterate through network device data and list the nodes, their
69+ # interfaces, status and to what they connect
70+ for n in r_json ["response" ]["nodes" ]:
71+ found = 0 # print header flag
72+ printed = 0 # formatting flag
73+ for i in r_json ["response" ]["links" ]:
74+ # Find interfaces that link to this one which means this node is
75+ # the target.
76+ if i ["target" ] == n ["id" ]:
77+ if found == 0 :
78+ if printed == 1 :
79+ print ()
80+ #print('{:>10}'.format("Source") + '{:>30}'.format("Source Interface") + '{:>25}'.format("Target Interface") + '{:>13}'.format("Status"))
81+ result .append ('\n ' + '{:>10}' .format ("Source" ) + '{:>30}' .format (
82+ "Source Interface" ) + '{:>25}' .format ("Target Interface" ) + '{:>13}' .format ("Status" ))
83+ found = 1
84+ for n1 in r_json ["response" ]["nodes" ]:
85+ # find name of node to that connects to this one
86+ if i ["source" ] == n1 ["id" ]:
87+ if "startPortName" in i :
88+ #print(" " + '{:<20}'.format(n1["label"]) + '{:<25}'.format(i["startPortName"]) + '{:<23}'.format(i["endPortName"]) + '{:<8}'.format(i["linkStatus"]))
89+ result .append (" " + '{:<20}' .format (n1 ["label" ]) + '{:<25}' .format (
90+ i ["startPortName" ]) + '{:<23}' .format (i ["endPortName" ]) + '{:<8}' .format (i ["linkStatus" ]))
91+ else :
92+ #print(" " + '{:<20}'.format(n1["label"]) + '{:<25}'.format("unknown") + '{:<23}'.format("unknown") + '{:<8}'.format(i["linkStatus"]))
93+ result .append (" " + '{:<20}' .format (n1 ["label" ]) + '{:<25}' .format (
94+ "unknown" ) + '{:<23}' .format ("unknown" ) + '{:<8}' .format (i ["linkStatus" ]))
95+ break
96+ return (result )
97+
98+
99+ def get_roomID ():
100+ # API Call for rooms
101+ api_call = "rooms"
102+
103+ # Please provide your personal authorization token bellow
104+
105+
106+ # Cisco Spark's API URL address
107+ url = "https://api.ciscospark.com/v1/" + api_call
108+
109+ # Content type as well as the authorization must be included in the header
110+ header = {"content-type" : "application/json; charset=utf-8" ,
111+ "Authorization" : "Bearer " + auth }
112+
113+ # this statement performs a GET on the specified network device url
114+ response = requests .get (url , headers = header , verify = False )
115+ r_json = response .json ()
116+
117+ for item in r_json ["items" ]:
118+ print ("Title " + item ["title" ])
119+ print ("Room ID " + item ["id\n \n " ])
120+ user_input = input (
121+ "Is this the room you are looking for to post?[y/n] " )
122+ if user_input .lower () == 'y' or user_input .lower () == 'yes' :
123+ return item ["id" ]
124+ else :
125+ continue
126+
127+
128+ def post_spark (text , room_id ):
129+ """
130+ This function will post your text to DevNet Express Room in Spark
131+ """
132+ # API Call to for messages
133+ api_call = "messages"
134+
135+ # Cisco Spark's API URL address
136+ url = "https://api.ciscospark.com/v1/" + api_call
137+
138+ # Content type as well as the authorization must be included in the header
139+ header = {"content-type" : "application/json; charset=utf-8" ,
140+ "Authorization" : "Bearer " + auth }
141+
142+ payload = {
143+ "roomId" : room_id ,
144+ "text" : '\n ' .join (text )
145+ }
146+ # this statement performs a GET on the specified network device url
147+ response = requests .post (url , data = json .dumps (
148+ payload ), headers = header , verify = False )
149+
150+ print ("\n Check the Spark Room, you just posted a message!" )
151+
152+
153+ if __name__ == "main" :
154+ # Get authentication ticket from APIC-EM
155+ theTicket = getTicket ()
156+
157+ # Use authentication ticket to get the topology information
158+ message = getTopology (theTicket )
159+
160+ # Get the room ID
161+ id = get_roomID ()
162+
163+ # Use room ID and retrieved topology information to post in the Spark
164+ post_spark (message , id )
0 commit comments