Skip to content

Commit

Permalink
source async delete followups (#2743)
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglas Curtis authored Mar 23, 2021
1 parent bf97882 commit c50bfd8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
2 changes: 1 addition & 1 deletion koku/sources/api/source_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(self, request, source_id):
self.user = request.user
self.source_id = source_id
self.source = Sources.objects.get(source_id=source_id)
if not source_settings_complete(self.source):
if not source_settings_complete(self.source) or self.source.pending_delete:
raise ObjectDoesNotExist(f"Source ID: {self.source_id} not ready for status")
self.sources_client = SourcesHTTPClient(self.source.auth_header, source_id=source_id)

Expand Down
13 changes: 13 additions & 0 deletions koku/sources/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,18 @@ def get_source(source_id, err_msg, logger):
raise error


def mark_provider_as_inactive(provider_uuid):
"""Mark provider as inactive so we do not continue to ingest data while the source is being deleted."""
try:
provider = Provider.objects.get(uuid=provider_uuid)
provider.active = False
provider.billing_source = None
provider.authentication = None
provider.save()
except Provider.DoesNotExist:
LOG.info(f"Provider {provider_uuid} does not exist. Unable to mark as inactive")


def enqueue_source_delete(source_id, offset, allow_out_of_order=False):
"""
Queues a source destroy event to be processed by the synchronize_sources method.
Expand All @@ -278,6 +290,7 @@ def enqueue_source_delete(source_id, offset, allow_out_of_order=False):
if not source.pending_delete and not source.out_of_order_delete:
source.pending_delete = True
source.save()
mark_provider_as_inactive(source.koku_uuid)
except Sources.DoesNotExist:
if allow_out_of_order:
LOG.info(f"Source ID: {source_id} not known. Marking as out of order delete.")
Expand Down
36 changes: 29 additions & 7 deletions koku/sources/test/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
from django.test import TestCase
from faker import Faker

from api.iam.models import Customer
from api.provider.models import Provider
from api.provider.models import ProviderAuthentication
from api.provider.models import ProviderBillingSource
from api.provider.models import Sources
from sources import storage
from sources.config import Config
Expand Down Expand Up @@ -387,19 +390,38 @@ def test_enqueue_source_delete(self):
"""Test for enqueuing source delete."""
test_source_id = 3
test_offset = 3
aws_obj = Sources(

account_name = "Test Provider"
provider_uuid = faker.uuid4()
ocp_provider = Provider.objects.create(
uuid=provider_uuid,
name=account_name,
type=Provider.PROVIDER_OCP,
authentication=ProviderAuthentication.objects.create(credentials={"cluster_id": "my-cluster'"}).save(),
billing_source=ProviderBillingSource.objects.create(data_source={}),
customer=Customer.objects.create(account_id="123", schema_name="myschema").save(),
setup_complete=False,
)
ocp_provider.save()

ocp_obj = Sources(
source_id=test_source_id,
auth_header=self.test_header,
offset=test_offset,
source_type=Provider.PROVIDER_AWS,
name="Test AWS Source",
billing_source={"bucket": "test-bucket"},
source_type=Provider.PROVIDER_OCP,
name=account_name,
koku_uuid=ocp_provider.uuid,
)
aws_obj.save()
ocp_obj.save()

storage.enqueue_source_delete(test_source_id, test_offset)
response = Sources.objects.get(source_id=test_source_id)
self.assertTrue(response.pending_delete)
source_response = Sources.objects.get(source_id=test_source_id)
self.assertTrue(source_response.pending_delete)

provider_response = Provider.objects.get(uuid=provider_uuid)
self.assertFalse(provider_response.active)
self.assertIsNone(provider_response.billing_source)
self.assertIsNone(provider_response.authentication)

def test_enqueue_source_delete_db_down(self):
"""Tests enqueues source_delete with database error."""
Expand Down

0 comments on commit c50bfd8

Please sign in to comment.