Skip to content

Commit 01c9404

Browse files
authored
feat: add subnets (#448)
``` aec ec2 subnets SubnetId VpcId AvailabilityZone CidrBlock Name ─────────────────────────────────────────────────────────────────────────── subnet-8ffb733b vpc-df045ae9 us-east-1a 172.31.0.0/20 subnet-50f11bb4 vpc-df045ae9 us-east-1b 172.31.16.0/20 subnet-93811557 vpc-df045ae9 us-east-1c 172.31.32.0/20 subnet-f17e6261 vpc-df045ae9 us-east-1d 172.31.48.0/20 subnet-1a5d6685 vpc-df045ae9 us-east-1e 172.31.64.0/20 subnet-b12557cf vpc-df045ae9 us-east-1f 172.31.80.0/20 ```
1 parent 3a3b56f commit 01c9404

File tree

6 files changed

+62
-10
lines changed

6 files changed

+62
-10
lines changed

docs/ami.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ aec ami describe
3939
4040
Name ImageId CreationDate RootDeviceName Size
4141
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
42-
ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-20170727 ami-1e749f67 2024-01-24T09:43:25.000Z /dev/sda1 15
43-
ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20170721 ami-785db401 2024-01-24T09:43:25.000Z /dev/sda1 15
42+
ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-20170727 ami-1e749f67 2024-01-24T10:50:11.000Z /dev/sda1 15
43+
ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20170721 ami-785db401 2024-01-24T10:50:11.000Z /dev/sda1 15
4444
```
4545
<!-- [[[end]]] -->
4646

docs/ec2.md

+26-6
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@ from aec.main import build_parser
1515
cog.out(f"```\n{build_parser()._subparsers._actions[1].choices['ec2'].format_help()}```")
1616
]]] -->
1717
```
18-
usage: aec ec2 [-h] {create-key-pair,describe,launch,logs,modify,start,stop,rename,tag,tags,status,templates,terminate,user-data} ...
18+
usage: aec ec2 [-h] {create-key-pair,describe,launch,logs,modify,start,stop,subnets,rename,tag,tags,status,templates,terminate,user-data} ...
1919
2020
optional arguments:
2121
-h, --help show this help message and exit
2222
2323
subcommands:
24-
{create-key-pair,describe,launch,logs,modify,start,stop,rename,tag,tags,status,templates,terminate,user-data}
24+
{create-key-pair,describe,launch,logs,modify,start,stop,subnets,rename,tag,tags,status,templates,terminate,user-data}
2525
create-key-pair Create a key pair.
2626
describe List EC2 instances in the region.
2727
launch Launch a tagged EC2 instance with an EBS volume.
2828
logs Show the system logs.
2929
modify Change an instance's type.
3030
start Start EC2 instance.
3131
stop Stop EC2 instance.
32+
subnets Describe subnets.
3233
rename Rename EC2 instance(s).
3334
tag Tag EC2 instance(s).
3435
tags List EC2 instances or volumes with their tags.
@@ -79,8 +80,8 @@ aec ec2 describe
7980
8081
InstanceId State Name Type DnsName LaunchTime ImageId
8182
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
82-
i-787bfc64b53337dbb running alice t3.small ec2-54-214-45-74.compute-1.amazonaws.com 2024-01-24 09:43:25+00:00 ami-03cf127a
83-
i-f9f09548c218a73a5 running sam t3.small ec2-54-214-132-201.compute-1.amazonaws.com 2024-01-24 09:43:26+00:00 ami-03cf127a
83+
i-b39b1ea60119e503e running alice t3.small ec2-54-214-187-100.compute-1.amazonaws.com 2024-01-24 10:50:11+00:00 ami-03cf127a
84+
i-52d4b17a9a8586a31 running sam t3.small ec2-54-214-105-52.compute-1.amazonaws.com 2024-01-24 10:50:11+00:00 ami-03cf127a
8485
```
8586
<!-- [[[end]]] -->
8687

@@ -118,8 +119,8 @@ aec ec2 describe -c Name,SubnetId,Volumes,Image.CreationDate
118119
119120
Name SubnetId Volumes Image.CreationDate
120121
──────────────────────────────────────────────────────────────────────
121-
alice subnet-338b9497 ['Size=15 GiB'] 2024-01-24T09:43:25.000Z
122-
sam subnet-338b9497 ['Size=15 GiB'] 2024-01-24T09:43:25.000Z
122+
alice subnet-8ffb733b ['Size=15 GiB'] 2024-01-24T10:50:11.000Z
123+
sam subnet-8ffb733b ['Size=15 GiB'] 2024-01-24T10:50:11.000Z
123124
```
124125
<!-- [[[end]]] -->
125126

@@ -204,6 +205,25 @@ aec ec2 describe -it i-02a840e0ca609c432 -c StateReason
204205
{'Code': 'Client.InternalError', 'Message': 'Client.InternalError: Client error on launch'}
205206
```
206207

208+
Describe subnets:
209+
210+
<!-- [[[cog
211+
cog.out(f"```\n{docs('aec ec2 subnets', ec2.subnets(config))}\n```")
212+
]]] -->
213+
```
214+
aec ec2 subnets
215+
216+
SubnetId VpcId AvailabilityZone CidrBlock Name
217+
───────────────────────────────────────────────────────────────────────────
218+
subnet-8ffb733b vpc-df045ae9 us-east-1a 172.31.0.0/20
219+
subnet-50f11bb4 vpc-df045ae9 us-east-1b 172.31.16.0/20
220+
subnet-93811557 vpc-df045ae9 us-east-1c 172.31.32.0/20
221+
subnet-f17e6261 vpc-df045ae9 us-east-1d 172.31.48.0/20
222+
subnet-1a5d6685 vpc-df045ae9 us-east-1e 172.31.64.0/20
223+
subnet-b12557cf vpc-df045ae9 us-east-1f 172.31.80.0/20
224+
```
225+
<!-- [[[end]]] -->
226+
207227
## Columns
208228

209229
Columns special to aec:

src/aec/command/ec2.py

+22
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,28 @@ def status_text(summary: InstanceStatusSummaryTypeDef, key: str = "reachability"
616616
)
617617

618618

619+
def subnets(config: Config, vpc_id: str | None = None) -> list[dict[str, Any]]:
620+
"""Describe subnets."""
621+
622+
ec2_client = boto3.client("ec2", region_name=config.get("region", None))
623+
624+
response = ec2_client.describe_subnets(Filters=[{"Name": "vpc-id", "Values": [vpc_id]}] if vpc_id else [])
625+
626+
return sorted(
627+
[
628+
{
629+
"SubnetId": subnet["SubnetId"],
630+
"VpcId": subnet["VpcId"],
631+
"AvailabilityZone": subnet["AvailabilityZone"],
632+
"CidrBlock": subnet["CidrBlock"],
633+
"Name": util_tags.get_value(subnet, "Name"),
634+
}
635+
for subnet in response["Subnets"]
636+
],
637+
key=lambda s: s["VpcId"] + s["AvailabilityZone"] + (s["Name"] or ""),
638+
)
639+
640+
619641
def user_data(config: Config, ident: str) -> str | None:
620642
"""Describe user data for an instance."""
621643
ec2_client = boto3.client("ec2", region_name=config.get("region", None))

src/aec/main.py

+4
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ def tag_arg_checker(tag: str) -> str:
8686
config_arg,
8787
Arg("ident", type=str, help="Name tag of instance or instance id")
8888
]),
89+
Cmd(ec2.subnets, [
90+
config_arg,
91+
Arg("-v", "--vpc-id", help="Filter to these VPCs"),
92+
]),
8993
Cmd(ec2.rename, [
9094
config_arg,
9195
Arg("ident", type=str, help="Name tag of instance or instance id"),

src/aec/util/tags.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from typing import TYPE_CHECKING
44

55
if TYPE_CHECKING:
6-
from mypy_boto3_ec2.type_defs import ImageTypeDef, InstanceTypeDef, VolumeTypeDef
6+
from mypy_boto3_ec2.type_defs import ImageTypeDef, InstanceTypeDef, SubnetTypeDef, VolumeTypeDef
77

88

9-
def get_value(resource: ImageTypeDef | InstanceTypeDef | VolumeTypeDef, key: str) -> str | None:
9+
def get_value(resource: ImageTypeDef | InstanceTypeDef | VolumeTypeDef | SubnetTypeDef, key: str) -> str | None:
1010
tag_value = [t["Value"] for t in resource.get("Tags", []) if t["Key"] == key]
1111
return tag_value[0] if tag_value else None

tests/test_ec2.py

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
start,
2424
status,
2525
stop,
26+
subnets,
2627
tag,
2728
terminate,
2829
user_data,
@@ -355,6 +356,11 @@ def test_stop_start(mock_aws_config: Config):
355356
start(mock_aws_config, ident="alice")
356357

357358

359+
def test_subnets(mock_aws_config: Config):
360+
assert len(subnets(mock_aws_config)) == 6
361+
assert len(subnets(mock_aws_config, vpc_id="foobar")) == 0
362+
363+
358364
def test_modify(mock_aws_config: Config):
359365
launch(mock_aws_config, "alice", ami_id)
360366

0 commit comments

Comments
 (0)