Skip to content

Commit 2dd7915

Browse files
authored
[COST-5012] add cost models to internal sources endpoint (#5084)
1 parent cf77b7a commit 2dd7915

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

koku/masu/api/sources/serializers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Meta:
4848

4949

5050
class ProviderBillingSourceSerializer(serializers.ModelSerializer):
51-
"""Serializer for the Provider Authentication model."""
51+
"""Serializer for the Provider Billing Source model."""
5252

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

koku/masu/api/sources/views.py

+23
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from django_filters import FilterSet
1010
from django_filters import UUIDFilter
1111
from django_filters.rest_framework import DjangoFilterBackend
12+
from django_tenants.utils import schema_context
1213
from rest_framework import mixins
1314
from rest_framework import viewsets
1415
from rest_framework.permissions import AllowAny
@@ -17,6 +18,7 @@
1718

1819
from api.common.filters import CharListFilter
1920
from api.provider.models import Sources
21+
from cost_models.models import CostModelMap
2022
from masu.api.sources.serializers import SourceSerializer
2123

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

99101
return obj
102+
103+
def list(self, request, *args, **kwargs):
104+
"""Obtain the list of sources."""
105+
response = super().list(request=request, args=args, kwargs=kwargs)
106+
for obj in response.data["data"]:
107+
obj["cost_models"] = self.get_cost_models(obj)
108+
return response
109+
110+
def retrieve(self, request, *args, **kwargs):
111+
"""Get a source."""
112+
response = super().retrieve(request=request, args=args, kwargs=kwargs)
113+
response.data["cost_models"] = self.get_cost_models(response.data)
114+
return response
115+
116+
def get_cost_models(self, obj):
117+
"""Get the cost models associated with this provider."""
118+
if not (schema := obj.get("provider", {}).get("customer", {}).get("schema_name")):
119+
return []
120+
with schema_context(schema):
121+
cost_models_map = CostModelMap.objects.filter(provider_uuid=obj["source_uuid"])
122+
return [{"name": m.cost_model.name, "uuid": m.cost_model.uuid} for m in cost_models_map]

0 commit comments

Comments
 (0)