Skip to content
Snippets Groups Projects

Ipam service

Merged JORGE SASIAIN requested to merge ipam-service into develop
3 unresolved threads
2 files
+ 77
35
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 32
16
@@ -38,6 +38,8 @@ class HostAddresses(BaseSettings):
class IPAMErrors(Enum):
# HTTP error code, match in error message
CONTAINER_FULL = 400, "Can not find requested number of networks"
EXTATTR_UNKNOWN = 400, "Unknown extensible attribute"
EXTATTR_BADVALUE = 400, "Bad value for extensible attribute"
# TODO: remove this!
@@ -144,7 +146,8 @@ def _get_network_capacity(network=None):
def _allocate_network(
infoblox_params: settings.InfoBloxParams,
network_params: Union[settings.V4NetworkParams, settings.V6NetworkParams],
ip_version=4
ip_version=4,
extattrs={}
) -> Union[V4ServiceNetwork, V6ServiceNetwork]:
assert ip_version in [4, 6]
endpoint = 'network' if ip_version == 4 else 'ipv6network'
@@ -152,6 +155,7 @@ def _allocate_network(
'ipv6networkcontainer'
# only return in the response the allocated network, not all available
# TODO: any validation needed for extrattrs wherever it's used?
req_payload = {
"network": {
"_object_function": "next_available_network",
@@ -163,7 +167,8 @@ def _allocate_network(
"network": str(network_params.containers[0])
},
"_result_field": "networks",
}
},
"extattrs": extattrs
}
container_index = 0
@@ -198,7 +203,7 @@ def _allocate_network(
return V6ServiceNetwork(v6=allocated_network)
def allocate_service_ipv4_network(service_type) -> V4ServiceNetwork:
def allocate_service_ipv4_network(service_type, extattrs) -> V4ServiceNetwork:
"""
Allocate IPv4 network within the container of the specified service type.
"""
@@ -209,10 +214,11 @@ def allocate_service_ipv4_network(service_type) -> V4ServiceNetwork:
and service_type != 'INFOBLOX', "Invalid service type."
return _allocate_network(ipam_params.INFOBLOX,
getattr(ipam_params, service_type).V4,
4)
4,
extattrs)
def allocate_service_ipv6_network(service_type) -> V6ServiceNetwork:
def allocate_service_ipv6_network(service_type, extattrs) -> V6ServiceNetwork:
"""
Allocate IPv6 network within the container of the specified service type.
"""
@@ -223,7 +229,8 @@ def allocate_service_ipv6_network(service_type) -> V6ServiceNetwork:
and service_type != 'INFOBLOX', "Invalid service type."
return _allocate_network(ipam_params.INFOBLOX,
getattr(ipam_params, service_type).V6,
6)
6,
extattrs)
def _find_next_available_ip(infoblox_params, network_ref):
@@ -242,7 +249,7 @@ def _find_next_available_ip(infoblox_params, network_ref):
return received_ip[0]
def _allocate_host(hostname=None, addr=None, network=None
def _allocate_host(hostname=None, addr=None, network=None, extattrs={}
) -> Union[V4HostAddress, V6HostAddress]:
"""
If network is not None, allocate host in that network.
@@ -278,7 +285,8 @@ def _allocate_host(hostname=None, addr=None, network=None
],
"name": hostname,
"configure_for_dns": False,
"view": "default"
"view": "default",
"extattrs": extattrs
}
r = requests.post(
@@ -296,7 +304,8 @@ def _allocate_host(hostname=None, addr=None, network=None
dns_req_payload = {
f"ipv{ip_version}addr": addr,
"name": hostname,
"view": "default"
"view": "default",
"extattrs": extattrs
}
endpoint = 'record:a' if ip_version == 4 else 'record:aaaa'
@@ -322,7 +331,8 @@ def _allocate_host(hostname=None, addr=None, network=None
def allocate_service_host(hostname=None,
service_type=None,
service_networks: ServiceNetworks = None,
host_addresses: HostAddresses = None
host_addresses: HostAddresses = None,
extattrs={}
) -> HostAddresses:
"""
Allocate host with both IPv4 and IPv6 address (and respective DNS
@@ -370,19 +380,22 @@ def allocate_service_host(hostname=None,
assert first_nonfull_ipv4_network, \
"No available IPv4 addresses for this service type."
v4_host = _allocate_host(hostname=hostname+domain_name,
network=first_nonfull_ipv4_network)
network=first_nonfull_ipv4_network,
extattrs=extattrs)
elif service_networks:
network = service_networks.v4
assert any(network.subnet_of(ipv4_container)
for ipv4_container in ipv4_containers)
v4_host = _allocate_host(hostname=hostname+domain_name,
network=str(network))
network=str(network),
extattrs=extattrs)
elif host_addresses:
addr = host_addresses.v4
assert any(addr in ipv4_container
for ipv4_container in ipv4_containers)
v4_host = _allocate_host(hostname=hostname+domain_name,
addr=str(addr))
addr=str(addr),
extattrs=extattrs)
# IPv6
if not service_networks and not host_addresses:
@@ -396,19 +409,22 @@ def allocate_service_host(hostname=None,
assert 'network' in ipv6_networks_info[0]
# TODO: if "no available IP" error, create a new network?
v6_host = _allocate_host(hostname=hostname+domain_name,
network=ipv6_networks_info[0]['network'])
network=ipv6_networks_info[0]['network'],
extattrs=extattrs)
elif service_networks:
network = service_networks.v6
assert any(network.subnet_of(ipv6_container)
for ipv6_container in ipv6_containers)
v6_host = _allocate_host(hostname=hostname+domain_name,
network=str(network))
network=str(network),
extattrs=extattrs)
elif host_addresses:
addr = host_addresses.v6
assert any(addr in ipv6_container
for ipv6_container in ipv6_containers)
v6_host = _allocate_host(hostname=hostname+domain_name,
addr=str(addr))
addr=str(addr),
extattrs=extattrs)
return HostAddresses(v4=v4_host.v4, v6=v6_host.v6)
Loading