Fix bug 2240 to get packages from id and _id
[osm/osmclient.git] / osmclient / cli_commands / nfpkg.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 import click
17 from osmclient.common.exceptions import ClientException
18 from osmclient.cli_commands import utils
19 from prettytable import PrettyTable
20 import yaml
21 import json
22 from datetime import datetime
23 import logging
24
25 logger = logging.getLogger("osmclient")
26
27
28 def vnfd_list(ctx, nf_type, filter, long):
29 logger.debug("")
30 if nf_type:
31 utils.check_client_version(ctx.obj, "--nf_type")
32 elif filter:
33 utils.check_client_version(ctx.obj, "--filter")
34 if filter:
35 filter = "&".join(filter)
36 if nf_type:
37 if nf_type == "vnf":
38 nf_filter = "_admin.type=vnfd"
39 elif nf_type == "pnf":
40 nf_filter = "_admin.type=pnfd"
41 elif nf_type == "hnf":
42 nf_filter = "_admin.type=hnfd"
43 else:
44 raise ClientException(
45 'wrong value for "--nf_type" option, allowed values: vnf, pnf, hnf'
46 )
47 if filter:
48 filter = "{}&{}".format(nf_filter, filter)
49 else:
50 filter = nf_filter
51 if filter:
52 resp = ctx.obj.vnfd.list(filter)
53 else:
54 resp = ctx.obj.vnfd.list()
55 # print(yaml.safe_dump(resp))
56 if long:
57 table = PrettyTable(
58 [
59 "nfpkg name",
60 "id",
61 "desc type",
62 "vendor",
63 "version",
64 "onboarding state",
65 "operational state",
66 "usage state",
67 "date",
68 "last update",
69 ]
70 )
71 else:
72 table = PrettyTable(["nfpkg name", "id", "desc type"])
73 for vnfd in resp:
74 name = vnfd.get("id", vnfd.get("product-name", "-"))
75 descriptor_type = "sol006" if "product-name" in vnfd else "rel8"
76 if long:
77 onb_state = vnfd["_admin"].get("onboardingState", "-")
78 op_state = vnfd["_admin"].get("operationalState", "-")
79 vendor = vnfd.get("provider", vnfd.get("vendor"))
80 version = vnfd.get("version")
81 usage_state = vnfd["_admin"].get("usageState", "-")
82 date = datetime.fromtimestamp(vnfd["_admin"]["created"]).strftime(
83 "%Y-%m-%dT%H:%M:%S"
84 )
85 last_update = datetime.fromtimestamp(vnfd["_admin"]["modified"]).strftime(
86 "%Y-%m-%dT%H:%M:%S"
87 )
88 table.add_row(
89 [
90 name,
91 vnfd["_id"],
92 descriptor_type,
93 vendor,
94 version,
95 onb_state,
96 op_state,
97 usage_state,
98 date,
99 last_update,
100 ]
101 )
102 else:
103 table.add_row([name, vnfd["_id"], descriptor_type])
104 table.align = "l"
105 print(table)
106
107
108 @click.command(name="vnfd-list", short_help="list all xNF packages (VNF, HNF, PNF)")
109 @click.option("--nf_type", help="type of NF (vnf, pnf, hnf)")
110 @click.option(
111 "--filter",
112 default=None,
113 multiple=True,
114 help="restricts the list to the NF pkg matching the filter",
115 )
116 @click.option("--long", is_flag=True, help="get more details")
117 @click.pass_context
118 def vnfd_list1(ctx, nf_type, filter, long):
119 """list all xNF packages (VNF, HNF, PNF)"""
120 logger.debug("")
121 vnfd_list(ctx, nf_type, filter, long)
122
123
124 @click.command(name="vnfpkg-list", short_help="list all xNF packages (VNF, HNF, PNF)")
125 @click.option("--nf_type", help="type of NF (vnf, pnf, hnf)")
126 @click.option(
127 "--filter",
128 default=None,
129 multiple=True,
130 help="restricts the list to the NFpkg matching the filter",
131 )
132 @click.option("--long", is_flag=True, help="get more details")
133 @click.pass_context
134 def vnfd_list2(ctx, nf_type, filter, long):
135 """list all xNF packages (VNF, HNF, PNF)"""
136 logger.debug("")
137 vnfd_list(ctx, nf_type, filter, long)
138
139
140 @click.command(name="nfpkg-list", short_help="list all xNF packages (VNF, HNF, PNF)")
141 @click.option("--nf_type", help="type of NF (vnf, pnf, hnf)")
142 @click.option(
143 "--filter",
144 default=None,
145 multiple=True,
146 help="restricts the list to the NFpkg matching the filter",
147 )
148 @click.option("--long", is_flag=True, help="get more details")
149 @click.pass_context
150 def nfpkg_list(ctx, nf_type, filter, long):
151 """list all xNF packages (VNF, HNF, PNF)"""
152 logger.debug("")
153 utils.check_client_version(ctx.obj, ctx.command.name)
154 vnfd_list(ctx, nf_type, filter, long)
155
156
157 def vnfd_show(ctx, name, literal):
158 logger.debug("")
159 resp = ctx.obj.vnfd.get(name)
160 # resp = ctx.obj.vnfd.get_individual(name)
161
162 if literal:
163 print(yaml.safe_dump(resp, indent=4, default_flow_style=False))
164 return
165
166 table = PrettyTable(["field", "value"])
167 for k, v in list(resp.items()):
168 table.add_row([k, utils.wrap_text(text=json.dumps(v, indent=2), width=100)])
169 table.align = "l"
170 print(table)
171
172
173 @click.command(name="vnfd-show", short_help="shows the details of a NF package")
174 @click.option("--literal", is_flag=True, help="print literally, no pretty table")
175 @click.argument("name")
176 @click.pass_context
177 def vnfd_show1(ctx, name, literal):
178 """shows the content of a VNFD
179
180 NAME: name or ID of the VNFD/VNFpkg
181 """
182 logger.debug("")
183 vnfd_show(ctx, name, literal)
184
185
186 @click.command(name="vnfpkg-show", short_help="shows the details of a NF package")
187 @click.option("--literal", is_flag=True, help="print literally, no pretty table")
188 @click.argument("name")
189 @click.pass_context
190 def vnfd_show2(ctx, name, literal):
191 """shows the content of a VNFD
192
193 NAME: name or ID of the VNFD/VNFpkg
194 """
195 logger.debug("")
196 vnfd_show(ctx, name, literal)
197
198
199 @click.command(name="nfpkg-show", short_help="shows the details of a NF package")
200 @click.option("--literal", is_flag=True, help="print literally, no pretty table")
201 @click.argument("name")
202 @click.pass_context
203 def nfpkg_show(ctx, name, literal):
204 """shows the content of a NF Descriptor
205
206 NAME: name or ID of the NFpkg
207 """
208 logger.debug("")
209 vnfd_show(ctx, name, literal)
210
211
212 def vnfd_create(
213 ctx,
214 filename,
215 overwrite,
216 skip_charm_build,
217 override_epa,
218 override_nonepa,
219 override_paravirt,
220 repo,
221 vendor,
222 version,
223 ):
224 logger.debug("")
225 utils.check_client_version(ctx.obj, ctx.command.name)
226 if repo:
227 filename = ctx.obj.osmrepo.get_pkg("vnf", filename, repo, vendor, version)
228 ctx.obj.vnfd.create(
229 filename,
230 overwrite=overwrite,
231 skip_charm_build=skip_charm_build,
232 override_epa=override_epa,
233 override_nonepa=override_nonepa,
234 override_paravirt=override_paravirt,
235 )
236
237
238 @click.command(name="vnfd-create", short_help="creates a new VNFD/VNFpkg")
239 @click.argument("filename")
240 @click.option(
241 "--overwrite", "overwrite", default=None, help="overwrite deprecated, use override"
242 )
243 @click.option(
244 "--override",
245 "overwrite",
246 default=None,
247 help="overrides fields in descriptor, format: "
248 '"key1.key2...=value[;key3...=value;...]"',
249 )
250 @click.option(
251 "--skip-charm-build",
252 default=False,
253 is_flag=True,
254 help="The charm will not be compiled, it is assumed to already exist",
255 )
256 @click.option(
257 "--override-epa",
258 required=False,
259 default=False,
260 is_flag=True,
261 help="adds guest-epa parameters to all VDU",
262 )
263 @click.option(
264 "--override-nonepa",
265 required=False,
266 default=False,
267 is_flag=True,
268 help="removes all guest-epa parameters from all VDU",
269 )
270 @click.option(
271 "--override-paravirt",
272 required=False,
273 default=False,
274 is_flag=True,
275 help="overrides all VDU interfaces to PARAVIRT",
276 )
277 @click.option("--repo", default=None, help="[repository]: Repository name")
278 @click.option("--vendor", default=None, help="[repository]: filter by vendor]")
279 @click.option(
280 "--version",
281 default="latest",
282 help="[repository]: filter by version. Default: latest",
283 )
284 @click.pass_context
285 def vnfd_create1(
286 ctx,
287 filename,
288 overwrite,
289 skip_charm_build,
290 override_epa,
291 override_nonepa,
292 override_paravirt,
293 repo,
294 vendor,
295 version,
296 ):
297 """creates a new VNFD/VNFpkg
298 \b
299 FILENAME: NF Package tar.gz file, NF Descriptor YAML file or NF Package folder
300 If FILENAME is a file (NF Package tar.gz or NF Descriptor YAML), it is onboarded.
301 If FILENAME is an NF Package folder, it is built and then onboarded.
302 """
303 logger.debug("")
304 vnfd_create(
305 ctx,
306 filename,
307 overwrite=overwrite,
308 skip_charm_build=skip_charm_build,
309 override_epa=override_epa,
310 override_nonepa=override_nonepa,
311 override_paravirt=override_paravirt,
312 repo=repo,
313 vendor=vendor,
314 version=version,
315 )
316
317
318 @click.command(name="vnfpkg-create", short_help="creates a new VNFD/VNFpkg")
319 @click.argument("filename")
320 @click.option(
321 "--overwrite",
322 "overwrite",
323 default=None, # hidden=True,
324 help="Deprecated. Use override",
325 )
326 @click.option(
327 "--override",
328 "overwrite",
329 default=None,
330 help="overrides fields in descriptor, format: "
331 '"key1.key2...=value[;key3...=value;...]"',
332 )
333 @click.option(
334 "--skip-charm-build",
335 default=False,
336 is_flag=True,
337 help="The charm will not be compiled, it is assumed to already exist",
338 )
339 @click.option(
340 "--override-epa",
341 required=False,
342 default=False,
343 is_flag=True,
344 help="adds guest-epa parameters to all VDU",
345 )
346 @click.option(
347 "--override-nonepa",
348 required=False,
349 default=False,
350 is_flag=True,
351 help="removes all guest-epa parameters from all VDU",
352 )
353 @click.option(
354 "--override-paravirt",
355 required=False,
356 default=False,
357 is_flag=True,
358 help="overrides all VDU interfaces to PARAVIRT",
359 )
360 @click.option("--repo", default=None, help="[repository]: Repository name")
361 @click.option("--vendor", default=None, help="[repository]: filter by vendor]")
362 @click.option(
363 "--version",
364 default="latest",
365 help="[repository]: filter by version. Default: latest",
366 )
367 @click.pass_context
368 def vnfd_create2(
369 ctx,
370 filename,
371 overwrite,
372 skip_charm_build,
373 override_epa,
374 override_nonepa,
375 override_paravirt,
376 repo,
377 vendor,
378 version,
379 ):
380 """creates a new VNFD/VNFpkg
381 \b
382 FILENAME: NF Package tar.gz file, NF Descriptor YAML file or NF Package folder
383 If FILENAME is a file (NF Package tar.gz or NF Descriptor YAML), it is onboarded.
384 If FILENAME is an NF Package folder, it is built and then onboarded.
385 """
386 logger.debug("")
387 vnfd_create(
388 ctx,
389 filename,
390 overwrite=overwrite,
391 skip_charm_build=skip_charm_build,
392 override_epa=override_epa,
393 override_nonepa=override_nonepa,
394 override_paravirt=override_paravirt,
395 repo=repo,
396 vendor=vendor,
397 version=version,
398 )
399
400
401 @click.command(name="nfpkg-create", short_help="creates a new NFpkg")
402 @click.argument("filename")
403 @click.option(
404 "--overwrite",
405 "overwrite",
406 default=None, # hidden=True,
407 help="Deprecated. Use override",
408 )
409 @click.option(
410 "--override",
411 "overwrite",
412 default=None,
413 help="overrides fields in descriptor, format: "
414 '"key1.key2...=value[;key3...=value;...]"',
415 )
416 @click.option(
417 "--skip-charm-build",
418 default=False,
419 is_flag=True,
420 help="The charm will not be compiled, it is assumed to already exist",
421 )
422 @click.option(
423 "--override-epa",
424 required=False,
425 default=False,
426 is_flag=True,
427 help="adds guest-epa parameters to all VDU",
428 )
429 @click.option(
430 "--override-nonepa",
431 required=False,
432 default=False,
433 is_flag=True,
434 help="removes all guest-epa parameters from all VDU",
435 )
436 @click.option(
437 "--override-paravirt",
438 required=False,
439 default=False,
440 is_flag=True,
441 help="overrides all VDU interfaces to PARAVIRT",
442 )
443 @click.option("--repo", default=None, help="[repository]: Repository name")
444 @click.option("--vendor", default=None, help="[repository]: filter by vendor]")
445 @click.option(
446 "--version",
447 default="latest",
448 help="[repository]: filter by version. Default: latest",
449 )
450 @click.pass_context
451 def nfpkg_create(
452 ctx,
453 filename,
454 overwrite,
455 skip_charm_build,
456 override_epa,
457 override_nonepa,
458 override_paravirt,
459 repo,
460 vendor,
461 version,
462 ):
463 """creates a new NFpkg
464
465 \b
466 FILENAME: NF Package tar.gz file, NF Descriptor YAML file or NF Package folder
467 If FILENAME is a file (NF Package tar.gz or NF Descriptor YAML), it is onboarded.
468 If FILENAME is an NF Package folder, it is built and then onboarded.
469 """
470 logger.debug("")
471 vnfd_create(
472 ctx,
473 filename,
474 overwrite=overwrite,
475 skip_charm_build=skip_charm_build,
476 override_epa=override_epa,
477 override_nonepa=override_nonepa,
478 override_paravirt=override_paravirt,
479 repo=repo,
480 vendor=vendor,
481 version=version,
482 )
483
484
485 def vnfd_update(ctx, name, content):
486 logger.debug("")
487 utils.check_client_version(ctx.obj, ctx.command.name)
488 ctx.obj.vnfd.update(name, content)
489
490
491 @click.command(name="vnfd-update", short_help="updates a new VNFD/VNFpkg")
492 @click.argument("name")
493 @click.option(
494 "--content",
495 default=None,
496 help="filename with the VNFD/VNFpkg replacing the current one",
497 )
498 @click.pass_context
499 def vnfd_update1(ctx, name, content):
500 """updates a VNFD/VNFpkg
501
502 NAME: name or ID of the VNFD/VNFpkg
503 """
504 logger.debug("")
505 vnfd_update(ctx, name, content)
506
507
508 @click.command(name="vnfpkg-update", short_help="updates a VNFD/VNFpkg")
509 @click.argument("name")
510 @click.option(
511 "--content",
512 default=None,
513 help="filename with the VNFD/VNFpkg replacing the current one",
514 )
515 @click.pass_context
516 def vnfd_update2(ctx, name, content):
517 """updates a VNFD/VNFpkg
518
519 NAME: VNFD yaml file or VNFpkg tar.gz file
520 """
521 logger.debug("")
522 vnfd_update(ctx, name, content)
523
524
525 @click.command(name="nfpkg-update", short_help="updates a NFpkg")
526 @click.argument("name")
527 @click.option(
528 "--content", default=None, help="filename with the NFpkg replacing the current one"
529 )
530 @click.pass_context
531 def nfpkg_update(ctx, name, content):
532 """updates a NFpkg
533
534 NAME: NF Descriptor yaml file or NFpkg tar.gz file
535 """
536 logger.debug("")
537 vnfd_update(ctx, name, content)
538
539
540 def vnfd_delete(ctx, name, force):
541 logger.debug("")
542 if not force:
543 ctx.obj.vnfd.delete(name)
544 else:
545 utils.check_client_version(ctx.obj, "--force")
546 ctx.obj.vnfd.delete(name, force)
547
548
549 @click.command(name="vnfd-delete", short_help="deletes a VNFD/VNFpkg")
550 @click.argument("name")
551 @click.option(
552 "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
553 )
554 @click.pass_context
555 def vnfd_delete1(ctx, name, force):
556 """deletes a VNFD/VNFpkg
557
558 NAME: name or ID of the VNFD/VNFpkg to be deleted
559 """
560 logger.debug("")
561 vnfd_delete(ctx, name, force)
562
563
564 @click.command(name="vnfpkg-delete", short_help="deletes a VNFD/VNFpkg")
565 @click.argument("name")
566 @click.option(
567 "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
568 )
569 @click.pass_context
570 def vnfd_delete2(ctx, name, force):
571 """deletes a VNFD/VNFpkg
572
573 NAME: name or ID of the VNFD/VNFpkg to be deleted
574 """
575 logger.debug("")
576 vnfd_delete(ctx, name, force)
577
578
579 @click.command(name="nfpkg-delete", short_help="deletes a NFpkg")
580 @click.argument("name")
581 @click.option(
582 "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
583 )
584 @click.pass_context
585 def nfpkg_delete(ctx, name, force):
586 """deletes a NFpkg
587
588 NAME: name or ID of the NFpkg to be deleted
589 """
590 logger.debug("")
591 vnfd_delete(ctx, name, force)