Support Keystone session in python runtime

Keystone session can be used directly in user function to access
OpenStack services.

Include openstack clients in python requirements as well.

Add a python function example.

Implements: blueprint qinling-openstack-clients
Change-Id: I4798c404cb57bafe14049f57ba8db7c7125106c7
This commit is contained in:
Lingxian Kong 2017-07-02 22:58:17 +12:00
parent 992c90e044
commit 786f83d182
6 changed files with 34 additions and 7 deletions

View File

@ -0,0 +1,12 @@
import swiftclient
def stat_object(context, container, object):
conn = swiftclient.Connection(
session=context['os_session'],
os_options={'region_name': 'RegionOne'},
)
obj_header = conn.head_object(container, object)
return obj_header

View File

@ -286,6 +286,7 @@ class KubernetesManager(base.OrchestratorBase):
'function_id': function_id,
'entry': entry,
'token': context.get_ctx().auth_token,
'auth_url': self.conf.keystone_authtoken.auth_url
}
LOG.debug(

View File

@ -1,7 +1,7 @@
FROM alpine:3.5
RUN apk update
RUN apk add --no-cache python2 python2-dev build-base py2-pip
RUN apk add --no-cache linux-headers python2 python2-dev build-base py2-pip py2-pbr
RUN pip install --upgrade pip
RUN rm -r /root/.cache

View File

@ -1,3 +1,6 @@
Flask>=0.11.1
httplib2
requests>=2.7.0
Flask>=0.10,!=0.11,<1.0 # BSD
python-openstackclient>=3.3.0,!=3.10.0 # Apache-2.0
python-neutronclient>=6.3.0 # Apache-2.0
python-swiftclient>=3.2.0 # Apache-2.0
python-ceilometerclient>=2.5.0 # Apache-2.0
keystoneauth1>=2.21.0 # Apache-2.0

View File

@ -23,12 +23,15 @@ from flask import abort
from flask import Flask
from flask import request
from flask import Response
from keystoneauth1.identity import generic
from keystoneauth1 import session
import requests
app = Flask(__name__)
zip_file = ''
function_module = 'main'
function_method = 'main'
openstack_session = None
@app.route('/download', methods=['POST'])
@ -37,11 +40,17 @@ def download():
function_id = request.form['function_id']
entry = request.form['entry']
token = request.form.get('token')
auth_url = request.form.get('auth_url')
headers = {}
if token:
headers = {'X-Auth-Token': token}
# Get openstack session.
global openstack_session
auth = generic.Token(auth_url=auth_url, token=token)
openstack_session = session.Session(auth=auth, verify=False)
global zip_file
zip_file = '%s.zip' % function_id
@ -73,6 +82,9 @@ def execute():
global zip_file
global function_module
global function_method
global openstack_session
context = {'os_session': openstack_session}
importer = zipimport.zipimporter(zip_file)
module = importer.load_module(function_module)
@ -88,10 +100,9 @@ def execute():
start = time.time()
try:
func = getattr(module, function_method)
result = func(**input)
result = func(context=context, **input)
except Exception as e:
result = str(e)
duration = time.time() - start
return Response(

View File

@ -45,4 +45,4 @@ commands = oslo_debug_helper {posargs}
ignore =
show-source = true
builtins = _
exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,tools,build
exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,tools,build,example