|
4 | 4 | large changes to the OpenAPI spec.
|
5 | 5 | """
|
6 | 6 |
|
| 7 | +from rich.align import Align |
| 8 | +from rich.console import Console |
| 9 | +from rich.table import Table |
| 10 | + |
7 | 11 | from linodecli.output import OutputMode
|
8 | 12 |
|
9 | 13 | OUTPUT_OVERRIDES = {}
|
@@ -37,3 +41,92 @@ def handle_domains_zone_file(operation, output_handler, json_data) -> bool:
|
37 | 41 | """
|
38 | 42 | print("\n".join(json_data["zone_file"]))
|
39 | 43 | return False
|
| 44 | + |
| 45 | + |
| 46 | +@output_override("linodes", "types", OutputMode.table) |
| 47 | +def handle_types_region_prices_list( |
| 48 | + operation, output_handler, json_data |
| 49 | +) -> bool: |
| 50 | + """ |
| 51 | + Override the output of 'linode-cli linodes types' to display regional pricing. |
| 52 | + """ |
| 53 | + return linode_types_with_region_prices(operation, output_handler, json_data) |
| 54 | + |
| 55 | + |
| 56 | +def linode_types_with_region_prices( |
| 57 | + operation, output_handler, json_data |
| 58 | +) -> bool: |
| 59 | + # pylint: disable=unused-argument |
| 60 | + """ |
| 61 | + Parse and reformat linode types output with region prices. |
| 62 | + """ |
| 63 | + if len(json_data["data"]) < 1: |
| 64 | + return True |
| 65 | + |
| 66 | + output = Table() |
| 67 | + |
| 68 | + # To ensure the order of the headers and make sure we have region_prices as the last column |
| 69 | + headers = sorted( |
| 70 | + json_data["data"][0].keys() - ["addons", "price", "region_prices"], |
| 71 | + key=len, |
| 72 | + ) |
| 73 | + headers += ["price.hourly", "price.monthly", "region_prices"] |
| 74 | + |
| 75 | + for header in headers: |
| 76 | + output.add_column(header, justify="center") |
| 77 | + |
| 78 | + for linode in json_data["data"]: |
| 79 | + row = [] |
| 80 | + for h in headers: |
| 81 | + if h == "region_prices": |
| 82 | + sub_table = format_region_prices(linode[h]) |
| 83 | + row.append(sub_table) |
| 84 | + |
| 85 | + elif h in ("price.hourly", "price.monthly"): |
| 86 | + price = format_prices(h, linode) |
| 87 | + row.append(Align(price, align="left")) |
| 88 | + |
| 89 | + else: |
| 90 | + row.append(Align(str(linode[h]), align="left")) |
| 91 | + |
| 92 | + output.add_row(*row) |
| 93 | + |
| 94 | + console = Console() |
| 95 | + console.print(output) |
| 96 | + |
| 97 | + print( |
| 98 | + "See our [Pricing Page](https://www.linode.com/pricing/) for Region-specific pricing, " |
| 99 | + + "which applies after migration is complete." |
| 100 | + ) |
| 101 | + |
| 102 | + return False |
| 103 | + |
| 104 | + |
| 105 | +def format_prices(prices, data: dict[str, any]) -> any: |
| 106 | + """ |
| 107 | + Format nested price entry. |
| 108 | + """ |
| 109 | + price_headers = prices.split(".") |
| 110 | + |
| 111 | + return str(data[price_headers[0]][price_headers[1]]) |
| 112 | + |
| 113 | + |
| 114 | +def format_region_prices(data: dict[str, any]) -> any: |
| 115 | + """ |
| 116 | + Format nested region price entry into a sub-table. |
| 117 | + """ |
| 118 | + subheaders = ["id", "hourly", "monthly"] |
| 119 | + |
| 120 | + sub_table = Table() |
| 121 | + |
| 122 | + for header in subheaders: |
| 123 | + sub_table.add_column(header, justify="center") |
| 124 | + |
| 125 | + for region_price in data: |
| 126 | + region_price_row = ( |
| 127 | + Align(str(region_price[header]), align="left") |
| 128 | + for header in subheaders |
| 129 | + ) |
| 130 | + sub_table.add_row(*region_price_row) |
| 131 | + |
| 132 | + return sub_table |
0 commit comments