Coverage for osmclient/cli_commands/pdus.py: 37%

117 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.common.exceptions import ClientException 

18from osmclient.cli_commands import utils 

19from prettytable import PrettyTable 

20import yaml 

21import json 

22import logging 

23 

24logger = logging.getLogger("osmclient") 

25 

26 

27@click.command(name="pdu-list", short_help="list all Physical Deployment Units (PDU)") 

28@click.option( 

29 "--filter", 

30 default=None, 

31 multiple=True, 

32 help="restricts the list to the Physical Deployment Units matching the filter", 

33) 

34@click.pass_context 

35def pdu_list(ctx, filter): 

36 """list all Physical Deployment Units (PDU)""" 

37 logger.debug("") 

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

39 if filter: 

40 filter = "&".join(filter) 

41 resp = ctx.obj.pdu.list(filter) 

42 table = PrettyTable(["pdu name", "id", "type", "mgmt ip address"]) 

43 for pdu in resp: 

44 pdu_name = pdu["name"] 

45 pdu_id = pdu["_id"] 

46 pdu_type = pdu["type"] 

47 pdu_ipaddress = "None" 

48 for iface in pdu["interfaces"]: 

49 if iface["mgmt"]: 

50 pdu_ipaddress = iface["ip-address"] 

51 break 

52 table.add_row([pdu_name, pdu_id, pdu_type, pdu_ipaddress]) 

53 table.align = "l" 

54 print(table) 

55 

56 

57@click.command( 

58 name="pdu-show", short_help="shows the content of a Physical Deployment Unit (PDU)" 

59) 

60@click.argument("name") 

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

62@click.option( 

63 "--filter", 

64 multiple=True, 

65 help="restricts the information to the fields in the filter", 

66) 

67@click.pass_context 

68def pdu_show(ctx, name, literal, filter): 

69 """shows the content of a Physical Deployment Unit (PDU) 

70 

71 NAME: name or ID of the PDU 

72 """ 

73 logger.debug("") 

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

75 pdu = ctx.obj.pdu.get(name) 

76 

77 if literal: 

78 print(yaml.safe_dump(pdu, indent=4, default_flow_style=False)) 

79 return 

80 

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

82 

83 for k, v in list(pdu.items()): 

84 if not filter or k in filter: 

85 table.add_row([k, json.dumps(v, indent=2)]) 

86 

87 table.align = "l" 

88 print(table) 

89 

90 

91@click.command( 

92 name="pdu-create", short_help="adds a new Physical Deployment Unit to the catalog" 

93) 

94@click.option("--name", help="name of the Physical Deployment Unit") 

95@click.option("--pdu_type", help="type of PDU (e.g. router, firewall, FW001)") 

96@click.option( 

97 "--interface", 

98 help="interface(s) of the PDU: name=<NAME>,mgmt=<true|false>,ip-address=<IP_ADDRESS>" 

99 + "[,type=<overlay|underlay>][,mac-address=<MAC_ADDRESS>][,vim-network-name=<VIM_NET_NAME>]", 

100 multiple=True, 

101) 

102@click.option("--description", help="human readable description") 

103@click.option( 

104 "--vim_account", 

105 help="list of VIM accounts (in the same VIM) that can reach this PDU\n" 

106 + "The format for multiple VIMs is --vim_account <vim_account_id_1> " 

107 + "--vim_account <vim_account_id_2> ... --vim_account <vim_account_id_N>", 

108 multiple=True, 

109) 

110@click.option( 

111 "--descriptor_file", 

112 default=None, 

113 help="PDU descriptor file (as an alternative to using the other arguments)", 

114) 

115@click.pass_context 

116def pdu_create( 

117 ctx, name, pdu_type, interface, description, vim_account, descriptor_file 

118): 

119 """creates a new Physical Deployment Unit (PDU)""" 

120 logger.debug("") 

121 

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

123 

124 pdu = create_pdu_dictionary( 

125 name, pdu_type, interface, description, vim_account, descriptor_file 

126 ) 

127 ctx.obj.pdu.create(pdu) 

128 

129 

130@click.command( 

131 name="pdu-update", short_help="updates a Physical Deployment Unit to the catalog" 

132) 

133@click.argument("name") 

134@click.option("--newname", help="New name for the Physical Deployment Unit") 

135@click.option("--pdu_type", help="type of PDU (e.g. router, firewall, FW001)") 

136@click.option( 

137 "--interface", 

138 help="interface(s) of the PDU: name=<NAME>,mgmt=<true|false>,ip-address=<IP_ADDRESS>" 

139 + "[,type=<overlay|underlay>][,mac-address=<MAC_ADDRESS>][,vim-network-name=<VIM_NET_NAME>]", 

140 multiple=True, 

141) 

142@click.option("--description", help="human readable description") 

143@click.option( 

144 "--vim_account", 

145 help="list of VIM accounts (in the same VIM) that can reach this PDU\n" 

146 + "The format for multiple VIMs is --vim_account <vim_account_id_1> " 

147 + "--vim_account <vim_account_id_2> ... --vim_account <vim_account_id_N>", 

148 multiple=True, 

149) 

150@click.option( 

151 "--descriptor_file", 

152 default=None, 

153 help="PDU descriptor file (as an alternative to using the other arguments)", 

154) 

155@click.pass_context 

156def pdu_update( 

157 ctx, name, newname, pdu_type, interface, description, vim_account, descriptor_file 

158): 

159 """Updates a new Physical Deployment Unit (PDU)""" 

160 logger.debug("") 

161 

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

163 

164 update = True 

165 

166 if not newname: 

167 newname = name 

168 

169 pdu = create_pdu_dictionary( 

170 newname, pdu_type, interface, description, vim_account, descriptor_file, update 

171 ) 

172 ctx.obj.pdu.update(name, pdu) 

173 

174 

175def create_pdu_dictionary( 

176 name, pdu_type, interface, description, vim_account, descriptor_file, update=False 

177): 

178 logger.debug("") 

179 pdu = {} 

180 

181 if not descriptor_file: 

182 if not update: 

183 if not name: 

184 raise ClientException( 

185 'in absence of descriptor file, option "--name" is mandatory' 

186 ) 

187 if not pdu_type: 

188 raise ClientException( 

189 'in absence of descriptor file, option "--pdu_type" is mandatory' 

190 ) 

191 if not interface: 

192 raise ClientException( 

193 'in absence of descriptor file, option "--interface" is mandatory (at least once)' 

194 ) 

195 if not vim_account: 

196 raise ClientException( 

197 'in absence of descriptor file, option "--vim_account" is mandatory (at least once)' 

198 ) 

199 else: 

200 with open(descriptor_file, "r") as df: 

201 pdu = yaml.safe_load(df.read()) 

202 if name: 

203 pdu["name"] = name 

204 if pdu_type: 

205 pdu["type"] = pdu_type 

206 if description: 

207 pdu["description"] = description 

208 if vim_account: 

209 pdu["vim_accounts"] = vim_account 

210 if interface: 

211 ifaces_list = [] 

212 for iface in interface: 

213 new_iface = {k: v for k, v in [i.split("=") for i in iface.split(",")]} 

214 new_iface["mgmt"] = new_iface.get("mgmt", "false").lower() == "true" 

215 ifaces_list.append(new_iface) 

216 pdu["interfaces"] = ifaces_list 

217 return pdu 

218 

219 

220@click.command(name="pdu-delete", short_help="deletes a Physical Deployment Unit (PDU)") 

221@click.argument("name") 

222@click.option( 

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

224) 

225@click.pass_context 

226def pdu_delete(ctx, name, force): 

227 """deletes a Physical Deployment Unit (PDU) 

228 

229 NAME: name or ID of the PDU to be deleted 

230 """ 

231 logger.debug("") 

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

233 ctx.obj.pdu.delete(name, force)