-
Notifications
You must be signed in to change notification settings - Fork 9
/
run.py
177 lines (135 loc) · 4.73 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import json
import os
import argparse
import sys
from flask import Flask, request
from webexteamssdk import WebexTeamsAPI
__app__ = "cisco-dnac-platform-webex-notifications"
__version__ = "1.3.3"
__author__ = "Robert Csapo"
__email__ = "rcsapo@cisco.com"
__version__ = "1.0"
__copyright__ = "Copyright (c) 2020 Cisco and/or its affiliates."
__license__ = "Cisco Sample Code License, Version 1.1"
""" Read Cisco Webex Teams Settings either for Environment or in Code """
if "WEBEX_TEAMS_ACCESS_TOKEN" not in os.environ:
webexAPI = WebexTeamsAPI(access_token="CHANGEME")
else:
webexAPI = WebexTeamsAPI()
if "WEBEX_TEAMS_ROOM_ID" not in os.environ:
os.environ["WEBEX_TEAMS_ROOM_ID"] = "CHANGEME"
webexRoomId = os.environ["WEBEX_TEAMS_ROOM_ID"]
else:
webexRoomId = os.environ["WEBEX_TEAMS_ROOM_ID"]
""" Verify Cisco Webex Teams Settings """
def webex_init_healthcheck():
results = {}
try:
bot = webexAPI.people.me()
results["bot_name"] = bot.displayName
emails = []
for email in bot.emails:
emails.append(email)
results["bot_emails"] = emails
room = webexAPI.rooms.get(webexRoomId)
results["room_title"] = room.title
results["status"] = True
except Exception as e:
results["status"] = False
print(e)
return results
app = Flask(__name__)
""" Main page """
@app.route("/", methods=["GET", "POST"])
def mainPage():
if request.method == "GET":
return "{} version {} -> by {} ({})".format(
__app__, __version__, __author__, __email__
)
if request.method == "POST":
return "{} healthcheck".format(__app_)
return None
""" Sample post with included file """
@app.route("/sample", methods=["GET"])
def sample():
jsonFile = "outputdata.json"
with open(jsonFile) as f:
data = json.load(f)
issueTitle = data["details"]["Type"] + " " + data["details"]["Device"]
issuePriority = data["details"]["Assurance Issue Priority"]
issueSeverity = data["severity"]
issueSummary = data["details"]["Assurance Issue Details"]
data = "Warning Severity %s (%s)! %s - %s" % (
issueSeverity,
issuePriority,
issueTitle,
issueSummary,
)
webex(str(data))
return "Sample data from -> %s" % jsonFile
""" Sample post with sent file """
@app.route("/postsample", methods=["POST"])
def postSample():
data = request.json
issueTitle = data["details"]["Type"] + " " + data["details"]["Device"]
issuePriority = data["details"]["Assurance Issue Priority"]
issueSeverity = data["severity"]
issueSummary = data["details"]["Assurance Issue Details"]
data = "Warning Severity %s (%s)! %s - %s" % (
issueSeverity,
issuePriority,
issueTitle,
issueSummary,
)
webex(str(data))
return "Sample JSON Payload received"
""" Cisco Webex Teams Interface """
@app.route("/webex", methods=["GET"])
def webex(*data):
if not len(data) == 0:
data = data[0]
webexAPI.messages.create(webexRoomId, text=data)
else:
webexAPI.messages.create(webexRoomId, text="Sample connection!")
return "Sample Cisco Webex Teams Message"
""" Cisco DNA Center Incoming Events Interface """
@app.route("/dnac", methods=["POST"])
def dnacPayload():
data = request.json
if not len(data) == 0:
issueTitle = data["details"]["Type"] + " " + data["details"]["Device"]
issuePriority = data["details"]["Assurance Issue Priority"]
issueSeverity = data["severity"]
issueSummary = data["details"]["Assurance Issue Details"]
data = "Warning Severity %s (%s)! %s - %s" % (
issueSeverity,
issuePriority,
issueTitle,
issueSummary,
)
webex(str(data))
return "Cisco DNA Center JSON Payload received"
return "Connection Alive"
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="{} {}".format(__app__, __version__))
parser.add_argument("--ssl", action="store_true", help="enable SSL ADHOC")
args = parser.parse_args()
webex_init = webex_init_healthcheck()
if webex_init["status"] is False:
print("Problem with Cisco Webex Teams Settings {}".format(webex_init))
sys.exit()
else:
print(
" * Cisco Webex Teams: {} - {} - {}".format(
webex_init["bot_name"],
webex_init["room_title"],
webex_init["bot_emails"],
)
)
if args.ssl is True:
print(" * SSL Enabled (ADHOC)")
app.run(
host="0.0.0.0", port=5000, threaded=True, debug=False, ssl_context="adhoc"
)
else:
app.run(host="0.0.0.0", port=5000, threaded=True, debug=False)