Skip to content

Instantly share code, notes, and snippets.

@CrashLaker
Created December 2, 2020 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CrashLaker/291668bc5d2bd1d21461ae003bdbb278 to your computer and use it in GitHub Desktop.
Save CrashLaker/291668bc5d2bd1d21461ae003bdbb278 to your computer and use it in GitHub Desktop.
medium-grafana-simplejson-search
from flask import Flask, request, jsonify
from flask_cors import CORS
app = Flask(__name__)
cors = CORS(app)
@app.route('/', methods=['GET', 'POST'])
def main():
return "você acessou o /"
@app.route('/search', methods=['GET', 'POST'])
def r_search():
req = request.json
cmdb = {
"Datacenter SP": {
"Cluster_Datacenter SP_A": {
"Host1": [
"VM1"
],
"Host2": [
"VM2"
]
}
},
"Datacenter RJ": {
"Cluster_Datacenter RJ_A": {
"Host5": [
"VM6",
"VM7"
],
"Host6": [
"VM8",
"VM9"
]
},
"Cluster_Datacenter RJ_B": {
"Host7": [
"VM10",
"VM11"
]
}
},
"Datacenter PR": {
"Cluster_Datacenter PR_A": {
"Host8": [
"VM12"
]
}
}
}
response = []
parsed = dict([(c.split("=")) for c in req["target"].split("&")])
order = ["datacenter", "cluster", "host", "vm"]
for item in order:
if parsed[item] == "":
if type(cmdb) is dict:
response = list(cmdb.keys())
elif type(cmdb) is list:
response = cmdb
break
cmdb = cmdb[parsed[item]]
response = cmdb
return jsonify(response)
@app.route('/query', methods=['GET', 'POST'])
def r_querey():
return "você acessou o /query"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8081)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment