Feature 10962 Refactoring of osmclient commands
[osm/osmclient.git] / 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 import click
17 from osmclient.cli_commands import utils
18 from prettytable import PrettyTable
19 import yaml
20 import json
21 from datetime import datetime
22 import logging
23
24 logger = logging.getLogger("osmclient")
25
26
27 def nsd_list(ctx, filter, long):
28 logger.debug("")
29 if filter:
30 utils.check_client_version(ctx.obj, "--filter")
31 filter = "&".join(filter)
32 resp = ctx.obj.nsd.list(filter)
33 else:
34 resp = ctx.obj.nsd.list()
35 # print(yaml.safe_dump(resp))
36 fullclassname = ctx.obj.__module__ + "." + ctx.obj.__class__.__name__
37 if fullclassname == "osmclient.sol005.client.Client":
38 if long:
39 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 table = PrettyTable(["nsd name", "id"])
52 for nsd in resp:
53 name = nsd.get("id", "-")
54 if long:
55 onb_state = nsd["_admin"].get("onboardingState", "-")
56 op_state = nsd["_admin"].get("operationalState", "-")
57 usage_state = nsd["_admin"].get("usageState", "-")
58 date = datetime.fromtimestamp(nsd["_admin"]["created"]).strftime(
59 "%Y-%m-%dT%H:%M:%S"
60 )
61 last_update = datetime.fromtimestamp(
62 nsd["_admin"]["modified"]
63 ).strftime("%Y-%m-%dT%H:%M:%S")
64 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 table.add_row([name, nsd["_id"]])
77 else:
78 table = PrettyTable(["nsd name", "id"])
79 for nsd in resp:
80 table.add_row([nsd["name"], nsd["id"]])
81 table.align = "l"
82 print(table)
83
84
85 @click.command(name="nsd-list", short_help="list all NS packages")
86 @click.option(
87 "--filter",
88 default=None,
89 multiple=True,
90 help="restricts the list to the NSD/NSpkg matching the filter",
91 )
92 @click.option("--long", is_flag=True, help="get more details")
93 @click.pass_context
94 def nsd_list1(ctx, filter, long):
95 """list all NSD/NS pkg in the system"""
96 logger.debug("")
97 nsd_list(ctx, filter, long)
98
99
100 @click.command(name="nspkg-list", short_help="list all NS packages")
101 @click.option(
102 "--filter",
103 default=None,
104 multiple=True,
105 help="restricts the list to the NSD/NSpkg matching the filter",
106 )
107 @click.option("--long", is_flag=True, help="get more details")
108 @click.pass_context
109 def nsd_list2(ctx, filter, long):
110 """list all NS packages"""
111 logger.debug("")
112 nsd_list(ctx, filter, long)
113
114
115 def nsd_show(ctx, name, literal):
116 logger.debug("")
117 resp = ctx.obj.nsd.get(name)
118 # resp = ctx.obj.nsd.get_individual(name)
119
120 if literal:
121 print(yaml.safe_dump(resp, indent=4, default_flow_style=False))
122 return
123
124 table = PrettyTable(["field", "value"])
125 for k, v in list(resp.items()):
126 table.add_row([k, utils.wrap_text(text=json.dumps(v, indent=2), width=100)])
127 table.align = "l"
128 print(table)
129
130
131 @click.command(name="nsd-show", short_help="shows the details of a NS package")
132 @click.option("--literal", is_flag=True, help="print literally, no pretty table")
133 @click.argument("name")
134 @click.pass_context
135 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 logger.debug("")
141 nsd_show(ctx, name, literal)
142
143
144 @click.command(name="nspkg-show", short_help="shows the details of a NS package")
145 @click.option("--literal", is_flag=True, help="print literally, no pretty table")
146 @click.argument("name")
147 @click.pass_context
148 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 logger.debug("")
154 nsd_show(ctx, name, literal)
155
156
157 def nsd_create(ctx, filename, overwrite, skip_charm_build, repo, vendor, version):
158 logger.debug("")
159 utils.check_client_version(ctx.obj, ctx.command.name)
160 if repo:
161 filename = ctx.obj.osmrepo.get_pkg("ns", filename, repo, vendor, version)
162 ctx.obj.nsd.create(filename, overwrite=overwrite, skip_charm_build=skip_charm_build)
163
164
165 @click.command(name="nsd-create", short_help="creates a new NSD/NSpkg")
166 @click.argument("filename")
167 @click.option(
168 "--overwrite",
169 "overwrite",
170 default=None, # hidden=True,
171 help="Deprecated. Use override",
172 )
173 @click.option(
174 "--override",
175 "overwrite",
176 default=None,
177 help="overrides fields in descriptor, format: "
178 '"key1.key2...=value[;key3...=value;...]"',
179 )
180 @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 @click.option("--repo", default=None, help="[repository]: Repository name")
187 @click.option("--vendor", default=None, help="[repository]: filter by vendor]")
188 @click.option(
189 "--version",
190 default="latest",
191 help="[repository]: filter by version. Default: latest",
192 )
193 @click.pass_context
194 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 logger.debug("")
203 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 @click.command(name="nspkg-create", short_help="creates a new NSD/NSpkg")
215 @click.argument("filename")
216 @click.option(
217 "--overwrite",
218 "overwrite",
219 default=None, # hidden=True,
220 help="Deprecated. Use override",
221 )
222 @click.option(
223 "--override",
224 "overwrite",
225 default=None,
226 help="overrides fields in descriptor, format: "
227 '"key1.key2...=value[;key3...=value;...]"',
228 )
229 @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 @click.option("--repo", default=None, help="[repository]: Repository name")
236 @click.option("--vendor", default=None, help="[repository]: filter by vendor]")
237 @click.option(
238 "--version",
239 default="latest",
240 help="[repository]: filter by version. Default: latest",
241 )
242 @click.pass_context
243 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 logger.debug("")
251 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 def nsd_update(ctx, name, content):
263 logger.debug("")
264 utils.check_client_version(ctx.obj, ctx.command.name)
265 ctx.obj.nsd.update(name, content)
266
267
268 @click.command(name="nsd-update", short_help="updates a NSD/NSpkg")
269 @click.argument("name")
270 @click.option(
271 "--content",
272 default=None,
273 help="filename with the NSD/NSpkg replacing the current one",
274 )
275 @click.pass_context
276 def nsd_update1(ctx, name, content):
277 """updates a NSD/NSpkg
278
279 NAME: name or ID of the NSD/NSpkg
280 """
281 logger.debug("")
282 nsd_update(ctx, name, content)
283
284
285 @click.command(name="nspkg-update", short_help="updates a NSD/NSpkg")
286 @click.argument("name")
287 @click.option(
288 "--content",
289 default=None,
290 help="filename with the NSD/NSpkg replacing the current one",
291 )
292 @click.pass_context
293 def nsd_update2(ctx, name, content):
294 """updates a NSD/NSpkg
295
296 NAME: name or ID of the NSD/NSpkg
297 """
298 logger.debug("")
299 nsd_update(ctx, name, content)
300
301
302 def nsd_delete(ctx, name, force):
303 logger.debug("")
304 if not force:
305 ctx.obj.nsd.delete(name)
306 else:
307 utils.check_client_version(ctx.obj, "--force")
308 ctx.obj.nsd.delete(name, force)
309
310
311 @click.command(name="nsd-delete", short_help="deletes a NSD/NSpkg")
312 @click.argument("name")
313 @click.option(
314 "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
315 )
316 @click.pass_context
317 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 logger.debug("")
323 nsd_delete(ctx, name, force)
324
325
326 @click.command(name="nspkg-delete", short_help="deletes a NSD/NSpkg")
327 @click.argument("name")
328 @click.option(
329 "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
330 )
331 @click.pass_context
332 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 logger.debug("")
338 nsd_delete(ctx, name, force)