Skip to content

Commit

Permalink
[COST-5887] - add handling for GCP Forbidden exception (#5518)
Browse files Browse the repository at this point in the history
  • Loading branch information
lcouzens authored Feb 26, 2025
1 parent 96dccda commit 7b2b6dd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions koku/providers/gcp/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging

import google.auth
from google.api_core.exceptions import Forbidden
from google.auth.exceptions import RefreshError
from google.cloud import bigquery
from google.cloud import storage
Expand Down Expand Up @@ -94,6 +95,10 @@ def _detect_billing_export_table(self, data_source, credentials):
f"Unable to find dataset: {data_source.get('dataset')} in project: {credentials.get('project_id')}"
)
raise serializers.ValidationError(error_obj(key, message))
except Forbidden as err:
key = "authentication.project_id"
message = f"403 forbidden error. {err.message}"
raise serializers.ValidationError(error_obj(key, message))
except BadRequest as e:
LOG.warning(str(e))
key = "billing_source"
Expand Down
12 changes: 12 additions & 0 deletions koku/providers/test/gcp/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from django.test import TestCase
from faker import Faker
from google.api_core.exceptions import Forbidden
from google.auth.exceptions import RefreshError
from google.cloud.exceptions import BadRequest
from google.cloud.exceptions import GoogleCloudError
Expand Down Expand Up @@ -222,6 +223,17 @@ def test_cost_usage_source_is_reachable_no_table_id_notready(self, mock_auth, mo
with self.assertRaises(SkipStatusPush):
provider.cost_usage_source_is_reachable(credentials_param, billing_source_param)

@patch("providers.gcp.provider.GCPProvider.get_table_id")
def test_detect_billing_export_table_forbidden(self, mock_get_table):
"""Verify forbidden error is raised."""
err_msg = "GCP Error"
mock_get_table.side_effect = Forbidden(err_msg)
provider = GCPProvider()
billing = {"dataset": FAKE.word()}
creds = {"project_id": FAKE.word()}
with self.assertRaises(ValidationError):
provider._detect_billing_export_table(billing, creds)

@patch("providers.gcp.provider.bigquery")
@patch("providers.gcp.provider.discovery")
@patch("providers.gcp.provider.google.auth.default")
Expand Down

0 comments on commit 7b2b6dd

Please sign in to comment.