3ceb1b398741d53284f1d10535fc796e6b2ebaa4
[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 nst
25 from osmclient.sol005 import nsi
26 from osmclient.sol005 import ns
27 from osmclient.sol005 import vnf
28 from osmclient.sol005 import vim
29 from osmclient.sol005 import wim
30 from osmclient.sol005 import package
31 from osmclient.sol005 import http
32 from osmclient.sol005 import sdncontroller
33 from osmclient.sol005 import project as projectmodule
34 from osmclient.sol005 import user as usermodule
35 from osmclient.sol005 import role
36 from osmclient.sol005 import pdud
37 from osmclient.sol005 import k8scluster
38 from osmclient.sol005 import repo
39 from osmclient.common import package_tool
40 import json
41 import logging
42
43
44 class Client(object):
45
46 def __init__(
47 self,
48 host=None,
49 so_port=9999,
50 user='admin',
51 password='admin',
52 project='admin',
53 **kwargs):
54
55 self._user = user
56 self._password = password
57 self._project = project
58 self._logger = logging.getLogger('osmclient')
59 self._auth_endpoint = '/admin/v1/tokens'
60 self._headers = {}
61 self._token = None
62
63 if len(host.split(':')) > 1:
64 # backwards compatible, port provided as part of host
65 self._host = host.split(':')[0]
66 self._so_port = host.split(':')[1]
67 else:
68 self._host = host
69 self._so_port = so_port
70
71 self._http_client = http.Http(
72 'https://{}:{}/osm'.format(self._host,self._so_port))
73 self._headers['Accept'] = 'application/json'
74 self._headers['Content-Type'] = 'application/yaml'
75 http_header = ['{}: {}'.format(key, val)
76 for (key, val) in list(self._headers.items())]
77 self._http_client.set_http_header(http_header)
78
79 self.vnfd = vnfd.Vnfd(self._http_client, client=self)
80 self.nsd = nsd.Nsd(self._http_client, client=self)
81 self.nst = nst.Nst(self._http_client, client=self)
82 self.package = package.Package(self._http_client, client=self)
83 self.ns = ns.Ns(self._http_client, client=self)
84 self.nsi = nsi.Nsi(self._http_client, client=self)
85 self.vim = vim.Vim(self._http_client, client=self)
86 self.wim = wim.Wim(self._http_client, client=self)
87 self.sdnc = sdncontroller.SdnController(self._http_client, client=self)
88 self.vnf = vnf.Vnf(self._http_client, client=self)
89 self.project = projectmodule.Project(self._http_client, client=self)
90 self.user = usermodule.User(self._http_client, client=self)
91 self.role = role.Role(self._http_client, client=self)
92 self.pdu = pdud.Pdu(self._http_client, client=self)
93 self.k8scluster = k8scluster.K8scluster(self._http_client, client=self)
94 self.repo = repo.Repo(self._http_client, client=self)
95 self.package_tool = package_tool.PackageTool(client=self)
96 '''
97 self.vca = vca.Vca(http_client, client=self, **kwargs)
98 self.utils = utils.Utils(http_client, **kwargs)
99 '''
100
101 def get_token(self):
102 self._logger.debug("")
103 if self._token is None:
104 postfields_dict = {'username': self._user,
105 'password': self._password,
106 'project_id': self._project}
107 http_code, resp = self._http_client.post_cmd(endpoint=self._auth_endpoint,
108 postfields_dict=postfields_dict)
109 # if http_code not in (200, 201, 202, 204):
110 # message ='Authentication error: not possible to get auth token\nresp:\n{}'.format(resp)
111 # raise ClientException(message)
112
113 token = json.loads(resp) if resp else None
114 self._token = token['id']
115
116 if self._token is not None:
117 self._headers['Authorization'] = 'Bearer {}'.format(self._token)
118 http_header = ['{}: {}'.format(key, val)
119 for (key, val) in list(self._headers.items())]
120 self._http_client.set_http_header(http_header)
121
122 def get_version(self):
123 _, resp = self._http_client.get2_cmd(endpoint="/version")
124 resp = json.loads(resp)
125 return "{} {}".format(resp.get("version"), resp.get("date"))