Ejemplo n.º 1
0
    def init_horey_cached_type(self, attr_name, value):
        """
        Init automatically cached values
        @param attr_name:
        @param value: {self.SELF_CACHED_TYPE_KEY_NAME: datetime/region/ip..., "value": value_to_init}
        @return:
        """

        if value.get(self.SELF_CACHED_TYPE_KEY_NAME) == "datetime":
            # Example: datetime.datetime.strptime('2017-07-26 15:54:10.000000+0000', '%Y-%m-%d %H:%M:%S.%f%z')
            new_value = datetime.datetime.strptime(value["value"], "%Y-%m-%d %H:%M:%S.%f%z")
        elif value.get(self.SELF_CACHED_TYPE_KEY_NAME) == "ip":
            new_value = IP(value["value"], from_dict=True)
        elif value.get(self.SELF_CACHED_TYPE_KEY_NAME) == "region":
            inited_region = Region()
            inited_region.init_from_dict(value["value"])

            new_value = Region.get_region(inited_region.region_mark)
            if inited_region.region_name is not None:
                if new_value.region_name is not None:
                    if new_value.region_name != inited_region.region_name:
                        raise ValueError(f"{new_value.region_name} != {inited_region.region_name}")
                else:
                    new_value.region_name = inited_region.region_name
        else:
            raise ValueError(f"{attr_name} : {value}")

        self.init_default_attr(attr_name, new_value)
Ejemplo n.º 2
0
def test_provision_s3_bucket():
    region = Region.get_region("us-west-2")
    s3_client = S3Client()

    s3_bucket = S3Bucket({})
    s3_bucket.region = region
    s3_bucket.name = TEST_BUCKET_NAME
    s3_bucket.acl = "private"

    s3_bucket.policy = S3Bucket.Policy({})
    s3_bucket.policy.version = "2012-10-17"
    s3_bucket.policy.statement = [{
        "Sid":
        "AllowReadAny",
        "Effect":
        "Allow",
        "Principal":
        "*",
        "Action":
        "s3:GetObject",
        "Resource":
        f"arn:aws:s3:::{s3_bucket.name}/*"
    }]

    s3_client.provision_bucket(s3_bucket)
Ejemplo n.º 3
0
    def region(self):
        if self._region is not None:
            return self._region

        if self.arn is not None:
            self._region = Region.get_region(self.arn.split(":")[3])

        return self._region
Ejemplo n.º 4
0
    def region(self):
        if self._region is not None:
            return self._region

        raise NotImplementedError()
        if self.arn is not None:
            self._region = Region.get_region(self.arn.split(":")[3])

        return self._region
Ejemplo n.º 5
0
def test_copy_db_cluster_snapshot():
    client = RDSClient()
    snapshot_src = RDSDBClusterSnapshot({})
    snapshot_src.region = Region.get_region("us-east-1")
    snapshot_src.db_cluster_identifier = mock_values[
        "snapshot_src.db_cluster_identifier"]

    snapshot_dst = RDSDBClusterSnapshot({})
    snapshot_dst.region = Region.get_region("us-west-2")
    snapshot_dst.id = "horey-test-snapshot-id"
    snapshot_dst.tags = [{
        'Key': 'lvl',
        'Value': "tst"
    }, {
        'Key': 'name',
        'Value': snapshot_dst.id
    }]
    client.copy_db_cluster_snapshot(snapshot_src, snapshot_dst)
Ejemplo n.º 6
0
def test_provision_lambda_event_source_mapping():
    event_mapping = LambdaEventSourceMapping({})
    event_mapping.region = Region.get_region("us-west-2")
    event_mapping.function_identification = "horey-test-lambda"
    event_mapping.event_source_arn = mock_values[
        "lambda_event_source_mapping:event_source_arn"]
    event_mapping.enabled = True

    aws_api.provision_lambda_event_source_mapping(event_mapping)

    assert event_mapping.state == "Enabled"
Ejemplo n.º 7
0
    def set_aws_region(value):
        """
        Set current region to work against.
        :return:
        """
        if isinstance(value, str):
            value = Region.get_region(value)

        if not isinstance(value, Region):
            raise ValueError(f"{value} is not of type Region")

        AWSAccount._CURRENT_REGION = value
Ejemplo n.º 8
0
    def init_role_last_used_attr(self, _, dict_src):
        """
        Init RoleLastUsed - split to time and region

        @param _:
        @param dict_src:
        @return:
        """
        if not dict_src:
            return
        for key in dict_src:
            if key == "LastUsedDate":
                self.role_last_used_time = dict_src.get(key)
            elif key == "Region":
                self.role_last_used_region = Region.get_region(dict_src.get(key))
            else:
                raise NotImplementedError(key)
Ejemplo n.º 9
0
def test_provision_certificate():
    cert = ACMCertificate({})
    cert.region = Region.get_region("us-east-1")
    cert.domain_name = "front.horey.com"
    cert.validation_method = "DNS"
    cert.tags = [{
        'Key': 'lvl',
        'Value': "tst"
    }, {
        'Key': 'name',
        'Value': cert.domain_name.replace("*", "star")
    }]

    hosted_zone_name = "horey.com"
    aws_api.provision_acm_certificate(cert, hosted_zone_name)

    assert cert.status == "ISSUED"
Ejemplo n.º 10
0
def test_provision_aws_lambda_from_filelist():
    aws_lambda = AWSLambda({})
    aws_lambda.region = Region.get_region("us-west-2")
    aws_lambda.name = "horey-test-lambda"
    aws_lambda.role = mock_values["lambda:execution_role"]
    aws_lambda.handler = "lambda_test.lambda_handler"
    aws_lambda.runtime = "python3.8"
    aws_lambda.tags = {"lvl": "tst", "name": "horey-test"}

    files_paths = [
        os.path.join(os.path.dirname(os.path.abspath(__file__)), filename)
        for filename in ["lambda_test.py", "lambda_test_2.py"]
    ]
    aws_api.provision_aws_lambda_from_filelist(aws_lambda,
                                               files_paths,
                                               force=True)

    assert aws_lambda.state == "Active"
Ejemplo n.º 11
0
def test_provision_lambda():
    packer = Packer()
    aws_api = AWSAPI()

    files_paths = [
        os.path.join(os.path.dirname(os.path.abspath(__file__)), "build",
                     "files_list_test", file_name)
        for file_name in ["dependency_1.py", "entrypoint.py"]
    ]
    packer.add_files_to_zip(f"{ZIP_FILE_NAME}.zip", files_paths)
    aws_lambda = AWSLambda({})
    aws_lambda.region = Region.get_region("us-west-2")
    aws_lambda.name = "horey-test-lambda"
    aws_lambda.role = mock_values["lambda:execution_role"]
    aws_lambda.handler = "entrypoint.main"
    aws_lambda.runtime = "python3.8"
    aws_lambda.tags = {"lvl": "tst", "name": "horey-test"}
    aws_lambda.policy = {
        "Version":
        "2012-10-17",
        "Id":
        "default",
        "Statement": [{
            "Sid": aws_lambda.name + "_" + "sid",
            "Effect": "Allow",
            "Principal": {
                "Service": "events.amazonaws.com"
            },
            "Action": "lambda:InvokeFunction",
            "Resource": None,
            "Condition": {
                "ArnLike": {
                    "AWS:SourceArn":
                    mock_values["lambda:policy_events_rule_arn"]
                }
            }
        }]
    }

    aws_api.provision_aws_lambda(aws_lambda, force=True)

    assert aws_lambda.state == "Active"
Ejemplo n.º 12
0
def test_provision_rule():
    rule = EventBridgeRule({})
    rule.name = "rule-alexey-test-trigger-lambda"
    rule.description = "rule-alexey-test-trigger-lambda"
    rule.region = Region.get_region("us-west-2")
    rule.schedule_expression = "rate(1 minute)"
    rule.event_bus_name = "default"
    rule.state = "ENABLED"
    rule.tags = [
        {
            'Key': 'string',
            'Value': 'string'
        },
    ]

    target = EventBridgeTarget({})
    target.id = "test-alexey-target"
    target.arn = "arn:aws:lambda:us-west-2:xxxxxx:function:horey-test-lambda"

    rule.targets = [target]
    client = EventsClient()
    client.provision_rule(rule)
Ejemplo n.º 13
0
        def __init__(self, dict_src):
            self.aws_access_key_id = None
            self.aws_secret_access_key = None
            self.profile_name = None
            self.role_arn = None
            self.region = None
            self.type = None
            self.external_id = None

            if "region_mark" in dict_src:
                self.region = Region.get_region(dict_src["region_mark"])

            if "credentials" in dict_src:
                raise NotImplementedError()

            if "profile" in dict_src:
                logger.info(
                    f"Setting connection step type to AWSAccount.ConnectionStep.Type.PROFILE: {dict_src}"
                )
                self.type = AWSAccount.ConnectionStep.Type.PROFILE
                self.profile_name = dict_src["profile"]
            elif "assume_role" in dict_src:
                self.type = AWSAccount.ConnectionStep.Type.ASSUME_ROLE
                self.role_arn = dict_src["assume_role"]
            elif "role" in dict_src:
                if dict_src["role"] != "current":
                    raise ValueError(dict_src["role"])
                logger.info(
                    f"Setting connection step type to AWSAccount.ConnectionStep.Type.CURRENT_ROLE: {dict_src}"
                )
                self.type = AWSAccount.ConnectionStep.Type.CURRENT_ROLE
            else:
                raise NotImplementedError(f"Unknown {dict_src}")

            if "external_id" in dict_src:
                self.external_id = dict_src["external_id"]
Ejemplo n.º 14
0
 def get_cluster_from_arn(cluster_arn):
     cluster = ECSCluster({})
     cluster.name = cluster_arn.split(":")[-1].split("/")[-1]
     cluster.arn = cluster_arn
     cluster.region = Region.get_region(cluster_arn.split(":")[3])
     return cluster
Ejemplo n.º 15
0
def test_add_managed_region():
    region = Region.get_region("us-west-2")
    aws_api.add_managed_region(region)
    assert region in aws_api.get_managed_regions()
Ejemplo n.º 16
0
import pdb

from horey.aws_api.aws_clients.events_client import EventsClient
from horey.aws_api.aws_services_entities.event_bridge_rule import EventBridgeRule
from horey.aws_api.aws_services_entities.event_bridge_target import EventBridgeTarget
from horey.aws_api.base_entities.aws_account import AWSAccount
from horey.aws_api.base_entities.region import Region

AWSAccount.set_aws_region(Region.get_region("us-west-2"))


def test_init_lambda_client():
    assert isinstance(EventsClient(), EventsClient)


def test_get_region_events():
    events_client = EventsClient()
    region_rules = events_client.get_region_rules(
        Region.get_region("us-east-1"))
    assert isinstance(region_rules, list)


def test_provision_rule():
    rule = EventBridgeRule({})
    rule.name = "rule-alexey-test-trigger-lambda"
    rule.description = "rule-alexey-test-trigger-lambda"
    rule.region = Region.get_region("us-west-2")
    rule.schedule_expression = "rate(1 minute)"
    rule.event_bus_name = "default"
    rule.state = "ENABLED"
    rule.tags = [
Ejemplo n.º 17
0
def test_add_managed_region():
    aws_api.add_managed_region(Region.get_region("us-west-2"))
Ejemplo n.º 18
0
def test_get_region_events():
    events_client = EventsClient()
    region_rules = events_client.get_region_rules(
        Region.get_region("us-east-1"))
    assert isinstance(region_rules, list)
Ejemplo n.º 19
0
import datetime
import json
import os
import pdb

from unittest.mock import Mock
from horey.aws_api.aws_clients.s3_client import S3Client
from horey.aws_api.aws_services_entities.s3_bucket import S3Bucket
from horey.aws_api.base_entities.aws_account import AWSAccount
from horey.aws_api.base_entities.region import Region

AWSAccount.set_aws_region(Region.get_region('us-west-2'))

TEST_BUCKET_NAME = "horey-test-bucket"


def test_init_s3_client():
    assert isinstance(S3Client(), S3Client)


def test_provision_s3_bucket():
    region = Region.get_region("us-west-2")
    s3_client = S3Client()

    s3_bucket = S3Bucket({})
    s3_bucket.region = region
    s3_bucket.name = TEST_BUCKET_NAME
    s3_bucket.acl = "private"

    s3_bucket.policy = S3Bucket.Policy({})
    s3_bucket.policy.version = "2012-10-17"
Ejemplo n.º 20
0
def test_get_region_lambdas():
    lambda_client = LambdaClient()
    lambdas = lambda_client.get_region_lambdas(
        Region.get_region("eu-central-1"))
    assert isinstance(lambdas, list)
Ejemplo n.º 21
0
import pdb

from horey.aws_api.aws_clients.lambda_client import LambdaClient
from horey.aws_api.aws_services_entities.aws_lambda import AWSLambda
from horey.aws_api.base_entities.aws_account import AWSAccount
from horey.aws_api.base_entities.region import Region

AWSAccount.set_aws_region(Region.get_region("eu-central-1"))


def test_init_lambda_client():
    assert isinstance(LambdaClient(), LambdaClient)


def test_get_region_lambdas():
    lambda_client = LambdaClient()
    lambdas = lambda_client.get_region_lambdas(
        Region.get_region("eu-central-1"))
    assert isinstance(lambdas, list)


if __name__ == "__main__":
    # test_init_lambda_client()
    test_get_region_lambdas()
    #test_provision_lambda()