From 78c31a2c36095d0ae77bc1ff5d75572a445ff840 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Mon, 30 Jan 2023 16:52:25 -0500 Subject: [PATCH] Replace auth & git branch fields with general-purpose parameters --- netbox/core/api/serializers.py | 5 +-- netbox/core/filtersets.py | 2 +- netbox/core/forms/bulk_edit.py | 17 ++------- netbox/core/forms/bulk_import.py | 2 +- netbox/core/forms/filtersets.py | 37 ++++--------------- netbox/core/forms/model_forms.py | 5 +-- ...2_remove_datasource_git_branch_and_more.py | 30 +++++++++++++++ netbox/core/models/data.py | 18 +++------ netbox/core/tables/data.py | 2 +- netbox/templates/core/datasource.html | 33 ++++++++--------- 10 files changed, 68 insertions(+), 83 deletions(-) create mode 100644 netbox/core/migrations/0002_remove_datasource_git_branch_and_more.py diff --git a/netbox/core/api/serializers.py b/netbox/core/api/serializers.py index 7483c91be..33665e273 100644 --- a/netbox/core/api/serializers.py +++ b/netbox/core/api/serializers.py @@ -31,10 +31,9 @@ class DataSourceSerializer(NetBoxModelSerializer): class Meta: model = DataSource fields = [ - 'id', 'url', 'display', 'name', 'type', 'url', 'enabled', 'status', 'description', 'git_branch', - 'ignore_rules', 'username', 'password', 'created', 'last_updated', 'file_count', + 'id', 'url', 'display', 'name', 'type', 'url', 'enabled', 'status', 'description', 'parameters', + 'ignore_rules', 'created', 'last_updated', 'file_count', ] - extra_kwargs = {'password': {'write_only': True}} class DataFileSerializer(NetBoxModelSerializer): diff --git a/netbox/core/filtersets.py b/netbox/core/filtersets.py index 64f5052c0..d05b2e61a 100644 --- a/netbox/core/filtersets.py +++ b/netbox/core/filtersets.py @@ -16,7 +16,7 @@ class DataSourceFilterSet(ChangeLoggedModelFilterSet): class Meta: model = DataSource - fields = ('id', 'name', 'type', 'enabled', 'status', 'git_branch', 'username') + fields = ('id', 'name', 'type', 'enabled', 'status') def search(self, queryset, name, value): if not value.strip(): diff --git a/netbox/core/forms/bulk_edit.py b/netbox/core/forms/bulk_edit.py index bcdd0ee72..3730c7235 100644 --- a/netbox/core/forms/bulk_edit.py +++ b/netbox/core/forms/bulk_edit.py @@ -29,29 +29,18 @@ class DataSourceBulkEditForm(NetBoxModelBulkEditForm): max_length=200, required=False ) - git_branch = forms.CharField( - max_length=100, + parameters = forms.JSONField( required=False ) ignore_rules = forms.CharField( required=False, widget=forms.Textarea() ) - username = forms.CharField( - max_length=100, - required=False - ) - password = forms.CharField( - max_length=100, - required=False, - widget=forms.PasswordInput() - ) model = DataSource fieldsets = ( - (None, ('type', 'enabled', 'description', 'git_branch', 'ignore_rules')), - ('Authentication', ('username', 'password')), + (None, ('type', 'enabled', 'description', 'parameters', 'ignore_rules')), ) nullable_fields = ( - 'description', 'description', 'git_branch', 'ignore_rules', 'username', 'password', + 'description', 'description', 'parameters', 'parameters', 'ignore_rules', ) diff --git a/netbox/core/forms/bulk_import.py b/netbox/core/forms/bulk_import.py index 8325ce8a5..773e5faa3 100644 --- a/netbox/core/forms/bulk_import.py +++ b/netbox/core/forms/bulk_import.py @@ -11,5 +11,5 @@ class DataSourceImportForm(NetBoxModelImportForm): class Meta: model = DataSource fields = ( - 'name', 'type', 'url', 'enabled', 'description', 'git_branch', 'ignore_rules', 'username', 'password', + 'name', 'type', 'url', 'enabled', 'description', 'parameters', 'ignore_rules', ) diff --git a/netbox/core/forms/filtersets.py b/netbox/core/forms/filtersets.py index e71591edd..433f07067 100644 --- a/netbox/core/forms/filtersets.py +++ b/netbox/core/forms/filtersets.py @@ -18,20 +18,21 @@ class DataSourceFilterForm(NetBoxModelFilterSetForm): model = DataSource fieldsets = ( (None, ('q', 'filter_id')), - ('Data Source', ('type', 'git_branch')), - ('Authentication', ('username',)), + ('Data Source', ('type', 'status')), ) type = MultipleChoiceField( choices=DataSourceTypeChoices, required=False ) - git_branch = forms.CharField( - max_length=100, + status = MultipleChoiceField( + choices=DataSourceStatusChoices, required=False ) - username = forms.CharField( - max_length=100, - required=False + enabled = forms.NullBooleanField( + required=False, + widget=StaticSelect( + choices=BOOLEAN_WITH_BLANK_CHOICES + ) ) @@ -46,25 +47,3 @@ class DataFileFilterForm(NetBoxModelFilterSetForm): required=False, label=_('Data source') ) - type = MultipleChoiceField( - choices=DataSourceTypeChoices, - required=False - ) - enabled = forms.NullBooleanField( - required=False, - widget=StaticSelect( - choices=BOOLEAN_WITH_BLANK_CHOICES - ) - ) - status = MultipleChoiceField( - choices=DataSourceStatusChoices, - required=False - ) - git_branch = forms.CharField( - max_length=100, - required=False - ) - username = forms.CharField( - max_length=100, - required=False - ) diff --git a/netbox/core/forms/model_forms.py b/netbox/core/forms/model_forms.py index 3c7d490f4..64d18febc 100644 --- a/netbox/core/forms/model_forms.py +++ b/netbox/core/forms/model_forms.py @@ -9,14 +9,13 @@ __all__ = ( class DataSourceForm(NetBoxModelForm): fieldsets = ( ('Source', ('name', 'type', 'url', 'enabled', 'description')), - ('Git', ('git_branch',)), - ('Authentication', ('username', 'password')), + ('Backend', ('parameters', 'ignore_rules')), ) class Meta: model = DataSource fields = [ - 'name', 'type', 'url', 'enabled', 'description', 'git_branch', 'ignore_rules', 'username', 'password', + 'name', 'type', 'url', 'enabled', 'description', 'parameters', 'ignore_rules', ] widgets = { 'type': StaticSelect(), diff --git a/netbox/core/migrations/0002_remove_datasource_git_branch_and_more.py b/netbox/core/migrations/0002_remove_datasource_git_branch_and_more.py new file mode 100644 index 000000000..09e7e4736 --- /dev/null +++ b/netbox/core/migrations/0002_remove_datasource_git_branch_and_more.py @@ -0,0 +1,30 @@ +# Generated by Django 4.1.5 on 2023-01-30 21:49 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0001_initial'), + ] + + operations = [ + migrations.RemoveField( + model_name='datasource', + name='git_branch', + ), + migrations.RemoveField( + model_name='datasource', + name='password', + ), + migrations.RemoveField( + model_name='datasource', + name='username', + ), + migrations.AddField( + model_name='datasource', + name='parameters', + field=models.JSONField(blank=True, null=True), + ), + ] diff --git a/netbox/core/models/data.py b/netbox/core/models/data.py index 96a39249c..435351171 100644 --- a/netbox/core/models/data.py +++ b/netbox/core/models/data.py @@ -58,22 +58,13 @@ class DataSource(ChangeLoggedModel): max_length=200, blank=True ) - git_branch = models.CharField( - max_length=100, - blank=True, - help_text=_("Branch to check out for git sources (if not using the default)") - ) ignore_rules = models.TextField( blank=True, help_text=_("Patterns (one per line) matching files to ignore when syncing") ) - username = models.CharField( - max_length=100, - blank=True - ) - password = models.CharField( - max_length=100, - blank=True + parameters = models.JSONField( + blank=True, + null=True ) last_synced = models.DateTimeField( blank=True, @@ -137,8 +128,9 @@ class DataSource(ChangeLoggedModel): DataSourceTypeChoices.LOCAL: LocalBakend, DataSourceTypeChoices.GIT: GitBackend, }.get(self.type) + backend_params = self.parameters or {} - return backend_cls(self.url) + return backend_cls(self.url, **backend_params) def sync(self): """ diff --git a/netbox/core/tables/data.py b/netbox/core/tables/data.py index e3e691bdd..dcd2478a0 100644 --- a/netbox/core/tables/data.py +++ b/netbox/core/tables/data.py @@ -23,7 +23,7 @@ class DataSourceTable(NetBoxTable): class Meta(NetBoxTable.Meta): model = DataSource fields = ( - 'pk', 'id', 'name', 'type', 'status', 'enabled', 'url', 'description', 'git_branch', 'username', 'created', + 'pk', 'id', 'name', 'type', 'status', 'enabled', 'url', 'description', 'parameters', 'created', 'last_updated', 'file_count', ) default_columns = ('pk', 'name', 'type', 'status', 'enabled', 'description', 'file_count') diff --git a/netbox/templates/core/datasource.html b/netbox/templates/core/datasource.html index e5047849c..e835f9e33 100644 --- a/netbox/templates/core/datasource.html +++ b/netbox/templates/core/datasource.html @@ -58,19 +58,6 @@ {{ object.url }} - - Git branch - {{ object.git_branch|placeholder }} - - - Ignore rules - - {% if object.ignore_rules %} -
{{ object.ignore_rules }}
- {% else %} - {{ ''|placeholder }} - {% endif %} - File count {{ object.datafiles.count }} @@ -82,16 +69,26 @@
-
Authentication
+
Backend
- - + + - - + +
Username{{ object.username|placeholder }}Parameters + {% if object.parameters %} +
{{ object.parameters }}
+ {% else %} + {{ ''|placeholder }} + {% endif %}
Password{% if object.password %}********{% else %}{{ ''|placeholder }}{% endif %}Ignore rules + {% if object.ignore_rules %} +
{{ object.ignore_rules }}
+ {% else %} + {{ ''|placeholder }} + {% endif %}