--
You received this message because you are subscribed to the Google Groups "OpenWISP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openwisp+u...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/openwisp/7c95a5ba-d106-4df1-abed-d6a1228ad887%40googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/openwisp/CAERYH6XqBvEA1PiuR9BHv1BaCQKga3DUHo_ZkgcC8Ew%3DwYLTmQ%40mail.gmail.com.
--
You received this message because you are subscribed to the Google Groups "OpenWISP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openwisp+u...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/openwisp/20200205203901.GA7980%40idefix.
To view this discussion on the web, visit https://groups.google.com/d/msgid/openwisp/CAO5jQiZqrFJh%2BjnEcDahzUTvvtiC_XR8g%3D01ej3GYo7_51O3RA%40mail.gmail.com.
from django.apps import AppConfig
from django.db.models.signals import post_save
class ExampleApp(AppConfig):
name = 'openwisp2.example_app'
label = 'example_app'
def ready(self):
from openwisp_controller.config.models import Device
post_save.connect(self.device_saved_receiver,
sender=Device,
dispatch_uid='device_saved_receiver')
@classmethod
def device_saved_receiver(cls, sender, instance, created, **kwargs):
# if not created:
# False
# do something
pass
@classmethod
def device_deleted_receiver(cls, sender, instance, **kwargs):
# may want to do something on delete
pass
--
You received this message because you are subscribed to the Google Groups "OpenWISP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openwisp+u...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/openwisp/80494464-8f8c-407a-aaa8-9f837c350658%40googlegroups.com.
if created: #get device_id and device_org_id in device object
device = Device.objects.first() device_id = str(getattr(device, 'id')) device_org_id = str(getattr(device, 'organization_id')) #define a config object config = Config.objects.filter(device_id=device_id).first # to be use later to filer subnets inside same orgs #get org subnet_id
sub_id= str(r["results"][0]['id']) mask = str(r["results"][0]['subnet'].split('/',3)[1]) req_ip = requests.get('http://127.0.0.1:8000/api/v1/subnet/'+str(sub_id)+'/get-first-available-ip/',auth=('user','pass')).json() p_data = { 'description': 'auto generated ip', 'ip_address': temp_ip, 'subnet': sub_id } #save ip to device's subnet config
r = requests.post('http://127.0.0.1:8000/api/v1/subnet/'+str(sub_id)+'/request-ip/', json=p_data, auth=('user','pass')) #config to be inserted in the database
config_list = {'interfaces': [{'type': 'bridge', 'bridge_members': ['eth0.1', 'eth0.2'], 'name': 'cable', 'disabled': False, 'addresses': [{'proto': 'static', 'family': 'ipv4', 'address': req_ip, 'mask': mask}], 'network': 'cable'}]} #insert json in config column of config_device table
setattr(config, 'config',json.dumps(config_list)) #get device config_id
I've made some more progress in a solution for my initial issue using openwisp_ipam. I've managed to request an IP address and save the resulting ip in the subnet with my signal function, now I need to assign to the static interface config of the device I'm trying to save.
I've defined the JSON with the network interface that I want to use, next step is set column config of the config_config table that has the device id, I'm not so sure how to access the confg_config with the foreign key pointing to my device object, should I import another model?
I noticed that when I'm manually adding the device from the Django admin interface I need to create the device config manually, but devices subscribing to the controller create this entry automatically.
Still need some improvements but the code looks like this:
if created:#get device_id and device_org_id in device objectdevice = Device.objects.first()
device_id = str(getattr(device, 'id'))device_org_id = str(getattr(device, 'organization_id'))#define a config objectconfig = Config.objects.filter(device_id=device_id).first
# to be use later to filer subnets inside same orgs#get org subnet_idsub_id= str(r["results"][0]['id'])mask = str(r["results"][0]['subnet'].split('/',3)[1])
req_ip = requests.get('http://127.0.0.1:8000/api/v1/subnet/'+str(sub_id)+'/get-first-available-ip/',auth=('user','pass')).json()
p_data = { 'description': 'auto generated ip', 'ip_address': temp_ip, 'subnet': sub_id }#save ip to device's subnet configr = requests.post('http://127.0.0.1:8000/api/v1/subnet/'+str(sub_id)+'/request-ip/', json=p_data, auth=('user','pass'))
#config to be inserted in the databaseconfig_list = {'interfaces': [{'type': 'bridge', 'bridge_members': ['eth0.1', 'eth0.2'], 'name': 'cable', 'disabled': False, 'addresses': [{'proto': 'static', 'family': 'ipv4', 'address': req_ip, 'mask': mask}], 'network': 'cable'}]}#insert json in config column of config_device tablesetattr(config, 'config',json.dumps(config_list))