Skip to content

Commit

Permalink
[COST-5711] Provide endpoint of all regions for integrations team (#5395
Browse files Browse the repository at this point in the history
)

* [COST-5711] Provide endpoint of all regions for integrations team
  • Loading branch information
chambridge authored Nov 26, 2024
1 parent 76c807c commit b61c403
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
37 changes: 37 additions & 0 deletions koku/api/resource_types/aws_regions/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
# SPDX-License-Identifier: Apache-2.0
#
"""View for AWS by regions."""
import boto3
from django.conf import settings
from django.db.models import F
from django.utils.decorators import method_decorator
from django.views.decorators.vary import vary_on_headers
from rest_framework import filters
from rest_framework import generics
from rest_framework import status
from rest_framework import views
from rest_framework.permissions import AllowAny
from rest_framework.response import Response

from api.common import CACHE_RH_IDENTITY_HEADER
Expand Down Expand Up @@ -55,3 +58,37 @@ def list(self, request):
return super().list(request)
self.queryset = self.queryset.filter(usage_account_id__in=user_access)
return super().list(request)


class AWSAllRegionView(views.APIView):
"""API GET list view for AWS of all regions"""

permission_classes = [AllowAny]

@method_decorator(vary_on_headers(CACHE_RH_IDENTITY_HEADER))
def get(self, request):
supported_query_params = ["limit", "offset", "search"]
error_message = {}
search_values = []
# Test for only supported query_params
if self.request.query_params:
for key in self.request.query_params:
if key not in supported_query_params:
error_message[key] = [{"Unsupported parameter"}]
return Response(error_message, status=status.HTTP_400_BAD_REQUEST)
search_value_str = self.request.query_params.get("search")
if search_value_str:
search_values = search_value_str.split(",")
regions = boto3.Session().get_available_regions("s3")
filtered_regions = []
for region in regions:
if any(value in region for value in search_values):
filtered_regions.append(region)
if not filtered_regions:
filtered_regions = regions
filtered_regions = list(set(filtered_regions))
queryset = [{"value": region} for region in filtered_regions]
paginator = ResourceTypeViewPaginator()
paginated_queryset = paginator.paginate_queryset(queryset, request)
serializer = ResourceTypeSerializer(paginated_queryset, many=True)
return paginator.get_paginated_response(serializer.data)
3 changes: 3 additions & 0 deletions koku/api/resource_types/test/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class ResourceTypesViewTest(IamTestCase):
ENDPOINTS_AWS = [
"aws-accounts",
"aws-regions",
"aws-all-regions",
"aws-services",
"aws-organizational-units",
"aws-categories",
Expand Down Expand Up @@ -249,6 +250,8 @@ def test_rbacpermissions_aws_account_data_returns_null(self):
"""Test that Aws endpoints accept valid Aws permissions."""
for endpoint in self.ENDPOINTS_AWS:
with self.subTest(endpoint=endpoint):
if endpoint == "aws-all-regions":
continue
url = reverse(endpoint)
response = self.client.get(url, **self.headers)
self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand Down
2 changes: 2 additions & 0 deletions koku/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from api.views import AccountSettings
from api.views import AWSAccountRegionView
from api.views import AWSAccountView
from api.views import AWSAllRegionView
from api.views import AWSCategoryView
from api.views import AWSCostForecastView
from api.views import AWSCostView
Expand Down Expand Up @@ -443,6 +444,7 @@
path("resource-types/azure-services/", AzureServiceView.as_view(), name="azure-services"),
path("resource-types/aws-services/", AWSServiceView.as_view(), name="aws-services"),
path("resource-types/aws-regions/", AWSAccountRegionView.as_view(), name="aws-regions"),
path("resource-types/aws-all-regions/", AWSAllRegionView.as_view(), name="aws-all-regions"),
path(
"resource-types/azure-subscription-guids/",
AzureSubscriptionGuidView.as_view(),
Expand Down
1 change: 1 addition & 0 deletions koku/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
from api.resource_types.aws_ec2_compute_os.view import AWSEC2ComputeOperatingSystemView
from api.resource_types.aws_org_unit.view import AWSOrganizationalUnitView
from api.resource_types.aws_regions.view import AWSAccountRegionView
from api.resource_types.aws_regions.view import AWSAllRegionView
from api.resource_types.aws_services.view import AWSServiceView
from api.resource_types.azure_regions.view import AzureRegionView
from api.resource_types.azure_services.view import AzureServiceView
Expand Down

0 comments on commit b61c403

Please sign in to comment.