# pylint: disable=W0613
"""
Views of the React Infrastructure Resource list in the Smarter web console.
The ledger, :class:`~smarter.apps.infrastructure.models.InfrastructureResource`, describes the
platform's cloud infrastructure and what it costs, so it is for superusers only. It is written
by the infrastructure signals' receivers, so the list is read-only.
"""
from http import HTTPStatus
from typing import Any, Optional
from django.core.handlers.asgi import ASGIRequest
from django.core.paginator import Paginator
from django.db.models import Count, Q, QuerySet
from django.http import JsonResponse
from smarter.apps.dashboard.views.console_app import ConsoleAppView
from smarter.lib import json, logging
from smarter.lib.django.views import SmarterAuthenticatedNeverCachedWebView
from smarter.lib.django.waffle import SmarterWaffleSwitches
from smarter.lib.typing import SmarterDictType
from ..models import InfrastructureResource
from ..serializers import InfrastructureResourceSerializer
logger = logging.getSmarterLogger(__name__, any_switches=[SmarterWaffleSwitches.INFRASTRUCTURE_LOGGING])
DEFAULT_PAGE_SIZE = 50
"""The resources of a page, unless the list asks for another number."""
MAX_PAGE_SIZE = 500
"""The most resources that a page may have."""
FORBIDDEN = "Only superusers may see the platform's infrastructure resources."
[docs]
def summary() -> dict[str, int]:
"""Counts of the ledger's resources: in total, active, active and billable, and destroyed."""
active = Q(status=InfrastructureResource.Status.ACTIVE)
return InfrastructureResource.objects.aggregate(
total=Count("id"),
active=Count("id", filter=active),
activeBillable=Count("id", filter=active & Q(billable=True)),
destroyed=Count("id", filter=Q(status=InfrastructureResource.Status.DESTROYED)),
)
[docs]
def filter_resources(body: SmarterDictType) -> QuerySet[InfrastructureResource]:
"""
The ledger's resources that match the list's filters, newest first.
:param body: The list's request, whose filters are optional: ``status``, ``active``,
``destroyed`` or ``all``; ``billableOnly``; ``provider``; ``resourceType``, e.g.
``kubernetes.node``; and ``text``, which the resource's name, type, id or service contains.
:returns: The matching resources.
"""
qs = InfrastructureResource.objects.order_by("-created_at", "-id")
status = body.get("status")
if status in InfrastructureResource.Status.values:
qs = qs.filter(status=status)
if body.get("billableOnly") is True:
qs = qs.filter(billable=True)
for key, field in (("provider", "provider"), ("resourceType", "resource_type")):
value = body.get(key)
if isinstance(value, str) and value:
qs = qs.filter(**{field: value})
text = body.get("text")
if isinstance(text, str) and text.strip():
text = text.strip()
qs = qs.filter(
Q(resource_name__icontains=text)
| Q(resource_type__icontains=text)
| Q(resource_id__icontains=text)
| Q(service__icontains=text)
)
return qs
[docs]
def choices() -> dict[str, list[str]]:
"""The providers and resource types of the whole ledger, for the list's filters."""
def distinct(field: str) -> list[str]:
return list(InfrastructureResource.objects.order_by(field).values_list(field, flat=True).distinct())
return {"providers": distinct("provider"), "resourceTypes": distinct("resource_type")}
def positive_int(value: Any, default: int, maximum: Optional[int] = None) -> int:
"""
An integer of the request, from 1 up to ``maximum``, if any, or else ``default``.
:param value: The request's value, of any type.
:param default: The value to use when ``value`` is not an integer from 1 up to ``maximum``.
A boolean is not an integer here.
:param maximum: The largest value allowed, or None for no limit.
:returns: ``value``, or ``default``.
"""
if isinstance(value, bool) or not isinstance(value, int) or value < 1:
return default
if maximum is not None and value > maximum:
return default
return value
[docs]
class InfrastructureResourceListView(ConsoleAppView):
"""
The Infrastructure Resource list page, ``/infrastructure/``, which the single-page console serves (ADR-035).
The console renders ``@smarter/infrastructure-resource-list``'s list at the page's path for a superuser, and shows
anyone else that the page is not found; the list calls :class:`InfrastructureResourceListApiView`, which answers
403 Forbidden to a user who is not a superuser.
"""
[docs]
class InfrastructureResourceListApiView(SmarterAuthenticatedNeverCachedWebView):
"""
A page of the ledger's resources that match the list's filters, newest first, and a summary of all of them.
The request's JSON body may have the filters of :func:`filter_resources`, and ``page``, from
1, and ``pageSize``, up to :data:`MAX_PAGE_SIZE`. A page past the last is the last.
**Response**::
{
"summary": {"total": 12, "active": 9, "activeBillable": 3, "destroyed": 3},
"objects": [{"id": 12, "provider": "aws", "resourceType": "dns.zone", ...}],
"pagination": {"page": 1, "pageSize": 50, "numPages": 1, "count": 9},
"choices": {"providers": ["aws"], "resourceTypes": ["dns.record", "dns.zone"]}
}
"""
@property
def formatted_class_name(self) -> str:
"""Returns a formatted string of the class name for logging purposes."""
return f"{self.formatted_text(__name__)}.{InfrastructureResourceListApiView.__name__}[{id(self)}]"
[docs]
def post(self, request: ASGIRequest, *args, **kwargs) -> JsonResponse:
"""
Return a page of the ledger's resources that match the filters in the JSON body, and a summary.
:param request: The request, whose JSON body has the filters, ``page`` and ``pageSize``.
:returns: The JSON response described above, or 403 Forbidden for a user who is not a superuser.
"""
if not request.user.is_superuser: # type: ignore[union-attr]
return JsonResponse({"error": FORBIDDEN}, status=HTTPStatus.FORBIDDEN)
try:
body = json.loads(request.body or b"{}")
except json.JSONDecodeError:
body = {}
if not isinstance(body, dict):
body = {}
page_size = positive_int(body.get("pageSize"), DEFAULT_PAGE_SIZE, MAX_PAGE_SIZE)
paginator = Paginator(filter_resources(body), page_size)
# get_page() returns the last page for a page past it.
page = paginator.get_page(positive_int(body.get("page"), 1))
retval = {
"summary": summary(),
"objects": InfrastructureResourceSerializer(page.object_list, many=True).data,
"pagination": {
"page": page.number,
"pageSize": page_size,
"numPages": paginator.num_pages,
"count": paginator.count,
},
"choices": choices(),
}
logger.debug("returning %s resources", len(retval["objects"]))
return JsonResponse(retval)
__all__ = [
"InfrastructureResourceListApiView",
"InfrastructureResourceListView",
"choices",
"filter_resources",
"summary",
]