Code Coverage

Cobertura Coverage Report > osmclient.cli_commands >

nspkg.py

Trend

Classes100%
 
Lines51%
   
Conditionals100%
 

File Coverage summary

NameClassesLinesConditionals
nspkg.py
100%
1/1
51%
73/142
100%
0/0

Coverage Breakdown by Class

NameLinesConditionals
nspkg.py
51%
73/142
N/A

Source

osmclient/cli_commands/nspkg.py
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 click
17 1 from osmclient.cli_commands import utils
18 1 from prettytable import PrettyTable
19 1 import yaml
20 1 import json
21 1 from datetime import datetime
22 1 import logging
23
24 1 logger = logging.getLogger("osmclient")
25
26
27 1 def nsd_list(ctx, filter, long):
28 0     logger.debug("")
29 0     if filter:
30 0         utils.check_client_version(ctx.obj, "--filter")
31 0         filter = "&".join(filter)
32 0         resp = ctx.obj.nsd.list(filter)
33     else:
34 0         resp = ctx.obj.nsd.list()
35     # print(yaml.safe_dump(resp))
36 0     fullclassname = ctx.obj.__module__ + "." + ctx.obj.__class__.__name__
37 0     if fullclassname == "osmclient.sol005.client.Client":
38 0         if long:
39 0             table = PrettyTable(
40                 [
41                     "nsd name",
42                     "id",
43                     "onboarding state",
44                     "operational state",
45                     "usage state",
46                     "date",
47                     "last update",
48                 ]
49             )
50         else:
51 0             table = PrettyTable(["nsd name", "id"])
52 0         for nsd in resp:
53 0             name = nsd.get("id", "-")
54 0             if long:
55 0                 onb_state = nsd["_admin"].get("onboardingState", "-")
56 0                 op_state = nsd["_admin"].get("operationalState", "-")
57 0                 usage_state = nsd["_admin"].get("usageState", "-")
58 0                 date = datetime.fromtimestamp(nsd["_admin"]["created"]).strftime(
59                     "%Y-%m-%dT%H:%M:%S"
60                 )
61 0                 last_update = datetime.fromtimestamp(
62                     nsd["_admin"]["modified"]
63                 ).strftime("%Y-%m-%dT%H:%M:%S")
64 0                 table.add_row(
65                     [
66                         name,
67                         nsd["_id"],
68                         onb_state,
69                         op_state,
70                         usage_state,
71                         date,
72                         last_update,
73                     ]
74                 )
75             else:
76 0                 table.add_row([name, nsd["_id"]])
77     else:
78 0         table = PrettyTable(["nsd name", "id"])
79 0         for nsd in resp:
80 0             table.add_row([nsd["name"], nsd["id"]])
81 0     table.align = "l"
82 0     print(table)
83
84
85 1 @click.command(name="nsd-list", short_help="list all NS packages")
86 1 @click.option(
87     "--filter",
88     default=None,
89     multiple=True,
90     help="restricts the list to the NSD/NSpkg matching the filter",
91 )
92 1 @click.option("--long", is_flag=True, help="get more details")
93 1 @click.pass_context
94 1 def nsd_list1(ctx, filter, long):
95     """list all NSD/NS pkg in the system"""
96 0     logger.debug("")
97 0     nsd_list(ctx, filter, long)
98
99
100 1 @click.command(name="nspkg-list", short_help="list all NS packages")
101 1 @click.option(
102     "--filter",
103     default=None,
104     multiple=True,
105     help="restricts the list to the NSD/NSpkg matching the filter",
106 )
107 1 @click.option("--long", is_flag=True, help="get more details")
108 1 @click.pass_context
109 1 def nsd_list2(ctx, filter, long):
110     """list all NS packages"""
111 0     logger.debug("")
112 0     nsd_list(ctx, filter, long)
113
114
115 1 def nsd_show(ctx, name, literal):
116 0     logger.debug("")
117 0     resp = ctx.obj.nsd.get(name)
118     # resp = ctx.obj.nsd.get_individual(name)
119
120 0     if literal:
121 0         print(yaml.safe_dump(resp, indent=4, default_flow_style=False))
122 0         return
123
124 0     table = PrettyTable(["field", "value"])
125 0     for k, v in list(resp.items()):
126 0         table.add_row([k, utils.wrap_text(text=json.dumps(v, indent=2), width=100)])
127 0     table.align = "l"
128 0     print(table)
129
130
131 1 @click.command(name="nsd-show", short_help="shows the details of a NS package")
132 1 @click.option("--literal", is_flag=True, help="print literally, no pretty table")
133 1 @click.argument("name")
134 1 @click.pass_context
135 1 def nsd_show1(ctx, name, literal):
136     """shows the content of a NSD
137
138     NAME: name or ID of the NSD/NSpkg
139     """
140 0     logger.debug("")
141 0     nsd_show(ctx, name, literal)
142
143
144 1 @click.command(name="nspkg-show", short_help="shows the details of a NS package")
145 1 @click.option("--literal", is_flag=True, help="print literally, no pretty table")
146 1 @click.argument("name")
147 1 @click.pass_context
148 1 def nsd_show2(ctx, name, literal):
149     """shows the content of a NSD
150
151     NAME: name or ID of the NSD/NSpkg
152     """
153 0     logger.debug("")
154 0     nsd_show(ctx, name, literal)
155
156
157 1 def nsd_create(ctx, filename, overwrite, skip_charm_build, repo, vendor, version):
158 0     logger.debug("")
159 0     utils.check_client_version(ctx.obj, ctx.command.name)
160 0     if repo:
161 0         filename = ctx.obj.osmrepo.get_pkg("ns", filename, repo, vendor, version)
162 0     ctx.obj.nsd.create(filename, overwrite=overwrite, skip_charm_build=skip_charm_build)
163
164
165 1 @click.command(name="nsd-create", short_help="creates a new NSD/NSpkg")
166 1 @click.argument("filename")
167 1 @click.option(
168     "--overwrite",
169     "overwrite",
170     default=None,  # hidden=True,
171     help="Deprecated. Use override",
172 )
173 1 @click.option(
174     "--override",
175     "overwrite",
176     default=None,
177     help="overrides fields in descriptor, format: "
178     '"key1.key2...=value[;key3...=value;...]"',
179 )
180 1 @click.option(
181     "--skip-charm-build",
182     default=False,
183     is_flag=True,
184     help="The charm will not be compiled, it is assumed to already exist",
185 )
186 1 @click.option("--repo", default=None, help="[repository]: Repository name")
187 1 @click.option("--vendor", default=None, help="[repository]: filter by vendor]")
188 1 @click.option(
189     "--version",
190     default="latest",
191     help="[repository]: filter by version. Default: latest",
192 )
193 1 @click.pass_context
194 1 def nsd_create1(ctx, filename, overwrite, skip_charm_build, repo, vendor, version):
195     """onboards a new NSpkg (alias of nspkg-create) (TO BE DEPRECATED)
196
197     \b
198     FILENAME: NF Package tar.gz file, NF Descriptor YAML file or NF Package folder
199               If FILENAME is a file (NF Package tar.gz or NF Descriptor YAML), it is onboarded.
200               If FILENAME is an NF Package folder, it is built and then onboarded.
201     """
202 0     logger.debug("")
203 0     nsd_create(
204         ctx,
205         filename,
206         overwrite=overwrite,
207         skip_charm_build=skip_charm_build,
208         repo=repo,
209         vendor=vendor,
210         version=version,
211     )
212
213
214 1 @click.command(name="nspkg-create", short_help="creates a new NSD/NSpkg")
215 1 @click.argument("filename")
216 1 @click.option(
217     "--overwrite",
218     "overwrite",
219     default=None,  # hidden=True,
220     help="Deprecated. Use override",
221 )
222 1 @click.option(
223     "--override",
224     "overwrite",
225     default=None,
226     help="overrides fields in descriptor, format: "
227     '"key1.key2...=value[;key3...=value;...]"',
228 )
229 1 @click.option(
230     "--skip-charm-build",
231     default=False,
232     is_flag=True,
233     help="The charm will not be compiled, it is assumed to already exist",
234 )
235 1 @click.option("--repo", default=None, help="[repository]: Repository name")
236 1 @click.option("--vendor", default=None, help="[repository]: filter by vendor]")
237 1 @click.option(
238     "--version",
239     default="latest",
240     help="[repository]: filter by version. Default: latest",
241 )
242 1 @click.pass_context
243 1 def nsd_create2(ctx, filename, overwrite, skip_charm_build, repo, vendor, version):
244     """onboards a new NSpkg
245     \b
246     FILENAME: NF Package tar.gz file, NF Descriptor YAML file or NF Package folder
247               If FILENAME is a file (NF Package tar.gz or NF Descriptor YAML), it is onboarded.
248               If FILENAME is an NF Package folder, it is built and then onboarded.
249     """
250 0     logger.debug("")
251 0     nsd_create(
252         ctx,
253         filename,
254         overwrite=overwrite,
255         skip_charm_build=skip_charm_build,
256         repo=repo,
257         vendor=vendor,
258         version=version,
259     )
260
261
262 1 def nsd_update(ctx, name, content):
263 0     logger.debug("")
264 0     utils.check_client_version(ctx.obj, ctx.command.name)
265 0     ctx.obj.nsd.update(name, content)
266
267
268 1 @click.command(name="nsd-update", short_help="updates a NSD/NSpkg")
269 1 @click.argument("name")
270 1 @click.option(
271     "--content",
272     default=None,
273     help="filename with the NSD/NSpkg replacing the current one",
274 )
275 1 @click.pass_context
276 1 def nsd_update1(ctx, name, content):
277     """updates a NSD/NSpkg
278
279     NAME: name or ID of the NSD/NSpkg
280     """
281 0     logger.debug("")
282 0     nsd_update(ctx, name, content)
283
284
285 1 @click.command(name="nspkg-update", short_help="updates a NSD/NSpkg")
286 1 @click.argument("name")
287 1 @click.option(
288     "--content",
289     default=None,
290     help="filename with the NSD/NSpkg replacing the current one",
291 )
292 1 @click.pass_context
293 1 def nsd_update2(ctx, name, content):
294     """updates a NSD/NSpkg
295
296     NAME: name or ID of the NSD/NSpkg
297     """
298 0     logger.debug("")
299 0     nsd_update(ctx, name, content)
300
301
302 1 def nsd_delete(ctx, name, force):
303 0     logger.debug("")
304 0     if not force:
305 0         ctx.obj.nsd.delete(name)
306     else:
307 0         utils.check_client_version(ctx.obj, "--force")
308 0         ctx.obj.nsd.delete(name, force)
309
310
311 1 @click.command(name="nsd-delete", short_help="deletes a NSD/NSpkg")
312 1 @click.argument("name")
313 1 @click.option(
314     "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
315 )
316 1 @click.pass_context
317 1 def nsd_delete1(ctx, name, force):
318     """deletes a NSD/NSpkg
319
320     NAME: name or ID of the NSD/NSpkg to be deleted
321     """
322 0     logger.debug("")
323 0     nsd_delete(ctx, name, force)
324
325
326 1 @click.command(name="nspkg-delete", short_help="deletes a NSD/NSpkg")
327 1 @click.argument("name")
328 1 @click.option(
329     "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
330 )
331 1 @click.pass_context
332 1 def nsd_delete2(ctx, name, force):
333     """deletes a NSD/NSpkg
334
335     NAME: name or ID of the NSD/NSpkg to be deleted
336     """
337 0     logger.debug("")
338 0     nsd_delete(ctx, name, force)