-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
133 lines (108 loc) · 4.1 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- coding: utf-8 -*-
import logging
import requests
import json
from urlparse import urlparse
import copy
from django.utils import six
from django.http import HttpResponse
from rest_framework.views import APIView
from sugon_api import settings
LOG = logging.getLogger(__name__)
def get_params(req):
LOG.debug('query: %s' % req.query_params)
if req.query_params:
qp = req.query_params.copy()
return six.iterlists(qp)
return {}
def get_data(req):
LOG.debug('data type: %s' % type(req.data))
LOG.debug(req.data)
if 'application/json' in req.content_type:
return json.dumps(req.data)
return req.data
def get_headers(req):
LOG.debug('headers:\n%s' % req.META)
# Added by Arthur
if 'CONTENT_TYPE' in req.META:
req.META['Content-Type'] = "application/json"
if 'HTTP_X_AUTH_TOKEN' in req.META:
req.META['X-Auth-Token'] = req.META['HTTP_X_AUTH_TOKEN']
return req.META
def get_cookies(req):
LOG.debug('cookies:\n%s' % req.COOKIES)
return req.COOKIES
def proxy(req, url, *args, **kw):
params = get_params(req)
data = get_data(req)
headers = get_headers(req)
cookies = get_cookies(req)
try:
LOG.info('Proxy a %s to %s' % (req.method, url))
return requests.request(req.method
, url
, params=params
, data=data
, headers=headers
, cookies=cookies
, timeout=5)
except Exception, e:
LOG.error(e)
return None
class ProxyView(APIView):
def _get_base_url(self):
raise NotImplementedError('Must be implemented in child')
def _proxy(self, req, *args, **kw):
res = proxy(req, self._get_base_url()+req.META['PATH_INFO'])
rsp = HttpResponse(res.text
, status=res.status_code
, content_type=res.headers.get('Content-Type'))
rsp['x-openstack-request-id'] = res.headers.get('x-openstack-request-id')
return rsp
def get(self, req, *args, **kw):
return self._proxy(req, *args, **kw)
def post(self, req, *args, **kw):
return self._proxy(req, *args, **kw)
catalog = {}
class IdentityView(ProxyView):
def _create_auth_tokens(self, req):
res = proxy(req, settings.PROXY_360_AUTH_URL+req.META['PATH_INFO'])
if res is None:
return HttpResponse('Error occured!', status=500)
try:
body = res.json()
LOG.debug('catalog: %s' % json.dumps(catalog))
LOG.debug('response body: %s' % json.dumps(body))
for svc in body['token']['catalog']:
LOG.debug(svc['name'])
catalog[svc['name']] = copy.deepcopy(svc)
for ep, ep_real in \
zip(svc['endpoints'], catalog[svc['name']]['endpoints']):
_url = urlparse(ep['url'])
ep["url"] = _url.scheme + '://' + settings.PROXY_LOCATION + _url.path
ep_real["host"] = _url.scheme + '://' + _url.netloc
LOG.debug(svc['endpoints'])
LOG.debug('catalog: %s' % json.dumps(catalog))
proxy_res = HttpResponse(json.dumps(body), status=res.status_code)
for k in [_ for _ in res.headers \
if _ in ['X-Subject-Token', 'Vary', 'Content-Type', \
'x-openstack-request-id']]:
LOG.debug(k)
proxy_res[k] = res.headers.get(k)
return proxy_res
except Exception, e:
LOG.error(e)
return HttpResponse(e, status=500)
def _get_base_url(self):
return settings.PROXY_360_AUTH_URL
def post(self, req, *args, **kw):
if req.META['PATH_INFO'].endswith('/auth/tokens'):
return self._create_auth_tokens(req)
return super(IdentityView, self).post(req, *args, **kw)
class ComputeView(ProxyView):
def _get_base_url(self):
host = catalog['nova']['endpoints'][0]['host']
for ep in catalog['nova']['endpoints'][1:]:
if ep['interface'] == 'internal':
host = ep['host']
return host