def test_get_source_type_name_non_200(self): """Test to get source type name from type id with bad response.""" source_type_id = 3 mock_source_name = "fakesource" client = SourcesHTTPClient(auth_header=Config.SOURCES_FAKE_HEADER) with requests_mock.mock() as m: m.get( f"http://www.sources.com/api/v1.0/source_types?filter[id]={source_type_id}", status_code=404, json={"data": [{ "name": mock_source_name }]}, ) with self.assertRaises(SourceNotFoundError): client.get_source_type_name(source_type_id) with requests_mock.mock() as m: m.get( f"http://www.sources.com/api/v1.0/source_types?filter[id]={source_type_id}", status_code=401, json={"data": [{ "name": mock_source_name }]}, ) with self.assertRaises(SourcesHTTPClientError): client.get_source_type_name(source_type_id)
def test_get_source_type_name_error(self): """Test to get source type name from type id with error.""" source_type_id = 3 client = SourcesHTTPClient(auth_header=Config.SOURCES_FAKE_HEADER) with requests_mock.mock() as m: m.get( f'http://www.sources.com/api/v1.0/source_types?filter[id]={source_type_id}', exc=requests.exceptions.RequestException) with self.assertRaises(SourcesHTTPClientError): client.get_source_type_name(source_type_id)
def test_get_source_type_name_exceptions(self): """Test to get source type name from type id with error.""" source_type_id = 3 client = SourcesHTTPClient(auth_header=Config.SOURCES_FAKE_HEADER) json_data = [None, [], [{"not_name": 4}]] for test in json_data: with self.subTest(test=test): with requests_mock.mock() as m: m.get( f"{MOCK_URL}/api/v1.0/{ENDPOINT_SOURCE_TYPES}?filter[id]={source_type_id}", json={"data": test}) with self.assertRaises(SourcesHTTPClientError): client.get_source_type_name(source_type_id)
def cost_mgmt_msg_filter(msg_data): """Verify that message is for cost management.""" event_type = msg_data.get("event_type") auth_header = msg_data.get("auth_header") if event_type in (KAFKA_APPLICATION_DESTROY, KAFKA_SOURCE_DESTROY): return msg_data if event_type in (KAFKA_AUTHENTICATION_CREATE, KAFKA_AUTHENTICATION_UPDATE): sources_network = SourcesHTTPClient(auth_header) source_id = sources_network.get_source_id_from_endpoint_id( msg_data.get("resource_id")) msg_data["source_id"] = source_id if not sources_network.get_application_type_is_cost_management( source_id): LOG.info( f"Resource id {msg_data.get('resource_id')} not associated with cost-management." ) return None else: source_id = msg_data.get("source_id") sources_network = SourcesHTTPClient(auth_header, source_id=source_id) source_details = sources_network.get_source_details() source_type_id = int(source_details.get("source_type_id")) source_type_name = sources_network.get_source_type_name(source_type_id) if source_type_name not in (SOURCES_OCP_SOURCE_NAME, SOURCES_AWS_SOURCE_NAME, SOURCES_AZURE_SOURCE_NAME): LOG.debug(f"Filtering unexpected source type {source_type_name}.") return None return msg_data
def __init__(self, auth_header, source_id): sources_network = SourcesHTTPClient(auth_header, source_id) details = sources_network.get_source_details() self.name = details.get("name") self.source_type_id = int(details.get("source_type_id")) self.source_uuid = details.get("uid") self.source_type_name = sources_network.get_source_type_name(self.source_type_id) self.source_type = SOURCE_PROVIDER_MAP.get(self.source_type_name)
def __init__(self, auth_header, source_id): """Constructor.""" sources_network = SourcesHTTPClient(auth_header, source_id) details = sources_network.get_source_details() self.name = details.get("name") self.source_type_id = int(details.get("source_type_id")) self.source_uuid = details.get("uid") self.source_type_name = sources_network.get_source_type_name( self.source_type_id) self.source_type = SOURCE_PROVIDER_MAP.get(self.source_type_name) self.app_settings = sources_network.get_application_settings( self.source_type)
def test_get_source_type_name(self): """Test to get source type name from type id.""" source_type_id = 3 mock_source_name = "fakesource" client = SourcesHTTPClient(auth_header=Config.SOURCES_FAKE_HEADER) with requests_mock.mock() as m: m.get( f"http://www.sources.com/api/v1.0/source_types?filter[id]={source_type_id}", status_code=200, json={"data": [{"name": mock_source_name}]}, ) response = client.get_source_type_name(source_type_id) self.assertEqual(response, mock_source_name)
def test_get_source_type_name(self): """Test to get source type name from type id.""" source_type_id = 3 mock_source_name = faker.name() client = SourcesHTTPClient(auth_header=Config.SOURCES_FAKE_HEADER) with requests_mock.mock() as m: m.get( f"{MOCK_URL}/api/v1.0/{ENDPOINT_SOURCE_TYPES}?filter[id]={source_type_id}", status_code=200, json={"data": [{"name": mock_source_name}]}, ) response = client.get_source_type_name(source_type_id) self.assertEqual(response, mock_source_name)
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 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)