embedd yaml in json #8056

Closed
opened 2025-12-29 20:31:47 +01:00 by adam · 1 comment
Owner

Originally created by @anushadandaibm on GitHub (May 15, 2023).

NetBox version

v4.3.1

Python version

3.9

Steps to Reproduce

i tried below format while creating device
example 1: {
"device_type": "1",
"serial": "DEV_SER_041344463",
"ibm_serial": "IBM_DEV_SER_0252125623",
"site": "1",
"room": "3",
"rack": 1,
"purchase_order_detail": 1,
"hostname": "test123",
"status": "Active",
"job_id": "3451234",
"job_status": "Active",
"network_context": |
name: VPC
routed_interfaces_v4: []
subinterface_routed_configuration:
- description: >-
DL: L::tor45.1.vpc.qz4.sr07.qzfed-sie01 Ethernet1/1 |
R::m-qz4-sr07-r3-s01.vp3c.ngc.dev s0
device_hostname: tor45.1
}

example 2:

{
"device_type": "1",
"serial": "DEV_SER_041344463",
"ibm_serial": "IBM_DEV_SER_0252125623",
"site": "1",
"room": "3",
"rack": 1,
"purchase_order_detail": 1,
"hostname": "test123",
"status": "Active",
"job_id": "3451234",
"job_status": "Active",
"network_context": "name: VPC\nrouted_interfaces_v4: []\nsubinterface_routed_configuration:\n - description: >-\n DL: L::tor45.1.vpc.qz4.sr07.qzfed-sie01 Ethernet1/1 |\n R::m-qz4-sr07-r3-s01.vp3c.ngc.dev s0\n device_hostname: tor45.1"
}

request is not accepting yaml in network_context

@register_model_view(Device, 'networkconfig', path='network-config')
class DeviceNetworkConfigContextView(generic.ObjectView):
queryset = Device.objects.annotate_config_context_data()
template_name = 'dcim/device/networkconfig.html'
tab = ViewTab(
label=_('Network Configuration'),
permission='extras.view_configcontext',
weight=2000
)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
if self.request.GET.get('format') in ['json', 'yaml']:
format = self.request.GET.get('format')
if format not in ['json', 'yaml']:
raise forms.ValidationError('Invalid format: {}'.format(format))
if self.request.user.is_authenticated:
self.request.user.config.set('extras.configcontext.format', format, commit=True)
elif self.request.user.is_authenticated:
format = self.request.user.config.get('extras.configcontext.format', 'json')
else:
format = 'json'
context.update({
'active_tab': 'network-config',
'format': format
})
return context

def post(self, request, instance):
try:
data = yaml.safe_load(request.body)

    network_context = {'network_context': data.get('network_context')}
    instance.network_context = network_context
    instance.save()

    return JsonResponse({'status': 'success', 'message': 'Network context updated successfully'})

except yaml.YAMLError:
    return JsonResponse({'status': 'error', 'message': 'Invalid YAML format'}, status=400)

except Exception as e:
    return JsonResponse({'status': 'error', 'message': str(e)}, status=400)
    
    
    Below is the code i have used

Expected Behavior

new device should be able to take yaml property inside json

Observed Behavior

request i s not processed successfully

Originally created by @anushadandaibm on GitHub (May 15, 2023). ### NetBox version v4.3.1 ### Python version 3.9 ### Steps to Reproduce i tried below format while creating device example 1: { "device_type": "1", "serial": "DEV_SER_041344463", "ibm_serial": "IBM_DEV_SER_0252125623", "site": "1", "room": "3", "rack": 1, "purchase_order_detail": 1, "hostname": "test123", "status": "Active", "job_id": "3451234", "job_status": "Active", "network_context": | name: VPC routed_interfaces_v4: [] subinterface_routed_configuration: - description: >- DL: L::tor45.1.vpc.qz4.sr07.qzfed-sie01 Ethernet1/1 | R::m-qz4-sr07-r3-s01.vp3c.ngc.dev s0 device_hostname: tor45.1 } example 2: { "device_type": "1", "serial": "DEV_SER_041344463", "ibm_serial": "IBM_DEV_SER_0252125623", "site": "1", "room": "3", "rack": 1, "purchase_order_detail": 1, "hostname": "test123", "status": "Active", "job_id": "3451234", "job_status": "Active", "network_context": "name: VPC\nrouted_interfaces_v4: []\nsubinterface_routed_configuration:\n - description: >-\n DL: L::tor45.1.vpc.qz4.sr07.qzfed-sie01 Ethernet1/1 |\n R::m-qz4-sr07-r3-s01.vp3c.ngc.dev s0\n device_hostname: tor45.1" } request is not accepting yaml in network_context @register_model_view(Device, 'networkconfig', path='network-config') class DeviceNetworkConfigContextView(generic.ObjectView): queryset = Device.objects.annotate_config_context_data() template_name = 'dcim/device/networkconfig.html' tab = ViewTab( label=_('Network Configuration'), permission='extras.view_configcontext', weight=2000 ) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) if self.request.GET.get('format') in ['json', 'yaml']: format = self.request.GET.get('format') if format not in ['json', 'yaml']: raise forms.ValidationError('Invalid format: {}'.format(format)) if self.request.user.is_authenticated: self.request.user.config.set('extras.configcontext.format', format, commit=True) elif self.request.user.is_authenticated: format = self.request.user.config.get('extras.configcontext.format', 'json') else: format = 'json' context.update({ 'active_tab': 'network-config', 'format': format }) return context def post(self, request, instance): try: data = yaml.safe_load(request.body) network_context = {'network_context': data.get('network_context')} instance.network_context = network_context instance.save() return JsonResponse({'status': 'success', 'message': 'Network context updated successfully'}) except yaml.YAMLError: return JsonResponse({'status': 'error', 'message': 'Invalid YAML format'}, status=400) except Exception as e: return JsonResponse({'status': 'error', 'message': str(e)}, status=400) Below is the code i have used ### Expected Behavior new device should be able to take yaml property inside json ### Observed Behavior request i s not processed successfully
adam closed this issue 2025-12-29 20:31:47 +01:00
Author
Owner

@kkthxbye-code commented on GitHub (May 15, 2023):

request is not accepting yaml in network_context

network_context does not exist in the netbox codebase. Your issue is almost impossible to understand with the bad formatting and missing context.

I will close this on the basis that the fields you are using are not present in core netbox, indicating that you probably modified netbox.

@kkthxbye-code commented on GitHub (May 15, 2023): > request is not accepting yaml in network_context `network_context` does not exist in the netbox codebase. Your issue is almost impossible to understand with the bad formatting and missing context. I will close this on the basis that the fields you are using are not present in core netbox, indicating that you probably modified netbox.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#8056