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