Repository: getmoto/moto

Issue:
EC2  NotImplementedError: Filter dicts have not been implemented in Moto for 'dns-name'
moto==2.2.9
boto3==1.18.2
botocore==1.21.2

instance = Instance:i-08a8fa0f2fb1b1007, filter_dict = {'dns-name': [''], 'instance-state-name': ['running']}

    def passes_filter_dict(instance, filter_dict):
        for filter_name, filter_values in filter_dict.items():
            if filter_name in filter_dict_attribute_mapping:
                instance_attr = filter_dict_attribute_mapping[filter_name]
                instance_value = get_object_value(instance, instance_attr)
                if not instance_value_in_filter_values(instance_value, filter_values):
                    return False
    
            elif is_tag_filter(filter_name):
                if not tag_filter_matches(instance, filter_name, filter_values):
                    return False
            else:
>               raise NotImplementedError(
                    "Filter dicts have not been implemented in Moto for '%s' yet. Feel free to open an issue at https://github.com/spulec/moto/issues"
                    % filter_name
                )
E               NotImplementedError: Filter dicts have not been implemented in Moto for 'dns-name' yet. Feel free to open an issue at https://github.com/spulec/moto/issues




Discussion:

Resolving patch:
diff --git a/moto/ec2/utils.py b/moto/ec2/utils.py
--- a/moto/ec2/utils.py
+++ b/moto/ec2/utils.py
@@ -477,6 +477,7 @@ def tag_filter_matches(obj, filter_name, filter_values):
     "private-dns-name": "private_dns",
     "owner-id": "owner_id",
     "subnet-id": "subnet_id",
+    "dns-name": "public_dns",
 }
 
 


Test patch:
diff --git a/tests/test_ec2/test_instances.py b/tests/test_ec2/test_instances.py
--- a/tests/test_ec2/test_instances.py
+++ b/tests/test_ec2/test_instances.py
@@ -919,6 +919,40 @@ def test_get_instances_filtering_by_vpc_id_boto3():
     res2[0]["Instances"][0]["SubnetId"].should.equal(subnet2.id)
 
 
+@mock_ec2
+def test_get_instances_filtering_by_dns_name():
+    ec2 = boto3.resource("ec2", "us-west-1")
+    client = boto3.client("ec2", "us-west-1")
+    vpc1 = ec2.create_vpc(CidrBlock="10.0.0.0/16")
+    subnet = ec2.create_subnet(VpcId=vpc1.id, CidrBlock="10.0.0.0/27")
+    client.modify_subnet_attribute(
+        SubnetId=subnet.id, MapPublicIpOnLaunch={"Value": True}
+    )
+    reservation1 = ec2.create_instances(
+        ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1, SubnetId=subnet.id
+    )
+    instance1 = reservation1[0]
+
+    reservation2 = ec2.create_instances(
+        ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1, SubnetId=subnet.id
+    )
+    instance2 = reservation2[0]
+
+    res1 = client.describe_instances(
+        Filters=[{"Name": "dns-name", "Values": [instance1.public_dns_name]}]
+    )["Reservations"]
+    res1.should.have.length_of(1)
+    res1[0]["Instances"].should.have.length_of(1)
+    res1[0]["Instances"][0]["InstanceId"].should.equal(instance1.id)
+
+    res2 = client.describe_instances(
+        Filters=[{"Name": "dns-name", "Values": [instance2.public_dns_name]}]
+    )["Reservations"]
+    res2.should.have.length_of(1)
+    res2[0]["Instances"].should.have.length_of(1)
+    res2[0]["Instances"][0]["InstanceId"].should.equal(instance2.id)
+
+
 # Has boto3 equivalent
 @mock_ec2_deprecated
 def test_get_instances_filtering_by_architecture():