Skip to content

Commit

Permalink
feat: add subnets (#448)
Browse files Browse the repository at this point in the history
```
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
```
  • Loading branch information
tekumara authored Jan 24, 2024
1 parent 3a3b56f commit 01c9404
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 10 deletions.
4 changes: 2 additions & 2 deletions docs/ami.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ aec ami describe
Name ImageId CreationDate RootDeviceName Size
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-20170727 ami-1e749f67 2024-01-24T09:43:25.000Z /dev/sda1 15
ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20170721 ami-785db401 2024-01-24T09:43:25.000Z /dev/sda1 15
ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-20170727 ami-1e749f67 2024-01-24T10:50:11.000Z /dev/sda1 15
ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20170721 ami-785db401 2024-01-24T10:50:11.000Z /dev/sda1 15
```
<!-- [[[end]]] -->

Expand Down
32 changes: 26 additions & 6 deletions docs/ec2.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@ from aec.main import build_parser
cog.out(f"```\n{build_parser()._subparsers._actions[1].choices['ec2'].format_help()}```")
]]] -->
```
usage: aec ec2 [-h] {create-key-pair,describe,launch,logs,modify,start,stop,rename,tag,tags,status,templates,terminate,user-data} ...
usage: aec ec2 [-h] {create-key-pair,describe,launch,logs,modify,start,stop,subnets,rename,tag,tags,status,templates,terminate,user-data} ...
optional arguments:
-h, --help show this help message and exit
subcommands:
{create-key-pair,describe,launch,logs,modify,start,stop,rename,tag,tags,status,templates,terminate,user-data}
{create-key-pair,describe,launch,logs,modify,start,stop,subnets,rename,tag,tags,status,templates,terminate,user-data}
create-key-pair Create a key pair.
describe List EC2 instances in the region.
launch Launch a tagged EC2 instance with an EBS volume.
logs Show the system logs.
modify Change an instance's type.
start Start EC2 instance.
stop Stop EC2 instance.
subnets Describe subnets.
rename Rename EC2 instance(s).
tag Tag EC2 instance(s).
tags List EC2 instances or volumes with their tags.
Expand Down Expand Up @@ -79,8 +80,8 @@ aec ec2 describe
InstanceId State Name Type DnsName LaunchTime ImageId
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
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
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
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
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
```
<!-- [[[end]]] -->

Expand Down Expand Up @@ -118,8 +119,8 @@ aec ec2 describe -c Name,SubnetId,Volumes,Image.CreationDate
Name SubnetId Volumes Image.CreationDate
──────────────────────────────────────────────────────────────────────
alice subnet-338b9497 ['Size=15 GiB'] 2024-01-24T09:43:25.000Z
sam subnet-338b9497 ['Size=15 GiB'] 2024-01-24T09:43:25.000Z
alice subnet-8ffb733b ['Size=15 GiB'] 2024-01-24T10:50:11.000Z
sam subnet-8ffb733b ['Size=15 GiB'] 2024-01-24T10:50:11.000Z
```
<!-- [[[end]]] -->

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

Describe subnets:

<!-- [[[cog
cog.out(f"```\n{docs('aec ec2 subnets', ec2.subnets(config))}\n```")
]]] -->
```
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
```
<!-- [[[end]]] -->

## Columns

Columns special to aec:
Expand Down
22 changes: 22 additions & 0 deletions src/aec/command/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,28 @@ def status_text(summary: InstanceStatusSummaryTypeDef, key: str = "reachability"
)


def subnets(config: Config, vpc_id: str | None = None) -> list[dict[str, Any]]:
"""Describe subnets."""

ec2_client = boto3.client("ec2", region_name=config.get("region", None))

response = ec2_client.describe_subnets(Filters=[{"Name": "vpc-id", "Values": [vpc_id]}] if vpc_id else [])

return sorted(
[
{
"SubnetId": subnet["SubnetId"],
"VpcId": subnet["VpcId"],
"AvailabilityZone": subnet["AvailabilityZone"],
"CidrBlock": subnet["CidrBlock"],
"Name": util_tags.get_value(subnet, "Name"),
}
for subnet in response["Subnets"]
],
key=lambda s: s["VpcId"] + s["AvailabilityZone"] + (s["Name"] or ""),
)


def user_data(config: Config, ident: str) -> str | None:
"""Describe user data for an instance."""
ec2_client = boto3.client("ec2", region_name=config.get("region", None))
Expand Down
4 changes: 4 additions & 0 deletions src/aec/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ def tag_arg_checker(tag: str) -> str:
config_arg,
Arg("ident", type=str, help="Name tag of instance or instance id")
]),
Cmd(ec2.subnets, [
config_arg,
Arg("-v", "--vpc-id", help="Filter to these VPCs"),
]),
Cmd(ec2.rename, [
config_arg,
Arg("ident", type=str, help="Name tag of instance or instance id"),
Expand Down
4 changes: 2 additions & 2 deletions src/aec/util/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from mypy_boto3_ec2.type_defs import ImageTypeDef, InstanceTypeDef, VolumeTypeDef
from mypy_boto3_ec2.type_defs import ImageTypeDef, InstanceTypeDef, SubnetTypeDef, VolumeTypeDef


def get_value(resource: ImageTypeDef | InstanceTypeDef | VolumeTypeDef, key: str) -> str | None:
def get_value(resource: ImageTypeDef | InstanceTypeDef | VolumeTypeDef | SubnetTypeDef, key: str) -> str | None:
tag_value = [t["Value"] for t in resource.get("Tags", []) if t["Key"] == key]
return tag_value[0] if tag_value else None
6 changes: 6 additions & 0 deletions tests/test_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
start,
status,
stop,
subnets,
tag,
terminate,
user_data,
Expand Down Expand Up @@ -355,6 +356,11 @@ def test_stop_start(mock_aws_config: Config):
start(mock_aws_config, ident="alice")


def test_subnets(mock_aws_config: Config):
assert len(subnets(mock_aws_config)) == 6
assert len(subnets(mock_aws_config, vpc_id="foobar")) == 0


def test_modify(mock_aws_config: Config):
launch(mock_aws_config, "alice", ami_id)

Expand Down

0 comments on commit 01c9404

Please sign in to comment.