Skip to content

Commit

Permalink
fix: remove more traces of mypy-extensions from the code
Browse files Browse the repository at this point in the history
  • Loading branch information
lyz-code committed May 24, 2022
1 parent e105ae2 commit e44e73c
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 45 deletions.
29 changes: 16 additions & 13 deletions src/drode/adapters/aws.py
@@ -1,11 +1,11 @@
"""Gather the integration with the AWS boto library."""

import logging
from dataclasses import dataclass
from typing import Dict, List

import boto3
from botocore.exceptions import ClientError, NoRegionError
from mypy_extensions import TypedDict

log = logging.getLogger(__name__)

Expand All @@ -19,9 +19,14 @@ class AWSStateError(Exception):


InstanceInfo = Dict[str, str]
AutoscalerInfo = TypedDict(
"AutoscalerInfo", {"template": str, "instances": List[InstanceInfo]}, total=False
)


@dataclass
class AutoscalerInfo:
"""Model the response of the AWS API regarding ASGs."""

instances: List[InstanceInfo]
template: str = ""


class AWS:
Expand Down Expand Up @@ -71,21 +76,19 @@ def get_autoscaling_group(autoscaling_name: str) -> AutoscalerInfo:
ec2 = boto3.client("ec2")
autoscaling = boto3.client("autoscaling")

autoscaler_info: AutoscalerInfo = {
"template": "",
"instances": [],
}
autoscaler_info = AutoscalerInfo(
template="",
instances=[],
)

try:
autoscaling_group = autoscaling.describe_auto_scaling_groups(
AutoScalingGroupNames=[autoscaling_name]
)["AutoScalingGroups"][0]
try:
autoscaler_info["template"] = autoscaling_group[
"LaunchConfigurationName"
]
autoscaler_info.template = autoscaling_group["LaunchConfigurationName"]
except KeyError:
autoscaler_info["template"] = (
autoscaler_info.template = (
f'{autoscaling_group["LaunchTemplate"]["LaunchTemplateName"][:35]}'
f':{autoscaling_group["LaunchTemplate"]["Version"]}'
)
Expand All @@ -107,7 +110,7 @@ def get_autoscaling_group(autoscaling_name: str) -> AutoscalerInfo:
f':{instance_data["LaunchTemplate"]["Version"]}'
)

autoscaler_info["instances"].append(
autoscaler_info.instances.append(
{
"Instance": instance_data["InstanceId"],
"IP": ec2_data["PrivateIpAddress"],
Expand Down
2 changes: 1 addition & 1 deletion src/drode/services.py
Expand Up @@ -185,7 +185,7 @@ def project_status(config: Config, aws: AWS) -> ProjectStatus:
raise ConfigError("The autoscaler name is not a string")
autoscaler_info = aws.get_autoscaling_group(autoscaler_name)
except ConfigError:
autoscaler_info = {}
autoscaler_info = AutoscalerInfo(instances=[])

project[environment] = autoscaler_info

Expand Down
8 changes: 3 additions & 5 deletions src/drode/views.py
Expand Up @@ -8,18 +8,16 @@

def print_autoscaling_group_info(autoscaler_info: AutoscalerInfo) -> None:
"""Print the information of the autoscaler information in table format."""
print(f"Active Template: {autoscaler_info['template']}")
print(f"Active Template: {autoscaler_info.template}")
print(
tabulate.tabulate(
autoscaler_info["instances"], headers="keys", tablefmt="simple"
)
tabulate.tabulate(autoscaler_info.instances, headers="keys", tablefmt="simple")
)


def print_status(project_status: ProjectStatus) -> None:
"""Print the project environment status."""
for environment, autoscaler_info in project_status.items():
if autoscaler_info == {}:
if len(autoscaler_info.instances) == 0:
continue
print(f"# {environment}")
print_autoscaling_group_info(autoscaler_info)
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/test_cli.py
Expand Up @@ -3,12 +3,12 @@
import logging
import os
import re
from typing import Any, Dict
from unittest.mock import patch

import pytest
from _pytest.logging import LogCaptureFixture
from click.testing import CliRunner
from mypy_extensions import TypedDict
from py._path.local import LocalPath
from tests.fake_adapters import FakeAWS, FakeDrone

Expand All @@ -18,7 +18,7 @@

from ..factories import BuildInfoFactory

FakeDeps = TypedDict("FakeDeps", {"drone": FakeDrone, "aws": FakeAWS})
FakeDeps = Dict[str, Any]


@pytest.fixture(name="fake_dependencies")
Expand Down
8 changes: 4 additions & 4 deletions tests/fake_adapters.py
Expand Up @@ -205,9 +205,9 @@ def get_autoscaling_group(autoscaling_name: str) -> AutoscalerInfo:
Raises:
AWSStateError: If no autoscaling groups are found with that name.
"""
return {
"template": "launch-config-name",
"instances": [
return AutoscalerInfo(
template="launch-config-name",
instances=[
{
"Instance": "i-xxxxxxxxxxxxxxxxx",
"IP": "192.168.1.13",
Expand All @@ -216,4 +216,4 @@ def get_autoscaling_group(autoscaling_name: str) -> AutoscalerInfo:
"Template": "old-launch-config-name",
}
],
}
)
18 changes: 9 additions & 9 deletions tests/unit/adapters/test_aws.py
Expand Up @@ -9,7 +9,7 @@
from _pytest.logging import LogCaptureFixture
from botocore.exceptions import NoRegionError

from drode.adapters.aws import AWS, AWSConfigurationError, AWSStateError
from drode.adapters.aws import AWS, AutoscalerInfo, AWSConfigurationError, AWSStateError


@pytest.fixture(name="boto")
Expand Down Expand Up @@ -118,9 +118,9 @@ def test_get_autoscaling_returns_instances_info(aws: AWS, boto: Mock) -> None:
],
"ResponseMetadata": {"HTTPStatusCode": 200},
}
desired_result = {
"template": "launch-config-name",
"instances": [
desired_result = AutoscalerInfo(
template="launch-config-name",
instances=[
{
"Instance": "i-xxxxxxxxxxxxxxxxx",
"IP": "192.168.1.13",
Expand All @@ -129,7 +129,7 @@ def test_get_autoscaling_returns_instances_info(aws: AWS, boto: Mock) -> None:
"Template": "old-launch-config-name",
}
],
}
)

result = aws.get_autoscaling_group("production_autoscaling_group_name")

Expand Down Expand Up @@ -223,9 +223,9 @@ def test_get_autoscaling_handles_launch_templates(aws: AWS, boto: Mock) -> None:
],
"ResponseMetadata": {"HTTPStatusCode": 200},
}
desired_result = {
"template": "launch-template-name:1",
"instances": [
desired_result = AutoscalerInfo(
template="launch-template-name:1",
instances=[
{
"Instance": "i-xxxxxxxxxxxxxxxxx",
"IP": "192.168.1.13",
Expand All @@ -234,7 +234,7 @@ def test_get_autoscaling_handles_launch_templates(aws: AWS, boto: Mock) -> None:
"Template": "old-launch-template-name:1",
}
],
}
)

result = aws.get_autoscaling_group("production_autoscaling_group_name")

Expand Down
22 changes: 11 additions & 11 deletions tests/unit/services/test_status.py
Expand Up @@ -3,7 +3,7 @@
import pytest

from drode import services
from drode.adapters.aws import AWS
from drode.adapters.aws import AWS, AutoscalerInfo
from drode.config import Config, ConfigError


Expand All @@ -16,9 +16,9 @@ def test_project_status_happy_path(aws: AWS, config: Config) -> None:
result = services.project_status(config, aws)

assert result == {
"Production": {
"template": "launch-config-name",
"instances": [
"Production": AutoscalerInfo(
template="launch-config-name",
instances=[
{
"Instance": "i-xxxxxxxxxxxxxxxxx",
"IP": "192.168.1.13",
Expand All @@ -27,10 +27,10 @@ def test_project_status_happy_path(aws: AWS, config: Config) -> None:
"Template": "old-launch-config-name",
}
],
},
"Staging": {
"template": "launch-config-name",
"instances": [
),
"Staging": AutoscalerInfo(
template="launch-config-name",
instances=[
{
"Instance": "i-xxxxxxxxxxxxxxxxx",
"IP": "192.168.1.13",
Expand All @@ -39,7 +39,7 @@ def test_project_status_happy_path(aws: AWS, config: Config) -> None:
"Template": "old-launch-config-name",
}
],
},
),
}


Expand All @@ -56,8 +56,8 @@ def test_project_status_works_for_undefined_autoscaling_groups(
result = services.project_status(config, aws)

assert result == {
"Production": {},
"Staging": {},
"Production": AutoscalerInfo(instances=[]),
"Staging": AutoscalerInfo(instances=[]),
}


Expand Down

0 comments on commit e44e73c

Please sign in to comment.