Skip to content

Commit c450f30

Browse files
refactor: code structure and doc
1 parent 90d2678 commit c450f30

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<p align="center" >
22
<h3 align="center">cloudsnake 🐍</h3>
3-
<p align="center">Some AWS CLI commands with a beautiful TUI</p>
4-
<p align="center">Implement some useful commands to operate AWS. Like EC2 SSM instance connection or RDS connection using IAM authentication</p>
3+
<p align="center">Wrapping some useful AWS cli commands to operate AWS like EC2 SSM instance connection or RDS connection using IAM authentication</p>
54
<p align="center">Built with ❤ in Python</p>
65
</p>
76

src/cloudsnake/cli/ssm.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@
66
from cloudsnake.sdk.ssm_parameter_store import SSMParameterStoreWrapper
77
from 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

1018
ssm = 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

Comments
 (0)