d1c90aabb4e6ee98aa140775553134a8b4df885d
[osm/osmclient.git] / osmclient / sol005 / client.py
1 # Copyright 2018 Telefonica
2 #
3 # All Rights Reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
15 # under the License.
16
17 """
18 OSM SOL005 client API
19 """
20
21 #from osmclient.v1 import vnf
22 #from osmclient.v1 import vca
23 from osmclient.sol005 import vnfd
24 from osmclient.sol005 import nsd
25 from osmclient.sol005 import ns
26 from osmclient.sol005 import vim
27 from osmclient.sol005 import package
28 from osmclient.sol005 import http
29 from osmclient.sol005 import sdncontroller
30 from osmclient.common.exceptions import ClientException
31
32 class Client(object):
33
34 def __init__(
35 self,
36 host=None,
37 so_port=9999,
38 so_project='admin',
39 ro_host=None,
40 ro_port=9090,
41 **kwargs):
42
43 self._user = 'admin'
44 self._password = 'admin'
45 #self._project = so_project
46 self._project = 'admin'
47 self._auth_endpoint = '/admin/v1/tokens'
48 self._headers = {}
49
50 if len(host.split(':')) > 1:
51 # backwards compatible, port provided as part of host
52 self._host = host.split(':')[0]
53 self._so_port = host.split(':')[1]
54 else:
55 self._host = host
56 self._so_port = so_port
57
58 if ro_host is None:
59 ro_host = host
60 ro_http_client = http.Http('http://{}:{}/'.format(ro_host, ro_port))
61 ro_http_client.set_http_header(
62 ['Accept: application/vnd.yand.data+json',
63 'Content-Type: application/json'])
64
65 self._http_client = http.Http(
66 'https://{}:{}/osm'.format(self._host,self._so_port))
67 self._headers['Accept'] = 'application/json'
68 self._headers['Content-Type'] = 'application/yaml'
69 http_header = ['{}: {}'.format(key,val)
70 for (key,val) in self._headers.items()]
71 self._http_client.set_http_header(http_header)
72
73 token = self.get_token()
74 if not token:
75 raise ClientException(
76 'Authentication error: not possible to get auth token')
77 self._headers['Authorization'] = 'Bearer {}'.format(token)
78 http_header.append('Authorization: Bearer {}'.format(token))
79 self._http_client.set_http_header(http_header)
80
81 self.vnfd = vnfd.Vnfd(self._http_client, client=self)
82 self.nsd = nsd.Nsd(self._http_client, client=self)
83 self.package = package.Package(self._http_client, client=self)
84 self.ns = ns.Ns(self._http_client, client=self)
85 self.vim = vim.Vim(self._http_client, client=self)
86 self.sdnc = sdncontroller.SdnController(self._http_client, client=self)
87 '''
88 self.vnf = vnf.Vnf(http_client, client=self, **kwargs)
89 self.vca = vca.Vca(http_client, client=self, **kwargs)
90 self.utils = utils.Utils(http_client, **kwargs)
91 '''
92
93 def get_token(self):
94 postfields_dict = {'username': self._user,
95 'password': self._password,
96 'project-id': self._project}
97 token = self._http_client.post_cmd(endpoint=self._auth_endpoint,
98 postfields_dict=postfields_dict)
99 if token is not None:
100 return token['_id']
101 return None
102