Skip to content

Commit

Permalink
fix: Use App without extras installed
Browse files Browse the repository at this point in the history
Currently, when installing without extras, ChatOps App fails to
initialize.

What's changed:

- Moved Grafana VALID_MODELS to grafana models to avoid unnecessary
  import.
- Encapsulate importing grafana urls to `try ... except` block
  • Loading branch information
snaselj committed Aug 9, 2023
1 parent 0ec90b9 commit 9489642
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
19 changes: 0 additions & 19 deletions nautobot_chatops/integrations/grafana/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@
from typing import List
from termcolor import colored

from nautobot.dcim import models as dcim_models
from nautobot.ipam import models as ipam_models
from nautobot.extras import models as extra_models
from nautobot.tenancy import models as tenancy_models
from nautobot.virtualization import models as virtualization_models
from nautobot.circuits import models as circuit_models

from schema_enforcer import config
from schema_enforcer.schemas.manager import SchemaManager
from schema_enforcer.instances.file import InstanceFileManager
Expand All @@ -23,18 +16,6 @@
">": "greater-than",
}

# Valid models to be used in Panel Variables as query options. If a model doesn't exist in
# this list, you cannot set or use the `query` field in a panel variable.
VALID_MODELS = (
dcim_models,
ipam_models,
extra_models,
tenancy_models,
virtualization_models,
circuit_models,
)


def format_command(command: str) -> str:
"""_format_command_name will format the panel titles into a valid slash command.
Expand Down
21 changes: 19 additions & 2 deletions nautobot_chatops/integrations/grafana/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,26 @@
from django.core.exceptions import ValidationError
from django.core.serializers.json import DjangoJSONEncoder
from django.utils.translation import gettext_lazy as _
from nautobot.extras.utils import extras_features
from nautobot.circuits import models as circuit_models
from nautobot.core.models.generics import PrimaryModel, OrganizationalModel
from nautobot_chatops.integrations.grafana.helpers import VALID_MODELS
from nautobot.dcim import models as dcim_models
from nautobot.extras import models as extra_models
from nautobot.extras.utils import extras_features
from nautobot.ipam import models as ipam_models
from nautobot.tenancy import models as tenancy_models
from nautobot.virtualization import models as virtualization_models


# Valid models to be used in Panel Variables as query options. If a model doesn't exist in
# this list, you cannot set or use the `query` field in a panel variable.
VALID_MODELS = (
dcim_models,
ipam_models,
extra_models,
tenancy_models,
virtualization_models,
circuit_models,
)


@extras_features(
Expand Down
3 changes: 1 addition & 2 deletions nautobot_chatops/integrations/grafana/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
from nautobot.utilities.querysets import RestrictedQuerySet
from nautobot_chatops.dispatchers import Dispatcher
from nautobot_chatops.workers import handle_subcommands, add_subcommand
from nautobot_chatops.integrations.grafana.models import Panel, PanelVariable
from nautobot_chatops.integrations.grafana.helpers import VALID_MODELS
from nautobot_chatops.integrations.grafana.models import Panel, PanelVariable, VALID_MODELS
from nautobot_chatops.integrations.grafana.grafana import (
SLASH_COMMAND,
LOGGER,
Expand Down
10 changes: 9 additions & 1 deletion nautobot_chatops/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Django urlpatterns declaration for nautobot_chatops plugin."""
import logging

from django.urls import path

Expand All @@ -17,7 +18,14 @@
AccessGrantBulkDeleteView,
)

from nautobot_chatops.integrations.grafana.urls import urlpatterns as grafana_urlpatterns
try:
from nautobot_chatops.integrations.grafana.urls import urlpatterns as grafana_urlpatterns
# pylint: disable-next=broad-except
except Exception:
grafana_urlpatterns = []
logger = logging.getLogger(__name__)
logger.warning("Grafana ChatOps integration is not available.", exc_info=True)


urlpatterns = [
path("", NautobotHomeView.as_view(), name="home"),
Expand Down

0 comments on commit 9489642

Please sign in to comment.