Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to validate cidr superset #562

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions plugins/modules/gcp_compute_subnetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@
)
import json
import time
import ipaddress

################################################################################
# Main
Expand Down Expand Up @@ -499,8 +500,33 @@ def is_different(module, response):
if k in response:
request_vals[k] = v

return GcpRequest(request_vals) != GcpRequest(response_vals)

if request_vals['ipCidrRange']:
try:
result_is_superset = cidr_superset(request_vals['ipCidrRange'], response_vals['ipCidrRange'])
if result_is_superset:
gcp_req_request=GcpRequest(request_vals)
gcp_req_response=GcpRequest(response_vals)
else:
module.fail_json(msg="The new CIDR must be a superset of the original IP ranage!")
except Exception as e:
module.fail_json(msg="CIDR Error! str(e)")

return gcp_req_request != gcp_req_response

def cidr_superset(cidr1, cidr2):
"""
Check if CIDR1 is a superset of CIDR2.

Args:
cidr1 (str): The first CIDR address.
cidr2 (str): The second CIDR address.

Returns:
bool: True if CIDR1 is a superset of CIDR2, False otherwise.
"""
net1 = ipaddress.ip_network(cidr1)
net2 = ipaddress.ip_network(cidr2)
return net2.subnet_of(net1)

# Remove unnecessary properties from the response.
# This is for doing comparisons with Ansible's current parameters.
Expand Down