Skip to content

Commit f5d1092

Browse files
authored
Override placement group view to display members (#675)
1 parent 12b8f25 commit f5d1092

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

linodecli/overrides.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,46 @@ def handle_image_replicate(operation, output_handler, json_data) -> bool:
7676
return image_replicate_output(json_data)
7777

7878

79+
@output_override("placement", "group-view", OutputMode.table)
80+
def handle_placement_group_view(operation, output_handler, json_data) -> bool:
81+
# pylint: disable=unused-argument
82+
"""
83+
Override the output of 'linode-cli placement group-view' to show PG members.
84+
"""
85+
return pg_view_output(json_data)
86+
87+
88+
@output_override("placement", "group-update", OutputMode.table)
89+
def handle_placement_group_update(operation, output_handler, json_data) -> bool:
90+
# pylint: disable=unused-argument
91+
"""
92+
Override the output of 'linode-cli placement group-update' to show PG members.
93+
"""
94+
return pg_view_output(json_data)
95+
96+
97+
@output_override("placement", "assign-linode", OutputMode.table)
98+
def handle_placement_assign_linode(
99+
operation, output_handler, json_data
100+
) -> bool:
101+
# pylint: disable=unused-argument
102+
"""
103+
Override the output of 'linode-cli placement assign-linode' to show PG members.
104+
"""
105+
return pg_view_output(json_data)
106+
107+
108+
@output_override("placement", "unassign-linode", OutputMode.table)
109+
def handle_placement_unassign_linode(
110+
operation, output_handler, json_data
111+
) -> bool:
112+
# pylint: disable=unused-argument
113+
"""
114+
Override the output of 'linode-cli placement unassign-linode' to show PG members.
115+
"""
116+
return pg_view_output(json_data)
117+
118+
79119
def linode_types_with_region_prices(
80120
operation, output_handler, json_data
81121
) -> bool:
@@ -206,3 +246,48 @@ def image_replicate_output(json_data) -> bool:
206246
console.print(output)
207247

208248
return False
249+
250+
251+
def build_pg_members(members: List) -> Table:
252+
"""
253+
Format nested linode members list to a sub-table.
254+
"""
255+
table = Table()
256+
257+
member_headers = members[0].keys()
258+
for h in member_headers:
259+
table.add_column(h, justify="center")
260+
261+
for member in members:
262+
row = []
263+
for h in member_headers:
264+
row.append(Align(str(member[h]), align="left"))
265+
table.add_row(*row)
266+
267+
return table
268+
269+
270+
def pg_view_output(json_data) -> bool:
271+
"""
272+
Parse and format the placement group output table.
273+
"""
274+
output = Table(
275+
header_style="bold",
276+
show_lines=True,
277+
)
278+
279+
row = []
280+
for header in json_data:
281+
if json_data[header] is not None:
282+
output.add_column(header, justify="center")
283+
if header == "members" and len(json_data[header]) > 0:
284+
row.append(build_pg_members(json_data[header]))
285+
else:
286+
row.append(Align(str(json_data[header]), align="left"))
287+
288+
output.add_row(*row)
289+
290+
console = Console()
291+
console.print(output)
292+
293+
return False

0 commit comments

Comments
 (0)