moto/moto/ec2/models/__init__.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

239 lines
9.8 KiB
Python
Raw Normal View History

2023-02-13 19:28:15 -01:00
from typing import Any, Dict, List
from moto.core import BaseBackend, BackendDict
from ..exceptions import (
EC2ClientError,
InvalidID,
MissingParameterError,
MotoNotImplementedError,
)
from .amis import AmiBackend
from .carrier_gateways import CarrierGatewayBackend
from .customer_gateways import CustomerGatewayBackend
from .dhcp_options import DHCPOptionsSetBackend
from .elastic_block_store import EBSBackend
from .elastic_ip_addresses import ElasticAddressBackend
from .elastic_network_interfaces import NetworkInterfaceBackend
2023-01-27 15:27:00 -01:00
from .hosts import HostsBackend
2022-08-12 06:33:39 -03:00
from .fleets import FleetsBackend
from .flow_logs import FlowLogsBackend
from .key_pairs import KeyPairBackend
from .launch_templates import LaunchTemplateBackend
from .managed_prefixes import ManagedPrefixListBackend
from .iam_instance_profile import IamInstanceProfileAssociationBackend
from .internet_gateways import (
InternetGatewayBackend,
EgressOnlyInternetGatewayBackend,
)
from .instances import InstanceBackend
from .instance_types import InstanceTypeBackend, InstanceTypeOfferingBackend
from .nat_gateways import NatGatewayBackend
from .network_acls import NetworkAclBackend
from .availability_zones_and_regions import RegionsAndZonesBackend
2023-02-07 22:04:11 -01:00
from .route_tables import RouteBackend
from .security_groups import SecurityGroupBackend
2023-02-11 10:48:22 -01:00
from .spot_requests import SpotRequestBackend
from .subnets import SubnetBackend
from .tags import TagBackend
from .transit_gateway import TransitGatewayBackend
2023-02-12 16:53:36 -01:00
from .transit_gateway_route_tables import TransitGatewayRouteTableBackend
from .transit_gateway_attachments import TransitGatewayAttachmentBackend
from .vpn_gateway import VpnGatewayBackend
from .vpn_connections import VPNConnectionBackend
from .vpcs import VPCBackend
from .vpc_peering_connections import VPCPeeringConnectionBackend
from .vpc_service_configuration import VPCServiceConfigurationBackend
2022-12-22 16:27:19 +05:30
from .windows import WindowsBackend
from ..utils import (
2014-10-05 21:55:23 -04:00
EC2_RESOURCE_TO_PREFIX,
is_valid_resource_id,
get_prefix,
)
2014-10-05 21:55:23 -04:00
2023-02-13 19:28:15 -01:00
def validate_resource_ids(resource_ids: List[str]) -> bool:
if not resource_ids:
raise MissingParameterError(parameter="resourceIdSet")
2014-10-05 21:55:23 -04:00
for resource_id in resource_ids:
if not is_valid_resource_id(resource_id):
raise InvalidID(resource_id=resource_id)
return True
2013-02-18 16:09:40 -05:00
class SettingsBackend:
2023-02-13 19:28:15 -01:00
def __init__(self) -> None:
self.ebs_encryption_by_default = False
2023-02-13 19:28:15 -01:00
def disable_ebs_encryption_by_default(self) -> None:
ec2_backend = ec2_backends[self.account_id][self.region_name] # type: ignore[attr-defined]
ec2_backend.ebs_encryption_by_default = False
2023-02-13 19:28:15 -01:00
def enable_ebs_encryption_by_default(self) -> None:
ec2_backend = ec2_backends[self.account_id][self.region_name] # type: ignore[attr-defined]
ec2_backend.ebs_encryption_by_default = True
2023-02-13 19:28:15 -01:00
def get_ebs_encryption_by_default(self) -> None:
ec2_backend = ec2_backends[self.account_id][self.region_name] # type: ignore[attr-defined]
return ec2_backend.ebs_encryption_by_default
class EC2Backend(
BaseBackend,
InstanceBackend,
2020-11-23 15:25:23 +05:30
InstanceTypeBackend,
InstanceTypeOfferingBackend,
TagBackend,
EBSBackend,
RegionsAndZonesBackend,
AmiBackend,
SecurityGroupBackend,
VPCBackend,
2021-08-14 21:01:06 +05:30
ManagedPrefixListBackend,
SubnetBackend,
FlowLogsBackend,
NetworkInterfaceBackend,
VPNConnectionBackend,
VPCServiceConfigurationBackend,
2014-07-31 14:41:30 -07:00
VPCPeeringConnectionBackend,
RouteBackend,
InternetGatewayBackend,
EgressOnlyInternetGatewayBackend,
2017-02-23 21:37:43 -05:00
SpotRequestBackend,
ElasticAddressBackend,
KeyPairBackend,
SettingsBackend,
DHCPOptionsSetBackend,
NetworkAclBackend,
VpnGatewayBackend,
CustomerGatewayBackend,
NatGatewayBackend,
Merge LocalStack changes into upstream moto (#4082) * fix OPTIONS requests on non-existing API GW integrations * add cloudformation models for API Gateway deployments * bump version * add backdoor to return CloudWatch metrics * Updating implementation coverage * Updating implementation coverage * add cloudformation models for API Gateway deployments * Updating implementation coverage * Updating implementation coverage * Implemented get-caller-identity returning real data depending on the access key used. * bump version * minor fixes * fix Number data_type for SQS message attribute * fix handling of encoding errors * bump version * make CF stack queryable before starting to initialize its resources * bump version * fix integration_method for API GW method integrations * fix undefined status in CF FakeStack * Fix apigateway issues with terraform v0.12.21 * resource_methods -> add handle for "DELETE" method * integrations -> fix issue that "httpMethod" wasn't included in body request (this value was set as the value from refer method resource) * bump version * Fix setting http method for API gateway integrations (#6) * bump version * remove duplicate methods * add storage class to S3 Key when completing multipart upload (#7) * fix SQS performance issues; bump version * add pagination to SecretsManager list-secrets (#9) * fix default parameter groups in RDS * fix adding S3 metadata headers with names containing dots (#13) * Updating implementation coverage * Updating implementation coverage * add cloudformation models for API Gateway deployments * Updating implementation coverage * Updating implementation coverage * Implemented get-caller-identity returning real data depending on the access key used. * make CF stack queryable before starting to initialize its resources * bump version * remove duplicate methods * fix adding S3 metadata headers with names containing dots (#13) * Update amis.json to support EKS AMI mocks (#15) * fix PascalCase for boolean value in ListMultipartUploads response (#17); fix _get_multi_param to parse nested list/dict query params * determine non-zero container exit code in Batch API * support filtering by dimensions in CW get_metric_statistics * fix storing attributes for ELBv2 Route entities; API GW refactorings for TF tests * add missing fields for API GW resources * fix error messages for Route53 (TF-compat) * various fixes for IAM resources (tf-compat) * minor fixes for API GW models (tf-compat) * minor fixes for API GW responses (tf-compat) * add s3 exception for bucket notification filter rule validation * change the way RESTErrors generate the response body and content-type header * fix lint errors and disable "black" syntax enforcement * remove return type hint in RESTError.get_body * add RESTError XML template for IAM exceptions * add support for API GW minimumCompressionSize * fix casing getting PrivateDnsEnabled API GW attribute * minor fixes for error responses * fix escaping special chars for IAM role descriptions (tf-compat) * minor fixes and tagging support for API GW and ELB v2 (tf-compat) * Merge branch 'master' into localstack * add "AlarmRule" attribute to enable support for composite CloudWatch metrics * fix recursive parsing of complex/nested query params * bump version * add API to delete S3 website configurations (#18) * use dict copy to allow parallelism and avoid concurrent modification exceptions in S3 * fix precondition check for etags in S3 (#19) * minor fix for user filtering in Cognito * fix API Gateway error response; avoid returning empty response templates (tf-compat) * support tags and tracingEnabled attribute for API GW stages * fix boolean value in S3 encryption response (#20) * fix connection arn structure * fix api destination arn structure * black format * release 2.0.3.37 * fix s3 exception tests see botocore/parsers.py:1002 where RequestId is removed from parsed * remove python 2 from build action * add test failure annotations in build action * fix events test arn comparisons * fix s3 encryption response test * return default value "0" if EC2 availableIpAddressCount is empty * fix extracting SecurityGroupIds for EC2 VPC endpoints * support deleting/updating API Gateway DomainNames * fix(events): Return empty string instead of null when no pattern is specified in EventPattern (tf-compat) (#22) * fix logic and revert CF changes to get tests running again (#21) * add support for EC2 customer gateway API (#25) * add support for EC2 Transit Gateway APIs (#24) * feat(logs): add `kmsKeyId` into `LogGroup` entity (#23) * minor change in ELBv2 logic to fix tests * feat(events): add APIs to describe and delete CloudWatch Events connections (#26) * add support for EC2 transit gateway route tables (#27) * pass transit gateway route table ID in Describe API, minor refactoring (#29) * add support for EC2 Transit Gateway Routes (#28) * fix region on ACM certificate import (#31) * add support for EC2 transit gateway attachments (#30) * add support for EC2 Transit Gateway VPN attachments (#32) * fix account ID for logs API * add support for DeleteOrganization API * feat(events): store raw filter representation for CloudWatch events patterns (tf-compat) (#36) * feat(events): add support to describe/update/delete CloudWatch API destinations (#35) * add Cognito UpdateIdentityPool, CW Logs PutResourcePolicy * feat(events): add support for tags in EventBus API (#38) * fix parameter validation for Batch compute environments (tf-compat) * revert merge conflicts in IMPLEMENTATION_COVERAGE.md * format code using black * restore original README; re-enable and fix CloudFormation tests * restore tests and old logic for CF stack parameters from SSM * parameterize RequestId/RequestID in response messages and revert related test changes * undo LocalStack-specific adaptations * minor fix * Update CodeCov config to reflect removal of Py2 * undo change related to CW metric filtering; add additional test for CW metric statistics with dimensions * Terraform - Extend whitelist of running tests Co-authored-by: acsbendi <acsbendi28@gmail.com> Co-authored-by: Phan Duong <duongpv@outlook.com> Co-authored-by: Thomas Rausch <thomas@thrau.at> Co-authored-by: Macwan Nevil <macnev2013@gmail.com> Co-authored-by: Dominik Schubert <dominik.schubert91@gmail.com> Co-authored-by: Gonzalo Saad <saad.gonzalo.ale@gmail.com> Co-authored-by: Mohit Alonja <monty16597@users.noreply.github.com> Co-authored-by: Miguel Gagliardo <migag9@gmail.com> Co-authored-by: Bert Blommers <info@bertblommers.nl>
2021-07-26 16:21:17 +02:00
TransitGatewayBackend,
TransitGatewayRouteTableBackend,
TransitGatewayAttachmentBackend,
LaunchTemplateBackend,
IamInstanceProfileAssociationBackend,
CarrierGatewayBackend,
2022-08-12 06:33:39 -03:00
FleetsBackend,
2022-12-22 16:27:19 +05:30
WindowsBackend,
2023-01-27 15:27:00 -01:00
HostsBackend,
):
"""
2022-11-30 20:09:37 -01:00
moto includes a limited set of AMIs in `moto/ec2/resources/amis.json`.
Additionally, the default AMI's specified by SSM will be provided.
2022-11-30 20:09:37 -01:00
If you require specific AMIs to be available during your tests, you can provide your own AMI definitions by setting the
environment variable `MOTO_AMIS_PATH` to point to a JSON file containing definitions of the required AMIs.
No other AMI's will be loaded if this environment variable is set.
To create such a file, refer to `scripts/get_amis.py`
.. note:: You must set `MOTO_AMIS_PATH` before importing moto.
"""
2023-02-13 19:28:15 -01:00
def __init__(self, region_name: str, account_id: str):
BaseBackend.__init__(self, region_name, account_id)
for backend in EC2Backend.__mro__:
if backend not in [EC2Backend, BaseBackend, object]:
2023-02-13 19:28:15 -01:00
backend.__init__(self) # type: ignore
2014-11-23 21:36:19 -05:00
# Default VPC exists by default, which is the current behavior
# of EC2-VPC. See for detail:
#
# docs.aws.amazon.com/AmazonVPC/latest/UserGuide/default-vpc.html
#
if not self.vpcs:
2022-06-10 12:05:10 +00:00
vpc = self.create_default_vpc()
else:
# For now this is included for potential
# backward-compatibility issues
2023-02-13 19:28:15 -01:00
vpc = list(self.vpcs.values())[0]
self.default_vpc = vpc
# Create default subnet for each availability zone
ip, _ = vpc.cidr_block.split("/")
2023-02-13 19:28:15 -01:00
ip = ip.split(".") # type: ignore
ip[2] = 0 # type: ignore
for zone in self.describe_availability_zones():
az_name = zone.name
cidr_block = ".".join(str(i) for i in ip) + "/20"
self.create_subnet(vpc.id, cidr_block, availability_zone=az_name)
2023-02-13 19:28:15 -01:00
ip[2] += 16 # type: ignore
@staticmethod
2023-02-13 19:28:15 -01:00
def default_vpc_endpoint_service(service_region: str, zones: List[str]) -> List[Dict[str, Any]]: # type: ignore[misc]
"""Default VPC endpoint service."""
return BaseBackend.default_vpc_endpoint_service_factory(
service_region, zones, "ec2"
) + BaseBackend.default_vpc_endpoint_service_factory(
service_region, zones, "ec2messages"
)
2017-02-23 21:37:43 -05:00
# Use this to generate a proper error template response when in a response
# handler.
2023-02-13 19:28:15 -01:00
def raise_error(self, code: str, message: str) -> None:
raise EC2ClientError(code, message)
2013-02-20 23:19:43 -05:00
2023-02-13 19:28:15 -01:00
def raise_not_implemented_error(self, blurb: str) -> None:
raise MotoNotImplementedError(blurb)
2023-02-13 19:28:15 -01:00
def do_resources_exist(self, resource_ids: List[str]) -> bool:
2014-10-05 21:55:23 -04:00
for resource_id in resource_ids:
resource_prefix = get_prefix(resource_id)
if resource_prefix == EC2_RESOURCE_TO_PREFIX["customer-gateway"]:
self.get_customer_gateway(customer_gateway_id=resource_id)
2014-10-05 21:55:23 -04:00
elif resource_prefix == EC2_RESOURCE_TO_PREFIX["dhcp-options"]:
self.describe_dhcp_options(dhcp_options_ids=[resource_id])
2014-10-05 21:55:23 -04:00
elif resource_prefix == EC2_RESOURCE_TO_PREFIX["image"]:
self.describe_images(ami_ids=[resource_id])
elif resource_prefix == EC2_RESOURCE_TO_PREFIX["instance"]:
self.get_instance_by_id(instance_id=resource_id)
elif resource_prefix == EC2_RESOURCE_TO_PREFIX["internet-gateway"]:
2017-02-23 21:37:43 -05:00
self.describe_internet_gateways(internet_gateway_ids=[resource_id])
elif resource_prefix == EC2_RESOURCE_TO_PREFIX["launch-template"]:
self.get_launch_template(resource_id)
2014-10-05 21:55:23 -04:00
elif resource_prefix == EC2_RESOURCE_TO_PREFIX["network-acl"]:
2023-02-06 20:57:02 -01:00
self.describe_network_acls()
2014-10-05 21:55:23 -04:00
elif resource_prefix == EC2_RESOURCE_TO_PREFIX["network-interface"]:
2017-02-23 21:37:43 -05:00
self.describe_network_interfaces(
filters={"network-interface-id": resource_id}
)
2014-10-05 21:55:23 -04:00
elif resource_prefix == EC2_RESOURCE_TO_PREFIX["reserved-instance"]:
self.raise_not_implemented_error("DescribeReservedInstances")
elif resource_prefix == EC2_RESOURCE_TO_PREFIX["route-table"]:
2014-11-20 14:02:36 -06:00
self.get_route_table(route_table_id=resource_id)
2014-10-05 21:55:23 -04:00
elif resource_prefix == EC2_RESOURCE_TO_PREFIX["security-group"]:
self.describe_security_groups(group_ids=[resource_id])
elif resource_prefix == EC2_RESOURCE_TO_PREFIX["snapshot"]:
self.get_snapshot(snapshot_id=resource_id)
elif resource_prefix == EC2_RESOURCE_TO_PREFIX["spot-instance-request"]:
2017-02-23 21:37:43 -05:00
self.describe_spot_instance_requests(
filters={"spot-instance-request-id": resource_id}
)
2014-10-05 21:55:23 -04:00
elif resource_prefix == EC2_RESOURCE_TO_PREFIX["subnet"]:
self.get_subnet(subnet_id=resource_id)
elif resource_prefix == EC2_RESOURCE_TO_PREFIX["volume"]:
self.get_volume(volume_id=resource_id)
elif resource_prefix == EC2_RESOURCE_TO_PREFIX["vpc"]:
self.get_vpc(vpc_id=resource_id)
elif resource_prefix == EC2_RESOURCE_TO_PREFIX["vpc-endpoint-service"]:
self.get_vpc_endpoint_service(resource_id)
2014-10-05 21:55:23 -04:00
elif resource_prefix == EC2_RESOURCE_TO_PREFIX["vpc-peering-connection"]:
self.get_vpc_peering_connection(vpc_pcx_id=resource_id)
elif resource_prefix == EC2_RESOURCE_TO_PREFIX["vpn-connection"]:
self.describe_vpn_connections(vpn_connection_ids=[resource_id])
2014-10-05 21:55:23 -04:00
elif resource_prefix == EC2_RESOURCE_TO_PREFIX["vpn-gateway"]:
2014-12-02 10:28:09 -06:00
self.get_vpn_gateway(vpn_gateway_id=resource_id)
elif (
resource_prefix
== EC2_RESOURCE_TO_PREFIX["iam-instance-profile-association"]
):
self.describe_iam_instance_profile_associations(
association_ids=[resource_id]
)
2014-10-05 21:55:23 -04:00
return True
2013-02-20 23:19:43 -05:00
2017-02-23 21:37:43 -05:00
2021-12-24 20:02:45 -01:00
ec2_backends = BackendDict(EC2Backend, "ec2")