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