Initial Checkin of REST API support
authorTony Mack <tmack@paris.CS.Princeton.EDU>
Mon, 10 Dec 2012 22:48:33 +0000 (17:48 -0500)
committerTony Mack <tmack@paris.CS.Princeton.EDU>
Mon, 10 Dec 2012 22:48:33 +0000 (17:48 -0500)
PLC/RestAPI.py [new file with mode: 0644]
RestClient.py [new file with mode: 0755]
RestServer.py [new file with mode: 0755]

diff --git a/PLC/RestAPI.py b/PLC/RestAPI.py
new file mode 100644 (file)
index 0000000..1e4f8ed
--- /dev/null
@@ -0,0 +1,36 @@
+from flask import Flask, url_for, request, Response, jsonify
+app = Flask(__name__)
+
+from PLC.API import PLCAPI
+from PLC.Config import Config
+from PLC.Logger import logger
+
+api = PLCAPI()
+
+get_response = """
+<html><head>
+<title>PLCAPI Nova JSON REST Interface</title>
+</head><body>
+<h1>PLCAPI Nova JSON REST Interface</h1>
+<p>Please use HTTP to access the PLCAPI. </p>
+</body></html>
+"""
+
+@app.route('/')
+def api_root():
+    return 'Welcome'
+
+@app.route('/auth', methods=['GET', 'POST'])
+def AuthCheck():
+    if request.method == 'POST':
+        args = [request.json.get('auth')]
+        return api.call(None, 'AuthCheck', *args)
+    return get_response
+      
+@app.route('/roles', methods=['GET', 'POST'])
+def GetRoles():
+    if request.method == 'POST':
+        logger.error(request.data) 
+        return jsonify(**request.json)
+    return get_response 
+
diff --git a/RestClient.py b/RestClient.py
new file mode 100755 (executable)
index 0000000..359759b
--- /dev/null
@@ -0,0 +1,14 @@
+#!/usr/bin/python
+import urllib2
+import json
+
+auth = {'AuthMethod': 'password',
+        'Username': 'USERNAME',
+        'AuthString': 'PASSWORD',
+        'Tenant': 'TENANT'}
+req_data = json.dumps({'auth': auth})
+req = urllib2.Request("http://0.0.0.0:8000/auth", req_data, {'Content-Type': 'application/json'})
+f = urllib2.urlopen(req) 
+response = f.read()
+f.close()
+
diff --git a/RestServer.py b/RestServer.py
new file mode 100755 (executable)
index 0000000..ebf511b
--- /dev/null
@@ -0,0 +1,56 @@
+#!/usr/bin/python
+#
+# Standalone WSGI PLCAPI Server
+#
+# Tony Mack <tmack@cs.princeton.edu>
+# Copyright (C) 2006 The Trustees of Princeton University
+#
+
+import os
+import sys
+import traceback
+from optparse import OptionParser
+from gevent import pywsgi
+# Append PLC to the system path
+sys.path.append(os.path.dirname(os.path.realpath(sys.argv[0])))
+from PLC.RestAPI import app
+
+# Defaults
+addr = "0.0.0.0"
+port = 8000
+config_file = '/etc/planetlab/plcapi_config'
+keyfile=None
+certfile=None
+
+
+# Get options
+parser = OptionParser()
+parser.add_option('-p', '--port', dest='port', metavar='<port>', help='TCP port number to listen on')
+parser.add_option('-f', '--config', dest='config', metavar='<config>', help='PLCAPI configuration file')
+options = None
+args = None 
+try:
+    (options, args) = parser.parse_args()
+except:
+    print "Usage: %s [OPTION]..." % sys.argv[0]
+    print "Options:"
+    print "     -p PORT, --port=PORT    TCP port number to listen on (default: %d)" % port
+    print "     -f FILE, --config=FILE  PLC configuration file (default: %s)" % config
+    print "     -h, --help              This message"
+    sys.exit(1) 
+
+if options.config:
+    config = Config(options.config)
+    addr = config.api_host
+    keyfile = config.api_ssl_key
+    certfile = config.api_ssl_cert
+if options.port:
+    port = int(options.port)
+
+app.debug = True
+if keyfile and certfile:
+    app.run(host=addr, port=port, ssl_context=(certfile, keyfile))
+else:    
+    app.run(host=addr, port=port)