Skip to content

Commit

Permalink
feat: add config/ endpoint to display eda-server version (ansible#972)
Browse files Browse the repository at this point in the history
* add a new endpoint api/eda/v1/config/ to retrieve eda-server config info including version
* add get_eda_version function to retrieve version from package
  • Loading branch information
Dostonbek1 authored Jul 18, 2024
1 parent 55e8897 commit 24cd4b3
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/aap_eda/api/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
PostActivationSerializer,
)
from .auth import LoginSerializer
from .config import ConfigSerializer
from .credential_type import (
CredentialTypeCreateSerializer,
CredentialTypeRefSerializer,
Expand Down Expand Up @@ -110,6 +111,8 @@
"UserListSerializer",
"UserCreateUpdateSerializer",
"UserDetailSerializer",
# config
"ConfigSerializer",
# credential type
"CredentialTypeSerializer",
"CredentialTypeCreateSerializer",
Expand Down
30 changes: 30 additions & 0 deletions src/aap_eda/api/serializers/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2024 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from rest_framework import serializers


class ConfigSerializer(serializers.Serializer):
"""Serializer for ConfigView response."""

time_zone = serializers.CharField(
required=True,
)

version = serializers.CharField(
required=True,
)

deployment_type = serializers.CharField(
required=True,
)
1 change: 1 addition & 0 deletions src/aap_eda/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
]

eda_v1_urls = [
path("config/", views.ConfigView.as_view(), name="config"),
path("status/", core_views.StatusView.as_view(), name="status"),
path("", include(openapi_urls)),
path(
Expand Down
3 changes: 3 additions & 0 deletions src/aap_eda/api/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from .activation import ActivationInstanceViewSet, ActivationViewSet
from .auth import SessionLoginView, SessionLogoutView
from .config import ConfigView
from .credential_type import CredentialTypeViewSet
from .decision_environment import DecisionEnvironmentViewSet
from .eda_credential import EdaCredentialViewSet
Expand Down Expand Up @@ -54,4 +55,6 @@
# root
"ApiV1RootView",
"ApiRootView",
# config
"ConfigView",
)
45 changes: 45 additions & 0 deletions src/aap_eda/api/views/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2024 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from django.conf import settings
from drf_spectacular.utils import OpenApiResponse, extend_schema
from rest_framework import status
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from aap_eda.api.serializers import ConfigSerializer
from aap_eda.utils import get_eda_version


class ConfigView(APIView):
permission_classes = [IsAuthenticated]

@extend_schema(
description="Get the current configuration info of EDA.",
responses={
status.HTTP_200_OK: OpenApiResponse(
ConfigSerializer,
description=("Return the current configuration info of EDA."),
)
},
)
def get(self, request):
data = {
"time_zone": settings.TIME_ZONE,
"version": get_eda_version(),
"deployment_type": settings.DEPLOYMENT_TYPE,
}

return Response(ConfigSerializer(data).data, status=status.HTTP_200_OK)
13 changes: 13 additions & 0 deletions src/aap_eda/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,20 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import importlib.metadata
import logging

logger = logging.getLogger(__name__)


def str_to_bool(value: str) -> bool:
return value.lower() in ("yes", "true", "1")


def get_eda_version() -> str:
"""Return EDA version as defined in the aap-eda package."""
try:
return importlib.metadata.version("aap-eda")
except importlib.metadata.PackageNotFoundError:
logger.error("Cannot read version from aap-eda package")
return "unknown"
29 changes: 29 additions & 0 deletions tests/integration/api/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2024 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
from django.conf import settings

from aap_eda.utils import get_eda_version
from tests.integration.constants import api_url_v1


@pytest.mark.django_db
def test_v1_config(admin_client):
response = admin_client.get(f"{api_url_v1}/config/")
assert response.status_code == 200
assert response.data == {
"time_zone": settings.TIME_ZONE,
"version": get_eda_version(),
"deployment_type": settings.DEPLOYMENT_TYPE,
}
2 changes: 2 additions & 0 deletions tests/integration/api/test_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
[
pytest.param(
[
"/config/",
"/status/",
"/openapi.json",
"/openapi.yaml",
Expand Down Expand Up @@ -50,6 +51,7 @@
),
pytest.param(
[
"/config/",
"/status/",
"/role_definitions/",
"/role_user_assignments/",
Expand Down
13 changes: 12 additions & 1 deletion tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from importlib.metadata import PackageNotFoundError, version
from unittest.mock import patch

import pytest

from aap_eda.utils import str_to_bool
from aap_eda.utils import get_eda_version, str_to_bool


@pytest.mark.parametrize(
Expand All @@ -34,3 +37,11 @@
)
def test_str_to_bool(value, expected):
assert str_to_bool(value) == expected


def test_get_eda_version():
assert get_eda_version() == version("aap-eda")

# assert outcome when aap-eda package is not found
with patch("importlib.metadata.version", side_effect=PackageNotFoundError):
assert get_eda_version() == "unknown"

0 comments on commit 24cd4b3

Please sign in to comment.