Coverage for osmclient/cli_commands/nspkg.py: 53%

137 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2024-06-22 09:01 +0000

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 

16import click 

17from osmclient.cli_commands import utils 

18from prettytable import PrettyTable 

19import yaml 

20import json 

21from datetime import datetime 

22import logging 

23 

24logger = logging.getLogger("osmclient") 

25 

26 

27def 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 if long: 

37 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 table = PrettyTable(["nsd name", "id"]) 

50 for nsd in resp: 

51 name = nsd.get("id", "-") 

52 if long: 

53 onb_state = nsd["_admin"].get("onboardingState", "-") 

54 op_state = nsd["_admin"].get("operationalState", "-") 

55 usage_state = nsd["_admin"].get("usageState", "-") 

56 date = datetime.fromtimestamp(nsd["_admin"]["created"]).strftime( 

57 "%Y-%m-%dT%H:%M:%S" 

58 ) 

59 last_update = datetime.fromtimestamp(nsd["_admin"]["modified"]).strftime( 

60 "%Y-%m-%dT%H:%M:%S" 

61 ) 

62 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 table.add_row([name, nsd["_id"]]) 

75 table.align = "l" 

76 print(table) 

77 

78 

79@click.command(name="nsd-list", short_help="list all NS packages") 

80@click.option( 

81 "--filter", 

82 default=None, 

83 multiple=True, 

84 help="restricts the list to the NSD/NSpkg matching the filter", 

85) 

86@click.option("--long", is_flag=True, help="get more details") 

87@click.pass_context 

88def nsd_list1(ctx, filter, long): 

89 """list all NSD/NS pkg in the system""" 

90 logger.debug("") 

91 nsd_list(ctx, filter, long) 

92 

93 

94@click.command(name="nspkg-list", short_help="list all NS packages") 

95@click.option( 

96 "--filter", 

97 default=None, 

98 multiple=True, 

99 help="restricts the list to the NSD/NSpkg matching the filter", 

100) 

101@click.option("--long", is_flag=True, help="get more details") 

102@click.pass_context 

103def nsd_list2(ctx, filter, long): 

104 """list all NS packages""" 

105 logger.debug("") 

106 nsd_list(ctx, filter, long) 

107 

108 

109def nsd_show(ctx, name, literal): 

110 logger.debug("") 

111 resp = ctx.obj.nsd.get(name) 

112 # resp = ctx.obj.nsd.get_individual(name) 

113 

114 if literal: 

115 print(yaml.safe_dump(resp, indent=4, default_flow_style=False)) 

116 return 

117 

118 table = PrettyTable(["field", "value"]) 

119 for k, v in list(resp.items()): 

120 table.add_row([k, utils.wrap_text(text=json.dumps(v, indent=2), width=100)]) 

121 table.align = "l" 

122 print(table) 

123 

124 

125@click.command(name="nsd-show", short_help="shows the details of a NS package") 

126@click.option("--literal", is_flag=True, help="print literally, no pretty table") 

127@click.argument("name") 

128@click.pass_context 

129def nsd_show1(ctx, name, literal): 

130 """shows the content of a NSD 

131 

132 NAME: name or ID of the NSD/NSpkg 

133 """ 

134 logger.debug("") 

135 nsd_show(ctx, name, literal) 

136 

137 

138@click.command(name="nspkg-show", short_help="shows the details of a NS package") 

139@click.option("--literal", is_flag=True, help="print literally, no pretty table") 

140@click.argument("name") 

141@click.pass_context 

142def nsd_show2(ctx, name, literal): 

143 """shows the content of a NSD 

144 

145 NAME: name or ID of the NSD/NSpkg 

146 """ 

147 logger.debug("") 

148 nsd_show(ctx, name, literal) 

149 

150 

151def nsd_create(ctx, filename, overwrite, skip_charm_build, repo, vendor, version): 

152 logger.debug("") 

153 utils.check_client_version(ctx.obj, ctx.command.name) 

154 if repo: 

155 filename = ctx.obj.osmrepo.get_pkg("ns", filename, repo, vendor, version) 

156 ctx.obj.nsd.create(filename, overwrite=overwrite, skip_charm_build=skip_charm_build) 

157 

158 

159@click.command(name="nsd-create", short_help="creates a new NSD/NSpkg") 

160@click.argument("filename") 

161@click.option( 

162 "--overwrite", 

163 "overwrite", 

164 default=None, # hidden=True, 

165 help="Deprecated. Use override", 

166) 

167@click.option( 

168 "--override", 

169 "overwrite", 

170 default=None, 

171 help="overrides fields in descriptor, format: " 

172 '"key1.key2...=value[;key3...=value;...]"', 

173) 

174@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@click.option("--repo", default=None, help="[repository]: Repository name") 

181@click.option("--vendor", default=None, help="[repository]: filter by vendor]") 

182@click.option( 

183 "--version", 

184 default="latest", 

185 help="[repository]: filter by version. Default: latest", 

186) 

187@click.pass_context 

188def 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 logger.debug("") 

197 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@click.command(name="nspkg-create", short_help="creates a new NSD/NSpkg") 

209@click.argument("filename") 

210@click.option( 

211 "--overwrite", 

212 "overwrite", 

213 default=None, # hidden=True, 

214 help="Deprecated. Use override", 

215) 

216@click.option( 

217 "--override", 

218 "overwrite", 

219 default=None, 

220 help="overrides fields in descriptor, format: " 

221 '"key1.key2...=value[;key3...=value;...]"', 

222) 

223@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@click.option("--repo", default=None, help="[repository]: Repository name") 

230@click.option("--vendor", default=None, help="[repository]: filter by vendor]") 

231@click.option( 

232 "--version", 

233 default="latest", 

234 help="[repository]: filter by version. Default: latest", 

235) 

236@click.pass_context 

237def 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 logger.debug("") 

245 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 

256def nsd_update(ctx, name, content): 

257 logger.debug("") 

258 utils.check_client_version(ctx.obj, ctx.command.name) 

259 ctx.obj.nsd.update(name, content) 

260 

261 

262@click.command(name="nsd-update", short_help="updates a NSD/NSpkg") 

263@click.argument("name") 

264@click.option( 

265 "--content", 

266 default=None, 

267 help="filename with the NSD/NSpkg replacing the current one", 

268) 

269@click.pass_context 

270def nsd_update1(ctx, name, content): 

271 """updates a NSD/NSpkg 

272 

273 NAME: name or ID of the NSD/NSpkg 

274 """ 

275 logger.debug("") 

276 nsd_update(ctx, name, content) 

277 

278 

279@click.command(name="nspkg-update", short_help="updates a NSD/NSpkg") 

280@click.argument("name") 

281@click.option( 

282 "--content", 

283 default=None, 

284 help="filename with the NSD/NSpkg replacing the current one", 

285) 

286@click.pass_context 

287def nsd_update2(ctx, name, content): 

288 """updates a NSD/NSpkg 

289 

290 NAME: name or ID of the NSD/NSpkg 

291 """ 

292 logger.debug("") 

293 nsd_update(ctx, name, content) 

294 

295 

296def nsd_delete(ctx, name, force): 

297 logger.debug("") 

298 if not force: 

299 ctx.obj.nsd.delete(name) 

300 else: 

301 utils.check_client_version(ctx.obj, "--force") 

302 ctx.obj.nsd.delete(name, force) 

303 

304 

305@click.command(name="nsd-delete", short_help="deletes a NSD/NSpkg") 

306@click.argument("name") 

307@click.option( 

308 "--force", is_flag=True, help="forces the deletion bypassing pre-conditions" 

309) 

310@click.pass_context 

311def 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 logger.debug("") 

317 nsd_delete(ctx, name, force) 

318 

319 

320@click.command(name="nspkg-delete", short_help="deletes a NSD/NSpkg") 

321@click.argument("name") 

322@click.option( 

323 "--force", is_flag=True, help="forces the deletion bypassing pre-conditions" 

324) 

325@click.pass_context 

326def 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 logger.debug("") 

332 nsd_delete(ctx, name, force)