mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
#5306: Standardize device/VM assignment for IP addresses & secrets
This commit is contained in:
@ -856,6 +856,7 @@ class IPAddressCSVForm(CustomFieldModelCSVForm):
|
|||||||
)
|
)
|
||||||
status = CSVChoiceField(
|
status = CSVChoiceField(
|
||||||
choices=IPAddressStatusChoices,
|
choices=IPAddressStatusChoices,
|
||||||
|
required=False,
|
||||||
help_text='Operational status'
|
help_text='Operational status'
|
||||||
)
|
)
|
||||||
role = CSVChoiceField(
|
role = CSVChoiceField(
|
||||||
@ -888,7 +889,10 @@ class IPAddressCSVForm(CustomFieldModelCSVForm):
|
|||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = IPAddress
|
model = IPAddress
|
||||||
fields = IPAddress.csv_headers
|
fields = [
|
||||||
|
'address', 'vrf', 'tenant', 'status', 'role', 'device', 'virtual_machine', 'interface', 'is_primary',
|
||||||
|
'dns_name', 'description',
|
||||||
|
]
|
||||||
|
|
||||||
def __init__(self, data=None, *args, **kwargs):
|
def __init__(self, data=None, *args, **kwargs):
|
||||||
super().__init__(data, *args, **kwargs)
|
super().__init__(data, *args, **kwargs)
|
||||||
|
@ -143,31 +143,56 @@ class SecretForm(BootstrapMixin, CustomFieldModelForm):
|
|||||||
|
|
||||||
|
|
||||||
class SecretCSVForm(CustomFieldModelCSVForm):
|
class SecretCSVForm(CustomFieldModelCSVForm):
|
||||||
assigned_object_type = CSVModelChoiceField(
|
|
||||||
queryset=ContentType.objects.all(),
|
|
||||||
limit_choices_to=SECRET_ASSIGNMENT_MODELS,
|
|
||||||
to_field_name='model',
|
|
||||||
help_text='Side A type'
|
|
||||||
)
|
|
||||||
role = CSVModelChoiceField(
|
role = CSVModelChoiceField(
|
||||||
queryset=SecretRole.objects.all(),
|
queryset=SecretRole.objects.all(),
|
||||||
to_field_name='name',
|
to_field_name='name',
|
||||||
help_text='Assigned role'
|
help_text='Assigned role'
|
||||||
)
|
)
|
||||||
|
device = CSVModelChoiceField(
|
||||||
|
queryset=Device.objects.all(),
|
||||||
|
required=False,
|
||||||
|
to_field_name='name',
|
||||||
|
help_text='Assigned device'
|
||||||
|
)
|
||||||
|
virtual_machine = CSVModelChoiceField(
|
||||||
|
queryset=VirtualMachine.objects.all(),
|
||||||
|
required=False,
|
||||||
|
to_field_name='name',
|
||||||
|
help_text='Assigned VM'
|
||||||
|
)
|
||||||
plaintext = forms.CharField(
|
plaintext = forms.CharField(
|
||||||
help_text='Plaintext secret data'
|
help_text='Plaintext secret data'
|
||||||
)
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Secret
|
model = Secret
|
||||||
fields = Secret.csv_headers
|
fields = ['role', 'name', 'plaintext', 'device', 'virtual_machine']
|
||||||
help_texts = {
|
help_texts = {
|
||||||
'name': 'Name or username',
|
'name': 'Name or username',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def clean(self):
|
||||||
|
super().clean()
|
||||||
|
|
||||||
|
device = self.cleaned_data.get('device')
|
||||||
|
virtual_machine = self.cleaned_data.get('virtual_machine')
|
||||||
|
|
||||||
|
# Validate device OR VM is assigned
|
||||||
|
if not device and not virtual_machine:
|
||||||
|
raise forms.ValidationError("Secret must be assigned to a device or a virtual machine")
|
||||||
|
if device and virtual_machine:
|
||||||
|
raise forms.ValidationError("Secret cannot be assigned to both a device and a virtual machine")
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
|
|
||||||
|
# Set device/VM assignment
|
||||||
|
self.instance.assigned_object = self.cleaned_data['device'] or self.cleaned_data['virtual_machine']
|
||||||
|
|
||||||
s = super().save(*args, **kwargs)
|
s = super().save(*args, **kwargs)
|
||||||
|
|
||||||
|
# Set plaintext on instance
|
||||||
s.plaintext = str(self.cleaned_data['plaintext'])
|
s.plaintext = str(self.cleaned_data['plaintext'])
|
||||||
|
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
@ -103,10 +103,10 @@ class SecretTestCase(
|
|||||||
|
|
||||||
device = Device.objects.get(name='Device 1')
|
device = Device.objects.get(name='Device 1')
|
||||||
csv_data = (
|
csv_data = (
|
||||||
"assigned_object_type,assigned_object_id,role,name,plaintext",
|
"device,role,name,plaintext",
|
||||||
f"device,{device.pk},Secret Role 1,Secret 4,abcdefghij",
|
f"{device.name},Secret Role 1,Secret 4,abcdefghij",
|
||||||
f"device,{device.pk},Secret Role 1,Secret 5,abcdefghij",
|
f"{device.name},Secret Role 1,Secret 5,abcdefghij",
|
||||||
f"device,{device.pk},Secret Role 1,Secret 6,abcdefghij",
|
f"{device.name},Secret Role 1,Secret 6,abcdefghij",
|
||||||
)
|
)
|
||||||
|
|
||||||
# Set the session_key cookie on the request
|
# Set the session_key cookie on the request
|
||||||
|
Reference in New Issue
Block a user