1 # Copyright ETSI Contributors and Others.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
17 from osmclient
.common
.exceptions
import ClientException
18 from osmclient
.cli_commands
import utils
19 from prettytable
import PrettyTable
24 logger
= logging
.getLogger("osmclient")
27 @click.command(name
="pdu-list", short_help
="list all Physical Deployment Units (PDU)")
32 help="restricts the list to the Physical Deployment Units matching the filter",
35 def pdu_list(ctx
, filter):
36 """list all Physical Deployment Units (PDU)"""
38 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
40 filter = "&".join(filter)
41 resp
= ctx
.obj
.pdu
.list(filter)
42 table
= PrettyTable(["pdu name", "id", "type", "mgmt ip address"])
44 pdu_name
= pdu
["name"]
46 pdu_type
= pdu
["type"]
47 pdu_ipaddress
= "None"
48 for iface
in pdu
["interfaces"]:
50 pdu_ipaddress
= iface
["ip-address"]
52 table
.add_row([pdu_name
, pdu_id
, pdu_type
, pdu_ipaddress
])
58 name
="pdu-show", short_help
="shows the content of a Physical Deployment Unit (PDU)"
60 @click.argument("name")
61 @click.option("--literal", is_flag
=True, help="print literally, no pretty table")
65 help="restricts the information to the fields in the filter",
68 def pdu_show(ctx
, name
, literal
, filter):
69 """shows the content of a Physical Deployment Unit (PDU)
71 NAME: name or ID of the PDU
74 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
75 pdu
= ctx
.obj
.pdu
.get(name
)
78 print(yaml
.safe_dump(pdu
, indent
=4, default_flow_style
=False))
81 table
= PrettyTable(["field", "value"])
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)])
92 name
="pdu-create", short_help
="adds a new Physical Deployment Unit to the catalog"
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)")
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>]",
102 @click.option("--description", help="human readable description")
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>",
113 help="PDU descriptor file (as an alternative to using the other arguments)",
117 ctx
, name
, pdu_type
, interface
, description
, vim_account
, descriptor_file
119 """creates a new Physical Deployment Unit (PDU)"""
122 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
124 pdu
= create_pdu_dictionary(
125 name
, pdu_type
, interface
, description
, vim_account
, descriptor_file
127 ctx
.obj
.pdu
.create(pdu
)
131 name
="pdu-update", short_help
="updates a Physical Deployment Unit to the catalog"
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)")
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>]",
142 @click.option("--description", help="human readable description")
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>",
153 help="PDU descriptor file (as an alternative to using the other arguments)",
157 ctx
, name
, newname
, pdu_type
, interface
, description
, vim_account
, descriptor_file
159 """Updates a new Physical Deployment Unit (PDU)"""
162 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
169 pdu
= create_pdu_dictionary(
170 newname
, pdu_type
, interface
, description
, vim_account
, descriptor_file
, update
172 ctx
.obj
.pdu
.update(name
, pdu
)
175 def create_pdu_dictionary(
176 name
, pdu_type
, interface
, description
, vim_account
, descriptor_file
, update
=False
181 if not descriptor_file
:
184 raise ClientException(
185 'in absence of descriptor file, option "--name" is mandatory'
188 raise ClientException(
189 'in absence of descriptor file, option "--pdu_type" is mandatory'
192 raise ClientException(
193 'in absence of descriptor file, option "--interface" is mandatory (at least once)'
196 raise ClientException(
197 'in absence of descriptor file, option "--vim_account" is mandatory (at least once)'
200 with
open(descriptor_file
, "r") as df
:
201 pdu
= yaml
.safe_load(df
.read())
205 pdu
["type"] = pdu_type
207 pdu
["description"] = description
209 pdu
["vim_accounts"] = vim_account
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
220 @click.command(name
="pdu-delete", short_help
="deletes a Physical Deployment Unit (PDU)")
221 @click.argument("name")
223 "--force", is_flag
=True, help="forces the deletion bypassing pre-conditions"
226 def pdu_delete(ctx
, name
, force
):
227 """deletes a Physical Deployment Unit (PDU)
229 NAME: name or ID of the PDU to be deleted
232 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
233 ctx
.obj
.pdu
.delete(name
, force
)