From 6bc505011fcef6b541d59090509fc82ec93a5f0c Mon Sep 17 00:00:00 2001 From: Uggla Date: Fri, 26 Jun 2015 00:33:57 +0200 Subject: [PATCH] Implement REST API example with Flask. --- .gitignore | 62 ++++++++++++++++++++++++++++++++++++++ alexandria/alexandria.conf | 20 ++++++++++++ alexandria/app.py | 50 ++++++++++++++++++++++++++++++ alexandria/config.py | 16 ++++++++++ 4 files changed, 148 insertions(+) create mode 100644 .gitignore create mode 100644 alexandria/alexandria.conf create mode 100644 alexandria/app.py create mode 100644 alexandria/config.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1d9192b --- /dev/null +++ b/.gitignore @@ -0,0 +1,62 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml + +# Translations +*.mo +*.pot + +# Django stuff: +*.log + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Pydev +.project +.pydevproject +.settings/ +.metadata diff --git a/alexandria/alexandria.conf b/alexandria/alexandria.conf new file mode 100644 index 0000000..0ca2580 --- /dev/null +++ b/alexandria/alexandria.conf @@ -0,0 +1,20 @@ +[alexandria] +port=80 + +[itop] +drvtype=cmdb +endpoint=http://itop/rest +loginItop=itopuser +passwordItop=itoppassword + +[redfish] +drvtype=hw + +[ironic] +drvtype=hw +endpoint=http://ironic/rest + +[mondorescue] +drvtype=hw +paramMondo1 +paramMondo2 diff --git a/alexandria/app.py b/alexandria/app.py new file mode 100644 index 0000000..bcd84b9 --- /dev/null +++ b/alexandria/app.py @@ -0,0 +1,50 @@ +# coding=utf-8 + +from flask import Flask +#from flask import Response +from flask import jsonify +import config + + +# Vars +alexandria_version="0.1" + +# Configuration file +conf_file = config.AlexandriaConfiguration('alexandria.conf') + +# Initialise Flask +app = Flask(__name__) +app.debug = True + +@app.route('/drivers', methods = ['GET']) +def api_drivers(): + data = {'drivers' : conf_file.get_drivers()} + resp = jsonify(data) + resp.status_code = 200 + return resp + +@app.route('/drivers/') +def api_driver(driver_name): + data = {driver_name : conf_file.get_driver_info(driver_name)} + resp = jsonify(data) + resp.status_code = 200 + return resp + +@app.route('/', methods = ['GET']) +def api_root(): + global alexandria_version + data = { + 'Service' : 'Alexandria', + 'Version' : alexandria_version + } + + resp = jsonify(data) + resp.status_code = 200 + + resp.headers['Link'] = 'http://luisrei.com' + + return resp + + +if __name__ == '__main__': + app.run() diff --git a/alexandria/config.py b/alexandria/config.py new file mode 100644 index 0000000..cd6ebab --- /dev/null +++ b/alexandria/config.py @@ -0,0 +1,16 @@ +# coding=utf-8 + +import ConfigParser + + +class AlexandriaConfiguration(object): + + def __init__(self, configuration_file): + self.config = ConfigParser.ConfigParser(allow_no_value=True) + self.config.read(configuration_file) + + def get_drivers(self): + return self.config.sections() + + def get_driver_info(self,driver): + return self.config.options(driver) \ No newline at end of file