EC2: Simplify describe_sg_rules() logic (#5875)
This commit is contained in:
parent
4700c3efea
commit
6d41ad72e0
@ -521,18 +521,11 @@ class SecurityGroupBackend:
|
|||||||
return matches
|
return matches
|
||||||
|
|
||||||
def describe_security_group_rules(self, group_ids=None, filters=None):
|
def describe_security_group_rules(self, group_ids=None, filters=None):
|
||||||
matches = itertools.chain(*[x.copy().values() for x in self.groups.values()])
|
matches = self.describe_security_groups(group_ids=group_ids, filters=filters)
|
||||||
if group_ids:
|
if not matches:
|
||||||
matches = [grp for grp in matches if grp.id in group_ids]
|
raise InvalidSecurityGroupNotFoundError(
|
||||||
if len(group_ids) > len(matches):
|
"No security groups found matching the filters provided."
|
||||||
unknown_ids = set(group_ids) - set(matches)
|
)
|
||||||
raise InvalidSecurityGroupNotFoundError(unknown_ids)
|
|
||||||
if filters:
|
|
||||||
matches = [grp for grp in matches if grp.matches_filters(filters)]
|
|
||||||
if not matches:
|
|
||||||
raise InvalidSecurityGroupNotFoundError(
|
|
||||||
"No security groups found matching the filters provided."
|
|
||||||
)
|
|
||||||
rules = []
|
rules = []
|
||||||
for group in matches:
|
for group in matches:
|
||||||
rules.extend(group.ingress_rules)
|
rules.extend(group.ingress_rules)
|
||||||
|
@ -251,20 +251,22 @@ DESCRIBE_SECURITY_GROUP_RULES_RESPONSE = """
|
|||||||
<DescribeSecurityGroupRulesResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/">
|
<DescribeSecurityGroupRulesResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/">
|
||||||
<requestId>{{ request_id }}</requestId>
|
<requestId>{{ request_id }}</requestId>
|
||||||
<securityGroupRuleSet>
|
<securityGroupRuleSet>
|
||||||
{% for rule in rules %}
|
{% for rule in rules %}
|
||||||
<item>
|
<item>
|
||||||
{% if rule.from_port is not none %}
|
{% if rule.from_port is not none %}
|
||||||
<fromPort>{{ rule.from_port }}</fromPort>
|
<fromPort>{{ rule.from_port }}</fromPort>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if rule.to_port is not none %}
|
{% if rule.to_port is not none %}
|
||||||
<toPort>{{ rule.to_port }}</toPort>
|
<toPort>{{ rule.to_port }}</toPort>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<cidrIpv4>{{ rule.ip_ranges[0]['CidrIp'] }}</cidrIpv4>
|
{% if rule.ip_ranges %}
|
||||||
<ipProtocol>{{ rule.ip_protocol }}</ipProtocol>
|
<cidrIpv4>{{ rule.ip_ranges[0]['CidrIp'] }}</cidrIpv4>
|
||||||
<groupOwnerId>{{ rule.owner_id }}</groupOwnerId>
|
{% endif %}
|
||||||
<isEgress>true</isEgress>
|
<ipProtocol>{{ rule.ip_protocol }}</ipProtocol>
|
||||||
<securityGroupRuleId>{{ rule.id }}</securityGroupRuleId>
|
<groupOwnerId>{{ rule.owner_id }}</groupOwnerId>
|
||||||
</item>
|
<isEgress>true</isEgress>
|
||||||
|
<securityGroupRuleId>{{ rule.id }}</securityGroupRuleId>
|
||||||
|
</item>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</securityGroupRuleSet>
|
</securityGroupRuleSet>
|
||||||
</DescribeSecurityGroupRulesResponse>"""
|
</DescribeSecurityGroupRulesResponse>"""
|
||||||
|
Loading…
Reference in New Issue
Block a user