1 |
|
# Copyright ETSI Contributors and Others. |
2 |
|
# All Rights Reserved. |
3 |
|
# |
4 |
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
5 |
|
# not use this file except in compliance with the License. You may obtain |
6 |
|
# a copy of the License at |
7 |
|
# |
8 |
|
# http://www.apache.org/licenses/LICENSE-2.0 |
9 |
|
# |
10 |
|
# Unless required by applicable law or agreed to in writing, software |
11 |
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
12 |
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
13 |
|
# License for the specific language governing permissions and limitations |
14 |
|
# under the License. |
15 |
|
|
16 |
1 |
import textwrap |
17 |
1 |
import logging |
18 |
1 |
import yaml |
19 |
1 |
from osmclient.common.exceptions import ClientException |
20 |
|
|
21 |
1 |
logger = logging.getLogger("osmclient") |
22 |
|
|
23 |
|
|
24 |
1 |
def wrap_text(text, width): |
25 |
1 |
wrapper = textwrap.TextWrapper(width=width) |
26 |
1 |
lines = text.splitlines() |
27 |
1 |
return "\n".join(map(wrapper.fill, lines)) |
28 |
|
|
29 |
|
|
30 |
1 |
def trunc_text(text, length): |
31 |
0 |
if len(text) > length: |
32 |
0 |
return text[: (length - 3)] + "..." |
33 |
|
else: |
34 |
0 |
return text |
35 |
|
|
36 |
|
|
37 |
1 |
def check_client_version(obj, what, version="sol005"): |
38 |
|
""" |
39 |
|
Checks the version of the client object and raises error if it not the expected. |
40 |
|
|
41 |
|
:param obj: the client object |
42 |
|
:what: the function or command under evaluation (used when an error is raised) |
43 |
|
:return: - |
44 |
|
:raises ClientError: if the specified version does not match the client version |
45 |
|
""" |
46 |
0 |
logger.debug("") |
47 |
0 |
if version == "sol005": |
48 |
0 |
message = f"The following commands or options are only supported with the version v1 of OSM SOL005': {what}" |
49 |
0 |
elif version == "sol005_v2": |
50 |
0 |
message = f"The following commands or options are only supported with the version v2 of OSM SOL005': {what}" |
51 |
|
else: |
52 |
0 |
message = f"The following commands or options are only supported with the version {version} of OSM SOL005': {what}" |
53 |
0 |
fullclassname = obj.__module__ + "." + obj.__class__.__name__ |
54 |
0 |
if fullclassname != "osmclient.{}.client.Client".format(version): |
55 |
0 |
raise ClientException(message) |
56 |
0 |
return |
57 |
|
|
58 |
|
|
59 |
1 |
def get_project(project_list, item): |
60 |
|
# project_list = ctx.obj.project.list() |
61 |
0 |
item_project_list = item.get("_admin", {}).get("projects_read") |
62 |
0 |
project_id = "None" |
63 |
0 |
project_name = "None" |
64 |
0 |
if item_project_list: |
65 |
0 |
for p1 in item_project_list: |
66 |
0 |
project_id = p1 |
67 |
0 |
for p2 in project_list: |
68 |
0 |
if p2["_id"] == project_id: |
69 |
0 |
project_name = p2["name"] |
70 |
0 |
return project_id, project_name |
71 |
0 |
return project_id, project_name |
72 |
|
|
73 |
|
|
74 |
1 |
def get_vim_name(vim_list, vim_id): |
75 |
0 |
vim_name = "-" |
76 |
0 |
for v in vim_list: |
77 |
0 |
if v["uuid"] == vim_id: |
78 |
0 |
vim_name = v["name"] |
79 |
0 |
break |
80 |
0 |
return vim_name |
81 |
|
|
82 |
|
|
83 |
1 |
def create_config(config_file, json_string): |
84 |
|
""" |
85 |
|
Combines a YAML or JSON file with a JSON string into a Python3 structure |
86 |
|
It loads the YAML or JSON file 'cfile' into a first dictionary. |
87 |
|
It loads the JSON string into a second dictionary. |
88 |
|
Then it updates the first dictionary with the info in the second dictionary. |
89 |
|
If the field is present in both cfile and cdict, the field in cdict prevails. |
90 |
|
If both cfile and cdict are None, it returns an empty dict (i.e. {}) |
91 |
|
""" |
92 |
0 |
config = {} |
93 |
0 |
if config_file: |
94 |
0 |
with open(config_file, "r") as cf: |
95 |
0 |
config = yaml.safe_load(cf.read()) |
96 |
0 |
if json_string: |
97 |
0 |
cdict = yaml.safe_load(json_string) |
98 |
0 |
for k, v in cdict.items(): |
99 |
0 |
config[k] = v |
100 |
0 |
return config |