Feature 11037 Change default NBI port
[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.sol005 import vnfd
22 from osmclient.sol005 import nsd
23 from osmclient.sol005 import nst
24 from osmclient.sol005 import nsi
25 from osmclient.sol005 import ns
26 from osmclient.sol005 import vnf
27 from osmclient.sol005 import vim
28 from osmclient.sol005 import wim
29 from osmclient.sol005 import package
30 from osmclient.sol005 import http
31 from osmclient.sol005 import sdncontroller
32 from osmclient.sol005 import project as projectmodule
33 from osmclient.sol005 import user as usermodule
34 from osmclient.sol005 import role
35 from osmclient.sol005 import pdud
36 from osmclient.sol005 import k8scluster
37 from osmclient.sol005 import vca
38 from osmclient.sol005 import repo
39 from osmclient.sol005 import osmrepo
40 from osmclient.sol005 import subscription
41 from osmclient.common import package_tool
42 from osmclient.common.exceptions import ClientException
43 import json
44 import logging
45
46
47 class Client(object):
48 def __init__(
49 self,
50 host=None,
51 so_port=443,
52 user="admin",
53 password="admin",
54 project="admin",
55 **kwargs
56 ):
57 self._user = user
58 self._password = password
59 self._project = project
60 self._project_domain_name = kwargs.get("project_domain_name")
61 self._user_domain_name = kwargs.get("user_domain_name")
62 self._logger = logging.getLogger("osmclient")
63 self._auth_endpoint = "/admin/v1/tokens"
64 self._headers = {}
65 self._token = None
66 self._url = None
67 if host.startswith("http://") or host.startswith("https://"):
68 self._url = self._host
69 else:
70 if len(host.split(":")) > 1:
71 # backwards compatible, port provided as part of host
72 self._host = host.split(":")[0]
73 self._so_port = host.split(":")[1]
74 else:
75 self._host = host
76 self._so_port = so_port
77 self._url = "https://{}:{}/osm".format(self._host, self._so_port)
78 self._http_client = http.Http(self._url, **kwargs)
79 self._headers["Accept"] = "application/json"
80 self._headers["Content-Type"] = "application/yaml"
81 self._http_client.set_http_header(self._headers)
82
83 self.vnfd = vnfd.Vnfd(self._http_client, client=self)
84 self.nsd = nsd.Nsd(self._http_client, client=self)
85 self.nst = nst.Nst(self._http_client, client=self)
86 self.package = package.Package(self._http_client, client=self)
87 self.ns = ns.Ns(self._http_client, client=self)
88 self.nsi = nsi.Nsi(self._http_client, client=self)
89 self.vim = vim.Vim(self._http_client, client=self)
90 self.wim = wim.Wim(self._http_client, client=self)
91 self.sdnc = sdncontroller.SdnController(self._http_client, client=self)
92 self.vnf = vnf.Vnf(self._http_client, client=self)
93 self.project = projectmodule.Project(self._http_client, client=self)
94 self.user = usermodule.User(self._http_client, client=self)
95 self.role = role.Role(self._http_client, client=self)
96 self.pdu = pdud.Pdu(self._http_client, client=self)
97 self.k8scluster = k8scluster.K8scluster(self._http_client, client=self)
98 self.vca = vca.VCA(self._http_client, client=self)
99 self.repo = repo.Repo(self._http_client, client=self)
100 self.osmrepo = osmrepo.OSMRepo(self._http_client, client=self)
101 self.package_tool = package_tool.PackageTool(client=self)
102 self.subscription = subscription.Subscription(self._http_client, client=self)
103 """
104 self.vca = vca.Vca(http_client, client=self, **kwargs)
105 self.utils = utils.Utils(http_client, **kwargs)
106 """
107
108 def get_token(self, pwd_change=False):
109 self._logger.debug("")
110 if self._token is None:
111 postfields_dict = {
112 "username": self._user,
113 "password": self._password,
114 "project_id": self._project,
115 }
116 if self._project_domain_name:
117 postfields_dict["project_domain_name"] = self._project_domain_name
118 if self._user_domain_name:
119 postfields_dict["user_domain_name"] = self._user_domain_name
120 http_code, resp = self._http_client.post_cmd(
121 endpoint=self._auth_endpoint,
122 postfields_dict=postfields_dict,
123 skip_query_admin=True,
124 )
125 # if http_code not in (200, 201, 202, 204):
126 # message ='Authentication error: not possible to get auth token\nresp:\n{}'.format(resp)
127 # raise ClientException(message)
128
129 token = json.loads(resp) if resp else None
130 if token.get("message") == "change_password" and not pwd_change:
131 raise ClientException(
132 "Password Expired. Please update the password using change_password option"
133 )
134 self._token = token["id"]
135
136 if self._token is not None:
137 self._headers["Authorization"] = "Bearer {}".format(self._token)
138 self._http_client.set_http_header(self._headers)
139 return token
140
141 def get_version(self):
142 _, resp = self._http_client.get2_cmd(endpoint="/version", skip_query_admin=True)
143 # print(http_code, resp)
144 try:
145 resp = json.loads(resp)
146 version = resp.get("version")
147 date = resp.get("date")
148 except ValueError:
149 version = resp.split()[2]
150 date = resp.split()[4]
151 return "{} {}".format(version, date)
152
153 def set_default_params(self, **kwargs):
154 host = kwargs.pop("host", None)
155 if host is not None:
156 self._host = host
157 port = kwargs.pop("port", None)
158 if port is not None:
159 self._so_port = port
160 self._http_client.set_query_admin(**kwargs)