Hey João,
On Mon, Mar 30, 2020 at 1:06 PM João Henrique Albuquerque <
joaohcca...@gmail.com> wrote:
> 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.
>
Great to see you're moving on.
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 did not understand this passage
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 object
>device = Device.objects.first()
>
The receiver function you connect to the signal, will receive the instance
of the device being created, so you don't need to query the DB again.
Moreover, calling first() may not yield the desired result.
>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
>
Try device.config to access the configuration.
try:
device.config
except ObjectDoesNotExist:
# you may create the config object here if it does not exist
# but I'm not sure what happens if you also create the device manually
># to be use later to filer subnets inside same orgs
>#get org subnet_id
>r = requests.get('http://127.0.0.1:8000/api/v1/subnet/',auth=(
> 'user','pass')).json()
>sub_id= str(r["results"][0]['id'])
>mask = str(r["results"][0]['subnet'].split('/',3)[1])
>
You can use the django models to get this information instead of using the
API, it would be a lot faster.
>req_ip = requests.get('http://127.0.0.1:8000/api/v1/subnet/'+str(
> sub_id)+'/get-first-available-ip/',auth=('user','pass')).json()
>
You can use the request_ip method on the Subnet model to do this:
https://github.com/openwisp/django-ipam/blob/master/django_ipam/base/models.py#L74-L85
The API is meant to be used on third-party applications. In this case
instead you have direct access to the python/django API of OpenWISP.
>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'))
>
You can do directly request_ip and avoid get_first_available_ip.
>#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))
>
I believe you can just do:
config.config = config_list
config_list should be renamed to config_dict, since it's a dictionary and
not a list.
I hope this helps.
Federico
--
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+unsubscr...@googlegroups.com.
To view this discussion on the web, visit
https://groups.google.com/d/msgid/openwisp/CAERYH6XHHmAvcTVdWpA7bo31Pjq4oN4TD%2BVhLNv22tUQvtJuwg%40mail.gmail.com.