class CircuitCSVForm(StatusModelCSVFormMixin, CustomFieldModelCSVForm): provider = CSVModelChoiceField( queryset=Provider.objects.all(), to_field_name="name", help_text="Assigned provider", ) type = CSVModelChoiceField( queryset=CircuitType.objects.all(), to_field_name="name", help_text="Type of circuit", ) tenant = CSVModelChoiceField( queryset=Tenant.objects.all(), required=False, to_field_name="name", help_text="Assigned tenant", ) class Meta: model = Circuit fields = [ "cid", "provider", "type", "status", "tenant", "install_date", "commit_rate", "description", "comments", ]
class ClusterCSVForm(CustomFieldModelCSVForm): type = CSVModelChoiceField( queryset=ClusterType.objects.all(), to_field_name="name", help_text="Type of cluster", ) group = CSVModelChoiceField( queryset=ClusterGroup.objects.all(), to_field_name="name", required=False, help_text="Assigned cluster group", ) site = CSVModelChoiceField( queryset=Site.objects.all(), to_field_name="name", required=False, help_text="Assigned site", ) tenant = CSVModelChoiceField( queryset=Tenant.objects.all(), to_field_name="name", required=False, help_text="Assigned tenant", ) class Meta: model = Cluster fields = Cluster.csv_headers
class VirtualMachineCSVForm(StatusModelCSVFormMixin, CustomFieldModelCSVForm): cluster = CSVModelChoiceField( queryset=Cluster.objects.all(), to_field_name="name", help_text="Assigned cluster", ) role = CSVModelChoiceField( queryset=DeviceRole.objects.filter(vm_role=True), required=False, to_field_name="name", help_text="Functional role", ) tenant = CSVModelChoiceField( queryset=Tenant.objects.all(), required=False, to_field_name="name", help_text="Assigned tenant", ) platform = CSVModelChoiceField( queryset=Platform.objects.all(), required=False, to_field_name="name", help_text="Assigned platform", ) class Meta: model = VirtualMachine fields = VirtualMachine.csv_headers
class AggregateCSVForm(PrefixFieldMixin, CustomFieldModelCSVForm): rir = CSVModelChoiceField(queryset=RIR.objects.all(), to_field_name="name", help_text="Assigned RIR") tenant = CSVModelChoiceField( queryset=Tenant.objects.all(), required=False, to_field_name="name", help_text="Assigned tenant", ) class Meta: model = Aggregate fields = Aggregate.csv_headers
class StatusModelCSVFormMixin(CSVModelForm): """Mixin to add a required `status` choice field to CSV import forms.""" status = CSVModelChoiceField( queryset=Status.objects.all(), to_field_name="slug", help_text="Operational status", )
class VLANCSVForm(StatusModelCSVFormMixin, CustomFieldModelCSVForm): site = CSVModelChoiceField( queryset=Site.objects.all(), required=False, to_field_name="name", help_text="Assigned site", ) group = CSVModelChoiceField( queryset=VLANGroup.objects.all(), required=False, to_field_name="name", help_text="Assigned VLAN group", ) tenant = CSVModelChoiceField( queryset=Tenant.objects.all(), to_field_name="name", required=False, help_text="Assigned tenant", ) role = CSVModelChoiceField( queryset=Role.objects.all(), required=False, to_field_name="name", help_text="Functional role", ) class Meta: model = VLAN fields = VLAN.csv_headers help_texts = { "vid": "Numeric VLAN ID (1-4095)", "name": "VLAN name", } def __init__(self, data=None, *args, **kwargs): super().__init__(data, *args, **kwargs) if data: # Limit vlan queryset by assigned group params = { f"site__{self.fields['site'].to_field_name}": data.get("site") } self.fields["group"].queryset = self.fields[ "group"].queryset.filter(**params)
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 VRFCSVForm(CustomFieldModelCSVForm): tenant = CSVModelChoiceField( queryset=Tenant.objects.all(), required=False, to_field_name="name", help_text="Assigned tenant", ) class Meta: model = VRF fields = VRF.csv_headers
class VLANGroupCSVForm(CustomFieldModelCSVForm): site = CSVModelChoiceField( queryset=Site.objects.all(), required=False, to_field_name="name", help_text="Assigned site", ) class Meta: model = VLANGroup fields = VLANGroup.csv_headers
class TenantCSVForm(CustomFieldModelCSVForm): group = CSVModelChoiceField( queryset=TenantGroup.objects.all(), required=False, to_field_name="name", help_text="Assigned group", ) class Meta: model = Tenant fields = Tenant.csv_headers
class TenantGroupCSVForm(CustomFieldModelCSVForm): parent = CSVModelChoiceField( queryset=TenantGroup.objects.all(), required=False, to_field_name="name", help_text="Parent group", ) class Meta: model = TenantGroup fields = TenantGroup.csv_headers
class ProviderNetworkCSVForm(CustomFieldModelCSVForm): provider = CSVModelChoiceField(queryset=Provider.objects.all(), to_field_name="name", help_text="Assigned provider") class Meta: model = ProviderNetwork fields = [ "provider", "name", "slug", "description", "comments", ]
class PrefixCSVForm(PrefixFieldMixin, StatusModelCSVFormMixin, CustomFieldModelCSVForm): vrf = CSVModelChoiceField( queryset=VRF.objects.all(), to_field_name="name", required=False, help_text="Assigned VRF", ) tenant = CSVModelChoiceField( queryset=Tenant.objects.all(), required=False, to_field_name="name", help_text="Assigned tenant", ) site = CSVModelChoiceField( queryset=Site.objects.all(), required=False, to_field_name="name", help_text="Assigned site", ) vlan_group = CSVModelChoiceField( queryset=VLANGroup.objects.all(), required=False, to_field_name="name", help_text="VLAN's group (if any)", ) vlan = CSVModelChoiceField( queryset=VLAN.objects.all(), required=False, to_field_name="vid", help_text="Assigned VLAN", ) role = CSVModelChoiceField( queryset=Role.objects.all(), required=False, to_field_name="name", help_text="Functional role", ) class Meta: model = Prefix fields = Prefix.csv_headers def __init__(self, data=None, *args, **kwargs): super().__init__(data, *args, **kwargs) if data: # Limit vlan queryset by assigned site and group params = { f"site__{self.fields['site'].to_field_name}": data.get("site"), f"group__{self.fields['vlan_group'].to_field_name}": data.get("vlan_group"), } self.fields["vlan"].queryset = self.fields["vlan"].queryset.filter( **params)
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 GitRepositoryCSVForm(CSVModelForm): secrets_group = CSVModelChoiceField( queryset=SecretsGroup.objects.all(), to_field_name="name", required=False, help_text="Secrets group for repository access (if any)", ) class Meta: model = GitRepository fields = GitRepository.csv_headers def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields["provided_contents"] = CSVMultipleChoiceField( choices=get_git_datasource_content_choices(), required=False, help_text=mark_safe( "The data types this repository provides. Multiple values must be comma-separated and wrapped in " 'double quotes (e.g. <code>"extras.job,extras.configcontext"</code>).' ), )
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