Code Coverage

Cobertura Coverage Report > osmclient.cli_commands >

nspkg.py

Trend

File Coverage summary

NameClassesLinesConditionals
nspkg.py
100%
1/1
53%
73/137
100%
0/0

Coverage Breakdown by Class

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