def update_provider_from_source(self, source): """Call to update provider.""" connection.set_schema_to_public() context, customer, _ = self._create_context() tenant = Tenant.objects.get(schema_name=customer.schema_name) provider_type = source.source_type json_data = { "name": source.name, "type": provider_type.lower(), "authentication": self._build_credentials_auth(source.authentication), "billing_source": self.get_billing_source_for_provider(provider_type, source.billing_source), } connection.set_tenant(tenant) instance = Provider.objects.get(uuid=source.koku_uuid) serializer = ProviderSerializer(instance=instance, data=json_data, partial=False, context=context) serializer.is_valid(raise_exception=True) serializer.save() invalidate_view_cache_for_tenant_and_cache_key( tenant.schema_name, cache_key_prefix=SOURCES_PREFIX) connection.set_schema_to_public() return instance
def create_provider_from_source(self, source): """Call to create provider.""" connection.set_schema_to_public() context, customer, _ = self._create_context() tenant = self._tenant_for_schema(customer.schema_name) provider_type = source.source_type json_data = { "name": source.name, "type": provider_type.lower(), "authentication": self._build_credentials_auth(source.authentication), "billing_source": self.get_billing_source_for_provider(provider_type, source.billing_source), } if source.source_uuid: json_data["uuid"] = str(source.source_uuid) connection.set_tenant(tenant) serializer = ProviderSerializer(data=json_data, context=context) try: if serializer.is_valid(raise_exception=True): instance = serializer.save() except ValidationError as error: connection.set_schema_to_public() raise error connection.set_schema_to_public() invalidate_view_cache_for_tenant_and_cache_key(customer.schema_name, SOURCES_CACHE_PREFIX) return instance
def update(self, request, *args, **kwargs): """Update a Source.""" invalidate_view_cache_for_tenant_and_cache_key( request.tenant.schema_name, cache_key_prefix=SOURCES_PREFIX) try: return super().update(request=request, args=args, kwargs=kwargs) except (SourcesStorageError, ParseError) as error: raise SourcesException(str(error)) except SourcesDependencyError as error: raise SourcesDependencyException(str(error))
def update(self, request, *args, **kwargs): """Update a Source.""" schema_name = request.user.customer.schema_name try: result = super().update(request=request, args=args, kwargs=kwargs) invalidate_view_cache_for_tenant_and_cache_key( schema_name, SOURCES_CACHE_PREFIX) return result except (SourcesStorageError, ParseError) as error: raise SourcesException(str(error)) except SourcesDependencyError as error: raise SourcesDependencyException(str(error))
def destroy(self, request, *args, **kwargs): """Delete a rate.""" uuid = kwargs.get("uuid") invalidate_view_cache_for_tenant_and_cache_key( request.tenant.schema_name, cache_key_prefix=SOURCES_PREFIX) try: manager = CostModelManager(cost_model_uuid=uuid) except CostModel.DoesNotExist: LOG.info("CostModel does not exist.") else: manager.update_provider_uuids([]) return super().destroy(request=request, args=args, kwargs=kwargs)
def setup_complete(self): """ Set setup_complete to True. Args: None Returns: None """ self.provider.setup_complete = True self.provider.save() invalidate_view_cache_for_tenant_and_cache_key(SOURCES_CACHE_PREFIX)
def test_invalidate_view_cache_for_tenant_and_cache_key(self): """Test that specific cache data is deleted.""" key_to_clear = f"{self.schema_name}:{self.cache_key_prefix}" remaining_key = f"keeper:{self.cache_key_prefix}" cache_data = {key_to_clear: "value", remaining_key: "value"} self.cache.set_many(cache_data) self.assertIsNotNone(self.cache.get(key_to_clear)) self.assertIsNotNone(self.cache.get(remaining_key)) invalidate_view_cache_for_tenant_and_cache_key(self.schema_name, self.cache_key_prefix) self.assertIsNone(self.cache.get(key_to_clear)) self.assertIsNotNone(self.cache.get(remaining_key))
def destroy_provider(self, provider_uuid, retry_count=None): """Call to destroy provider.""" connection.set_schema_to_public() _, customer, user = self._create_context() tenant = self._tenant_for_schema(customer.schema_name) connection.set_tenant(tenant) try: manager = ProviderManager(provider_uuid) except ProviderManagerError: LOG.info("Provider does not exist, skipping Provider delete.") else: manager.remove(user=user, from_sources=True, retry_count=retry_count) invalidate_view_cache_for_tenant_and_cache_key(customer.schema_name, SOURCES_CACHE_PREFIX) connection.set_schema_to_public()
def destroy_provider(self, provider_uuid): """Call to destroy provider.""" connection.set_schema_to_public() _, customer, user = self._create_context() tenant = Tenant.objects.get(schema_name=customer.schema_name) connection.set_tenant(tenant) try: manager = ProviderManager(provider_uuid) except ProviderManagerError: LOG.info("Provider does not exist, skipping Provider delete.") else: manager.remove(user=user, from_sources=True) invalidate_view_cache_for_tenant_and_cache_key( tenant.schema_name, cache_key_prefix=SOURCES_PREFIX) connection.set_schema_to_public()
def destroy(self, request, *args, **kwargs): """Delete a source.""" invalidate_view_cache_for_tenant_and_cache_key( request.tenant.schema_name, cache_key_prefix=SOURCES_PREFIX) source = self.get_object() manager = ProviderBuilder(request.user.identity_header.get("encoded")) for _ in range(5): try: manager.destroy_provider(source.koku_uuid) except IntegrityError as error: LOG.warning(f"Retrying Source delete due to error: {error}") except Exception as error: # catch everything else. return immediately msg = f"Source removal resulted in UNKNOWN error: {type(error).__name__}: {error}" LOG.error(msg) return Response(msg, status=500) else: return super().destroy(request, *args, **kwargs) LOG.error("Failed to remove Source") return Response("Failed to remove Source", status=500)
def set_infrastructure(self, infrastructure_provider_uuid, infrastructure_type): """Create an infrastructure mapping for an OpenShift provider. Args: infrastructure_type (str): The provider type this cluster is installed on. Ex. AWS, AZURE, GCP infrastructure_provider_uuid (str): The UUID of the provider this cluster is installed on. Returns: None """ mapping, _ = ProviderInfrastructureMap.objects.get_or_create( infrastructure_provider_id=infrastructure_provider_uuid, infrastructure_type=infrastructure_type ) self.provider.infrastructure = mapping self.provider.save() invalidate_view_cache_for_tenant_and_cache_key(SOURCES_CACHE_PREFIX)
def set_additional_context(self, new_value): """Sets the additional context value.""" if self.provider: self.provider.additional_context = new_value self.provider.save() invalidate_view_cache_for_tenant_and_cache_key(SOURCES_CACHE_PREFIX)
def set_data_updated_timestamp(self): """Set the data updated timestamp to the current time.""" if self.provider: self.provider.data_updated_timestamp = self.date_accessor.today_with_timezone("UTC") self.provider.save() invalidate_view_cache_for_tenant_and_cache_key(SOURCES_CACHE_PREFIX)
def test_invalidate_view_cache_for_tenant_and_cache_key_unsupported_backend( self): """Test that an unsupported cache backend raises an error.""" with self.assertRaises(KokuCacheError): invalidate_view_cache_for_tenant_and_cache_key( self.schema_name, self.cache_key_prefix)
def test_invalidate_view_cache_for_tenant_and_cache_key_dummy_cache(self): """Test that using DummyCache logs correctly.""" with self.assertLogs(logger="koku.cache", level="INFO"): invalidate_view_cache_for_tenant_and_cache_key( self.schema_name, self.cache_key_prefix)
def update(self, request, *args, **kwargs): invalidate_view_cache_for_tenant_and_cache_key( request.tenant.schema_name, cache_key_prefix=SOURCES_PREFIX) return super().update(request=request, args=args, kwargs=kwargs)