Pass headers for multi-tenant support
[multicloud/framework.git] / multivimbroker / multivimbroker / pub / utils / syscomm.py
1 # Copyright (c) 2017 Wind River Systems, Inc.
2 # Copyright (c) 2017-2018 VMware, Inc.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #       http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
13 import inspect
14 import json
15 import os
16 import re
17
18 import multivimbroker.pub.exceptions as exceptions
19 from multivimbroker.pub.msapi import extsys
20
21
22 def fun_name():
23     return inspect.stack()[1][3]
24
25
26 # Which headers are hop-by-hop headers by default
27 HOP_BY_HOP = ['connection', 'keep-alive', 'proxy-authenticate',
28               'proxy-authorization', 'te', 'trailers',
29               'transfer-encoding', 'upgrade']
30
31
32 def getHeadersKeys(response):
33     hopbyhop = HOP_BY_HOP
34     hopbyhop.extend([x.strip()
35                      for x in response.get('connection', '').split(',')])
36     return [header for header in response.keys() if header not in hopbyhop]
37
38
39 # trim out 'HTTP_' prefix part and replace "_" wiht "-".
40 def originHeaders(request):
41     headers = {}
42     for key, value in request.META.items():
43         if key.startswith('HTTP_') and key != 'HTTP_HOST':
44             headers[key[5:].replace('_', '-')] = value
45         elif key in ('CONTENT_TYPE', 'CONTENT_LENGTH'):
46             headers[key.replace('_', '-')] = value
47         elif key.lower() in ('project', 'project_id', 'project_name',
48                              'tenant', 'tenant_id', 'tenant_name'):
49             # support API to specify project other than the default one
50             headers[key] = value
51         # elif key.lower() in ('x-auth-token',
52         #                      'http_x_auth_token', 'x_auth_token'):
53         #     # pass the token to plugins
54         #     headers["X-Auth-Token"] = value
55     return headers
56
57
58 def findMultivimDriver(vim=None):
59     json_file = os.path.join(os.path.dirname(__file__),
60                              '../config/provider-plugin.json')
61     with open(json_file, "r") as f:
62         plugins = json.load(f)
63     if not vim or vim.get("type") not in plugins.keys():
64         raise exceptions.NotFound("Not support VIM type")
65     plugin = plugins[vim["type"]]
66     if vim.get("version") in plugin["versions"].keys():
67         return plugin["versions"][vim["version"]]["provider_plugin"]
68     return plugin["provider_plugin"]
69
70
71 def getMultivimDriver(vimid, full_path=""):
72     multcloud = "multicloud"
73     vim = extsys.get_vim_by_id(vimid)
74     multclouddriver = findMultivimDriver(vim=vim)
75     return re.sub(multcloud, multclouddriver, full_path)
76
77
78 def getVIMTypes():
79     # Fix here unless we have plugin registry
80     json_file = os.path.join(os.path.dirname(__file__),
81                              '../config/provider-plugin.json')
82     with open(json_file, "r") as f:
83         plugins = json.load(f)
84     ret = []
85     for k, v in plugins.items():
86         item = {}
87         item["vim_type"] = v.get("vim_type")
88         item["versions"] = [k for k in v.get('versions', {})]
89         ret.append(item)
90
91     return ret