|
16 | 16 | import os |
17 | 17 | import sys |
18 | 18 | import subprocess |
19 | | -from typing import Dict, List |
20 | 19 |
|
21 | 20 | from openstack_cli.core.shell import shell |
22 | 21 | from openstack_cli.modules.apputils.terminal.get_terminal_size import get_terminal_size |
23 | | -from openstack_cli.modules.apputils.terminal import TableOutput, TableColumn, Console |
24 | | -from openstack_cli.modules.openstack.objects import OpenStackVMInfo |
| 22 | +from openstack_cli.modules.apputils.terminal import Console |
25 | 23 | from openstack_cli.modules.openstack import OpenStack |
26 | 24 | from openstack_cli.core.config import Configuration |
27 | 25 | from openstack_cli.modules.apputils.discovery import CommandMetaInfo |
|
37 | 35 | .add_argument("port", int, "SSH Port", default=22)\ |
38 | 36 | .add_argument("internal", bool, "Use internal SSH Client", alias="buildin", default=False) |
39 | 37 |
|
| 38 | +from openstack_cli.modules.utils import host_selector |
40 | 39 |
|
41 | 40 | IS_WIN: bool = sys.platform == "win32" |
42 | 41 |
|
@@ -129,70 +128,12 @@ def _open_console(internal:bool, host: str, port: int = 22, user_name: str = "ro |
129 | 128 |
|
130 | 129 | def __init__(conf: Configuration, name: str, node_number: int, user_name: str, use_password: bool, use_key: str, |
131 | 130 | own: bool, port: int, internal: bool): |
132 | | - ostack = OpenStack(conf) |
133 | | - if name == "None": |
134 | | - name = "" |
135 | 131 |
|
136 | 132 | if use_key == "None": |
137 | 133 | use_key = None |
138 | 134 |
|
139 | | - if name and node_number == -1: |
140 | | - _name, _, _node_number = name.rpartition("-") |
141 | | - |
142 | | - try: |
143 | | - node_number = int(_node_number) |
144 | | - name = _name |
145 | | - except (ValueError, TypeError): |
146 | | - pass |
147 | | - |
148 | | - if "." in name: |
149 | | - name, _ = name.split(".") |
150 | | - |
151 | | - search_result: Dict[str, List[OpenStackVMInfo]] = ostack.get_server_by_cluster(name, sort=True, only_owned=own) |
152 | | - to = TableOutput( |
153 | | - TableColumn("Cluster name", 40), |
154 | | - print_row_number=True |
155 | | - ) |
156 | | - if len(search_result.keys()) > 1: |
157 | | - to.print_header() |
158 | | - for cluster_name in search_result.keys(): |
159 | | - to.print_row(cluster_name) |
160 | | - |
161 | | - selection: int = Console.ask("Choose cluster from the list", int) |
162 | | - try: |
163 | | - name = list(search_result.keys())[selection:][0] |
164 | | - except IndexError: |
165 | | - raise ValueError("Wrong selection, please select item within an provided range") |
166 | | - elif search_result: |
167 | | - name = list(search_result.keys())[0] |
168 | | - else: |
169 | | - raise ValueError(f"No matching cluster matching pattern'{name}' found") |
170 | | - |
171 | | - nodes: List[OpenStackVMInfo] = search_result[name] |
172 | | - if node_number == -1: |
173 | | - if len(nodes) > 1: |
174 | | - to = TableOutput( |
175 | | - TableColumn("IP", 18), |
176 | | - TableColumn("Host name", 40), |
177 | | - |
178 | | - print_row_number=True |
179 | | - ) |
180 | | - to.print_header() |
181 | | - for node in nodes: |
182 | | - to.print_row(node.ip_address, node.fqdn) |
183 | | - node_number: int = Console.ask("Choose host from the list", int) |
184 | | - if node_number > len(nodes): |
185 | | - raise ValueError("Wrong selection, please select item within an provided range") |
186 | | - else: |
187 | | - node_number = 0 |
188 | | - else: |
189 | | - node_number -= 1 # the node name starts for 1, while list from 0 |
190 | | - |
191 | | - try: |
192 | | - node: OpenStackVMInfo = nodes[node_number] |
193 | | - except IndexError: |
194 | | - raise ValueError("Unknown host name, please check the name") |
195 | | - |
| 135 | + ostack = OpenStack(conf) |
| 136 | + node = host_selector(ostack, name, node_number, own) |
196 | 137 | print(f"Establishing connection to {node.fqdn}({node.ip_address}) as '{user_name}' user...") |
197 | 138 | if use_password: |
198 | 139 | _open_console(internal, node.ip_address, port=port, user_name=user_name, password=True) |
|
0 commit comments