Skip to content

Commit

Permalink
feat: enable v1 root drf browsable
Browse files Browse the repository at this point in the history
Signed-off-by: Alex <aizquier@redhat.com>
  • Loading branch information
Alex-Izquierdo committed Jun 18, 2024
1 parent a347c4a commit c66ad75
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 23 deletions.
2 changes: 2 additions & 0 deletions docs/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,5 @@ Minikube is the recommended method for macOS users
**Note**: For fedora, the binary may be `go-task`.

You can now access the UI at <http://localhost:8080/eda/> using the above credentials.
Also you can inspect the API documentation at <https://localhost:8443/api/eda/v1/docs>
or navigate through the resources with the DRF browsable API at <https://localhost:8443/api/eda/v1/>
65 changes: 47 additions & 18 deletions src/aap_eda/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,47 @@
router = routers.SimpleRouter()
# basename has to be set when queryset is user-dependent
# which is any model with permissions
router.register("projects", views.ProjectViewSet)
router.register("rulebooks", views.RulebookViewSet)
router.register("activations", views.ActivationViewSet)
router.register("projects", views.ProjectViewSet, basename="projects")
router.register("rulebooks", views.RulebookViewSet, basename="rulebooks")
router.register("activations", views.ActivationViewSet, basename="activations")
router.register(
"activation-instances",
views.ActivationInstanceViewSet,
basename="activationinstance",
basename="activation-instances",
)
router.register("audit-rules", views.AuditRuleViewSet, basename="audit-rules")
router.register("users", views.UserViewSet, basename="users")
router.register(
"event-streams",
views.EventStreamViewSet,
basename="event-streams",
)
router.register("audit-rules", views.AuditRuleViewSet)
router.register("users", views.UserViewSet)
router.register("event-streams", views.EventStreamViewSet)
router.register(
"users/me/awx-tokens",
views.CurrentUserAwxTokenViewSet,
basename="controller-token",
basename="awx-tokens",
)
router.register(
"credential-types",
views.CredentialTypeViewSet,
basename="credential-types",
)
router.register(
"eda-credentials",
views.EdaCredentialViewSet,
basename="eda-credentials",
)
router.register(
"decision-environments",
views.DecisionEnvironmentViewSet,
basename="decision-environments",
)
router.register(
"organizations",
views.OrganizationViewSet,
basename="organizations",
)
router.register("credential-types", views.CredentialTypeViewSet)
router.register("eda-credentials", views.EdaCredentialViewSet)
router.register("decision-environments", views.DecisionEnvironmentViewSet)
router.register("organizations", views.OrganizationViewSet)
router.register("teams", views.TeamViewSet)
router.register("teams", views.TeamViewSet, basename="teams")

openapi_urls = [
path(
Expand All @@ -80,21 +100,30 @@
]

v1_urls = [
path("status/", core_views.StatusView.as_view()),
path("status/", core_views.StatusView.as_view(), name="status"),
path("", include(dab_urls)),
path("", include(resource_api_urls)),
path("", include(openapi_urls)),
path("auth/session/login/", views.SessionLoginView.as_view()),
path("auth/session/logout/", views.SessionLogoutView.as_view()),
path(
"auth/session/login/",
views.SessionLoginView.as_view(),
name="session-login",
),
path(
"auth/session/logout/",
views.SessionLogoutView.as_view(),
name="session-logout",
),
path(
"auth/token/refresh/",
jwt_views.TokenRefreshView.as_view(),
name="token_refresh",
name="token-refresh",
),
path("users/me/", views.CurrentUserView.as_view()),
path("users/me/", views.CurrentUserView.as_view(), name="current-user"),
*router.urls,
]

urlpatterns = [
path("v1/", views.ApiV1RootView.as_view()),
path("v1/", include(v1_urls)),
]
4 changes: 3 additions & 1 deletion src/aap_eda/api/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .event_stream import EventStreamViewSet
from .organization import OrganizationViewSet
from .project import ProjectViewSet
from .root import ApiV1RootView
from .rulebook import AuditRuleViewSet, RulebookViewSet
from .team import TeamViewSet
from .user import CurrentUserAwxTokenViewSet, CurrentUserView, UserViewSet
Expand All @@ -28,7 +29,6 @@
# auth
"SessionLoginView",
"SessionLogoutView",
"RoleViewSet",
# project
"ProjectViewSet",
"AuditRuleViewSet",
Expand All @@ -51,4 +51,6 @@
"OrganizationViewSet",
# teams
"TeamViewSet",
# root
"ApiV1RootView",
)
53 changes: 53 additions & 0 deletions src/aap_eda/api/views/root.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# 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 logging

from django.urls import URLPattern, URLResolver
from rest_framework.response import Response
from rest_framework.reverse import reverse
from rest_framework.views import APIView

LOGGER = logging.getLogger(__name__)


class ApiV1RootView(APIView):
def get(self, request, *args, **kwargs):
urls = get_api_v1_urls(request=request)
return Response(urls)


def get_api_v1_urls(request=None):
from aap_eda.api.urls import v1_urls

def list_urls(urls):
url_list = {}
for url in urls:
if isinstance(url, URLResolver):
url_list.update(list_urls(url.url_patterns))
elif isinstance(url, URLPattern):
name = url.name
if not name:
LOGGER.warning(
"URL %s has no name, DRF browsable API will omit it",
url.pattern,
)
continue
if url.pattern.regex.groups:
continue
url_list[name] = reverse(name, request=request)
return url_list

return list_urls(v1_urls)
1 change: 1 addition & 0 deletions src/aap_eda/settings/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ def _get_databases_settings() -> dict:
# https://docs.djangoproject.com/en/4.1/howto/static-files/

STATIC_URL = "static/"
STATIC_ROOT = settings.get("STATIC_ROOT", "/var/lib/eda/static")

MEDIA_ROOT = settings.get("MEDIA_ROOT", "/var/lib/eda/files")

Expand Down
60 changes: 60 additions & 0 deletions tests/integration/api/test_v1_root.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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 tests.integration.constants import api_url_v1


@pytest.mark.django_db
def test_v1_root(admin_client):
expected_slugs = [
"/status/",
"/role_definitions/",
"/role_user_assignments/",
"/role_team_assignments/",
"/role_metadata/",
"/service-index/metadata/",
"/service-index/validate-local-account/",
"/service-index/resources/",
"/service-index/resource-types/",
"/service-index/",
"/openapi.json",
"/openapi.yaml",
"/docs",
"/redoc",
"/auth/session/login/",
"/auth/session/logout/",
"/auth/token/refresh/",
"/users/me/",
"/projects/",
"/rulebooks/",
"/activations/",
"/activation-instances/",
"/audit-rules/",
"/users/",
"/event-streams/",
"/users/me/awx-tokens/",
"/credential-types/",
"/eda-credentials/",
"/decision-environments/",
"/organizations/",
"/teams/",
]

response = admin_client.get(f"{api_url_v1}/")
assert response.status_code == 200
for slug in expected_slugs:
assert any(
slug in url for url in response.data.values()
), f"Expected slug {slug} not found in response"
3 changes: 2 additions & 1 deletion tools/docker/docker-compose-dev-redis-tls.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ services:
- /bin/bash
- -c
- >-
aap-eda-manage migrate
aap-eda-manage collectstatic --noinput
&& aap-eda-manage migrate
&& aap-eda-manage create_initial_data
&& scripts/create_superuser.sh
&& aap-eda-manage runserver 0.0.0.0:8000
Expand Down
3 changes: 2 additions & 1 deletion tools/docker/docker-compose-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ services:
- /bin/bash
- -c
- >-
aap-eda-manage migrate
aap-eda-manage collectstatic --noinput
&& aap-eda-manage migrate
&& aap-eda-manage create_initial_data
&& scripts/create_superuser.sh
&& aap-eda-manage runserver 0.0.0.0:8000
Expand Down
3 changes: 2 additions & 1 deletion tools/docker/docker-compose-mac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ services:
- /bin/bash
- -c
- >-
aap-eda-manage migrate
aap-eda-manage collectstatic --noinput
&& aap-eda-manage migrate
&& aap-eda-manage create_initial_data
&& scripts/create_superuser.sh
&& aap-eda-manage runserver 0.0.0.0:8000
Expand Down
3 changes: 2 additions & 1 deletion tools/docker/docker-compose-stage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ services:
- /bin/bash
- -c
- >-
aap-eda-manage migrate
aap-eda-manage collectstatic --noinput
&& aap-eda-manage migrate
&& aap-eda-manage create_initial_data
&& scripts/create_superuser.sh
&& gunicorn -b 0.0.0.0:8000
Expand Down

0 comments on commit c66ad75

Please sign in to comment.