From 23ffad7f6655e64e330f8b2bdbff9c69499991c7 Mon Sep 17 00:00:00 2001 From: Tony Mack Date: Mon, 10 Dec 2012 17:48:33 -0500 Subject: [PATCH] Initial Checkin of REST API support --- PLC/RestAPI.py | 36 ++++++++++++++++++++++++++++++++ RestClient.py | 14 +++++++++++++ RestServer.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 PLC/RestAPI.py create mode 100755 RestClient.py create mode 100755 RestServer.py diff --git a/PLC/RestAPI.py b/PLC/RestAPI.py new file mode 100644 index 0000000..1e4f8ed --- /dev/null +++ b/PLC/RestAPI.py @@ -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 = """ + +PLCAPI Nova JSON REST Interface + +

PLCAPI Nova JSON REST Interface

+

Please use HTTP to access the PLCAPI.

+ +""" + +@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 index 0000000..359759b --- /dev/null +++ b/RestClient.py @@ -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 index 0000000..ebf511b --- /dev/null +++ b/RestServer.py @@ -0,0 +1,56 @@ +#!/usr/bin/python +# +# Standalone WSGI PLCAPI Server +# +# Tony Mack +# 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='', help='TCP port number to listen on') +parser.add_option('-f', '--config', dest='config', metavar='', 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) + -- 2.43.0