EC2 - Implement DryRun-flag on various operations (#4420)
This commit is contained in:
parent
c62bd5ca41
commit
deeabfc6e5
@ -802,7 +802,11 @@ class BaseResponse(_TemplateEnvironmentMixin, ActionAuthenticatorMixin):
|
|||||||
def request_json(self):
|
def request_json(self):
|
||||||
return "JSON" in self.querystring.get("ContentType", [])
|
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"]):
|
if "true" in self.querystring.get("DryRun", ["false"]):
|
||||||
message = (
|
message = (
|
||||||
"An error occurred (DryRunOperation) when calling the %s operation: Request would have succeeded, but DryRun flag is set"
|
"An error occurred (DryRunOperation) when calling the %s operation: Request would have succeeded, but DryRun flag is set"
|
||||||
|
@ -40,6 +40,7 @@ class AmisResponse(BaseResponse):
|
|||||||
return template.render(success=str(success).lower())
|
return template.render(success=str(success).lower())
|
||||||
|
|
||||||
def describe_images(self):
|
def describe_images(self):
|
||||||
|
self.error_on_dryrun()
|
||||||
ami_ids = self._get_multi_param("ImageId")
|
ami_ids = self._get_multi_param("ImageId")
|
||||||
filters = filters_from_querystring(self.querystring)
|
filters = filters_from_querystring(self.querystring)
|
||||||
owners = self._get_multi_param("Owner")
|
owners = self._get_multi_param("Owner")
|
||||||
|
@ -4,11 +4,13 @@ from moto.core.responses import BaseResponse
|
|||||||
|
|
||||||
class AvailabilityZonesAndRegions(BaseResponse):
|
class AvailabilityZonesAndRegions(BaseResponse):
|
||||||
def describe_availability_zones(self):
|
def describe_availability_zones(self):
|
||||||
|
self.error_on_dryrun()
|
||||||
zones = self.ec2_backend.describe_availability_zones()
|
zones = self.ec2_backend.describe_availability_zones()
|
||||||
template = self.response_template(DESCRIBE_ZONES_RESPONSE)
|
template = self.response_template(DESCRIBE_ZONES_RESPONSE)
|
||||||
return template.render(zones=zones)
|
return template.render(zones=zones)
|
||||||
|
|
||||||
def describe_regions(self):
|
def describe_regions(self):
|
||||||
|
self.error_on_dryrun()
|
||||||
region_names = self._get_multi_param("RegionName")
|
region_names = self._get_multi_param("RegionName")
|
||||||
regions = self.ec2_backend.describe_regions(region_names)
|
regions = self.ec2_backend.describe_regions(region_names)
|
||||||
template = self.response_template(DESCRIBE_REGIONS_RESPONSE)
|
template = self.response_template(DESCRIBE_REGIONS_RESPONSE)
|
||||||
|
@ -26,6 +26,7 @@ class CustomerGateways(BaseResponse):
|
|||||||
return template.render(delete_status=delete_status)
|
return template.render(delete_status=delete_status)
|
||||||
|
|
||||||
def describe_customer_gateways(self):
|
def describe_customer_gateways(self):
|
||||||
|
self.error_on_dryrun()
|
||||||
filters = filters_from_querystring(self.querystring)
|
filters = filters_from_querystring(self.querystring)
|
||||||
customer_gateway_ids = self._get_multi_param("CustomerGatewayId")
|
customer_gateway_ids = self._get_multi_param("CustomerGatewayId")
|
||||||
customer_gateways = self.ec2_backend.get_all_customer_gateways(
|
customer_gateways = self.ec2_backend.get_all_customer_gateways(
|
||||||
|
@ -70,6 +70,7 @@ class ElasticIPAddresses(BaseResponse):
|
|||||||
return template.render(address=eip)
|
return template.render(address=eip)
|
||||||
|
|
||||||
def describe_addresses(self):
|
def describe_addresses(self):
|
||||||
|
self.error_on_dryrun()
|
||||||
allocation_ids = self._get_multi_param("AllocationId")
|
allocation_ids = self._get_multi_param("AllocationId")
|
||||||
public_ips = self._get_multi_param("PublicIp")
|
public_ips = self._get_multi_param("PublicIp")
|
||||||
filters = filters_from_querystring(self.querystring)
|
filters = filters_from_querystring(self.querystring)
|
||||||
|
@ -16,6 +16,7 @@ from copy import deepcopy
|
|||||||
|
|
||||||
class InstanceResponse(BaseResponse):
|
class InstanceResponse(BaseResponse):
|
||||||
def describe_instances(self):
|
def describe_instances(self):
|
||||||
|
self.error_on_dryrun()
|
||||||
filter_dict = filters_from_querystring(self.querystring)
|
filter_dict = filters_from_querystring(self.querystring)
|
||||||
instance_ids = self._get_multi_param("InstanceId")
|
instance_ids = self._get_multi_param("InstanceId")
|
||||||
token = self._get_param("NextToken")
|
token = self._get_param("NextToken")
|
||||||
|
@ -39,6 +39,7 @@ class Subnets(BaseResponse):
|
|||||||
return template.render(subnet=subnet)
|
return template.render(subnet=subnet)
|
||||||
|
|
||||||
def describe_subnets(self):
|
def describe_subnets(self):
|
||||||
|
self.error_on_dryrun()
|
||||||
subnet_ids = self._get_multi_param("SubnetId")
|
subnet_ids = self._get_multi_param("SubnetId")
|
||||||
filters = filters_from_querystring(self.querystring)
|
filters = filters_from_querystring(self.querystring)
|
||||||
subnets = self.ec2_backend.get_all_subnets(subnet_ids, filters)
|
subnets = self.ec2_backend.get_all_subnets(subnet_ids, filters)
|
||||||
|
@ -40,6 +40,7 @@ class VPCs(BaseResponse):
|
|||||||
return template.render(vpc=vpc)
|
return template.render(vpc=vpc)
|
||||||
|
|
||||||
def describe_vpcs(self):
|
def describe_vpcs(self):
|
||||||
|
self.error_on_dryrun()
|
||||||
vpc_ids = self._get_multi_param("VpcId")
|
vpc_ids = self._get_multi_param("VpcId")
|
||||||
filters = filters_from_querystring(self.querystring)
|
filters = filters_from_querystring(self.querystring)
|
||||||
vpcs = self.ec2_backend.describe_vpcs(vpc_ids=vpc_ids, filters=filters)
|
vpcs = self.ec2_backend.describe_vpcs(vpc_ids=vpc_ids, filters=filters)
|
||||||
|
@ -1761,3 +1761,16 @@ def test_ami_filter_by_unknown_ownerid():
|
|||||||
Filters=[{"Name": "owner-alias", "Values": ["unknown",]},]
|
Filters=[{"Name": "owner-alias", "Values": ["unknown",]},]
|
||||||
)["Images"]
|
)["Images"]
|
||||||
images.should.have.length_of(0)
|
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"
|
||||||
|
)
|
||||||
|
@ -2,8 +2,10 @@ from __future__ import unicode_literals
|
|||||||
import boto
|
import boto
|
||||||
import boto.ec2
|
import boto.ec2
|
||||||
import boto3
|
import boto3
|
||||||
|
import pytest
|
||||||
import sure # noqa
|
import sure # noqa
|
||||||
|
|
||||||
|
from botocore.exceptions import ClientError
|
||||||
from moto import mock_ec2, mock_ec2_deprecated
|
from moto import mock_ec2, mock_ec2_deprecated
|
||||||
|
|
||||||
|
|
||||||
@ -65,6 +67,19 @@ def test_boto3_availability_zones():
|
|||||||
rec["ZoneName"].should.contain(region)
|
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
|
@mock_ec2
|
||||||
def test_boto3_zoneId_in_availability_zones():
|
def test_boto3_zoneId_in_availability_zones():
|
||||||
conn = boto3.client("ec2", "us-east-1")
|
conn = boto3.client("ec2", "us-east-1")
|
||||||
|
@ -43,6 +43,19 @@ def test_describe_customer_gateways():
|
|||||||
cgws[0].id.should.match(customer_gateway.id)
|
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
|
@mock_ec2
|
||||||
def test_describe_customer_gateways_boto3():
|
def test_describe_customer_gateways_boto3():
|
||||||
ec2 = boto3.client("ec2", region_name="us-east-1")
|
ec2 = boto3.client("ec2", region_name="us-east-1")
|
||||||
|
@ -106,6 +106,19 @@ def test_eip_allocate_vpc():
|
|||||||
vpc.release()
|
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
|
@mock_ec2
|
||||||
def test_eip_allocate_vpc_boto3():
|
def test_eip_allocate_vpc_boto3():
|
||||||
"""Allocate/release VPC EIP"""
|
"""Allocate/release VPC EIP"""
|
||||||
|
@ -3096,6 +3096,19 @@ def test_run_instance_and_associate_public_ip():
|
|||||||
addresses["Association"].should.have.key("PublicIp")
|
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=[]):
|
def retrieve_all_reservations(client, filters=[]):
|
||||||
resp = client.describe_instances(Filters=filters)
|
resp = client.describe_instances(Filters=filters)
|
||||||
all_reservations = resp["Reservations"]
|
all_reservations = resp["Reservations"]
|
||||||
|
@ -3,9 +3,11 @@ import boto.ec2
|
|||||||
import boto.ec2.autoscale
|
import boto.ec2.autoscale
|
||||||
import boto.ec2.elb
|
import boto.ec2.elb
|
||||||
import boto3
|
import boto3
|
||||||
import sure
|
import pytest
|
||||||
|
import sure # noqa
|
||||||
from boto3 import Session
|
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_ec2_deprecated, mock_autoscaling_deprecated, mock_elb_deprecated
|
||||||
from moto import mock_autoscaling, mock_ec2, mock_elb
|
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["LoadBalancerNames"].should.equal([lb_name])
|
||||||
group["PlacementGroup"].should.equal("us_test_placement")
|
group["PlacementGroup"].should.equal("us_test_placement")
|
||||||
group["TerminationPolicies"].should.equal(["OldestInstance", "NewestInstance"])
|
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"
|
||||||
|
)
|
||||||
|
@ -1026,3 +1026,16 @@ def test_disassociate_subnet_cidr_block():
|
|||||||
association_set = subnets[0]["Ipv6CidrBlockAssociationSet"]
|
association_set = subnets[0]["Ipv6CidrBlockAssociationSet"]
|
||||||
association_set.should.have.length_of(1)
|
association_set.should.have.length_of(1)
|
||||||
association_set[0]["Ipv6CidrBlock"].should.equal("1080::1:200C:417A/111")
|
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"
|
||||||
|
)
|
||||||
|
@ -1296,3 +1296,16 @@ def test_delete_vpc_end_points():
|
|||||||
"VpcEndpoints"
|
"VpcEndpoints"
|
||||||
][0]
|
][0]
|
||||||
ep2["State"].should.equal("available")
|
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"
|
||||||
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user