@@ -555,6 +555,10 @@ class ServiceImportForm(NetBoxModelImportForm):
555
555
to_field_name = 'name' ,
556
556
help_text = _ ('Parent object name' )
557
557
)
558
+ parent_object_id = forms .IntegerField (
559
+ required = False ,
560
+ help_text = _ ('Parent object ID' ),
561
+ )
558
562
protocol = CSVChoiceField (
559
563
label = _ ('Protocol' ),
560
564
choices = ServiceProtocolChoices ,
@@ -570,8 +574,7 @@ class ServiceImportForm(NetBoxModelImportForm):
570
574
class Meta :
571
575
model = Service
572
576
fields = (
573
- 'parent_object_type' , 'ipaddresses' , 'name' , 'protocol' , 'ports' , 'description' , 'comments' ,
574
- 'tags' , 'parent_object_id' ,
577
+ 'ipaddresses' , 'name' , 'protocol' , 'ports' , 'description' , 'comments' , 'tags' ,
575
578
)
576
579
577
580
def __init__ (self , data = None , * args , ** kwargs ):
@@ -603,16 +606,20 @@ def clean(self):
603
606
parent = parent_ct .model_class ().objects .filter (id = parent_id ).first ()
604
607
self .cleaned_data ['parent' ] = parent
605
608
else :
606
- # If a parent object type is passed and we've made it to here, then raise a validation
607
- # error since an associated parent object or parent object id has nto be passed
609
+ # If a parent object type is passed and we've made it here, then raise a validation
610
+ # error since an associated parent object or parent object id has not been passed
608
611
raise forms .ValidationError (
609
- _ ("One of parent or parent_object_id needs to be included with parent_object_type" )
612
+ _ ("One of parent or parent_object_id must be included with parent_object_type" )
610
613
)
611
614
615
+ # making sure parent is defined. In cases where an import is resulting in an update, the
616
+ # import data might not include the parent object and so the above logic might not be
617
+ # triggered
618
+ parent = self .cleaned_data .get ('parent' )
612
619
for ip_address in self .cleaned_data .get ('ipaddresses' , []):
613
620
if not ip_address .assigned_object or getattr (ip_address .assigned_object , 'parent_object' ) != parent :
614
621
raise forms .ValidationError (
615
- _ ("{ip} is not assigned to this device/VM ." ).format (ip = ip_address )
622
+ _ ("{ip} is not assigned to this parent ." ).format (ip = ip_address )
616
623
)
617
624
618
625
return self .cleaned_data
0 commit comments