66from cloudsnake .sdk .ssm_parameter_store import SSMParameterStoreWrapper
77from cloudsnake .sdk .ssm_session import SSMStartSessionWrapper
88
9+ EC2_RUNNING_FILTER = "Name=instance-state-name,Values=running"
10+
11+ EC2_INSTANCE_SELECTOR_QUERY = (
12+ "Reservations[*].Instances[*].{"
13+ "TargetId: InstanceId, "
14+ "Name: Tags[?Key=='Name'].Value | [0]"
15+ "}"
16+ )
917
1018ssm = typer .Typer (
1119 no_args_is_help = True ,
1220 pretty_exceptions_short = True ,
1321 pretty_exceptions_show_locals = False ,
1422)
1523
16-
1724@ssm .command (
1825 "start-session" , help = "Start session with the given target id" , no_args_is_help = True
1926)
@@ -30,24 +37,33 @@ def start_session(
3037 help = "Prompt a terminal menu and select the instance you want to connect. --target flag is no longer used" ,
3138 ),
3239):
33- ssm = SSMStartSessionWrapper ()
40+ ssm = SSMStartSessionWrapper (
41+ profile = ctx .obj .profile ,
42+ region = ctx .obj .region ,
43+ )
3444 ssm .create_client (ctx .obj .session )
3545 if with_instance_selector :
36- filters = "Name=instance-state-name,Values=running"
37- query = "Reservations[*].Instances[*].{TargetId: InstanceId, Name: Tags[?Key=='Name'].Value | [0]}"
38- ec2 = EC2InstanceWrapper (filters = filters , query = query )
46+ ec2 = EC2InstanceWrapper (
47+ filters = EC2_RUNNING_FILTER ,
48+ query = EC2_INSTANCE_SELECTOR_QUERY ,
49+ profile = ctx .obj .profile ,
50+ region = ctx .obj .region ,
51+ )
3952 ec2 .create_client (ctx .obj .session )
4053 instances = ec2 .describe_ec2_instances ()
41- if len ( instances ) == 0 :
54+ if not instances :
4255 typer .echo ("No running instances found." )
43- exit (1 )
56+ raise typer .Exit (1 )
57+
4458 instance_name = ctx .obj .tui .interactive_menu (
45- instances , title = "Select the EC2 you want to connect"
59+ instances ,
60+ title = "Select the EC2 you want to connect"
4661 )
4762 instance_id = ctx .obj .tui .get_target_id_by_name (instances , instance_name )
48- ssm .start_session (instance_id )
49- else :
50- ssm .start_session (target )
63+
64+ return ssm .start_session (instance_id )
65+
66+ return ssm .start_session (target )
5167
5268
5369# @ssm.command("get-parameters", help="Get parameters from parameter store")
0 commit comments