Skip to content

Commit 16af3ef

Browse files
Add get_related_vpc() function
1 parent 1872308 commit 16af3ef

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

‎src/celpy/c7nlib.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,14 @@ def get_related_security_configs(resource: celtypes.MapType,) -> celtypes.Value:
889889
return json_to_cel(security_configs)
890890

891891

892+
def get_related_vpc(resource: celtypes.MapType,) -> celtypes.Value:
893+
"""
894+
Reach into C7N and make a get_related_vpc() request using the current C7N filter.
895+
"""
896+
vpc = C7N.filter.get_related_vpc(resource)
897+
return json_to_cel(vpc)
898+
899+
892900
def security_group(security_group_id: celtypes.MapType,) -> celtypes.Value:
893901
"""
894902
Reach into C7N and make a get_related() request using the current C7N filter to get
@@ -1449,6 +1457,7 @@ def web_acls(resource: celtypes.MapType) -> celtypes.Value:
14491457
"get_related_nat_gateways": celtypes.FunctionType,
14501458
"get_related_igws": celtypes.FunctionType,
14511459
"get_related_security_configs": celtypes.FunctionType,
1460+
"get_related_vpc": celtypes.FunctionType,
14521461
"get_vpcs": celtypes.FunctionType,
14531462
"get_vpces": celtypes.FunctionType,
14541463
"get_orgids": celtypes.FunctionType,
@@ -1512,6 +1521,7 @@ def web_acls(resource: celtypes.MapType) -> celtypes.Value:
15121521
get_related_nat_gateways,
15131522
get_related_igws,
15141523
get_related_security_configs,
1524+
get_related_vpc,
15151525
get_vpcs,
15161526
get_vpces,
15171527
get_orgids,

‎tests/test_c7nlib.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ class RelatedResourceMixin:
528528
get_related_nat_gateways = Mock(return_value=[str(sentinel.nat_gateway)])
529529
get_related_igws = Mock(return_value=[str(sentinel.igw)])
530530
get_related_security_configs = Mock(return_value=[str(sentinel.sec_config)])
531+
get_related_vpc = Mock(return_value=[str(sentinel.vpc)])
531532

532533
get_related = Mock(side_effect=get_related_results)
533534

@@ -1100,6 +1101,14 @@ def test_C7N_CELFilter_get_related_security_configs(celfilter_instance):
11001101
assert sec_config == [str(sentinel.sec_config)]
11011102
assert mock_filter.get_related_security_configs.mock_calls == [call(glue_doc)]
11021103

1104+
def test_C7N_CELFilter_get_related_vpc(celfilter_instance):
1105+
mock_filter = celfilter_instance['the_filter']
1106+
ec2_doc = {"ResourceType": "ec2", "InstanceId": "i-123456789"}
1107+
with celpy.c7nlib.C7NContext(filter=mock_filter):
1108+
vpc = celpy.c7nlib.get_related_vpc(ec2_doc)
1109+
assert vpc == [str(sentinel.vpc)]
1110+
assert mock_filter.get_related_vpc.mock_calls == [call(ec2_doc)]
1111+
11031112
def test_C7N_CELFilter_security_group(celfilter_instance):
11041113
mock_filter = celfilter_instance['the_filter']
11051114
ec2_doc = {"ResourceType": "ec2", "InstanceId": "i-123456789"}

0 commit comments

Comments
 (0)