Skip to content

Commit

Permalink
Add signal which send data with old and new value of address and host… (
Browse files Browse the repository at this point in the history
#2776)

Add signal which send data with old and new value of address and hostname
  • Loading branch information
ar4s committed Sep 20, 2016
1 parent 86f7888 commit 012e373
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/ralph/dns/dnsaas.py
Expand Up @@ -200,7 +200,7 @@ def _post(self, url, data):

def delete_dns_record(self, record_id):
"""
Delete record in DNSAAS
Delete record in DNSAAS.
Args:
record_ids: ID's to delete
Expand All @@ -216,3 +216,35 @@ def delete_dns_record(self, record_id):
}
elif response.status_code != 204:
return response.json()

def send_ipaddress_data(self, ip_record_data):
"""
Send data about IP address and hostname.
Args:
ip_record_data: dict contains keys: new, old, action
Structure of parameter:
``
{
'old': {
'address': 127.0.0.1,
'hostname': 'localhost.local'
}
'new': {
'address': 127.0.0.1,
'hostname': 'localhost'
},
'action': 'update'
}
``
``old`` and ``new`` contains previous and next state of
IP address record (in Ralph).
Returns:
JSON response from API
"""
logger.info('Send update data: {}'.format(ip_record_data))
url = self.build_url('ip_record')
response = self._post(url, ip_record_data)
if response.status_code >= 400:
logger.error('DNSaaS returned {}'.format(response.status_code))
return response.json()
1 change: 1 addition & 0 deletions src/ralph/networks/__init__.py
@@ -0,0 +1 @@
default_app_config = 'ralph.networks.apps.Networks'
25 changes: 25 additions & 0 deletions src/ralph/networks/apps.py
@@ -0,0 +1,25 @@
from django.conf import settings
from django.db.models.signals import post_delete, post_save

from ralph.apps import RalphAppConfig


class Networks(RalphAppConfig):
name = 'ralph.networks'

def ready(self):
if not settings.ENABLE_DNSAAS_INTEGRATION:
return
from ralph.networks.receivers import (
delete_dns_record,
update_dns_record
)
ip_model = self.get_model('IPAddress')
post_save.connect(
receiver=update_dns_record,
sender=ip_model
)
post_delete.connect(
receiver=delete_dns_record,
sender=ip_model,
)
2 changes: 2 additions & 0 deletions src/ralph/networks/models/networks.py
Expand Up @@ -20,6 +20,7 @@
AdminAbsoluteUrlMixin,
LastSeenMixin,
NamedMixin,
PreviousStateMixin,
TimeStampMixin
)
from ralph.networks.fields import IPNetwork
Expand Down Expand Up @@ -553,6 +554,7 @@ class IPAddress(
AdminAbsoluteUrlMixin,
LastSeenMixin,
TimeStampMixin,
PreviousStateMixin,
NetworkMixin,
models.Model
):
Expand Down
24 changes: 24 additions & 0 deletions src/ralph/networks/receivers.py
@@ -0,0 +1,24 @@
from ralph.dns.dnsaas import DNSaaS


def update_dns_record(instance, created, *args, **kwargs):
keys = ['address', 'hostname']
data_to_send = {
'old': {
key: instance._previous_state[key] for key in keys
},
'new': {
key: instance.__dict__[key] for key in keys
}
}
data_to_send['action'] = 'add' if created else 'update'
if data_to_send['old']['hostname'] is not None:
DNSaaS().send_ipaddress_data(data_to_send)


def delete_dns_record(instance, *args, **kwargs):
DNSaaS().send_ipaddress_data({
'address': instance.address,
'hostname': instance.hostname,
'action': 'delete'
})

0 comments on commit 012e373

Please sign in to comment.