Implement REST API example with Flask.
This commit is contained in:
parent
99c22b0d1e
commit
6bc505011f
62
.gitignore
vendored
Normal file
62
.gitignore
vendored
Normal file
@ -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
|
20
alexandria/alexandria.conf
Normal file
20
alexandria/alexandria.conf
Normal file
@ -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
|
50
alexandria/app.py
Normal file
50
alexandria/app.py
Normal file
@ -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/<driver_name>')
|
||||||
|
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()
|
16
alexandria/config.py
Normal file
16
alexandria/config.py
Normal file
@ -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)
|
Loading…
x
Reference in New Issue
Block a user