From e37068b68d4627444190641bdef4bae031c7ed60 Mon Sep 17 00:00:00 2001 From: yash-pal1 Date: Fri, 10 May 2024 11:49:14 +0530 Subject: [PATCH 01/16] NB-6 plugin name changed to netbox_cloud_pilot to netbox_paas --- Dockerfile | 2 +- MANIFEST.in | 2 +- configuration/plugins.py | 4 +- .../__init__.py | 2 +- .../api/__init__.py | 0 .../api/serializers.py | 6 +- .../api/urls.py | 2 +- .../api/views.py | 0 .../constants.py | 0 {netbox_cloud_pilot => netbox_paas}/forms.py | 0 {netbox_cloud_pilot => netbox_paas}/iaas.py | 2 +- .../migrations/0001_initial.py | 0 .../migrations/0002_rename_application.py | 92 +++++++++++++++++++ .../migrations/__init__.py | 0 {netbox_cloud_pilot => netbox_paas}/models.py | 6 +- .../navigation.py | 12 +-- .../nb_settings.py | 0 {netbox_cloud_pilot => netbox_paas}/tables.py | 2 +- .../netbox_cloud_pilot/inc/env_info.html | 0 .../inc/env_node_groups.html | 4 +- .../netbox_cloud_pilot/inc/plugin_store.html | 10 +- .../netboxconfiguration.html | 16 ++-- .../netbox_cloud_pilot/netboxdbbackup.html | 2 +- .../netbox_cloud_pilot/nodelogs.html | 2 +- .../netbox_cloud_pilot/plugin_disable.html | 2 +- .../netbox_cloud_pilot/plugin_enable.html | 2 +- .../netbox_cloud_pilot/plugin_uninstall.html | 2 +- .../netbox_cloud_pilot/plugins_store.html | 6 +- {netbox_cloud_pilot => netbox_paas}/urls.py | 2 +- {netbox_cloud_pilot => netbox_paas}/utils.py | 0 {netbox_cloud_pilot => netbox_paas}/views.py | 48 +++++----- pyproject.toml | 2 +- setup.py | 8 +- test.sh | 2 +- 34 files changed, 166 insertions(+), 74 deletions(-) rename {netbox_cloud_pilot => netbox_paas}/__init__.py (94%) rename {netbox_cloud_pilot => netbox_paas}/api/__init__.py (100%) rename {netbox_cloud_pilot => netbox_paas}/api/serializers.py (95%) rename {netbox_cloud_pilot => netbox_paas}/api/urls.py (90%) rename {netbox_cloud_pilot => netbox_paas}/api/views.py (100%) rename {netbox_cloud_pilot => netbox_paas}/constants.py (100%) rename {netbox_cloud_pilot => netbox_paas}/forms.py (100%) rename {netbox_cloud_pilot => netbox_paas}/iaas.py (99%) rename {netbox_cloud_pilot => netbox_paas}/migrations/0001_initial.py (100%) create mode 100644 netbox_paas/migrations/0002_rename_application.py rename {netbox_cloud_pilot => netbox_paas}/migrations/__init__.py (100%) rename {netbox_cloud_pilot => netbox_paas}/models.py (98%) rename {netbox_cloud_pilot => netbox_paas}/navigation.py (54%) rename {netbox_cloud_pilot => netbox_paas}/nb_settings.py (100%) rename {netbox_cloud_pilot => netbox_paas}/tables.py (91%) rename {netbox_cloud_pilot => netbox_paas}/templates/netbox_cloud_pilot/inc/env_info.html (100%) rename {netbox_cloud_pilot => netbox_paas}/templates/netbox_cloud_pilot/inc/env_node_groups.html (96%) rename {netbox_cloud_pilot => netbox_paas}/templates/netbox_cloud_pilot/inc/plugin_store.html (72%) rename {netbox_cloud_pilot => netbox_paas}/templates/netbox_cloud_pilot/netboxconfiguration.html (82%) rename {netbox_cloud_pilot => netbox_paas}/templates/netbox_cloud_pilot/netboxdbbackup.html (94%) rename {netbox_cloud_pilot => netbox_paas}/templates/netbox_cloud_pilot/nodelogs.html (91%) rename {netbox_cloud_pilot => netbox_paas}/templates/netbox_cloud_pilot/plugin_disable.html (91%) rename {netbox_cloud_pilot => netbox_paas}/templates/netbox_cloud_pilot/plugin_enable.html (91%) rename {netbox_cloud_pilot => netbox_paas}/templates/netbox_cloud_pilot/plugin_uninstall.html (91%) rename {netbox_cloud_pilot => netbox_paas}/templates/netbox_cloud_pilot/plugins_store.html (74%) rename {netbox_cloud_pilot => netbox_paas}/urls.py (97%) rename {netbox_cloud_pilot => netbox_paas}/utils.py (100%) rename {netbox_cloud_pilot => netbox_paas}/views.py (91%) diff --git a/Dockerfile b/Dockerfile index 0ba4236..3f5805d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,4 +6,4 @@ RUN mkdir -pv /plugins/netbox-cloud-pilot COPY . /plugins/netbox-cloud-pilot RUN python3 /plugins/netbox-cloud-pilot/setup.py develop -RUN cp -rf /plugins/netbox-cloud-pilot/netbox_cloud_pilot/ /opt/netbox/venv/lib/python3.11/site-packages/netbox_cloud_pilot +RUN cp -rf /plugins/netbox-cloud-pilot/netbox_paas/ /opt/netbox/venv/lib/python3.11/site-packages/netbox_paas diff --git a/MANIFEST.in b/MANIFEST.in index 8f937b7..b20c58e 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,2 +1,2 @@ include README.md -recursive-include netbox_cloud_pilot/templates * +recursive-include netbox_pass/templates * diff --git a/configuration/plugins.py b/configuration/plugins.py index 874d3a5..ebf05f5 100644 --- a/configuration/plugins.py +++ b/configuration/plugins.py @@ -5,9 +5,9 @@ # See https://github.com/netbox-community/netbox-docker/wiki/Using-Netbox-Plugins PLUGINS = [ - "netbox_cloud_pilot", + "netbox_paas", ] PLUGINS_CONFIG = { # type: ignore - "netbox_cloud_pilot": {}, + "netbox_paas": {}, } diff --git a/netbox_cloud_pilot/__init__.py b/netbox_paas/__init__.py similarity index 94% rename from netbox_cloud_pilot/__init__.py rename to netbox_paas/__init__.py index edf484a..4dc0e31 100644 --- a/netbox_cloud_pilot/__init__.py +++ b/netbox_paas/__init__.py @@ -3,7 +3,7 @@ from extras.plugins import PluginConfig -metadata = metadata("netbox_cloud_pilot") +metadata = metadata("netbox_paas") class NetBoxCloudPilot(PluginConfig): diff --git a/netbox_cloud_pilot/api/__init__.py b/netbox_paas/api/__init__.py similarity index 100% rename from netbox_cloud_pilot/api/__init__.py rename to netbox_paas/api/__init__.py diff --git a/netbox_cloud_pilot/api/serializers.py b/netbox_paas/api/serializers.py similarity index 95% rename from netbox_cloud_pilot/api/serializers.py rename to netbox_paas/api/serializers.py index 85ef849..590488c 100644 --- a/netbox_cloud_pilot/api/serializers.py +++ b/netbox_paas/api/serializers.py @@ -20,7 +20,7 @@ class NetBoxConfigurationSerializer(NetBoxModelSerializer): url = serializers.HyperlinkedIdentityField( - view_name="plugins-api:netbox_cloud_pilot-api:netboxconfiguration-detail" + view_name="plugins-api:netbox_paas-api:netboxconfiguration-detail" ) env_name = serializers.CharField( @@ -55,7 +55,7 @@ class Meta: class NestedNetBoxConfigurationSerializer(WritableNestedSerializer): url = serializers.HyperlinkedIdentityField( - view_name="plugins-api:netbox_cloud_pilot-api:netboxconfiguration-detail" + view_name="plugins-api:netbox_paas-api:netboxconfiguration-detail" ) class Meta: @@ -64,7 +64,7 @@ class Meta: class NetBoxDBBackupSerializer(NetBoxModelSerializer): - url = serializers.HyperlinkedIdentityField(view_name="plugins-api:netbox_cloud_pilot-api:netboxdbbackup-detail") + url = serializers.HyperlinkedIdentityField(view_name="plugins-api:netbox_paas-api:netboxdbbackup-detail") netbox_env = NestedNetBoxConfigurationSerializer() diff --git a/netbox_cloud_pilot/api/urls.py b/netbox_paas/api/urls.py similarity index 90% rename from netbox_cloud_pilot/api/urls.py rename to netbox_paas/api/urls.py index 05c401d..cc9f840 100644 --- a/netbox_cloud_pilot/api/urls.py +++ b/netbox_paas/api/urls.py @@ -2,7 +2,7 @@ from . import views -app_name = "netbox_cloud_pilot" +app_name = "netbox_paas" router = NetBoxRouter() router.register("configurations", views.NetBoxConfigurationViewSet) diff --git a/netbox_cloud_pilot/api/views.py b/netbox_paas/api/views.py similarity index 100% rename from netbox_cloud_pilot/api/views.py rename to netbox_paas/api/views.py diff --git a/netbox_cloud_pilot/constants.py b/netbox_paas/constants.py similarity index 100% rename from netbox_cloud_pilot/constants.py rename to netbox_paas/constants.py diff --git a/netbox_cloud_pilot/forms.py b/netbox_paas/forms.py similarity index 100% rename from netbox_cloud_pilot/forms.py rename to netbox_paas/forms.py diff --git a/netbox_cloud_pilot/iaas.py b/netbox_paas/iaas.py similarity index 99% rename from netbox_cloud_pilot/iaas.py rename to netbox_paas/iaas.py index 40be86c..3d9165d 100644 --- a/netbox_cloud_pilot/iaas.py +++ b/netbox_paas/iaas.py @@ -18,7 +18,7 @@ from . import utils from .constants import JELASTIC_API, NODE_GROUP_CP, NODE_GROUP_SQLDB, DISABLED_PLUGINS_FILE_NAME, PLUGINS_FILE_NAME -logger = logging.getLogger("netbox_cloud_pilot") +logger = logging.getLogger("netbox_paas") __all__ = ( "IaaS", diff --git a/netbox_cloud_pilot/migrations/0001_initial.py b/netbox_paas/migrations/0001_initial.py similarity index 100% rename from netbox_cloud_pilot/migrations/0001_initial.py rename to netbox_paas/migrations/0001_initial.py diff --git a/netbox_paas/migrations/0002_rename_application.py b/netbox_paas/migrations/0002_rename_application.py new file mode 100644 index 0000000..607f8b2 --- /dev/null +++ b/netbox_paas/migrations/0002_rename_application.py @@ -0,0 +1,92 @@ +# Generated by Django 4.2.8 on 2024-05-10 05:43 + +from django.db import migrations + + +def named_tuple_fetch_all(cursor): + "Return all rows from a cursor as a namedtuple" + from collections import namedtuple + + desc = cursor.description + Result = namedtuple("Result", [col[0] for col in desc]) + return [Result(*row) for row in cursor.fetchall()] + + +def rename_indexes(apps, schema_editor): + from django.db import connection + + with connection.cursor() as cursor: + cursor.execute( + """SELECT indexname FROM pg_indexes + WHERE tablename LIKE 'netbox_cloud_pilot%'""" + ) + for result in named_tuple_fetch_all(cursor): + old_index_name = result.indexname + new_index_name = old_index_name.replace( + "netbox_cloud_pilot_", "netbox_paas_", 1 + ) + cursor.execute( + f"""ALTER INDEX IF EXISTS {old_index_name} + RENAME TO {new_index_name}""" + ) + + +def rename_foreignkeys(apps, schema_editor): + from django.db import connection + + with connection.cursor() as cursor: + cursor.execute( + """SELECT table_name, constraint_name + FROM information_schema.key_column_usage + WHERE constraint_catalog=CURRENT_CATALOG + AND table_name LIKE 'netbox_cloud_pilot%' + AND position_in_unique_constraint notnull""" + ) + for result in named_tuple_fetch_all(cursor): + table_name = result.table_name + old_foreignkey_name = result.constraint_name + new_foreignkey_name = old_foreignkey_name.replace( + "netbox_cloud_pilot_", "netbox_paas_", 1 + ) + cursor.execute( + f"""ALTER TABLE {table_name} + RENAME CONSTRAINT {old_foreignkey_name} + TO {new_foreignkey_name}""" + ) + + +def migrate_models(apps, schema_editor): + OldModel1 = apps.get_model('netbox_cloud_pilot', 'NetBoxConfiguration') + NewModel1 = apps.get_model('netbox_paas', 'NetBoxConfiguration') + + OldModel2 = apps.get_model('netbox_cloud_pilot', 'NetBoxDBBackup') + NewModel2 = apps.get_model('netbox_paas', 'NetBoxDBBackup') + + # Check if the table with the old app name exists for both models + if schema_editor.connection.introspection.table_name_converter( + OldModel1._meta.db_table) in schema_editor.connection.introspection.table_names(): + # Rename the table with the old app name to the new app name for Model 1 + schema_editor.execute('ALTER TABLE %s RENAME TO %s' % ( + schema_editor.quote_name(OldModel1._meta.db_table), + schema_editor.quote_name(NewModel1._meta.db_table), + )) + + if schema_editor.connection.introspection.table_name_converter( + OldModel2._meta.db_table) in schema_editor.connection.introspection.table_names(): + # Rename the table with the old app name to the new app name for Model 2 + schema_editor.execute('ALTER TABLE %s RENAME TO %s' % ( + schema_editor.quote_name(OldModel2._meta.db_table), + schema_editor.quote_name(NewModel2._meta.db_table), + )) + + +class Migration(migrations.Migration): + dependencies = [ + ('netbox_paas', '0001_initial'), + ] + + operations = [ + migrations.RunPython(rename_indexes, migrations.RunPython.noop), + migrations.RunPython(rename_foreignkeys, migrations.RunPython.noop), + migrations.RunPython(migrate_models), + ] diff --git a/netbox_cloud_pilot/migrations/__init__.py b/netbox_paas/migrations/__init__.py similarity index 100% rename from netbox_cloud_pilot/migrations/__init__.py rename to netbox_paas/migrations/__init__.py diff --git a/netbox_cloud_pilot/models.py b/netbox_paas/models.py similarity index 98% rename from netbox_cloud_pilot/models.py rename to netbox_paas/models.py index 63a9066..2ba9285 100644 --- a/netbox_cloud_pilot/models.py +++ b/netbox_paas/models.py @@ -65,7 +65,7 @@ def env_name(self): return os.environ.get("ENV_NAME") def get_absolute_url(self): - return reverse("plugins:netbox_cloud_pilot:netboxconfiguration", args=[self.pk]) + return reverse("plugins:netbox_paas:netboxconfiguration", args=[self.pk]) def clean(self): if self.__class__.objects.exists() and not self.pk: @@ -237,7 +237,7 @@ def schedule_restart(self): class NetBoxDBBackup(ChangeLoggedModel): netbox_env = models.ForeignKey( - to="netbox_cloud_pilot.NetBoxConfiguration", + to="netbox_paas.NetBoxConfiguration", on_delete=models.CASCADE, related_name="db_backups", verbose_name="NetBox Environment", @@ -262,7 +262,7 @@ class Meta: verbose_name_plural = "NetBox DB Backups" def get_absolute_url(self): - return reverse("plugins:netbox_cloud_pilot:netboxdbbackup", args=[self.pk]) + return reverse("plugins:netbox_paas:netboxdbbackup", args=[self.pk]) def __str__(self): return self.crontab diff --git a/netbox_cloud_pilot/navigation.py b/netbox_paas/navigation.py similarity index 54% rename from netbox_cloud_pilot/navigation.py rename to netbox_paas/navigation.py index 275db8f..ca7e764 100644 --- a/netbox_cloud_pilot/navigation.py +++ b/netbox_paas/navigation.py @@ -10,18 +10,18 @@ ( PluginMenuItem( link_text="Manage", - link="plugins:netbox_cloud_pilot:netboxconfiguration_list", - permissions=["netbox_cloud_pilot.view_netboxconfiguration"], + link="plugins:netbox_paas:netboxconfiguration_list", + permissions=["netbox_paas.view_netboxconfiguration"], ), PluginMenuItem( link_text="DB Backups", - link="plugins:netbox_cloud_pilot:netboxdbbackup_list", - permissions=["netbox_cloud_pilot.change_netboxconfiguration"], + link="plugins:netbox_paas:netboxdbbackup_list", + permissions=["netbox_paas.change_netboxconfiguration"], ), PluginMenuItem( link_text="Plugins Store", - link="plugins:netbox_cloud_pilot:netboxplugin_list", - permissions=["netbox_cloud_pilot.view_netboxconfiguration"], + link="plugins:netbox_paas:netboxplugin_list", + permissions=["netbox_paas.view_netboxconfiguration"], ), ), ), diff --git a/netbox_cloud_pilot/nb_settings.py b/netbox_paas/nb_settings.py similarity index 100% rename from netbox_cloud_pilot/nb_settings.py rename to netbox_paas/nb_settings.py diff --git a/netbox_cloud_pilot/tables.py b/netbox_paas/tables.py similarity index 91% rename from netbox_cloud_pilot/tables.py rename to netbox_paas/tables.py index e49374e..9bd64c2 100644 --- a/netbox_cloud_pilot/tables.py +++ b/netbox_paas/tables.py @@ -5,7 +5,7 @@ __all__ = ("NetBoxBackupsTable",) RESTORE_BUTTON = """ -
+ {% csrf_token %} @@ -61,7 +61,7 @@

Logs diff --git a/netbox_cloud_pilot/templates/netbox_cloud_pilot/inc/plugin_store.html b/netbox_paas/templates/netbox_cloud_pilot/inc/plugin_store.html similarity index 72% rename from netbox_cloud_pilot/templates/netbox_cloud_pilot/inc/plugin_store.html rename to netbox_paas/templates/netbox_cloud_pilot/inc/plugin_store.html index f84f9cd..aa1b5ac 100644 --- a/netbox_cloud_pilot/templates/netbox_cloud_pilot/inc/plugin_store.html +++ b/netbox_paas/templates/netbox_cloud_pilot/inc/plugin_store.html @@ -35,28 +35,28 @@

{{ plugin_name }}

{% if plugin.installed %} {% if plugin.disabled %} + href="{% url 'plugins:netbox_paas:netboxconfiguration_plugin_enable' pk=object.pk %}?name={{ plugin_name }}&return_url={{ request.path }}"> Enable {% else %} + href="{% url 'plugins:netbox_paas:netboxconfiguration_plugin_disable' pk=object.pk %}?name={{ plugin_name }}&return_url={{ request.path }}"> Disable {% endif %} + href="{% url 'plugins:netbox_paas:netboxconfiguration_plugin_install' pk=object.pk %}?name={{ plugin_name }}&version={{ plugin.current_version }}&type=update&return_url={{ request.path }}"> Configure + href="{% url 'plugins:netbox_paas:netboxconfiguration_plugin_uninstall' pk=object.pk %}?name={{ plugin_name }}&return_url={{ request.path }}"> Uninstall {% else %} + href="{% url 'plugins:netbox_paas:netboxconfiguration_plugin_install' pk=object.pk %}?name={{ plugin_name }}&type=install&return_url={{ request.path }}"> Install {% endif %} diff --git a/netbox_cloud_pilot/templates/netbox_cloud_pilot/netboxconfiguration.html b/netbox_paas/templates/netbox_cloud_pilot/netboxconfiguration.html similarity index 82% rename from netbox_cloud_pilot/templates/netbox_cloud_pilot/netboxconfiguration.html rename to netbox_paas/templates/netbox_cloud_pilot/netboxconfiguration.html index 1cd2cfb..d6cbf06 100644 --- a/netbox_cloud_pilot/templates/netbox_cloud_pilot/netboxconfiguration.html +++ b/netbox_paas/templates/netbox_cloud_pilot/netboxconfiguration.html @@ -6,13 +6,13 @@
{% if object.get_env.is_upgrade_available %} - + Upgrade Available {% endif %} - {% if perms.netbox_cloud_pilot.change_netboxconfiguration %} - Edit NetBox Settings @@ -51,7 +51,7 @@
Overview
{% if object.env_name_storage %} {{ object.env_name_storage }} {% else %} - Add Backup Storage @@ -108,19 +108,19 @@
NetBox Admin Access
- {% include 'netbox_cloud_pilot/inc/env_info.html' with card_header="NetBox" env_info=object.get_env.get_env %} + {% include 'netbox_paas/inc/env_info.html' with card_header="NetBox" env_info=object.get_env.get_env %}
- {% include 'netbox_cloud_pilot/inc/env_node_groups.html' with card_header="NetBox" node_groups=object.get_env.get_node_groups env_name=object.env_name object=object %} + {% include 'netbox_paas/inc/env_node_groups.html' with card_header="NetBox" node_groups=object.get_env.get_node_groups env_name=object.env_name object=object %}
{% if object.env_name_storage %}
- {% include 'netbox_cloud_pilot/inc/env_info.html' with card_header="Storage" env_info=object.get_env_storage.get_env %} + {% include 'netbox_paas/inc/env_info.html' with card_header="Storage" env_info=object.get_env_storage.get_env %}
- {% include 'netbox_cloud_pilot/inc/env_node_groups.html' with card_header="Storage" node_groups=object.get_env_storage.get_node_groups env_name=object.env_name_storage object=object %} + {% include 'netbox_paas/inc/env_node_groups.html' with card_header="Storage" node_groups=object.get_env_storage.get_node_groups env_name=object.env_name_storage object=object %}
{% endif %} diff --git a/netbox_cloud_pilot/templates/netbox_cloud_pilot/netboxdbbackup.html b/netbox_paas/templates/netbox_cloud_pilot/netboxdbbackup.html similarity index 94% rename from netbox_cloud_pilot/templates/netbox_cloud_pilot/netboxdbbackup.html rename to netbox_paas/templates/netbox_cloud_pilot/netboxdbbackup.html index 673bd4f..ae885ff 100644 --- a/netbox_cloud_pilot/templates/netbox_cloud_pilot/netboxdbbackup.html +++ b/netbox_paas/templates/netbox_cloud_pilot/netboxdbbackup.html @@ -4,7 +4,7 @@ {% block extra_controls %} {% csrf_token %} - diff --git a/netbox_cloud_pilot/templates/netbox_cloud_pilot/nodelogs.html b/netbox_paas/templates/netbox_cloud_pilot/nodelogs.html similarity index 91% rename from netbox_cloud_pilot/templates/netbox_cloud_pilot/nodelogs.html rename to netbox_paas/templates/netbox_cloud_pilot/nodelogs.html index 0f8c498..fa48d83 100644 --- a/netbox_cloud_pilot/templates/netbox_cloud_pilot/nodelogs.html +++ b/netbox_paas/templates/netbox_cloud_pilot/nodelogs.html @@ -9,7 +9,7 @@
{% csrf_token %} diff --git a/netbox_cloud_pilot/templates/netbox_cloud_pilot/plugin_disable.html b/netbox_paas/templates/netbox_cloud_pilot/plugin_disable.html similarity index 91% rename from netbox_cloud_pilot/templates/netbox_cloud_pilot/plugin_disable.html rename to netbox_paas/templates/netbox_cloud_pilot/plugin_disable.html index 7493c47..222df10 100644 --- a/netbox_cloud_pilot/templates/netbox_cloud_pilot/plugin_disable.html +++ b/netbox_paas/templates/netbox_cloud_pilot/plugin_disable.html @@ -10,7 +10,7 @@