Skip to content

Commit

Permalink
[COST-5012] add cost models to internal sources endpoint (#5084)
Browse files Browse the repository at this point in the history
  • Loading branch information
maskarb authored May 3, 2024
1 parent cf77b7a commit 2dd7915
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion koku/masu/api/sources/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Meta:


class ProviderBillingSourceSerializer(serializers.ModelSerializer):
"""Serializer for the Provider Authentication model."""
"""Serializer for the Provider Billing Source model."""

data_source = serializers.JSONField(allow_null=False, required=True)

Expand Down
23 changes: 23 additions & 0 deletions koku/masu/api/sources/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django_filters import FilterSet
from django_filters import UUIDFilter
from django_filters.rest_framework import DjangoFilterBackend
from django_tenants.utils import schema_context
from rest_framework import mixins
from rest_framework import viewsets
from rest_framework.permissions import AllowAny
Expand All @@ -17,6 +18,7 @@

from api.common.filters import CharListFilter
from api.provider.models import Sources
from cost_models.models import CostModelMap
from masu.api.sources.serializers import SourceSerializer

MIXIN_LIST = [mixins.ListModelMixin, mixins.RetrieveModelMixin, mixins.UpdateModelMixin, viewsets.GenericViewSet]
Expand Down Expand Up @@ -97,3 +99,24 @@ def get_object(self):
raise Http404

return obj

def list(self, request, *args, **kwargs):
"""Obtain the list of sources."""
response = super().list(request=request, args=args, kwargs=kwargs)
for obj in response.data["data"]:
obj["cost_models"] = self.get_cost_models(obj)
return response

def retrieve(self, request, *args, **kwargs):
"""Get a source."""
response = super().retrieve(request=request, args=args, kwargs=kwargs)
response.data["cost_models"] = self.get_cost_models(response.data)
return response

def get_cost_models(self, obj):
"""Get the cost models associated with this provider."""
if not (schema := obj.get("provider", {}).get("customer", {}).get("schema_name")):
return []
with schema_context(schema):
cost_models_map = CostModelMap.objects.filter(provider_uuid=obj["source_uuid"])
return [{"name": m.cost_model.name, "uuid": m.cost_model.uuid} for m in cost_models_map]

0 comments on commit 2dd7915

Please sign in to comment.