def test_get_application_type_is_cost_management_misconfigured(self): """Test to get application_type_id from source_id with route not found.""" application_type_id = 2 source_id = 3 client = SourcesHTTPClient(auth_header=Config.SOURCES_FAKE_HEADER, source_id=source_id) responses.add( responses.GET, f"http://www.sources.com/api/v1.0/application_types/{application_type_id}/sources", json={"data": [{ "name": "test-source" }]}, status=404, ) responses.add( responses.GET, "http://www.sources.com/api/v1.0/application_types", json={"data": [{ "id": self.application_type }]}, status=200, ) with self.assertRaises(SourceNotFoundError): client.get_application_type_is_cost_management(source_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 test_get_application_type_is_cost_management(self): """Test to get application_type_id from source_id.""" application_type_id = 2 source_id = 3 client = SourcesHTTPClient(auth_header=Config.SOURCES_FAKE_HEADER, source_id=source_id) responses.add( responses.GET, f"http://www.sources.com/api/v1.0/application_types/{application_type_id}/sources", json={"data": [{ "name": "test-source" }]}, status=200, ) responses.add( responses.GET, "http://www.sources.com/api/v1.0/application_types", json={"data": [{ "id": self.application_type }]}, status=200, ) response = client.get_application_type_is_cost_management(source_id) self.assertTrue(response)
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) if msg_data.get("resource_type") == "Application": source_id = sources_network.get_source_id_from_applications_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") return msg_data
def test_get_application_type_is_cost_management(self): """Test if app belongs to cost management.""" client = SourcesHTTPClient(auth_header=Config.SOURCES_FAKE_HEADER, source_id=self.source_id) table = [ { "data": None, "expected": False }, { "data": [], "expected": False }, { "data": [{ "not": "empty" }], "expected": True }, { "cost-id": COST_MGMT_APP_TYPE_ID, "data": None, "expected": False }, { "cost-id": COST_MGMT_APP_TYPE_ID, "data": [], "expected": False }, { "cost-id": COST_MGMT_APP_TYPE_ID, "data": [{ "not": "empty" }], "expected": True }, ] for test in table: with self.subTest(test=test): with patch.object(SourcesHTTPClient, "get_cost_management_application_type_id", return_value=2): with requests_mock.mock() as m: m.get( f"{MOCK_URL}/api/v1.0/{ENDPOINT_APPLICATION_TYPES}/{COST_MGMT_APP_TYPE_ID}/sources?filter[id]={self.source_id}", # noqa: E501 json={"data": test.get("data")}, ) result = client.get_application_type_is_cost_management( test.get("cost-id")) self.assertEqual(result, test.get("expected"))