Feature 10962 Refactoring of osmclient commands
[osm/osmclient.git] / osmclient / cli_commands / pdus.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.common.exceptions import ClientException
18 from osmclient.cli_commands import utils
19 from prettytable import PrettyTable
20 import yaml
21 import json
22 import logging
23
24 logger = 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
35 def 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
68 def 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
116 def 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
156 def 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
175 def create_pdu_dictionary(
176 name, pdu_type, interface, description, vim_account, descriptor_file, update=False
177 ):
178
179 logger.debug("")
180 pdu = {}
181
182 if not descriptor_file:
183 if not update:
184 if not name:
185 raise ClientException(
186 'in absence of descriptor file, option "--name" is mandatory'
187 )
188 if not pdu_type:
189 raise ClientException(
190 'in absence of descriptor file, option "--pdu_type" is mandatory'
191 )
192 if not interface:
193 raise ClientException(
194 'in absence of descriptor file, option "--interface" is mandatory (at least once)'
195 )
196 if not vim_account:
197 raise ClientException(
198 'in absence of descriptor file, option "--vim_account" is mandatory (at least once)'
199 )
200 else:
201 with open(descriptor_file, "r") as df:
202 pdu = yaml.safe_load(df.read())
203 if name:
204 pdu["name"] = name
205 if pdu_type:
206 pdu["type"] = pdu_type
207 if description:
208 pdu["description"] = description
209 if vim_account:
210 pdu["vim_accounts"] = vim_account
211 if interface:
212 ifaces_list = []
213 for iface in interface:
214 new_iface = {k: v for k, v in [i.split("=") for i in iface.split(",")]}
215 new_iface["mgmt"] = new_iface.get("mgmt", "false").lower() == "true"
216 ifaces_list.append(new_iface)
217 pdu["interfaces"] = ifaces_list
218 return pdu
219
220
221 @click.command(name="pdu-delete", short_help="deletes a Physical Deployment Unit (PDU)")
222 @click.argument("name")
223 @click.option(
224 "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
225 )
226 @click.pass_context
227 def pdu_delete(ctx, name, force):
228 """deletes a Physical Deployment Unit (PDU)
229
230 NAME: name or ID of the PDU to be deleted
231 """
232 logger.debug("")
233 utils.check_client_version(ctx.obj, ctx.command.name)
234 ctx.obj.pdu.delete(name, force)