5de4ff05c8724c509a1cb474913b4159130dc8cb
[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 vca
22 from osmclient.sol005 import vnfd
23 from osmclient.sol005 import nsd
24 from osmclient.sol005 import ns
25 from osmclient.sol005 import vnf
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.sol005 import project as projectmodule
31 from osmclient.sol005 import user as usermodule
32 from osmclient.common.exceptions import ClientException
33 import json
34
35 class Client(object):
36
37 def __init__(
38 self,
39 host=None,
40 so_port=9999,
41 user='admin',
42 password='admin',
43 project='admin',
44 **kwargs):
45
46 self._user = user
47 self._password = password
48 self._project = project
49 self._auth_endpoint = '/admin/v1/tokens'
50 self._headers = {}
51
52 if len(host.split(':')) > 1:
53 # backwards compatible, port provided as part of host
54 self._host = host.split(':')[0]
55 self._so_port = host.split(':')[1]
56 else:
57 self._host = host
58 self._so_port = so_port
59
60 self._http_client = http.Http(
61 'https://{}:{}/osm'.format(self._host,self._so_port))
62 self._headers['Accept'] = 'application/json'
63 self._headers['Content-Type'] = 'application/yaml'
64 http_header = ['{}: {}'.format(key,val)
65 for (key,val) in list(self._headers.items())]
66 self._http_client.set_http_header(http_header)
67
68 token = self.get_token()
69 if not token:
70 raise ClientException(
71 'Authentication error: not possible to get auth token')
72 self._headers['Authorization'] = 'Bearer {}'.format(token)
73 http_header.append('Authorization: Bearer {}'.format(token))
74 self._http_client.set_http_header(http_header)
75
76 self.vnfd = vnfd.Vnfd(self._http_client, client=self)
77 self.nsd = nsd.Nsd(self._http_client, client=self)
78 self.package = package.Package(self._http_client, client=self)
79 self.ns = ns.Ns(self._http_client, client=self)
80 self.vim = vim.Vim(self._http_client, client=self)
81 self.sdnc = sdncontroller.SdnController(self._http_client, client=self)
82 self.vnf = vnf.Vnf(self._http_client, client=self)
83 self.project = projectmodule.Project(self._http_client, client=self)
84 self.user = usermodule.User(self._http_client, client=self)
85 '''
86 self.vca = vca.Vca(http_client, client=self, **kwargs)
87 self.utils = utils.Utils(http_client, **kwargs)
88 '''
89
90 def get_token(self):
91 postfields_dict = {'username': self._user,
92 'password': self._password,
93 'project_id': self._project}
94 http_code, resp = self._http_client.post_cmd(endpoint=self._auth_endpoint,
95 postfields_dict=postfields_dict)
96 if http_code not in (200, 201, 202, 204):
97 raise ClientException(resp)
98 token = json.loads(resp) if resp else None
99 if token is not None:
100 return token['_id']
101 return None
102