Skip to content
Snippets Groups Projects
Commit c7f482aa authored by Simone Spinelli's avatar Simone Spinelli
Browse files

Merge branch 'develop' into feature/nat178-trunks_isis_metric_modify

parents 1e69df93 45b9f41e
No related branches found
No related tags found
1 merge request!28Feature/nat178 trunks isis metric modify
...@@ -12,87 +12,34 @@ ...@@ -12,87 +12,34 @@
"password": "robot-user-password" "password": "robot-user-password"
}, },
"LO": { "LO": {
"V4": { "V4": {"containers": [], "networks": ["1.1.0.0/24"], "mask": 0},
"containers": [ "V6": {"containers": [], "networks": ["dead:beef::/64"], "mask": 0},
"1.1.0.0/24" "domain_name": ".lo",
], "dns_view": "default"
"networks": [],
"mask": 32
},
"V6": {
"containers": [
"dead:beef::/64"
],
"networks": [],
"mask": 128
},
"domain_name": ".lo"
}, },
"TRUNK": { "TRUNK": {
"V4": { "V4": {"containers": ["1.1.1.0/24"], "networks": [], "mask": 31},
"containers": [ "V6": {"containers": ["dead:beef::/64"], "networks": [], "mask": 126},
"1.1.1.0/24" "domain_name": ".trunk",
], "dns_view": "default"
"networks": [],
"mask": 31
},
"V6": {
"containers": [
"dead:beef::/64"
],
"networks": [],
"mask": 126
},
"domain_name": ".trunk"
}, },
"GEANT_IP": { "GEANT_IP": {
"V4": { "V4": {"containers": ["1.1.2.0/24"], "networks": [], "mask": 31},
"containers": [ "V6": {"containers": ["dead:beef::/64"], "networks": [], "mask": 126},
"1.1.2.0/24" "domain_name": ".geantip",
], "dns_view": "default"
"networks": [],
"mask": 31
},
"V6": {
"containers": [
"dead:beef::/64"
],
"networks": [],
"mask": 126
},
"domain_name": ".geantip"
}, },
"SI": { "SI": {
"V4": { "V4": {"containers": ["1.1.3.0/24"], "networks": [], "mask": 31},
"containers": [ "V6": {"containers": ["dead:beef::/64"], "networks": [], "mask": 126},
"10.255.253.128/25" "domain_name": ".si",
], "dns_view": "default"
"networks": [],
"mask": 31
},
"V6": {
"containers": [],
"networks": [],
"mask": 126
},
"domain_name": ".geantip"
}, },
"LT_IAS": { "LT_IAS": {
"V4": { "V4": {"containers": ["1.1.4.0/24"], "networks": [], "mask": 31},
"containers": [ "V6": {"containers": ["dead:beef::/64"], "networks": [], "mask": 126},
"10.255.255.0/24" "domain_name": ".ltias",
], "dns_view": "default"
"networks": [],
"mask": 31
},
"V6": {
"containers": [
"dead:beef:cc::/48"
],
"networks": [],
"mask": 126
},
"domain_name": ".geantip"
} }
}, },
"PROVISIONING_PROXY": { "PROVISIONING_PROXY": {
......
## IPAM
### Example configuration
The following kind of configuration file needs to be exported in the variable `OSS_PARAMS_FILENAME`.
```
"LO": {
"V4": {"containers": [], "networks": ["10.255.255.32/32", "10.255.255.0/28", "10.255.255.16/28"], "mask": 0},
"V6": {"containers": [], "networks": ["dead:beef::/80", "dead:beef:0:1::/80"], "mask": 0},
"domain_name": ".gso"
},
"TRUNK": {
"V4": {"containers": ["10.255.255.0/24", "10.255.254.0/24"], "networks": [], "mask": 31},
"V6": {"containers": ["dead:beef::/64", "dead:beee::/64"], "networks": [], "mask": 126},
"domain_name": ".gso"
},
```
Either a non-empty list of `networks` or a non-empty list of `containers` is required in the params file per service type. Having both is redundant, but in that case containers take precedence. The `mask` parameter is irrelevant if the service type uses networks instead of containers.
### Host/network allocation
Hosts can be allocated through `ipam.new_service_host()` resulting in a host record (IPv4, IPv6, A, AAAA) and zero or more cname records. This function can be called by passing domain name, hostname, and service type (mandatory). Optinally, you can pass either ipv4/ipv6 addresses, ipv4/ipv6 networks, or nothing. Passing nothing is considered default. In that case:
- If the service type has any container specified in the params file, networks have to be allocated before allocating hosts (through `ipam.new_service_networks()`). The mask parameter is used to create the new networks. Containers are filled up in the order that they appear in the params file.
- If the service type has any network specified in the params file, those networks are used directly to allocate hosts. Networks are filled up in the order that they appear in the params file.
If you pass addresses or networks, the module will always attempt to use those, but will fail if they don't match the configuration in the params file for the service type (i.e. if you request an address or network that places outside of the configured containers/networks).
Networks and hosts can be allocated with extensible attributes. Networks can be created with a comment. CNAME records can be optionally created.
### Host/network deletion
The code checks that the resource you are trying to delete is allocated (in terms of IP address space) to the service as per `containers` or `networks` in the service's configuration. If not, you are not allowed to delete it.
No host or cname records are deleted via the `delete_service_host()` function until all passed arguments match the hostname, IP, and CNAME data of the host. If anything doesn't match, no record is deleted.
### Usage examples
#### Host/network allocation
The following is a sample call flow to allocate two loopback interfaces and a trunk service. It assumes the _Example configuration_ from the above section is used, where TRUNK has configured containers, and LO has configured networks.
In this example:
- Host hA for service LO uses specific ipv4/ipv6 address pair.
- Host hB for service LO uses nothing (just the service type).
- Host hA for service TRUNK uses a specific ipv4/ipv6 address pair.
- Host hB for service TRUNK uses the ipv4/ipv6 network pair.
Because TRUNK has configured containers rather than networks, `new_service_networks()` must be called first to create a network for the hA-hB TRUNK. This is not needed for LO, which automatically uses the first configured network with available space to allocate new hosts.
```
hostname_A = 'hA'
hostname_B = 'hB'
# hA LO (loopback)
loA_v4_host_address = ipaddress.ip_address('10.255.255.0')
loA_v6_host_address = ipaddress.ip_address('dead:beef::0')
loA_host_addresses = HostAddresses(v4=loA_v4_host_address,
v6=loA_v6_host_address)
new_service_host(hostname=hostname_A+"_LO",
host_addresses=loA_host_addresses,
cname_aliases=["alias1.hA", "alias2.hA"],
service_type='LO')
# hB LO (loopback)
new_service_host(hostname=hostname_B+"_LO",
cname_aliases=["alias1.hB"],
service_type='LO')
# hA-hB TRUNK
trunkAB_network_extattrs = {
"vrf_name": {"value": "dummy_vrf"},
}
trunkAB_host_extattrs = {
"Site": {"value": "dummy_site"},
}
trunkAB_service_networks = new_service_networks(
service_type='TRUNK',
extattrs=trunkAB_network_extattrs,
comment="Network for hA-hB TRUNK"
)
trunkAB_v4_host_address = trunkAB_service_networks.v4.network_address
trunkAB_v6_host_address = trunkAB_service_networks.v6.network_address
trunkAB_host_addresses = HostAddresses(v4=trunkAB_v4_host_address,
v6=trunkAB_v6_host_address)
new_service_host(hostname=hostname_A+"_TRUNK",
service_type='TRUNK',
host_addresses=trunkAB_host_addresses,
extattrs=trunkAB_host_extattrs)
new_service_host(hostname=hostname_B+"_TRUNK",
service_type='TRUNK',
service_networks=trunkAB_service_networks,
extattrs=trunkAB_host_extattrs)
```
#### Host/network deletion
```
# Delete network
service_network = ipam.delete_service_network(
network=ipaddress.ip_network('10.255.255.0/26'), service_type='LO'
)
# Delete host
input_host_addresses = ipam.HostAddresses(
v4=ipaddress.ip_address('10.255.255.1'),
v6=ipaddress.ip_address('dead:beef::1')
)
host_addresses = ipam.delete_service_host(
hostname='ha_lo',
host_addresses=input_host_addresses,
cname_aliases=['alias1.ha', 'alias2.ha'],
service_type='LO'
)
```
This diff is collapsed.
import ipaddress import ipaddress
from pydantic import BaseSettings from pydantic import BaseSettings
from typing import Union
from gso.services import _ipam from gso.services import _ipam
...@@ -30,7 +31,7 @@ class HostAddresses(BaseSettings): ...@@ -30,7 +31,7 @@ class HostAddresses(BaseSettings):
v6: ipaddress.IPv6Address v6: ipaddress.IPv6Address
def new_service_networks(service_type, def new_service_networks(service_type='',
comment="", comment="",
extattrs={}) -> ServiceNetworks: extattrs={}) -> ServiceNetworks:
v4_service_network = _ipam.allocate_service_ipv4_network( v4_service_network = _ipam.allocate_service_ipv4_network(
...@@ -43,7 +44,7 @@ def new_service_networks(service_type, ...@@ -43,7 +44,7 @@ def new_service_networks(service_type,
def new_service_host(hostname, def new_service_host(hostname,
service_type, service_type='',
service_networks: ServiceNetworks = None, service_networks: ServiceNetworks = None,
host_addresses: HostAddresses = None, host_addresses: HostAddresses = None,
cname_aliases=None, cname_aliases=None,
...@@ -57,53 +58,24 @@ def new_service_host(hostname, ...@@ -57,53 +58,24 @@ def new_service_host(hostname,
extattrs=extattrs) extattrs=extattrs)
if __name__ == '__main__': def delete_service_network(
# sample call flow to allocate two loopback interfaces and a trunk service network: ipaddress.ip_network = None, service_type=''
# new_service_host can be called passing networks, addresses, or nothing. ) -> Union[V4ServiceNetwork, V6ServiceNetwork]:
# - host h1 for service TRUNK uses a specific ipv4/ipv6 address pair return _ipam.delete_service_network(
# - host h2 for service TRUNK uses the ipv4/ipv6 network pair ipnetwork=network,
# - service LO uses nothing service_type=service_type
# networks and hosts can be allocated with extensible attributes
# networks can be created with a comment
# CNAME records can be optionally created
hostname_A = 'hA'
hostname_B = 'hB'
# h1 LO (loopback)
new_service_host(hostname=hostname_A+"_LO",
cname_aliases=["alias1.hA", "alias2.hA"],
service_type='LO')
# h2 LO (loopback)
new_service_host(hostname=hostname_B+"_LO",
cname_aliases=["alias1.hB"],
service_type='LO')
# h1-h2 TRUNK
trunk12_network_extattrs = {
"vrf_name": {"value": "dummy_vrf"},
}
trunk12_host_extattrs = {
"Site": {"value": "dummy_site"},
}
trunk12_service_networks = new_service_networks(
service_type='TRUNK',
extattrs=trunk12_network_extattrs,
comment="Network for h1-h2 TRUNK"
) )
trunk12_v4_host_address = trunk12_service_networks.v4.network_address
trunk12_v6_host_address = trunk12_service_networks.v6.network_address
trunk12_host_addresses = HostAddresses(v4=trunk12_v4_host_address,
v6=trunk12_v6_host_address)
new_service_host(hostname=hostname_A+"_TRUNK", def delete_service_host(
service_type='TRUNK', hostname='',
host_addresses=trunk12_host_addresses, host_addresses: HostAddresses = None,
extattrs=trunk12_host_extattrs) cname_aliases=[],
service_type=''
new_service_host(hostname=hostname_B+"_TRUNK", ) -> HostAddresses:
service_type='TRUNK', return _ipam.delete_service_host(
service_networks=trunk12_service_networks, hostname=hostname,
extattrs=trunk12_host_extattrs) host_addresses=host_addresses,
cname_aliases=cname_aliases,
service_type=service_type
)
...@@ -33,7 +33,7 @@ class V4NetworkParams(BaseSettings): ...@@ -33,7 +33,7 @@ class V4NetworkParams(BaseSettings):
""" """
containers: list[ipaddress.IPv4Network] containers: list[ipaddress.IPv4Network]
networks: list[ipaddress.IPv4Network] networks: list[ipaddress.IPv4Network]
mask: int = Field(None, ge=0, le=32) mask: int # TODO: validation on mask?
class V6NetworkParams(BaseSettings): class V6NetworkParams(BaseSettings):
...@@ -42,7 +42,7 @@ class V6NetworkParams(BaseSettings): ...@@ -42,7 +42,7 @@ class V6NetworkParams(BaseSettings):
""" """
containers: list[ipaddress.IPv6Network] containers: list[ipaddress.IPv6Network]
networks: list[ipaddress.IPv6Network] networks: list[ipaddress.IPv6Network]
mask: int = Field(None, ge=0, le=128) mask: int # TODO: validation on mask?
class ServiceNetworkParams(BaseSettings): class ServiceNetworkParams(BaseSettings):
...@@ -53,6 +53,7 @@ class ServiceNetworkParams(BaseSettings): ...@@ -53,6 +53,7 @@ class ServiceNetworkParams(BaseSettings):
V4: V4NetworkParams V4: V4NetworkParams
V6: V6NetworkParams V6: V6NetworkParams
domain_name: str domain_name: str
dns_view: str
class IPAMParams(BaseSettings): class IPAMParams(BaseSettings):
......
...@@ -19,7 +19,6 @@ from gso.products.product_types import device ...@@ -19,7 +19,6 @@ from gso.products.product_types import device
from gso.products.product_types.device import DeviceInactive, \ from gso.products.product_types.device import DeviceInactive, \
DeviceProvisioning DeviceProvisioning
from gso.products.product_types.site import Site from gso.products.product_types.site import Site
# noinspection PyProtectedMember
from gso.services import _ipam from gso.services import _ipam
from gso.services import provisioning_proxy from gso.services import provisioning_proxy
from gso.services.provisioning_proxy import await_pp_results, \ from gso.services.provisioning_proxy import await_pp_results, \
...@@ -97,14 +96,11 @@ def get_info_from_ipam(subscription: DeviceProvisioning) -> State: ...@@ -97,14 +96,11 @@ def get_info_from_ipam(subscription: DeviceProvisioning) -> State:
subscription.device.device_lo_iso_address \ subscription.device.device_lo_iso_address \
= iso_from_ipv4(str(subscription.device.device_lo_ipv4_address)) = iso_from_ipv4(str(subscription.device.device_lo_ipv4_address))
subscription.device.device_si_ipv4_network \ subscription.device.device_si_ipv4_network \
= _ipam.allocate_service_ipv4_network(service_type='SI', = _ipam.allocate_service_ipv4_network(service_type='SI', comment=f"SI for {lo0_name}").v4
comment=f'SI for {lo0_name}').v4
subscription.device.device_ias_lt_ipv4_network \ subscription.device.device_ias_lt_ipv4_network \
= _ipam.allocate_service_ipv4_network(service_type='LT_IAS', = _ipam.allocate_service_ipv4_network(service_type='LT_IAS', comment=f"LT for {lo0_name}").v4
comment=f'LT for {lo0_name}').v4
subscription.device.device_ias_lt_ipv6_network \ subscription.device.device_ias_lt_ipv6_network \
= _ipam.allocate_service_ipv6_network(service_type='LT_IAS', = _ipam.allocate_service_ipv6_network(service_type='LT_IAS', comment=f"LT for {lo0_name}").v6
comment=f'LT for {lo0_name}').v6
return {'subscription': subscription} return {'subscription': subscription}
......
...@@ -17,7 +17,6 @@ from gso.products.product_blocks.iptrunk import IptrunkType ...@@ -17,7 +17,6 @@ from gso.products.product_blocks.iptrunk import IptrunkType
from gso.products.product_types.device import Device from gso.products.product_types.device import Device
from gso.products.product_types.iptrunk import IptrunkInactive, \ from gso.products.product_types.iptrunk import IptrunkInactive, \
IptrunkProvisioning IptrunkProvisioning
# noinspection PyProtectedMember
from gso.services import provisioning_proxy, _ipam from gso.services import provisioning_proxy, _ipam
from gso.services.provisioning_proxy import confirm_pp_results, \ from gso.services.provisioning_proxy import confirm_pp_results, \
await_pp_results await_pp_results
...@@ -106,16 +105,11 @@ def create_subscription(product: UUIDstr) -> State: ...@@ -106,16 +105,11 @@ def create_subscription(product: UUIDstr) -> State:
@step('Get information from IPAM') @step('Get information from IPAM')
def get_info_from_ipam(subscription: IptrunkProvisioning) -> State: def get_info_from_ipam(subscription: IptrunkProvisioning) -> State:
# TODO: get info about how these should be generated
subscription.iptrunk.iptrunk_ipv4_network \ subscription.iptrunk.iptrunk_ipv4_network \
= _ipam.allocate_service_ipv4_network( = _ipam.allocate_service_ipv4_network(service_type="TRUNK", comment=subscription.iptrunk.iptrunk_description).v4
service_type='TRUNK',
comment=subscription.iptrunk.iptrunk_description
).v4
subscription.iptrunk.iptrunk_ipv6_network \ subscription.iptrunk.iptrunk_ipv6_network \
= _ipam.allocate_service_ipv6_network( = _ipam.allocate_service_ipv6_network(service_type="TRUNK", comment=subscription.iptrunk.iptrunk_description).v6
service_type='TRUNK',
comment=subscription.iptrunk.iptrunk_description
).v6
return {'subscription': subscription} return {'subscription': subscription}
......
...@@ -27,11 +27,18 @@ def configuration_data(): ...@@ -27,11 +27,18 @@ def configuration_data():
"password": "robot-user-password" "password": "robot-user-password"
}, },
"LO": { "LO": {
"V4": {"containers": ["10.255.255.0/24"], "networks": [], "V4": {
"mask": 32}, "containers": [],
"V6": {"containers": ["dead:beef::/64"], "networks": [], "networks": ["10.255.255.0/26"],
"mask": 128}, "mask": 32
"domain_name": ".lo" },
"V6": {
"containers": [],
"networks": ["dead:beef::/80"],
"mask": 128
},
"domain_name": ".lo",
"dns_view": "default"
}, },
"TRUNK": { "TRUNK": {
"V4": { "V4": {
...@@ -44,7 +51,8 @@ def configuration_data(): ...@@ -44,7 +51,8 @@ def configuration_data():
"networks": [], "networks": [],
"mask": 126 "mask": 126
}, },
"domain_name": ".trunk" "domain_name": ".trunk",
"dns_view": "default"
}, },
"GEANT_IP": { "GEANT_IP": {
"V4": { "V4": {
...@@ -57,7 +65,8 @@ def configuration_data(): ...@@ -57,7 +65,8 @@ def configuration_data():
"networks": [], "networks": [],
"mask": 126 "mask": 126
}, },
"domain_name": ".geantip" "domain_name": ".geantip",
"dns_view": "default"
}, },
"SI": { "SI": {
"V4": { "V4": {
...@@ -70,7 +79,8 @@ def configuration_data(): ...@@ -70,7 +79,8 @@ def configuration_data():
"networks": [], "networks": [],
"mask": 126 "mask": 126
}, },
"domain_name": ".geantip" "domain_name": ".geantip",
"dns_view": "default"
}, },
"LT_IAS": { "LT_IAS": {
"V4": { "V4": {
...@@ -83,7 +93,8 @@ def configuration_data(): ...@@ -83,7 +93,8 @@ def configuration_data():
"networks": [], "networks": [],
"mask": 126 "mask": 126
}, },
"domain_name": ".geantip" "domain_name": ".geantip",
"dns_view": "default"
} }
}, },
"PROVISIONING_PROXY": { "PROVISIONING_PROXY": {
......
import ipaddress import ipaddress
import pytest
import re import re
import responses import responses
...@@ -12,8 +13,7 @@ def test_new_service_networks(data_config_filename): ...@@ -12,8 +13,7 @@ def test_new_service_networks(data_config_filename):
method=responses.POST, method=responses.POST,
url=re.compile(r'.*/wapi.*/network.*'), url=re.compile(r'.*/wapi.*/network.*'),
json={ json={
'_ref': 'network/ZG5zLm5ldHdvcmskMTAuMjU1LjI1NS4yMC8zMi8w:10.255.2' '_ref': 'network/ZG5zLm5ldHdvcmskMTAuMjU1LjI1NS4yMC8zMi8w:10.255.255.20/32/default', # noqa: E501
'55.20/32/default', # noqa: E501
'network': '10.255.255.20/32' 'network': '10.255.255.20/32'
} }
) )
...@@ -22,88 +22,126 @@ def test_new_service_networks(data_config_filename): ...@@ -22,88 +22,126 @@ def test_new_service_networks(data_config_filename):
method=responses.POST, method=responses.POST,
url=re.compile(r'.*/wapi.*/ipv6network.*'), url=re.compile(r'.*/wapi.*/ipv6network.*'),
json={ json={
'_ref': 'ipv6network/ZG5zLm5ldHdvcmskZGVhZDpiZWVmOjoxOC8xMjgvMA:de' '_ref': 'ipv6network/ZG5zLm5ldHdvcmskZGVhZDpiZWVmOjoxOC8xMjgvMA:dead%3Abeef%3A%3A18/128/default', # noqa: E501
'ad%3Abeef%3A%3A18/128/default', # noqa: E501
'network': 'dead:beef::18/128' 'network': 'dead:beef::18/128'
} }
) )
service_networks = ipam.new_service_networks(service_type='LO') service_networks = ipam.new_service_networks(service_type='TRUNK')
assert service_networks == ipam.ServiceNetworks( assert service_networks == ipam.ServiceNetworks(
v4=ipaddress.ip_network('10.255.255.20/32'), v4=ipaddress.ip_network('10.255.255.20/32'),
v6=ipaddress.ip_network('dead:beef::18/128') v6=ipaddress.ip_network('dead:beef::18/128')
) )
# should fail because this service type has networks instead of containers
with pytest.raises(AssertionError):
service_networks = ipam.new_service_networks(service_type='LO')
assert service_networks is None
@responses.activate @responses.activate
def test_new_service_host(data_config_filename): def test_new_service_host(data_config_filename):
responses.add( responses.add(
method=responses.POST, method=responses.POST,
url=re.compile(r'.*/wapi.*/record:host$'), url=re.compile(r'.*/wapi.*/record:host$'),
json='record:host/ZG5zLmhvc3QkLm5vbl9ETlNfaG9zdF9yb290LjAuMTY4MzcwNTU4' json='record:host/ZG5zLmhvc3QkLm5vbl9ETlNfaG9zdF9yb290LjAuMTY4MzcwNTU4MzY3MC5nc28udGVzdA:test.lo/%20' # noqa: E501
'MzY3MC5nc28udGVzdA:test.lo/%20' # noqa: E501
) )
responses.add( responses.add(
method=responses.POST, method=responses.POST,
url=re.compile(r'.*/wapi.*/record:a$'), url=re.compile(r'.*/wapi.*/record:a$'),
json='record:a/ZG5zLmJpbmRfYSQuX2RlZmF1bHQuZ3NvLHRlc3QsMTAuMjU1LjI1NS4' json='record:a/ZG5zLmJpbmRfYSQuX2RlZmF1bHQuZ3NvLHRlc3QsMTAuMjU1LjI1NS44:test.lo/default' # noqa: E501
'4:test.lo/default' # noqa: E501
) )
responses.add( responses.add(
method=responses.POST, method=responses.POST,
url=re.compile(r'.*/wapi.*/record:aaaa$'), url=re.compile(r'.*/wapi.*/record:aaaa$'),
json='record:aaaa/ZG5zLmJpbmRfYSQuX2RlZmF1bHQuZ3NvLHRlc3QsMTAuMjU1LjI1' json='record:aaaa/ZG5zLmJpbmRfYSQuX2RlZmF1bHQuZ3NvLHRlc3QsMTAuMjU1LjI1NS44:test.lo/default' # noqa: E501
'NS44:test.lo/default' # noqa: E501
) )
responses.add( responses.add(
method=responses.GET, method=responses.GET,
url=re.compile(r'.*/wapi.*/network.*'), url=re.compile(r'.*/wapi.*/network.*10.255.255.*'),
json=[ json=[
{ {
"_ref": "network/ZG5zLm5ldHdvcmskMTAuMjU1LjI1NS4yMC8zMi8w:10.2" "_ref": "network/ZG5zLm5ldHdvcmskMTAuMjU1LjI1NS4yMC8zMi8w:10.255.255.20/32/default", # noqa: E501
"55.255.20/32/default", # noqa: E501
"network": "10.255.255.20/32", "network": "10.255.255.20/32",
"network_view": "default" "network_view": "default"
} }
] ]
)
responses.add(
method=responses.GET,
url=re.compile(r'.*/wapi.*/network.*10.255.254.*'),
json=[
{
"_ref": "network/ZG5zLm5Gd0VHQkRQUjMzLjMwNzIuMzE1LzAyLzI:10.255.254.20/32/default", # noqa: E501
"network": "10.255.254.20/32",
"network_view": "default"
}
]
) )
responses.add( responses.add(
method=responses.GET, method=responses.GET,
url=re.compile(r'.*/wapi.*/ipv6network.*'), url=re.compile(r'.*/wapi.*/ipv6network.*dead.*beef.*'),
json=[ json=[
{ {
"_ref": "ipv6network/ZG5zLm5ldHdvcmskZGVhZDpiZWVmOjoxOC8xMjgvM" "_ref": "ipv6network/ZG5zLm5ldHdvcmskZGVhZDpiZWVmOjoxOC8xMjgvMA:dead%3Abeef%3A%3A18/128/default", # noqa: E501
"A:dead%3Abeef%3A%3A18/128/default", # noqa: E501
"network": "dead:beef::18/128", "network": "dead:beef::18/128",
"network_view": "default" "network_view": "default"
} }
] ]
) )
responses.add(
method=responses.GET,
url=re.compile(r'.*/wapi.*/ipv6network.*beef.*dead.*'),
json=[]
)
responses.add( responses.add(
method=responses.POST, method=responses.POST,
url=re.compile( url=re.compile(r'.*/wapi.*/network/.*10.255.255.*?_function=next_available_ip&num=1$'), # noqa: E501
r'.*/wapi.*/network.*/.*?_function=next_available_ip&num=1.*'),
# noqa: E501
json={'ips': ['10.255.255.20']} json={'ips': ['10.255.255.20']}
) )
responses.add( responses.add(
method=responses.POST, method=responses.POST,
url=re.compile( url=re.compile(r'.*/wapi.*/network/.*10.255.254.*?_function=next_available_ip&num=1$'), # noqa: E501
r'.*/wapi.*/ipv6network.*/.*?_function=next_available_ip&num=1.*'), body="Cannot find 1 available IP address(es) in this network",
# noqa: E501 status=400
)
responses.add(
method=responses.POST,
url=re.compile(r'.*/wapi.*/ipv6network/.*?_function=next_available_ip&num=1$'), # noqa: E501
json={'ips': ['dead:beef::18']} json={'ips': ['dead:beef::18']}
) )
responses.add(
method=responses.POST,
url=re.compile(r'.*/wapi.*/network.*_return_fields.*'),
json={
'_ref': 'network/ZG5zLm5ldHdvcmskMTAuMjU1LjI1NS4yMC8zMi8w:10.255.255.20/32/default', # noqa: E501
'network': '10.255.255.20/32'
}
)
responses.add(
method=responses.POST,
url=re.compile(r'.*/wapi.*/ipv6network.*_return_fields.*'),
json={
'_ref': 'ipv6network/ZG5zLm5ldHdvcmskZGVhZDpiZWVmOjoxOC8xMjgvMA:dead%3Abeef%3A%3A18/128/default', # noqa: E501
'network': 'dead:beef::18/128'
}
)
# test host creation by IP addresses
service_hosts = ipam.new_service_host( service_hosts = ipam.new_service_host(
hostname='test', hostname='test',
service_type='LO', service_type='TRUNK',
host_addresses=ipam.HostAddresses( host_addresses=ipam.HostAddresses(
v4=ipaddress.ip_address('10.255.255.20'), v4=ipaddress.ip_address('10.255.255.20'),
v6=ipaddress.ip_address('dead:beef::18') v6=ipaddress.ip_address('dead:beef::18')
...@@ -114,9 +152,10 @@ def test_new_service_host(data_config_filename): ...@@ -114,9 +152,10 @@ def test_new_service_host(data_config_filename):
v6=ipaddress.ip_address('dead:beef::18') v6=ipaddress.ip_address('dead:beef::18')
) )
# test host creation by network addresses
service_hosts = ipam.new_service_host( service_hosts = ipam.new_service_host(
hostname='test', hostname='test',
service_type='LO', service_type='TRUNK',
service_networks=ipam.ServiceNetworks( service_networks=ipam.ServiceNetworks(
v4=ipaddress.ip_network('10.255.255.20/32'), v4=ipaddress.ip_network('10.255.255.20/32'),
v6=ipaddress.ip_network('dead:beef::18/128') v6=ipaddress.ip_network('dead:beef::18/128')
...@@ -126,3 +165,245 @@ def test_new_service_host(data_config_filename): ...@@ -126,3 +165,245 @@ def test_new_service_host(data_config_filename):
v4=ipaddress.ip_address('10.255.255.20'), v4=ipaddress.ip_address('10.255.255.20'),
v6=ipaddress.ip_address('dead:beef::18') v6=ipaddress.ip_address('dead:beef::18')
) )
# test host creation by just service_type when service cfg uses networks
service_hosts = ipam.new_service_host(
hostname='test',
service_type='LO'
)
assert service_hosts == ipam.HostAddresses(
v4=ipaddress.ip_address('10.255.255.20'),
v6=ipaddress.ip_address('dead:beef::18')
)
# test host creation by just service_type when service cfg uses containers
service_hosts = ipam.new_service_host(
hostname='test',
service_type='TRUNK'
)
assert service_hosts == ipam.HostAddresses(
v4=ipaddress.ip_address('10.255.255.20'),
v6=ipaddress.ip_address('dead:beef::18')
)
# test host creation that should return a no available IP error
with pytest.raises(AssertionError):
service_hosts = ipam.new_service_host(
hostname='test',
service_type='TRUNK',
service_networks=ipam.ServiceNetworks(
v4=ipaddress.ip_network('10.255.254.20/32'),
v6=ipaddress.ip_network('dead:beef::18/128')
)
)
assert service_hosts is None
# test host creation that should return a network not exist error
with pytest.raises(AssertionError):
service_hosts = ipam.new_service_host(
hostname='test',
service_type='TRUNK',
service_networks=ipam.ServiceNetworks(
v4=ipaddress.ip_network('10.255.255.20/32'),
v6=ipaddress.ip_network('beef:dead::18/128')
)
)
assert service_hosts is None
@responses.activate
def test_delete_service_network(data_config_filename):
responses.add(
method=responses.GET,
url=re.compile(r'.*/wapi.*/network.*10.255.255.0.*'),
json=[
{
"_ref": "network/ZG5zLm5ldHdvcmskMTAuMjU1LjI1NS4yMC8zMi8w:10.255.255.0/26/default", # noqa: E501
"network": "10.255.255.0/26",
"network_view": "default"
}
]
)
responses.add(
method=responses.GET,
url=re.compile(r'.*/wapi.*/network.*10.255.255.20.*'),
json=[
{
"_ref": "network/ZG5zLm5Gd0VHQkRQUjMzLjMwNzIuMzE1LzAyLzI:100.255.255.20/32/default", # noqa: E501
"network": "100.255.255.20/32",
"network_view": "default"
}
]
)
responses.add(
method=responses.GET,
url=re.compile(r'.*/wapi.*/ipv6network.*dead.*beef.*'),
json=[
{
"_ref": "ipv6network/ZG5zLm5ldHdvcmskZGVhZDpiZWVmOjoxOC8xMjgvMA:dead%3Abeef%3A%3A18/128/default", # noqa: E501
"network": "dead:beef::18/128",
"network_view": "default"
}
]
)
responses.add(
method=responses.GET,
url=re.compile(r'.*/wapi.*/ipv6network.*beef.*dead.*'),
json=[
{
"_ref": "ipv6network/ZG5zLm5ldHdvcmskZGVhZDpiZWVmOjoxOC8xMjgvMA:beef%3Adead%3A%3A18/128/default", # noqa: E501
"network": "beef:dead::18/128",
"network_view": "default"
}
]
)
responses.add(
method=responses.DELETE,
url=re.compile(r'.*/wapi.*/network.*10.255.255.0.*'),
body="network/ZG5zLm5ldHdvcmskMTAuMjU1LjI1NS4yMC8zMi8w:10.255.255.0/26/default" # noqa: E501
)
responses.add(
method=responses.DELETE,
url=re.compile(r'.*/wapi.*/network.*100.255.255.*'),
body="network/ZG5zLm5Gd0VHQkRQUjMzLjMwNzIuMzE1LzAyLzI:100.255.255.20/32/default" # noqa: E501
)
responses.add(
method=responses.DELETE,
url=re.compile(r'.*/wapi.*/ipv6network.*dead.*beef.*'),
body="ipv6network/ZG5zLm5ldHdvcmskZGVhZDpiZWVmOjoxOC8xMjgvMA:dead%3Abeef%3A%3A18/128/default" # noqa: E501
)
responses.add(
method=responses.DELETE,
url=re.compile(r'.*/wapi.*/ipv6network.*beef.*dead.*'),
body="ipv6network/ZG5zLm5ldHdvcmskZGVhZDpiZWVmOjoxOC8xMjgvMA:beef%3Adead%3A%3A18/128/default" # noqa: E501
)
service_network = ipam.delete_service_network(
network=ipaddress.ip_network('10.255.255.0/26'), service_type='LO'
)
assert service_network == ipam.V4ServiceNetwork(
v4=ipaddress.ip_network('10.255.255.0/26')
)
with pytest.raises(AssertionError):
service_network = ipam.delete_service_network(
network=ipaddress.ip_network('10.255.255.20/32'),
service_type='LO'
)
assert service_network is None
service_network = ipam.delete_service_network(
network=ipaddress.ip_network('dead:beef::18/128'),
service_type='TRUNK'
)
assert service_network == ipam.V6ServiceNetwork(
v6=ipaddress.ip_network('dead:beef::18/128')
)
with pytest.raises(AssertionError):
service_network = ipam.delete_service_network(
network=ipaddress.ip_network('beef:dead::18/128'),
service_type='TRUNK'
)
assert service_network is None
@responses.activate
def test_delete_service_host(data_config_filename):
responses.add(
method=responses.GET,
url=re.compile(r'.*/wapi.*record:host.*'),
json=[
{
'_ref': 'record:host/ZG5zLmhvc3QkLl9kZWZhdWx0Lmdzby5oYV9sbw:ha_lo.gso/default', # noqa: E501
'ipv4addrs': [
{
'_ref': 'record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQuZ3NvLmhhX2xvLjEwLjI1NS4yNTUuMS40.255.255.1/ha_lo.gso/default', # noqa: E501
'configure_for_dhcp': False,
'host': 'ha_lo.gso', 'ipv4addr': '10.255.255.1'
}
],
'ipv6addrs': [
{
'_ref': 'record:host_ipv6addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQuZvLmhhX2xvLmRlYWQ6YmVlZjo6MS4:dead%3Abeef%3A%3A1/ha_lo.gso/default', # noqa: E501
'configure_for_dhcp': False,
'host': 'ha_lo.gso', 'ipv6addr': 'dead:beef::1'
}
],
'name': 'ha_lo.gso', 'view': 'default'
}
]
)
responses.add(
method=responses.GET,
url=re.compile(r'.*/wapi.*record:cname.*'),
json=[
{
'_ref': 'record:cname/ZG5zLmJpbmRfY25hbWUkLl9kZWZhdWx0Lmdzby5oYS5hbGlhczE:alias1.ha.gso/default', # noqa: E501
'canonical': 'hA_LO.lo', 'name': 'alias1.ha.lo',
'view': 'default'
},
{
'_ref': 'record:cname/5zLmJpbmRfY25hbWUkLl9kZWZhdWx0Lmdzby5oYS5hbGlhczI:alias2.ha.gso/default', # noqa: E501
'canonical': 'hA_LO.lo', 'name': 'alias2.ha.lo',
'view': 'default'
}
]
)
responses.add(
method=responses.DELETE,
url=re.compile(r'.*/wapi.*record:host.*'),
body='record:host/ZG5zLmhvc3QkLl9kZWZhdWx0Lmdzby5oYl9sbw:hb_lo.gso/default' # noqa: E501
)
responses.add(
method=responses.DELETE,
url=re.compile(r'.*/wapi.*record:cname.*'),
body='record:cname/ZG5zLmJpbmRfY25hbWUkLl9kZWZhdWx0Lmdzby5oYi5hbGlhczE:alias1.hb.gso/default' # noqa: E501
)
input_host_addresses = ipam.HostAddresses(
v4=ipaddress.ip_address('10.255.255.1'),
v6=ipaddress.ip_address('dead:beef::1')
)
host_addresses = ipam.delete_service_host(
hostname='ha_lo',
host_addresses=input_host_addresses,
cname_aliases=['alias1.ha', 'alias2.ha'],
service_type='LO'
)
assert host_addresses == ipam.HostAddresses(
v4=ipaddress.ip_address('10.255.255.1'),
v6=ipaddress.ip_address('dead:beef::1')
)
# Fail because missing CNAME
with pytest.raises(AssertionError):
host_addresses = ipam.delete_service_host(
hostname='ha_lo',
host_addresses=input_host_addresses,
cname_aliases=['alias1.ha'],
service_type='LO'
)
assert host_addresses is None
# Fail because non-matching CNAME
with pytest.raises(AssertionError):
host_addresses = ipam.delete_service_host(
hostname='ha_lo',
host_addresses=input_host_addresses,
cname_aliases=['alias1.ha', 'alias2.ha', 'alias3.ha'],
service_type='LO'
)
assert host_addresses is None
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment