Skip to content

Commit

Permalink
Results of running ruff --fix
Browse files Browse the repository at this point in the history
  • Loading branch information
VirginiaDooley committed Aug 1, 2023
1 parent ac08828 commit 92d7cae
Show file tree
Hide file tree
Showing 163 changed files with 505 additions and 544 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
Powers off servers when called.
"""
import random
import time

import boto3

Expand All @@ -19,10 +17,9 @@ def reduce_alb_size(tag_name, tag_value, at_most=0, region="eu-west-2"):
for asg in asg_list:
name = asg["AutoScalingGroupName"]
for tag in asg["Tags"]:
if tag["Key"] == "dc-environment":
if tag["Value"] == "production":
print(f"Not touching {name} as it's a production ASG")
continue
if tag["Key"] == "dc-environment" and tag["Value"] == "production":
print(f"Not touching {name} as it's a production ASG")
continue
print(f"Updating {name} to MinSize={at_most} DesiredCapacity={at_most}")
client.update_auto_scaling_group(
AutoScalingGroupName=name,
Expand Down
28 changes: 13 additions & 15 deletions cdk_stacks/stacks/code_deploy.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
from aws_cdk.aws_cloudfront import PriceClass
from aws_cdk.core import Stack, Construct, Duration

import aws_cdk.aws_ec2 as ec2
import aws_cdk.aws_iam as iam
import aws_cdk.aws_elasticloadbalancingv2 as elbv2
from aws_cdk.aws_ssm import StringParameter
import aws_cdk.aws_codedeploy as codedeploy
import aws_cdk.aws_certificatemanager as acm
import aws_cdk.aws_cloudfront as cloudfront
import aws_cdk.aws_cloudfront_origins as origins
import aws_cdk.aws_certificatemanager as acm
import aws_cdk.aws_codedeploy as codedeploy
import aws_cdk.aws_ec2 as ec2
import aws_cdk.aws_elasticloadbalancingv2 as elbv2
import aws_cdk.aws_iam as iam
import aws_cdk.aws_route53 as route_53
import aws_cdk.aws_route53_targets as route_53_target
from aws_cdk.aws_cloudfront import PriceClass
from aws_cdk.aws_ssm import StringParameter
from aws_cdk.core import Construct, Duration, Stack

from cdk_stacks.stacks.code_deploy_policies import (
EE_DEPLOYER_POLICY,
EE_CODE_DEPLOY_POLICY,
EE_CODE_DEPLOY_EC2_POLICY,
EE_CODE_DEPLOY_LAUNCH_TEMPLATE_POLICY,
EE_CODE_DEPLOY_POLICY,
EE_DEPLOYER_POLICY,
)

EE_IMAGE = "ami-014a706cddda2a79a"
Expand Down Expand Up @@ -63,7 +62,7 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
self.cloudfront = self.create_cloudfront(self.alb)

def create_code_deploy(self):
application = codedeploy.ServerApplication(
codedeploy.ServerApplication(
self, "CodeDeployApplicationID", application_name="EECodeDeploy"
)

Expand All @@ -73,7 +72,7 @@ def create_launch_template(
security_group: ec2.SecurityGroup,
role: iam.Role,
) -> ec2.LaunchTemplate:
lt = ec2.LaunchTemplate(
return ec2.LaunchTemplate(
self,
"ee-launch-template-id",
instance_type=ec2.InstanceType("t3a.medium"),
Expand All @@ -94,7 +93,6 @@ def create_launch_template(
)
],
)
return lt

def create_target_group(self):
return elbv2.ApplicationTargetGroup(
Expand Down Expand Up @@ -390,7 +388,7 @@ def create_cloudfront(self, alb: elbv2.ApplicationLoadBalancer):
hosted_zone = route_53.HostedZone.from_lookup(
self, "EEDomain", domain_name=fqdn, private_zone=False
)
a_record = route_53.ARecord(
route_53.ARecord(
self,
"FQDN_A_RECORD_TO_CF",
zone=hosted_zone,
Expand Down
2 changes: 1 addition & 1 deletion cdk_stacks/stacks/command_runner.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from aws_cdk import (
aws_events,
aws_events_targets,
aws_iam,
aws_lambda,
aws_lambda_python,
aws_iam,
core,
)
from aws_cdk.core import Construct, Stack
Expand Down
8 changes: 3 additions & 5 deletions cdk_stacks/stacks/image_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
import re
from pathlib import Path

import yaml
from aws_cdk.core import Stack, Construct
import aws_cdk.aws_iam as iam
import aws_cdk.aws_imagebuilder as image_builder
import yaml
from aws_cdk.aws_ssm import StringParameter

from aws_cdk.core import Construct, Stack

COMPONENTS = [
{
Expand Down Expand Up @@ -157,13 +156,12 @@ def make_instance_profile(self):
)

# create an instance profile to attach the role
instanceprofile = iam.CfnInstanceProfile(
return iam.CfnInstanceProfile(
self,
"EEImageInstanceProfile",
instance_profile_name="EEImageInstanceProfile",
roles=["EEImageRole"],
)
return instanceprofile

def make_infra_config(self) -> image_builder.CfnInfrastructureConfiguration:
"""
Expand Down
2 changes: 1 addition & 1 deletion cdk_stacks/stacks/power_off_at_end_of_day.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from aws_cdk import (
aws_events,
aws_events_targets,
aws_iam,
aws_lambda,
aws_lambda_python,
aws_iam,
core,
)
from aws_cdk.core import Construct, Stack
Expand Down
3 changes: 2 additions & 1 deletion deployscripts/create_deployment.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import boto3
import time

import boto3

session = boto3.Session(region_name=os.environ.get("AWS_REGION"))


Expand Down
6 changes: 4 additions & 2 deletions deployscripts/create_deployment_group.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import time

import boto3
from botocore.exceptions import ClientError
import time

session = boto3.Session()

Expand Down Expand Up @@ -92,7 +93,7 @@ def create_default_asg():
for asg in client.describe_auto_scaling_groups()["AutoScalingGroups"]
]
if "default" in existing_asgs:
return
return None
return client.create_auto_scaling_group(
AutoScalingGroupName="default",
AvailabilityZones=[
Expand Down Expand Up @@ -199,6 +200,7 @@ def main():
# step as the instance needs to have initialised and be in ready state
# before code deploy can create a start deployment
time.sleep(60)
return None


if __name__ == "__main__":
Expand Down
11 changes: 3 additions & 8 deletions docs/aws-resource-diagram.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
from diagrams import Cluster, Diagram
from diagrams.aws.compute import (
Lambda,
EC2AutoScaling,
EC2Instances,
EC2Ami,
EC2AutoScaling,
EC2ImageBuilder,
EC2Instances,
)
from diagrams.aws.database import RDS
from diagrams.aws.devtools import Codedeploy
from diagrams.aws.general import InternetAlt1
from diagrams.aws.management import Config, Organizations
from diagrams.aws.network import ALB, Route53, CloudFront
from diagrams.aws.network import ALB, CloudFront, Route53
from diagrams.aws.security import CertificateManager
from diagrams.aws.storage import S3
from diagrams.generic.network import Subnet
from diagrams.onprem.database import PostgreSQL
from diagrams.programming.framework import Django

with Diagram(
"AWS deployment resources", filename="docs/aws-resources", show=False
Expand Down
1 change: 0 additions & 1 deletion every_election/apps/api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from django.core.exceptions import ValidationError
from django.db.models import Q
from django.utils.timezone import now

from elections.models import Election
from organisations.models import OrganisationGeography

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import contextlib
import os
import os.path
import subprocess
from datetime import datetime

import geojson

from django.conf import settings
from django.core.management.base import BaseCommand
from elections.models import Election
Expand Down Expand Up @@ -53,7 +53,7 @@ def add_arguments(self, parser):
)

def handle(self, *args, **options):
try:
with contextlib.suppress(FileExistsError):
os.mkdir(options["output"])

if not (options["from"] or options["to"]):
Expand Down
24 changes: 16 additions & 8 deletions every_election/apps/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
from rest_framework import serializers
from rest_framework_gis.serializers import (
GeoFeatureModelSerializer,
GeometrySerializerMethodField,
)
from uk_election_ids.datapackage import VOTING_SYSTEMS

from elections.models import (
Election,
ElectionType,
ElectionSubType,
ElectionType,
ModerationStatuses,
)
from organisations.models import (
Organisation,
OrganisationDivision,
OrganisationDivisionSet,
)
from rest_framework import serializers
from rest_framework_gis.serializers import (
GeoFeatureModelSerializer,
GeometrySerializerMethodField,
)
from uk_election_ids.datapackage import VOTING_SYSTEMS


class OrganisationHyperlinkedIdentityField(
Expand Down Expand Up @@ -256,3 +255,12 @@ class Meta:

fields = election_fields
depth = 1
model = Election
extra_kwargs = {
"url": {"view_name": "election-geo", "lookup_field": "pk"}
}

geo_field = "geography_model"

fields = election_fields
depth = 1
23 changes: 11 additions & 12 deletions every_election/apps/api/tests/test_api_election_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import json
import pytest
from datetime import datetime, timedelta
from urllib.parse import urlencode

import pytest
import vcr
from rest_framework.test import APITestCase

from elections.models import ElectionType, MetaData
from elections.tests.factories import (
ElectionWithStatusFactory,
ModerationHistoryFactory,
ModerationStatusFactory,
related_status,
)
from organisations.tests.factories import (
OrganisationFactory,
OrganisationDivisionSetFactory,
DivisionGeographyFactory,
OrganisationDivisionFactory,
OrganisationDivisionSetFactory,
OrganisationFactory,
OrganisationGeographyFactory,
DivisionGeographyFactory,
)
from elections.models import ElectionType, MetaData
from rest_framework.test import APITestCase


class TestElectionAPIQueries(APITestCase):
Expand Down Expand Up @@ -49,7 +48,7 @@ def test_election_endpoint_current(self):

assert len(data["results"]) == 1
assert data["results"][0]["election_id"] == id_current
assert data["results"][0]["current"] == True
assert data["results"][0]["current"] is True

def test_election_endpoint_future(self):
ElectionWithStatusFactory(
Expand Down Expand Up @@ -561,8 +560,8 @@ def test_election_intersects_local_authority_filter(self):
)
data = resp.json()
self.assertSetEqual(
set(["overlaps", "same", "contains", "within"]),
set([e["election_title"] for e in data["results"]]),
{"overlaps", "same", "contains", "within"},
{e["election_title"] for e in data["results"]},
)

# BIG_TEST1 has an area bigger than 2. So should hoover up everything
Expand All @@ -572,6 +571,6 @@ def test_election_intersects_local_authority_filter(self):
)
data = resp.json()
self.assertSetEqual(
set(["overlaps", "same", "contains", "within"]),
set([e["election_title"] for e in data["results"]]),
{"overlaps", "same", "contains", "within"},
{e["election_title"] for e in data["results"]},
)
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from datetime import date
from rest_framework.test import APITestCase

from organisations.tests.factories import (
OrganisationFactory,
OrganisationGeographyFactory,
)
from rest_framework.test import APITestCase


class TestOrganisationAPIEndpoint(APITestCase):
Expand Down
Loading

0 comments on commit 92d7cae

Please sign in to comment.