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 |
1 |
import click |
17 |
1 |
from osmclient.common.exceptions import ClientException |
18 |
1 |
from osmclient.cli_commands import utils |
19 |
1 |
from prettytable import PrettyTable |
20 |
1 |
import yaml |
21 |
1 |
import json |
22 |
1 |
import logging |
23 |
|
|
24 |
1 |
logger = logging.getLogger("osmclient") |
25 |
|
|
26 |
|
|
27 |
1 |
@click.command(name="pdu-list", short_help="list all Physical Deployment Units (PDU)") |
28 |
1 |
@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 |
1 |
@click.pass_context |
35 |
1 |
def pdu_list(ctx, filter): |
36 |
|
"""list all Physical Deployment Units (PDU)""" |
37 |
0 |
logger.debug("") |
38 |
0 |
utils.check_client_version(ctx.obj, ctx.command.name) |
39 |
0 |
if filter: |
40 |
0 |
filter = "&".join(filter) |
41 |
0 |
resp = ctx.obj.pdu.list(filter) |
42 |
0 |
table = PrettyTable(["pdu name", "id", "type", "mgmt ip address"]) |
43 |
0 |
for pdu in resp: |
44 |
0 |
pdu_name = pdu["name"] |
45 |
0 |
pdu_id = pdu["_id"] |
46 |
0 |
pdu_type = pdu["type"] |
47 |
0 |
pdu_ipaddress = "None" |
48 |
0 |
for iface in pdu["interfaces"]: |
49 |
0 |
if iface["mgmt"]: |
50 |
0 |
pdu_ipaddress = iface["ip-address"] |
51 |
0 |
break |
52 |
0 |
table.add_row([pdu_name, pdu_id, pdu_type, pdu_ipaddress]) |
53 |
0 |
table.align = "l" |
54 |
0 |
print(table) |
55 |
|
|
56 |
|
|
57 |
1 |
@click.command( |
58 |
|
name="pdu-show", short_help="shows the content of a Physical Deployment Unit (PDU)" |
59 |
|
) |
60 |
1 |
@click.argument("name") |
61 |
1 |
@click.option("--literal", is_flag=True, help="print literally, no pretty table") |
62 |
1 |
@click.option( |
63 |
|
"--filter", |
64 |
|
multiple=True, |
65 |
|
help="restricts the information to the fields in the filter", |
66 |
|
) |
67 |
1 |
@click.pass_context |
68 |
1 |
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 |
0 |
logger.debug("") |
74 |
0 |
utils.check_client_version(ctx.obj, ctx.command.name) |
75 |
0 |
pdu = ctx.obj.pdu.get(name) |
76 |
|
|
77 |
0 |
if literal: |
78 |
0 |
print(yaml.safe_dump(pdu, indent=4, default_flow_style=False)) |
79 |
0 |
return |
80 |
|
|
81 |
0 |
table = PrettyTable(["field", "value"]) |
82 |
|
|
83 |
0 |
for k, v in list(pdu.items()): |
84 |
0 |
if not filter or k in filter: |
85 |
0 |
table.add_row([k, json.dumps(v, indent=2)]) |
86 |
|
|
87 |
0 |
table.align = "l" |
88 |
0 |
print(table) |
89 |
|
|
90 |
|
|
91 |
1 |
@click.command( |
92 |
|
name="pdu-create", short_help="adds a new Physical Deployment Unit to the catalog" |
93 |
|
) |
94 |
1 |
@click.option("--name", help="name of the Physical Deployment Unit") |
95 |
1 |
@click.option("--pdu_type", help="type of PDU (e.g. router, firewall, FW001)") |
96 |
1 |
@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 |
1 |
@click.option("--description", help="human readable description") |
103 |
1 |
@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 |
1 |
@click.option( |
111 |
|
"--descriptor_file", |
112 |
|
default=None, |
113 |
|
help="PDU descriptor file (as an alternative to using the other arguments)", |
114 |
|
) |
115 |
1 |
@click.pass_context |
116 |
1 |
def pdu_create( |
117 |
|
ctx, name, pdu_type, interface, description, vim_account, descriptor_file |
118 |
|
): |
119 |
|
"""creates a new Physical Deployment Unit (PDU)""" |
120 |
0 |
logger.debug("") |
121 |
|
|
122 |
0 |
utils.check_client_version(ctx.obj, ctx.command.name) |
123 |
|
|
124 |
0 |
pdu = create_pdu_dictionary( |
125 |
|
name, pdu_type, interface, description, vim_account, descriptor_file |
126 |
|
) |
127 |
0 |
ctx.obj.pdu.create(pdu) |
128 |
|
|
129 |
|
|
130 |
1 |
@click.command( |
131 |
|
name="pdu-update", short_help="updates a Physical Deployment Unit to the catalog" |
132 |
|
) |
133 |
1 |
@click.argument("name") |
134 |
1 |
@click.option("--newname", help="New name for the Physical Deployment Unit") |
135 |
1 |
@click.option("--pdu_type", help="type of PDU (e.g. router, firewall, FW001)") |
136 |
1 |
@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 |
1 |
@click.option("--description", help="human readable description") |
143 |
1 |
@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 |
1 |
@click.option( |
151 |
|
"--descriptor_file", |
152 |
|
default=None, |
153 |
|
help="PDU descriptor file (as an alternative to using the other arguments)", |
154 |
|
) |
155 |
1 |
@click.pass_context |
156 |
1 |
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 |
0 |
logger.debug("") |
161 |
|
|
162 |
0 |
utils.check_client_version(ctx.obj, ctx.command.name) |
163 |
|
|
164 |
0 |
update = True |
165 |
|
|
166 |
0 |
if not newname: |
167 |
0 |
newname = name |
168 |
|
|
169 |
0 |
pdu = create_pdu_dictionary( |
170 |
|
newname, pdu_type, interface, description, vim_account, descriptor_file, update |
171 |
|
) |
172 |
0 |
ctx.obj.pdu.update(name, pdu) |
173 |
|
|
174 |
|
|
175 |
1 |
def create_pdu_dictionary( |
176 |
|
name, pdu_type, interface, description, vim_account, descriptor_file, update=False |
177 |
|
): |
178 |
0 |
logger.debug("") |
179 |
0 |
pdu = {} |
180 |
|
|
181 |
0 |
if not descriptor_file: |
182 |
0 |
if not update: |
183 |
0 |
if not name: |
184 |
0 |
raise ClientException( |
185 |
|
'in absence of descriptor file, option "--name" is mandatory' |
186 |
|
) |
187 |
0 |
if not pdu_type: |
188 |
0 |
raise ClientException( |
189 |
|
'in absence of descriptor file, option "--pdu_type" is mandatory' |
190 |
|
) |
191 |
0 |
if not interface: |
192 |
0 |
raise ClientException( |
193 |
|
'in absence of descriptor file, option "--interface" is mandatory (at least once)' |
194 |
|
) |
195 |
0 |
if not vim_account: |
196 |
0 |
raise ClientException( |
197 |
|
'in absence of descriptor file, option "--vim_account" is mandatory (at least once)' |
198 |
|
) |
199 |
|
else: |
200 |
0 |
with open(descriptor_file, "r") as df: |
201 |
0 |
pdu = yaml.safe_load(df.read()) |
202 |
0 |
if name: |
203 |
0 |
pdu["name"] = name |
204 |
0 |
if pdu_type: |
205 |
0 |
pdu["type"] = pdu_type |
206 |
0 |
if description: |
207 |
0 |
pdu["description"] = description |
208 |
0 |
if vim_account: |
209 |
0 |
pdu["vim_accounts"] = vim_account |
210 |
0 |
if interface: |
211 |
0 |
ifaces_list = [] |
212 |
0 |
for iface in interface: |
213 |
0 |
new_iface = {k: v for k, v in [i.split("=") for i in iface.split(",")]} |
214 |
0 |
new_iface["mgmt"] = new_iface.get("mgmt", "false").lower() == "true" |
215 |
0 |
ifaces_list.append(new_iface) |
216 |
0 |
pdu["interfaces"] = ifaces_list |
217 |
0 |
return pdu |
218 |
|
|
219 |
|
|
220 |
1 |
@click.command(name="pdu-delete", short_help="deletes a Physical Deployment Unit (PDU)") |
221 |
1 |
@click.argument("name") |
222 |
1 |
@click.option( |
223 |
|
"--force", is_flag=True, help="forces the deletion bypassing pre-conditions" |
224 |
|
) |
225 |
1 |
@click.pass_context |
226 |
1 |
def 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 |
0 |
logger.debug("") |
232 |
0 |
utils.check_client_version(ctx.obj, ctx.command.name) |
233 |
0 |
ctx.obj.pdu.delete(name, force) |