EC2 - Implement DryRun-flag on various operations (#4420)

This commit is contained in:
Bert Blommers 2021-10-15 22:43:00 +00:00 committed by GitHub
parent c62bd5ca41
commit deeabfc6e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 122 additions and 2 deletions

View File

@ -802,7 +802,11 @@ class BaseResponse(_TemplateEnvironmentMixin, ActionAuthenticatorMixin):
def request_json(self):
return "JSON" in self.querystring.get("ContentType", [])
def is_not_dryrun(self, action):
def error_on_dryrun(self):
self.is_not_dryrun()
def is_not_dryrun(self, action=None):
action = action or self._get_param("Action")
if "true" in self.querystring.get("DryRun", ["false"]):
message = (
"An error occurred (DryRunOperation) when calling the %s operation: Request would have succeeded, but DryRun flag is set"

View File

@ -40,6 +40,7 @@ class AmisResponse(BaseResponse):
return template.render(success=str(success).lower())
def describe_images(self):
self.error_on_dryrun()
ami_ids = self._get_multi_param("ImageId")
filters = filters_from_querystring(self.querystring)
owners = self._get_multi_param("Owner")

View File

@ -4,11 +4,13 @@ from moto.core.responses import BaseResponse
class AvailabilityZonesAndRegions(BaseResponse):
def describe_availability_zones(self):
self.error_on_dryrun()
zones = self.ec2_backend.describe_availability_zones()
template = self.response_template(DESCRIBE_ZONES_RESPONSE)
return template.render(zones=zones)
def describe_regions(self):
self.error_on_dryrun()
region_names = self._get_multi_param("RegionName")
regions = self.ec2_backend.describe_regions(region_names)
template = self.response_template(DESCRIBE_REGIONS_RESPONSE)

View File

@ -26,6 +26,7 @@ class CustomerGateways(BaseResponse):
return template.render(delete_status=delete_status)
def describe_customer_gateways(self):
self.error_on_dryrun()
filters = filters_from_querystring(self.querystring)
customer_gateway_ids = self._get_multi_param("CustomerGatewayId")
customer_gateways = self.ec2_backend.get_all_customer_gateways(

View File

@ -70,6 +70,7 @@ class ElasticIPAddresses(BaseResponse):
return template.render(address=eip)
def describe_addresses(self):
self.error_on_dryrun()
allocation_ids = self._get_multi_param("AllocationId")
public_ips = self._get_multi_param("PublicIp")
filters = filters_from_querystring(self.querystring)

View File

@ -16,6 +16,7 @@ from copy import deepcopy
class InstanceResponse(BaseResponse):
def describe_instances(self):
self.error_on_dryrun()
filter_dict = filters_from_querystring(self.querystring)
instance_ids = self._get_multi_param("InstanceId")
token = self._get_param("NextToken")

View File

@ -39,6 +39,7 @@ class Subnets(BaseResponse):
return template.render(subnet=subnet)
def describe_subnets(self):
self.error_on_dryrun()
subnet_ids = self._get_multi_param("SubnetId")
filters = filters_from_querystring(self.querystring)
subnets = self.ec2_backend.get_all_subnets(subnet_ids, filters)

View File

@ -40,6 +40,7 @@ class VPCs(BaseResponse):
return template.render(vpc=vpc)
def describe_vpcs(self):
self.error_on_dryrun()
vpc_ids = self._get_multi_param("VpcId")
filters = filters_from_querystring(self.querystring)
vpcs = self.ec2_backend.describe_vpcs(vpc_ids=vpc_ids, filters=filters)

View File

@ -1761,3 +1761,16 @@ def test_ami_filter_by_unknown_ownerid():
Filters=[{"Name": "owner-alias", "Values": ["unknown",]},]
)["Images"]
images.should.have.length_of(0)
@mock_ec2
def test_describe_images_dryrun():
client = boto3.client("ec2", region_name="us-east-1")
with pytest.raises(ClientError) as ex:
client.describe_images(DryRun=True)
ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(412)
ex.value.response["Error"]["Code"].should.equal("DryRunOperation")
ex.value.response["Error"]["Message"].should.equal(
"An error occurred (DryRunOperation) when calling the DescribeImages operation: Request would have succeeded, but DryRun flag is set"
)

View File

@ -2,8 +2,10 @@ from __future__ import unicode_literals
import boto
import boto.ec2
import boto3
import pytest
import sure # noqa
from botocore.exceptions import ClientError
from moto import mock_ec2, mock_ec2_deprecated
@ -65,6 +67,19 @@ def test_boto3_availability_zones():
rec["ZoneName"].should.contain(region)
@mock_ec2
def test_describe_availability_zones_dryrun():
client = boto3.client("ec2", region_name="us-east-1")
with pytest.raises(ClientError) as ex:
client.describe_availability_zones(DryRun=True)
ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(412)
ex.value.response["Error"]["Code"].should.equal("DryRunOperation")
ex.value.response["Error"]["Message"].should.equal(
"An error occurred (DryRunOperation) when calling the DescribeAvailabilityZones operation: Request would have succeeded, but DryRun flag is set"
)
@mock_ec2
def test_boto3_zoneId_in_availability_zones():
conn = boto3.client("ec2", "us-east-1")

View File

@ -43,6 +43,19 @@ def test_describe_customer_gateways():
cgws[0].id.should.match(customer_gateway.id)
@mock_ec2
def test_describe_customer_gateways_dryrun():
client = boto3.client("ec2", region_name="us-east-1")
with pytest.raises(ClientError) as ex:
client.describe_customer_gateways(DryRun=True)
ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(412)
ex.value.response["Error"]["Code"].should.equal("DryRunOperation")
ex.value.response["Error"]["Message"].should.equal(
"An error occurred (DryRunOperation) when calling the DescribeCustomerGateways operation: Request would have succeeded, but DryRun flag is set"
)
@mock_ec2
def test_describe_customer_gateways_boto3():
ec2 = boto3.client("ec2", region_name="us-east-1")

View File

@ -106,6 +106,19 @@ def test_eip_allocate_vpc():
vpc.release()
@mock_ec2
def test_describe_addresses_dryrun():
client = boto3.client("ec2", region_name="us-east-1")
with pytest.raises(ClientError) as ex:
client.describe_addresses(DryRun=True)
ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(412)
ex.value.response["Error"]["Code"].should.equal("DryRunOperation")
ex.value.response["Error"]["Message"].should.equal(
"An error occurred (DryRunOperation) when calling the DescribeAddresses operation: Request would have succeeded, but DryRun flag is set"
)
@mock_ec2
def test_eip_allocate_vpc_boto3():
"""Allocate/release VPC EIP"""

View File

@ -3096,6 +3096,19 @@ def test_run_instance_and_associate_public_ip():
addresses["Association"].should.have.key("PublicIp")
@mock_ec2
def test_describe_instances_dryrun():
client = boto3.client("ec2", region_name="us-east-1")
with pytest.raises(ClientError) as ex:
client.describe_instances(DryRun=True)
ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(412)
ex.value.response["Error"]["Code"].should.equal("DryRunOperation")
ex.value.response["Error"]["Message"].should.equal(
"An error occurred (DryRunOperation) when calling the DescribeInstances operation: Request would have succeeded, but DryRun flag is set"
)
def retrieve_all_reservations(client, filters=[]):
resp = client.describe_instances(Filters=filters)
all_reservations = resp["Reservations"]

View File

@ -3,9 +3,11 @@ import boto.ec2
import boto.ec2.autoscale
import boto.ec2.elb
import boto3
import sure
import pytest
import sure # noqa
from boto3 import Session
from botocore.exceptions import ClientError
from moto import mock_ec2_deprecated, mock_autoscaling_deprecated, mock_elb_deprecated
from moto import mock_autoscaling, mock_ec2, mock_elb
@ -283,3 +285,16 @@ def test_create_autoscaling_group_boto3():
group["LoadBalancerNames"].should.equal([lb_name])
group["PlacementGroup"].should.equal("us_test_placement")
group["TerminationPolicies"].should.equal(["OldestInstance", "NewestInstance"])
@mock_ec2
def test_describe_regions_dryrun():
client = boto3.client("ec2", region_name="us-east-1")
with pytest.raises(ClientError) as ex:
client.describe_regions(DryRun=True)
ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(412)
ex.value.response["Error"]["Code"].should.equal("DryRunOperation")
ex.value.response["Error"]["Message"].should.equal(
"An error occurred (DryRunOperation) when calling the DescribeRegions operation: Request would have succeeded, but DryRun flag is set"
)

View File

@ -1026,3 +1026,16 @@ def test_disassociate_subnet_cidr_block():
association_set = subnets[0]["Ipv6CidrBlockAssociationSet"]
association_set.should.have.length_of(1)
association_set[0]["Ipv6CidrBlock"].should.equal("1080::1:200C:417A/111")
@mock_ec2
def test_describe_subnets_dryrun():
client = boto3.client("ec2", region_name="us-east-1")
with pytest.raises(ClientError) as ex:
client.describe_subnets(DryRun=True)
ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(412)
ex.value.response["Error"]["Code"].should.equal("DryRunOperation")
ex.value.response["Error"]["Message"].should.equal(
"An error occurred (DryRunOperation) when calling the DescribeSubnets operation: Request would have succeeded, but DryRun flag is set"
)

View File

@ -1296,3 +1296,16 @@ def test_delete_vpc_end_points():
"VpcEndpoints"
][0]
ep2["State"].should.equal("available")
@mock_ec2
def test_describe_vpcs_dryrun():
client = boto3.client("ec2", region_name="us-east-1")
with pytest.raises(ClientError) as ex:
client.describe_vpcs(DryRun=True)
ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(412)
ex.value.response["Error"]["Code"].should.equal("DryRunOperation")
ex.value.response["Error"]["Message"].should.equal(
"An error occurred (DryRunOperation) when calling the DescribeVpcs operation: Request would have succeeded, but DryRun flag is set"
)