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

Ralph2 sync: network publisher #2620

Merged
merged 6 commits into from
Jul 25, 2016
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion src/ralph/deployment/tests/test_transitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def setUpClass(cls):
network_environment=cls.net_env,
address='10.20.30.0/24',
)
cls.net.reserve_margin_addresses(bottom_count=6)
# reserve 10.20.30.1, 10.20.30.2, 10.20.30.3, 10.20.30.4, 10.20.30.5
cls.net.reserve_margin_addresses(bottom_count=5)
cls.net_2 = NetworkFactory(
network_environment=cls.net_env_2,
address='11.20.30.0/24',
Expand Down
10 changes: 5 additions & 5 deletions src/ralph/networks/models/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def reserved_top(self):
reserved = IPAddress.objects.filter(
network=self, status=IPAddressStatus.reserved
).values_list('number', flat=True).order_by('-number')
return self._get_reserved_count(reserved, self.max_ip)
return self._get_reserved_count(reserved, self.max_ip - 1)

class Meta:
verbose_name = _('network')
Expand Down Expand Up @@ -419,14 +419,14 @@ def reserve_margin_addresses(self, bottom_count=0, top_count=0):
ips = []
existing_ips = set(IPAddress.objects.filter(
Q(
number__gte=self.min_ip,
number__lte=self.min_ip + bottom_count
number__gte=self.min_ip + 1,
number__lte=self.min_ip + bottom_count + 1
) |
Q(number__gte=self.max_ip - top_count, number__lte=self.max_ip)
).values_list('number', flat=True))
to_create = set(chain.from_iterable([
range(int(self.min_ip + 1), int(self.min_ip + bottom_count)),
range(int(self.max_ip - top_count), int(self.max_ip - 1))
range(int(self.min_ip + 1), int(self.min_ip + bottom_count + 1)),
range(int(self.max_ip - top_count), int(self.max_ip))
]))
to_create = to_create - existing_ips
for ip_as_int in to_create:
Expand Down
10 changes: 5 additions & 5 deletions src/ralph/networks/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ def test_reserve_margin_addresses_should_reserve_free_addresses(self):
ip1 = IPAddress.objects.create(address='10.1.1.1')
ip2 = IPAddress.objects.create(address='10.1.1.254')
result = net.reserve_margin_addresses(bottom_count=10, top_count=10)
self.assertEqual(17, net.ips.filter(status=IPAddressStatus.reserved).count()) # noqa
self.assertEqual(17, result[0])
self.assertEqual(18, net.ips.filter(status=IPAddressStatus.reserved).count()) # noqa
self.assertEqual(18, result[0])
self.assertEqual(set([ip1.number, ip2.number]), result[1])

def test_create_ip_address(self):
Expand Down Expand Up @@ -269,7 +269,7 @@ def test_reserved_count(self):
)
self.assertEqual(net.reserved_bottom, 1)
ip21 = IPAddress.objects.create(
address='192.169.58.255', status=IPAddressStatus.reserved
address='192.169.58.254', status=IPAddressStatus.reserved
)
self.assertEqual(net.reserved_top, 1)

Expand All @@ -279,7 +279,7 @@ def test_reserved_count(self):
)
self.assertEqual(net.reserved_bottom, 2)
ip22 = IPAddress.objects.create(
address='192.169.58.254', status=IPAddressStatus.reserved
address='192.169.58.253', status=IPAddressStatus.reserved
)
self.assertEqual(net.reserved_top, 2)

Expand All @@ -288,7 +288,7 @@ def test_reserved_count(self):
address='192.169.58.3', status=IPAddressStatus.reserved
)
IPAddress.objects.create(
address='192.169.58.253', status=IPAddressStatus.reserved
address='192.169.58.252', status=IPAddressStatus.reserved
)
IPAddress.objects.create(
address='192.169.58.10', status=IPAddressStatus.reserved
Expand Down
31 changes: 31 additions & 0 deletions src/ralph/ralph2_sync/publishers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
ImportedObjects
)
from ralph.lib.custom_fields.models import CustomField, CustomFieldTypes
from ralph.networks.models import Network
from ralph.virtual.models import VirtualServer

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -299,3 +300,33 @@ def sync_stacked_switch_to_ralph2(sender, instance=None, created=False, **kwargs
data.update(_add_custom_fields(instance))
data.update(_add_ips(instance))
return data


@ralph2_sync(Network)
def sync_network_to_ralph2(sender, instance=None, created=False, **kwargs):
"""
Network publisher
"""
ralph2_id = _get_obj_id_ralph_2(instance)
data = {
'id': instance.id,
'ralph2_id': ralph2_id,
'name': instance.name,
'address': str(instance.address),
'gateway': str(instance.gateway.address) if instance.gateway else None,
'vlan': instance.vlan,
'remarks': instance.remarks,
'dhcp_broadcast': instance.dhcp_broadcast,
'reserved_bottom': instance.reserved_bottom,
'reserved_top': instance.reserved_top,
'network_environment': _get_obj_id_ralph_2(
instance.network_environment
),
'kind': _get_obj_id_ralph_2(instance.kind),
'dns_servers': [dns.ip_address for dns in instance.dns_servers.all()],
'terminators': [
bo.last_descendant.hostname for bo in instance.terminators.all()
],
'racks': list(map(_get_obj_id_ralph_2, instance.racks.all())),
}
return data
6 changes: 5 additions & 1 deletion src/ralph/ralph2_sync/subscribers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
sync_configuration_class_to_ralph2,
sync_configuration_module_to_ralph2,
sync_dc_asset_to_ralph2,
sync_network_to_ralph2,
sync_stacked_switch_to_ralph2,
sync_virtual_server_to_ralph2
)
Expand Down Expand Up @@ -471,7 +472,10 @@ def sync_virtual_server_to_ralph3(data):
ImportedObjects.create(virtual_server, data['id'])


@sync_subscriber(topic='sync_network_to_ralph3')
@sync_subscriber(
topic='sync_network_to_ralph3',
disable_publishers=[sync_network_to_ralph2]
)
def sync_network_to_ralph3(data):
net, created = _get_obj(Network, data['id'], creating=True)
net.name = data['name']
Expand Down
66 changes: 65 additions & 1 deletion src/ralph/ralph2_sync/tests/test_publishers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@
)
from ralph.data_importer.models import ImportedObjects
from ralph.lib.custom_fields.models import CustomField, CustomFieldTypes
from ralph.networks.tests.factories import IPAddressFactory
from ralph.networks.models import Network
from ralph.networks.tests.factories import (
IPAddressFactory,
NetworkEnvironmentFactory,
NetworkFactory,
NetworkKindFactory
)
from ralph.ralph2_sync.publishers import (
sync_dc_asset_to_ralph2,
sync_model_to_ralph2,
sync_network_to_ralph2,
sync_stacked_switch_to_ralph2,
sync_virtual_server_to_ralph2
)
Expand Down Expand Up @@ -338,3 +345,60 @@ def test_publish_stacked_switch(self):
'children': ['1111', '2222'],
'custom_fields': {'test_field': 'abc'},
})


@override_settings(RALPH2_HERMES_SYNC_ENABLED=True)
class NetworkPublisherTestCase(TestCase):
def setUp(self):
self.net_kind = _create_imported_object(NetworkKindFactory, 1111)
self.net_env = _create_imported_object(NetworkEnvironmentFactory, 2222)
self.net = NetworkFactory(
name='my network',
address='192.168.0.0/15',
vlan=10,
remarks='qwerty',
dhcp_broadcast=False,
network_environment=self.net_env,
kind=self.net_kind,
gateway=IPAddressFactory(address='192.168.0.1'),
)
ImportedObjects.create(
self.net, 10
)
self.net.reserve_margin_addresses(10, 5)
self.terminator1 = DataCenterAssetFactory(hostname='h1.mydc.net')
self.terminator2 = ClusterFactory(hostname='ss1.mydc.net')
self.net.terminators.add(self.terminator1, self.terminator2)
self.net.dns_servers.create(ip_address='1.2.3.4')
self.net.dns_servers.create(ip_address='4.3.2.1')
self.rack1 = _create_imported_object(RackFactory, 11)
self.rack2 = _create_imported_object(RackFactory, 12)
self.net.racks.add(self.rack1, self.rack2)
self.maxDiff = None

@override_settings(RALPH2_HERMES_SYNC_FUNCTIONS=[
'sync_network_to_ralph2'
])
def test_network_publisher(self):
result = sync_network_to_ralph2(Network, self.net)
self.assertCountEqual(
result.pop('terminators'), ['h1.mydc.net', 'ss1.mydc.net']
)
self.assertCountEqual(
result.pop('dns_servers'), ['1.2.3.4', '4.3.2.1']
)
self.assertEqual(result, {
'id': self.net.id,
'ralph2_id': '10',
'name': 'my network',
'address': '192.168.0.0/15',
'gateway': '192.168.0.1',
'vlan': 10,
'remarks': 'qwerty',
'dhcp_broadcast': False,
'reserved_bottom': 10,
'reserved_top': 5,
'network_environment': '2222',
'kind': '1111',
'racks': ['11', '12'],
})