b2ecd9a75872d58c5561e2a9b08f42aa6ee09230
1 # Copyright ETSI Contributors and Others.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
17 from osmclient
.common
.exceptions
import NotFound
, ClientException
18 from osmclient
.cli_commands
import utils
19 from prettytable
import PrettyTable
24 logger
= logging
.getLogger("osmclient")
27 @click.command(name
="repo-add", short_help
="adds a repo to OSM")
28 @click.argument("name")
29 @click.argument("uri")
32 type=click
.Choice(["osm", "helm-chart", "juju-bundle"]),
34 help="type of repo (osm for OSM repositories, helm-chart for Helm Charts, juju-bundle for Juju Bundles)",
36 @click.option("--description", default
=None, help="human readable description")
38 "--user", default
=None, help="OSM repository: The username of the OSM repository"
43 help="OSM repository: The password of the OSM repository",
48 help="enable OCI (only for helm-chart repos, default: false, automatically set to true for oci:// URI)",
53 help="cert to use when adding a repository (only for helm)",
56 def repo_add(ctx
, **kwargs
):
59 NAME: name of the repo
62 kwargs
= {k
: v
for k
, v
in kwargs
.items() if v
is not None}
64 repo
["url"] = repo
.pop("uri")
65 if repo
["url"].startswith("oci://"):
67 if "ca_file" in kwargs
:
69 with
open(kwargs
["ca_file"], "r") as ca_cert
:
70 repo
["cacert"] = ca_cert
.read()
72 except FileNotFoundError
:
73 raise ClientException("CA file not found !")
75 raise ClientException("Empty CA file !")
76 except PermissionError
:
77 raise ClientException("Can not read CA file ! Insufficient permissions")
78 except Exception as e
:
79 raise ClientException(f
"Can not read the cert file ! Error: {e}")
80 if repo
["type"] in ["helm-chart", "juju-bundle"]:
81 ctx
.obj
.repo
.create(repo
["name"], repo
)
83 ctx
.obj
.osmrepo
.create(repo
["name"], repo
)
86 @click.command(name
="repo-update", short_help
="updates a repo in OSM")
87 @click.argument("name")
88 @click.option("--newname", help="New name for the repo")
89 @click.option("--uri", help="URI of the repo")
90 @click.option("--description", help="human readable description")
94 help="enable OCI (only for helm-chart repos, default: false, automatically set to true for oci:// URI)",
97 def repo_update(ctx
, name
, newname
, uri
, description
, oci
):
98 """updates a repo in OSM
100 NAME: name of the repo
102 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
105 repo
["name"] = newname
108 if uri
.startswith("oci://"):
111 repo
["description"] = description
115 ctx
.obj
.repo
.update(name
, repo
)
117 ctx
.obj
.osmrepo
.update(name
, repo
)
121 name
="repo-index", short_help
="Index a repository from a folder with artifacts"
124 "--origin", default
=".", help="origin path where the artifacts are located"
127 "--destination", default
=".", help="destination path where the index is deployed"
130 def repo_index(ctx
, origin
, destination
):
131 """Index a repository
133 NAME: name or ID of the repo to be deleted
135 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
136 ctx
.obj
.osmrepo
.repo_index(origin
, destination
)
139 @click.command(name
="repo-delete", short_help
="deletes a repo")
140 @click.argument("name")
142 "--force", is_flag
=True, help="forces the deletion from the DB (not recommended)"
145 def repo_delete(ctx
, name
, force
):
148 NAME: name or ID of the repo to be deleted
152 ctx
.obj
.repo
.delete(name
, force
=force
)
154 ctx
.obj
.osmrepo
.delete(name
, force
=force
)
157 @click.command(name
="repo-list")
162 help="restricts the list to the repos matching the filter",
164 @click.option("--literal", is_flag
=True, help="print literally, no pretty table")
166 def repo_list(ctx
, filter, literal
):
169 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
171 filter = "&".join(filter)
172 resp
= ctx
.obj
.repo
.list(filter)
173 resp
+= ctx
.obj
.osmrepo
.list(filter)
175 print(yaml
.safe_dump(resp
, indent
=4, default_flow_style
=False))
177 table
= PrettyTable(["Name", "Id", "Type", "URI", "Description"])
179 # cluster['k8s-nets'] = json.dumps(yaml.safe_load(cluster['k8s-nets']))
186 utils
.trunc_text(repo
.get("description") or "", 40),
193 @click.command(name
="repo-show", short_help
="shows the details of a repo")
194 @click.argument("name")
195 @click.option("--literal", is_flag
=True, help="print literally, no pretty table")
197 def repo_show(ctx
, name
, literal
):
198 """shows the details of a repo
200 NAME: name or ID of the repo
203 resp
= ctx
.obj
.repo
.get(name
)
205 resp
= ctx
.obj
.osmrepo
.get(name
)
209 print(yaml
.safe_dump(resp
, indent
=4, default_flow_style
=False))
211 table
= PrettyTable(["key", "attribute"])
213 for k
, v
in list(resp
.items()):
214 table
.add_row([k
, json
.dumps(v
, indent
=2)])
220 ########################
222 ########################
225 def pkg_repo_list(ctx
, pkgtype
, filter, repo
, long):
226 resp
= ctx
.obj
.osmrepo
.pkg_list(pkgtype
, filter, repo
)
229 ["nfpkg name", "vendor", "version", "latest", "description", "repository"]
232 table
= PrettyTable(["nfpkg name", "repository"])
234 name
= vnfd
.get("id", vnfd
.get("name", "-"))
235 repository
= vnfd
.get("repository")
237 vendor
= vnfd
.get("provider", vnfd
.get("vendor"))
238 version
= vnfd
.get("version")
239 description
= vnfd
.get("description")
240 latest
= vnfd
.get("latest")
241 table
.add_row([name
, vendor
, version
, latest
, description
, repository
])
243 table
.add_row([name
, repository
])
248 def pkg_repo_show(ctx
, pkgtype
, name
, repo
, version
, filter, literal
):
251 filter = "&".join(filter)
252 resp
= ctx
.obj
.osmrepo
.pkg_get(pkgtype
, name
, repo
, version
, filter)
255 print(yaml
.safe_dump(resp
, indent
=4, default_flow_style
=False))
258 catalog
= pkgtype
+ "-catalog"
259 full_catalog
= pkgtype
+ ":" + catalog
260 if resp
.get(catalog
):
261 resp
= resp
.pop(catalog
)[pkgtype
][0]
262 elif resp
.get(full_catalog
):
263 resp
= resp
.pop(full_catalog
)[pkgtype
][0]
265 table
= PrettyTable(["field", "value"])
266 for k
, v
in list(resp
.items()):
267 table
.add_row([k
, utils
.wrap_text(text
=json
.dumps(v
, indent
=2), width
=100)])
272 @click.command(name
="vnfpkg-repo-list", short_help
="list all xNF from OSM repositories")
277 help="restricts the list to the NFpkg matching the filter",
280 "--repo", default
=None, help="restricts the list to a particular OSM repository"
282 @click.option("--long", is_flag
=True, help="get more details")
284 def nfpkg_repo_list1(ctx
, filter, repo
, long):
285 """list xNF packages from OSM repositories"""
287 pkg_repo_list(ctx
, pkgtype
, filter, repo
, long)
290 @click.command(name
="nfpkg-repo-list", short_help
="list all xNF from OSM repositories")
295 help="restricts the list to the NFpkg matching the filter",
298 "--repo", default
=None, help="restricts the list to a particular OSM repository"
300 @click.option("--long", is_flag
=True, help="get more details")
302 def nfpkg_repo_list2(ctx
, filter, repo
, long):
303 """list xNF packages from OSM repositories"""
305 pkg_repo_list(ctx
, pkgtype
, filter, repo
, long)
308 @click.command(name
="nsd-repo-list", short_help
="list all NS from OSM repositories")
313 help="restricts the list to the NS matching the filter",
316 "--repo", default
=None, help="restricts the list to a particular OSM repository"
318 @click.option("--long", is_flag
=True, help="get more details")
320 def nspkg_repo_list(ctx
, filter, repo
, long):
321 """list xNF packages from OSM repositories"""
323 pkg_repo_list(ctx
, pkgtype
, filter, repo
, long)
326 @click.command(name
="nspkg-repo-list", short_help
="list all NS from OSM repositories")
331 help="restricts the list to the NS matching the filter",
334 "--repo", default
=None, help="restricts the list to a particular OSM repository"
336 @click.option("--long", is_flag
=True, help="get more details")
338 def nspkg_repo_list2(ctx
, filter, repo
, long):
339 """list xNF packages from OSM repositories"""
341 pkg_repo_list(ctx
, pkgtype
, filter, repo
, long)
345 name
="vnfpkg-repo-show",
346 short_help
="shows the details of a NF package in an OSM repository",
348 @click.option("--literal", is_flag
=True, help="print literally, no pretty table")
349 @click.option("--repo", required
=True, help="Repository name")
350 @click.argument("name")
351 @click.option("--filter", default
=None, multiple
=True, help="filter by fields")
352 @click.option("--version", default
="latest", help="package version")
354 def vnfd_show1(ctx
, name
, repo
, version
, literal
=None, filter=None):
355 """shows the content of a VNFD in a repository
357 NAME: name or ID of the VNFD/VNFpkg
360 pkg_repo_show(ctx
, pkgtype
, name
, repo
, version
, filter, literal
)
364 name
="nsd-repo-show",
365 short_help
="shows the details of a NS package in an OSM repository",
367 @click.option("--literal", is_flag
=True, help="print literally, no pretty table")
368 @click.option("--repo", required
=True, help="Repository name")
369 @click.argument("name")
370 @click.option("--filter", default
=None, multiple
=True, help="filter by fields")
371 @click.option("--version", default
="latest", help="package version")
373 def nsd_repo_show(ctx
, name
, repo
, version
, literal
=None, filter=None):
374 """shows the content of a VNFD in a repository
376 NAME: name or ID of the VNFD/VNFpkg
379 pkg_repo_show(ctx
, pkgtype
, name
, repo
, version
, filter, literal
)
383 name
="nspkg-repo-show",
384 short_help
="shows the details of a NS package in an OSM repository",
386 @click.option("--literal", is_flag
=True, help="print literally, no pretty table")
387 @click.option("--repo", required
=True, help="Repository name")
388 @click.argument("name")
389 @click.option("--filter", default
=None, multiple
=True, help="filter by fields")
390 @click.option("--version", default
="latest", help="package version")
392 def nsd_repo_show2(ctx
, name
, repo
, version
, literal
=None, filter=None):
393 """shows the content of a VNFD in a repository
395 NAME: name or ID of the VNFD/VNFpkg
398 pkg_repo_show(ctx
, pkgtype
, name
, repo
, version
, filter, literal
)
402 name
="nfpkg-repo-show",
403 short_help
="shows the details of a NF package in an OSM repository",
405 @click.option("--literal", is_flag
=True, help="print literally, no pretty table")
406 @click.option("--repo", required
=True, help="Repository name")
407 @click.argument("name")
408 @click.option("--filter", default
=None, multiple
=True, help="filter by fields")
409 @click.option("--version", default
="latest", help="package version")
411 def vnfd_show2(ctx
, name
, repo
, version
, literal
=None, filter=None):
412 """shows the content of a VNFD in a repository
414 NAME: name or ID of the VNFD/VNFpkg
417 pkg_repo_show(ctx
, pkgtype
, name
, repo
, version
, filter, literal
)