Skip to content

Instantly share code, notes, and snippets.

@paigeadelethompson
Last active December 4, 2023 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paigeadelethompson/3a05ed73c7da3848731f7626282e52ef to your computer and use it in GitHub Desktop.
Save paigeadelethompson/3a05ed73c7da3848731f7626282e52ef to your computer and use it in GitHub Desktop.
def get_network_object(self, network, prefix_length):
net = ip_address(network)
if type(net) == IPv4Address:
return IPv4Network(net).supernet(new_prefix = prefix_length)
elif type(net) == IPv6Address:
return IPv6Network(net).supernet(new_prefix = prefix_length)
def network_object_to_uuid(self, network):
if type(network) == IPv4Network:
return uuid.UUID(bytes = bytes([0x00] * 12) + network.network_address.packed)
elif type(network) == IPv6Network:
return uuid.UUID(bytes = network.network_address.packed)
def uuid_to_address_object(self, id):
if int.from_bytes(id.bytes[:12]) == 0:
return IPv4Address(int.from_bytes(id.bytes[12:16]))
else:
return IPv6Address(int.from_bytes(id.bytes))
def get_ipv6_network_object_from_token(self, parent, token, prefix_len):
net_bytes = [index for index in parent.network_address.packed[::-1] if index != 0][::-1]
token_bytes = [index for index in token.network_address.packed if index != 0]
zeroes = 16 - len(net_bytes + token_bytes)
return IPv6Network(IPv6Address(int.from_bytes(bytes(net_bytes) + bytes(token_bytes) + bytes([0] * zeroes)))).supernet(new_prefix = prefix_len)
def template_to_network_objects(self, template, parent_v4 = None, parent_v6 = None):
for scope in template.get('scopes'):
cur4 = None
cur6 = None
v4_out = []
v6_out = []
if parent_v4 != None:
v4_subnets = parent_v4.subnets(new_prefix = scope.get('ipv4_prefix_length'))
while True:
cur4 = next(v4_subnets)
if len([index for index in v4_out if index == cur4 or cur4.supernet_of(index) or cur4.subnet_of(index)]) == 0:
v4_out.append(cur4)
break
if parent_v6 != None:
v6_subnets = parent_v6.subnets(new_prefix = scope.get('ipv4_prefix_length'))
while True:
cur6 = next(v6_subnets)
if len([index for index in v6_out if index == cur6 or cur6.supernet_of(index) or cur6.subnet_of(index)]) == 0:
v6_out.append(cur6)
break
yield (cur4, cur6)
def interpolate_network_objects(self, parent_v4 = None, new_v4_prefix_length = None, parent_v6 = None, new_v6_prefix_length = None):
try:
interp_v4 = None
interp_v6 = None
if parent_v4 != None:
interp_v4 = parent_v4.subnets(new_prefix = new_v4_prefix_length)
if parent_v6 != None:
interp_v6 = parent_v6.subnets(new_prefix = new_v6_prefix_length)
while True:
if interp_v4 != None and interp_v6 != None:
yield(next(interp_v4), next(interp_v6))
elif interp_v4 != None and interp_v6 == None:
yield(next(interp_v4), None)
elif interp_v4 == None and interp_v6 != None:
yield(None, next(interp_v6))
else:
return
except:
return
class schema():
_default_schema = {
'reserved_vrf_ids': [1],
'schema_version': 2,
'templates': [{
'name': 'default',
'scopes': [{
'ipv4_prefix_length': 24,
'ipv4_allocation_length': [32],
'ipv6_prefix_length': 120,
'ipv6_allocation_length': [127]
}, {
'ipv4_prefix_length': 24,
'ipv4_allocation_length': [31],
'ipv6_prefix_length': 120,
'ipv6_allocation_length': [127]
}, {
'ipv4_prefix_length': 24,
'ipv4_allocation_length': [30],
'ipv6_prefix_length': 120,
'ipv6_allocation_length': [125]
}, {
'ipv4_prefix_length': 24,
'ipv4_allocation_length': [29],
'ipv6_prefix_length': 120,
'ipv6_allocation_length': [124]
}, {
'ipv4_prefix_length': 24,
'ipv4_allocation_length': [28],
'ipv6_prefix_length': 120,
'ipv6_allocation_length': [123]
}, {
'ipv4_prefix': 24,
'ipv4_allocation_length': [27],
'ipv6_prefix_length': 120,
'ipv6_allocation_length': [122]
}, {
'ipv4_prefix': 24,
'ipv4_allocation_length': [26],
'ipv6_prefix_length': 120,
'ipv6_allocation_length': [121]
}, {
'ipv4_prefix': 24,
'ipv4_allocation_length': [25],
'ipv6_prefix_length': 120,
'ipv6_allocation_length': [120]
}, {
'ipv4_prefix': 21,
'ipv4_allocation_length': [22, 23, 24],
'ipv6_prefix_length': 117,
'ipv6_allocation_length': [118]
}]}],
'scopes': [{
'ipv4_network': '10.0.0.0',
'ipv4_prefix_length': 16,
'vrf_id': 2,
'ipv6_network': ['fc00:ffff:ffff:ffff::'],
'ipv6_prefix_length': 64,
'tags': ['docker-local'],
'scopes': [{
'ipv4_network': '10.0.0.0',
'ipv4_prefix_length': 20,
'tags': ['default-container-network'],
'egress': ['cluster-local-ingress', 'docker-local-ingress', 'internet'],
'ingress': ['cluster-local-egress', 'docker-local-egress'],
'ipv6_network': '::ff10:fff0:fff0',
'ipv6_scope': ['ULA', 'GUA'],
'ipv6_prefix_length': 116,
'subnet_template': 'default'
}, {
'ipv4_network': '10.0.16.0',
'ipv4_prefix_length': 20,
'tags': 'docker-local-egress',
'egress': ['docker-local-ingress'],
'ingress': [],
'ipv6_network': '::ff10:fff0:ff16',
'ipv6_scope': ['ULA'],
'ipv6_prefix_length': 116,
'subnet_template': 'default'
}, {
'ipv4_network': '10.0.32.0',
'ipv4_prefix_length': 20,
'tags': ['docker-local-ingress'],
'egress': [],
'ingress': ['docker-local-egress'],
'ipv6_network': '::ff10:fff0:ff32',
'ipv6_scope': ['ULA'],
'ipv6_prefix_length': 116,
'subnet_template': 'default'
}, {
'ipv4_network': '10.0.48.0',
'ipv4_prefix_length': 20,
'tags': ['docker-local-layer2'],
'egress': [],
'ingress': [],
'ipv6_network': '::ff10:fff0:ff48',
'ipv6_scope': ['LL'],
'ipv6_prefix_length': 116,
'subnet_template': 'default'
}, {
'ipv4_network': '10.0.64.0',
'ipv4_prefix_length': 20,
'tags': ['docker-internet-egress'],
'egress': ["internet"],
'ingress': [],
'ipv6_network': '::ff10:fff0:ff64',
'ipv6_scope': ['GUA'],
'ipv6_prefix_length': 116,
'subnet_template': 'default'
}, {
'ipv4_network': '10.0.80.0',
'ipv4_prefix_length': 20,
'tags': ['docker-internet-ingress'],
'egress': [],
'ingress': ["internet"],
'ipv6_network': '::ff10:fff0:ff80',
'ipv6_scope': ['GUA'],
'ipv6_prefix_length': 116,
'subnet_template': 'default'
}, {
'ipv4_network': '10.0.96.0',
'ipv4_prefix_length': 20,
'ipv6_network': '::ff10:fff0:ff96',
'ipv6_scope': [],
'ipv6_prefix_length': 116,
'subnet_template': 'default'
}, {
'ipv4_network': '10.0.112.0',
'ipv4_prefix_length': 20,
'ipv6_network': '::ff10:fff0:f112',
'ipv6_scope': [],
'ipv6_prefix_length': 116,
'subnet_template': 'default'
}, {
'ipv4_network': '10.0.128.0',
'ipv4_prefix_length': 20,
'ipv6_network': '::ff10:fff0:f128',
'ipv6_scope': [],
'ipv6_prefix_length': 116,
'subnet_template': 'default'
}]
}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment