def test_create_provider_exceptions(self): """Test to create a provider with a non-recoverable error.""" client = ProviderBuilder(auth_header=Config.SOURCES_FAKE_HEADER) with self.assertRaises(ValidationError): source_uuid = faker.uuid4() client.create_provider(self.name, self.provider_type, self.authentication, self.billing_source, source_uuid)
def test_update_provider_error(self): """Test to update a provider with a koku server error.""" client = ProviderBuilder(auth_header=Config.SOURCES_FAKE_HEADER) source_uuid = faker.uuid4() with patch.object(ProviderAccessor, "cost_usage_source_ready", returns=True): client.create_provider(self.name, self.provider_type, self.authentication, self.billing_source, source_uuid) with self.assertRaises(ValidationError): client.update_provider(source_uuid, "Aws Test", Provider.PROVIDER_AWS, {"resource_name": "arn:test"}, {"bucket": "bucket"})
def test_create_provider(self): """Test to create a provider.""" client = ProviderBuilder(auth_header=Config.SOURCES_FAKE_HEADER) with patch.object(ProviderAccessor, "cost_usage_source_ready", returns=True): provider = client.create_provider(self.name, self.provider_type, self.authentication, self.billing_source) self.assertEqual(provider.name, self.name)
def create(self, validated_data): """Create a source from validated data.""" auth_header = get_auth_header(self.context.get("request")) manager = ProviderBuilder(auth_header) validated_data["auth_header"] = auth_header source = Sources.objects.create(**validated_data) provider = manager.create_provider(source.name, source.source_type, source.authentication, source.billing_source, source.source_uuid) source.koku_uuid = provider.uuid source.save() LOG.info("Admin created Source and Provider.") return source
def test_destroy_provider(self): """Test to destroy a provider.""" client = ProviderBuilder(auth_header=Config.SOURCES_FAKE_HEADER) source_uuid = faker.uuid4() with patch.object(ProviderAccessor, "cost_usage_source_ready", returns=True): provider = client.create_provider(self.name, self.provider_type, self.authentication, self.billing_source, source_uuid) self.assertEqual(provider.name, self.name) self.assertEqual(str(provider.uuid), source_uuid) client.destroy_provider(source_uuid) with self.assertRaises(Provider.DoesNotExist): Provider.objects.get(uuid=source_uuid)
def test_update_provider(self): """Test to update a provider.""" client = ProviderBuilder(auth_header=Config.SOURCES_FAKE_HEADER) source_uuid = faker.uuid4() with patch.object(ProviderAccessor, "cost_usage_source_ready", returns=True): provider = client.create_provider(self.name, self.provider_type, self.authentication, self.billing_source, source_uuid) new_name = "Aws Test" updated_provider = client.update_provider( source_uuid, new_name, Provider.PROVIDER_AWS, {"resource_name": "arn:test"}, {"bucket": "bucket"}) self.assertEqual(updated_provider.uuid, provider.uuid) self.assertEqual(updated_provider.name, new_name)
def test_destroy_provider_exception(self): """Test to destroy a provider with a connection error.""" client = ProviderBuilder(auth_header=Config.SOURCES_FAKE_HEADER) source_uuid = faker.uuid4() with patch.object(ProviderAccessor, "cost_usage_source_ready", returns=True): provider = client.create_provider(self.name, self.provider_type, self.authentication, self.billing_source, source_uuid) self.assertEqual(provider.name, self.name) self.assertEqual(str(provider.uuid), source_uuid) logging.disable(logging.NOTSET) with self.assertLogs(logger="api.provider.provider_builder", level=logging.INFO): client.destroy_provider(faker.uuid4())
class SourcesProviderCoordinator: """Coordinator to control source and provider operations.""" def __init__(self, source_id, auth_header): """Initialize the client.""" header = {"x-rh-identity": auth_header, "sources-client": "True"} self._source_id = source_id self._identity_header = header self._provider_builder = ProviderBuilder(self._identity_header) def create_account(self, name, provider_type, authentication, billing_source, source_uuid=None): """Call to create provider.""" try: provider = self._provider_builder.create_provider( name, provider_type, authentication, billing_source, source_uuid) add_provider_koku_uuid(self._source_id, provider.uuid) except ProviderBuilderError as provider_err: raise SourcesProviderCoordinatorError(str(provider_err)) return provider def update_account(self, provider_uuid, name, provider_type, authentication, billing_source): """Call to update provider.""" try: provider = self._provider_builder.update_provider( provider_uuid, name, provider_type, authentication, billing_source) clear_update_flag(self._source_id) except ProviderBuilderError as provider_err: raise SourcesProviderCoordinatorError(str(provider_err)) return provider def destroy_account(self, provider_uuid): """Call to destroy provider.""" try: self._provider_builder.destroy_provider(provider_uuid) destroy_source_event(self._source_id) except ProviderBuilderError as provider_err: LOG.error(f"Failed to remove provider. Error: {str(provider_err)}")