class VMInterfaceCSVForm(CustomFieldModelCSVForm): virtual_machine = CSVModelChoiceField(queryset=VirtualMachine.objects.all(), to_field_name="name") mode = CSVChoiceField( choices=InterfaceModeChoices, required=False, help_text="IEEE 802.1Q operational mode (for L2 interfaces)", ) class Meta: model = VMInterface fields = VMInterface.csv_headers def clean_enabled(self): # Make sure enabled is True when it's not included in the uploaded data if "enabled" not in self.data: return True else: return self.cleaned_data["enabled"]
class ServiceCSVForm(CustomFieldModelCSVForm): device = CSVModelChoiceField( queryset=Device.objects.all(), required=False, to_field_name="name", help_text="Required if not assigned to a VM", ) virtual_machine = CSVModelChoiceField( queryset=VirtualMachine.objects.all(), required=False, to_field_name="name", help_text="Required if not assigned to a device", ) protocol = CSVChoiceField(choices=ServiceProtocolChoices, help_text="IP protocol") class Meta: model = Service fields = Service.csv_headers
class IPAddressCSVForm(StatusModelCSVFormMixin, CustomFieldModelCSVForm): vrf = CSVModelChoiceField( queryset=VRF.objects.all(), to_field_name="name", required=False, help_text="Assigned VRF", ) tenant = CSVModelChoiceField( queryset=Tenant.objects.all(), to_field_name="name", required=False, help_text="Assigned tenant", ) role = CSVChoiceField(choices=IPAddressRoleChoices, required=False, help_text="Functional role") device = CSVModelChoiceField( queryset=Device.objects.all(), required=False, to_field_name="name", help_text="Parent device of assigned interface (if any)", ) virtual_machine = CSVModelChoiceField( queryset=VirtualMachine.objects.all(), required=False, to_field_name="name", help_text="Parent VM of assigned interface (if any)", ) interface = CSVModelChoiceField( queryset=Interface.objects.none(), # Can also refer to VMInterface required=False, to_field_name="name", help_text="Assigned interface", ) is_primary = forms.BooleanField( help_text="Make this the primary IP for the assigned device", required=False) class Meta: model = IPAddress fields = [ "address", "vrf", "tenant", "status", "role", "device", "virtual_machine", "interface", "is_primary", "dns_name", "description", ] def __init__(self, data=None, *args, **kwargs): super().__init__(data, *args, **kwargs) if data: # Limit interface queryset by assigned device if data.get("device"): self.fields["interface"].queryset = Interface.objects.filter( **{ f"device__{self.fields['device'].to_field_name}": data["device"] }) # Limit interface queryset by assigned device elif data.get("virtual_machine"): self.fields["interface"].queryset = VMInterface.objects.filter( **{ f"virtual_machine__{self.fields['virtual_machine'].to_field_name}": data["virtual_machine"] }) def clean(self): super().clean() device = self.cleaned_data.get("device") virtual_machine = self.cleaned_data.get("virtual_machine") is_primary = self.cleaned_data.get("is_primary") # Validate is_primary if is_primary and not device and not virtual_machine: raise forms.ValidationError( "No device or virtual machine specified; cannot set as primary IP" ) def save(self, *args, **kwargs): # Set interface assignment if self.cleaned_data["interface"]: self.instance.assigned_object = self.cleaned_data["interface"] ipaddress = super().save(*args, **kwargs) # Set as primary for device/VM if self.cleaned_data["is_primary"]: parent = self.cleaned_data["device"] or self.cleaned_data[ "virtual_machine"] if self.instance.address.version == 4: parent.primary_ip4 = ipaddress elif self.instance.address.version == 6: parent.primary_ip6 = ipaddress parent.save() return ipaddress