def sources_network_info(source_id, auth_header): """ Get additional sources context from Sources REST API. Additional details retrieved from the network includes: - Source Name - Source ID Type -> AWS, Azure, or OCP - Authentication: OCP -> Source uid; AWS -> Network call to Sources Authentication Store Details are stored in the Sources database table. Args: source_id (Integer): Source identifier auth_header (String): Authentication Header. Returns: None """ src_details = SourceDetails(auth_header, source_id) if not src_details.source_type: LOG.warning(f"Unexpected source type ID: {src_details.source_type_id}") return storage.add_provider_sources_network_info(src_details, source_id) save_auth_info(auth_header, source_id) app_settings = src_details.app_settings if app_settings: try: storage.update_application_settings(source_id, app_settings) except storage.SourcesStorageError as error: LOG.error( f"Unable to apply application settings. error: {str(error)}") return
def sources_network_info(source_id, auth_header): """ Get additional sources context from Sources REST API. Additional details retrieved from the network includes: - Source Name - Source ID Type -> AWS, Azure, or OCP - Authentication: OCP -> Source uid; AWS -> Network call to Sources Authentication Store Details are stored in the Sources database table. Args: source_id (Integer): Source identifier auth_header (String): Authentication Header. Returns: None """ src_details = SourceDetails(auth_header, source_id) if not src_details.endpoint_id and src_details.source_type_name != SOURCES_OCP_SOURCE_NAME: LOG.warning(f"Unable to find endpoint for Source ID: {source_id}") return if not src_details.source_type: LOG.warning(f"Unexpected source type ID: {src_details.source_type_id}") return storage.add_provider_sources_network_info(src_details, source_id) save_auth_info(auth_header, source_id)
def test_post_billing_source(self): """Test the POST billing_source endpoint.""" billing_source = {'bucket': 'cost-usage-bucket'} test_name = 'AWS Test' test_source_type = 'AWS' test_source_id = 1 test_resource_id = 1 test_matrix = [{ 'source_id': test_source_id, 'billing_source': billing_source }, { 'source_name': test_name, 'billing_source': billing_source }] for params in test_matrix: add_provider_sources_network_info(self.test_source_id, test_name, test_source_type, test_resource_id) response = self.client.post(reverse('billing-source'), json.dumps(params), content_type='application/json') body = response.json() self.assertEqual(response.status_code, 201) self.assertIn(str(billing_source), str(body)) self.assertEqual( Sources.objects.get( source_id=self.test_source_id).billing_source, billing_source)
def test_add_provider_billing_source_non_existent(self): """Tests that add a billing source to a non-existent Source.""" s3_bucket = {'bucket': 'test-bucket'} storage.add_provider_sources_network_info(self.test_source_id + 1, 'AWS Account', 'AWS', 1) with self.assertRaises(SourcesStorageError): storage.add_provider_billing_source(self.test_source_id, s3_bucket)
def test_add_provider_billing_source_non_aws(self): """Tests that add a non-AWS billing source to a Source.""" s3_bucket = {'bucket': 'test-bucket'} storage.add_provider_sources_network_info(self.test_source_id, 'OCP Account', 'OCP', 1) with self.assertRaises(SourcesStorageError): storage.add_provider_billing_source(self.test_source_id, s3_bucket)
def test_add_provider_billing_source(self): """Tests that add an AWS billing source to a Source.""" s3_bucket = {'bucket': 'test-bucket'} storage.add_provider_sources_network_info(self.test_source_id, faker.uuid4(), 'AWS Account', 'AWS', 1) storage.add_provider_billing_source({'source_id': self.test_source_id}, s3_bucket) self.assertEqual(Sources.objects.get(source_id=self.test_source_id).billing_source, s3_bucket)
def test_add_provider_network_info_not_found(self): """Tests that adding information retrieved from the sources network API is not successful.""" try: test_name = "My Source Name" source_type = Provider.PROVIDER_AWS mock_details = MockDetails(test_name, faker.uuid4(), source_type, 1) storage.add_provider_sources_network_info(mock_details, self.test_source_id + 1) except Exception as error: self.fail(str(error))
def test_add_provider_network_info_not_found(self): """Tests that adding information retrieved from the sources network API is not successful.""" try: test_name = 'My Source Name' source_type = 'AWS' authentication = 'testauth' storage.add_provider_sources_network_info(self.test_source_id + 1, test_name, source_type, authentication) except Exception as error: self.fail(str(error))
def test_add_provider_network_info_not_found(self): """Tests that adding information retrieved from the sources network API is not successful.""" try: test_name = "My Source Name" source_type = Provider.PROVIDER_AWS authentication = "testauth" storage.add_provider_sources_network_info(self.test_source_id + 1, faker.uuid4(), test_name, source_type, authentication) except Exception as error: self.fail(str(error))
def sources_network_info(source_id, auth_header): """ Get additional sources context from Sources REST API. Additional details retrieved from the network includes: - Source Name - Source ID Type -> AWS, Azure, or OCP - Authentication: OCP -> Source uid; AWS -> Network call to Sources Authentication Store Details are stored in the Sources database table. Args: source_id (Integer): Source identifier auth_header (String): Authentication Header. Returns: None """ sources_network = SourcesHTTPClient(auth_header, source_id) try: source_details = sources_network.get_source_details() except SourcesHTTPClientError as conn_err: err_msg = f'Unable to get for Source {source_id} information. Reason: {str(conn_err)}' LOG.error(err_msg) return source_name = source_details.get('name') source_type_id = int(source_details.get('source_type_id')) source_uuid = source_details.get('uid') source_type_name = sources_network.get_source_type_name(source_type_id) endpoint_id = sources_network.get_endpoint_id() if not endpoint_id and not source_type_name == SOURCES_OCP_SOURCE_NAME: LOG.error(f'Unable to find endpoint for Source ID: {source_id}') return if source_type_name == SOURCES_OCP_SOURCE_NAME: source_type = 'OCP' elif source_type_name == SOURCES_AWS_SOURCE_NAME: source_type = 'AWS' elif source_type_name == SOURCES_AZURE_SOURCE_NAME: source_type = 'AZURE' else: LOG.error(f'Unexpected source type ID: {source_type_id}') return storage.add_provider_sources_network_info(source_id, source_uuid, source_name, source_type, endpoint_id) save_auth_info(auth_header, source_id)
def test_post_billing_source_non_aws(self): """Test the POST billing_source endpoint for a non-AWS source.""" params = { 'source_id': '1', 'billing_source': {'bucket': 'cost-usage-bucket'}, } expected_string = 'Source is not AWS nor AZURE.' test_name = 'OCP Test' test_source_type = 'OCP' test_resource_id = 1 add_provider_sources_network_info(self.test_source_id, test_name, test_source_type, test_resource_id) response = self.client.post(reverse('billing-source'), params) body = response.json() self.assertEqual(response.status_code, 400) self.assertIn(expected_string, str(body))
def test_add_provider_network_info(self): """Tests that adding information retrieved from the sources network API is successful.""" test_source = Sources.objects.get(source_id=self.test_source_id) self.assertIsNone(test_source.name) self.assertEqual(test_source.source_type, '') self.assertEqual(test_source.authentication, {}) test_name = 'My Source Name' source_type = 'AWS' endpoint_id = 1 storage.add_provider_sources_network_info(self.test_source_id, test_name, source_type, endpoint_id) test_source = Sources.objects.get(source_id=self.test_source_id) self.assertEqual(test_source.name, test_name) self.assertEqual(test_source.source_type, source_type) self.assertEqual(test_source.endpoint_id, endpoint_id)
def test_add_provider_network_info(self): """Tests that adding information retrieved from the sources network API is successful.""" test_source = Sources.objects.get(source_id=self.test_source_id) self.assertIsNone(test_source.name) self.assertEqual(test_source.source_type, "") self.assertEqual(test_source.authentication, {}) test_name = "My Source Name" source_type = Provider.PROVIDER_AWS endpoint_id = 1 source_uuid = faker.uuid4() mock_details = MockDetails(test_name, source_uuid, source_type, endpoint_id) storage.add_provider_sources_network_info(mock_details, self.test_source_id) test_source = Sources.objects.get(source_id=self.test_source_id) self.assertEqual(test_source.name, test_name) self.assertEqual(test_source.source_type, source_type) self.assertEqual(str(test_source.source_uuid), source_uuid)
def sources_network_info(source_id, auth_header): """ Get additional sources context from Sources REST API. Additional details retrieved from the network includes: - Source Name - Source ID Type -> AWS, Azure, or OCP - Authentication: OCP -> Source uid; AWS -> Network call to Sources Authentication Store Details are stored in the Sources database table. Args: source_id (Integer): Source identifier auth_header (String): Authentication Header. Returns: None """ sources_network = SourcesHTTPClient(auth_header, source_id) source_details = sources_network.get_source_details() source_name = source_details.get("name") source_type_id = int(source_details.get("source_type_id")) source_uuid = source_details.get("uid") source_type_name = sources_network.get_source_type_name(source_type_id) endpoint_id = sources_network.get_endpoint_id() if not endpoint_id and not source_type_name == SOURCES_OCP_SOURCE_NAME: LOG.warning(f"Unable to find endpoint for Source ID: {source_id}") return source_type = SOURCE_PROVIDER_MAP.get(source_type_name) if not source_type: LOG.warning(f"Unexpected source type ID: {source_type_id}") return storage.add_provider_sources_network_info(source_id, source_uuid, source_name, source_type, endpoint_id) save_auth_info(auth_header, source_id)