diff --git a/buildpack/databroker/business_events.py b/buildpack/databroker/business_events.py index e8f7a39b5..67c8342e9 100644 --- a/buildpack/databroker/business_events.py +++ b/buildpack/databroker/business_events.py @@ -3,11 +3,15 @@ Extract Business Events configuration from vcap services and create mx constants """ +import os import logging import requests +from requests.adapters import HTTPAdapter, Retry from buildpack import util +BASE_PATH = os.getcwd() + CONSTANTS_PREFIX = "BusinessEvents" BE_CONSTANTS_TO_INJECT = [ @@ -30,15 +34,21 @@ def update_config(m2ee, vcap_services_data): logging.debug("Business Events config added to MicroflowConstants") -def _get_client_config(url, auth_token, version): +def _put_client_config(url, auth_token, version, dependencies_json_str): headers = { "Authorization": f"Bearer {auth_token}", "X-Version": version, } - resp = requests.get( + session = requests.Session() + retries = Retry(total=2, backoff_factor=0.1, status_forcelist=[500, 502, 503, 504]) + adapter = HTTPAdapter(max_retries=retries) + session.mount("https://", adapter) + + resp = session.put( url=url, headers=headers, + json={"dependencies": dependencies_json_str}, timeout=30, ) resp.raise_for_status() @@ -55,6 +65,16 @@ def _configure_business_events_metrics(be_config, existing_constants): be_config[f"{CONSTANTS_PREFIX}.EnableHeartbeat"] = "true" +def _read_dependencies_json_as_str(): + file_path = os.path.join(BASE_PATH, "model", "dependencies.json") + try: + with open(file_path) as f: + return f.read() + except FileNotFoundError: + logging.error("Business Events: dependencies.json not found %s", file_path) + raise + + def _get_config(vcap_services, existing_constants): be_config = {} try: @@ -76,10 +96,11 @@ def _get_config(vcap_services, existing_constants): be_config[ f"{CONSTANTS_PREFIX}.{constant}" ] = kafka_creds.get(constant, "") - client_config = _get_client_config( + client_config = _put_client_config( kafka_creds.get(CLIENT_CONFIG_URL_KEY, ""), auth_token, "1", + _read_dependencies_json_as_str(), ) be_config[ f"{CONSTANTS_PREFIX}.ClientConfiguration" diff --git a/tests/unit/test_databroker_business_events.py b/tests/unit/test_databroker_business_events.py index 02fa2765e..d3c1e6e23 100644 --- a/tests/unit/test_databroker_business_events.py +++ b/tests/unit/test_databroker_business_events.py @@ -166,13 +166,18 @@ class TestDataBrokerBusinessEvents(unittest.TestCase): def _verify_vcap_info(self, is_apply_limits_present=True): with mock.patch( - "buildpack.databroker.business_events._get_client_config", + "buildpack.databroker.business_events._put_client_config", mock.MagicMock(return_value=self.expected_client_config), - ): + ), mock.patch( + "buildpack.databroker.business_events._read_dependencies_json_as_str", + mock.MagicMock(return_value=""), + ) as mock_read_dependencies_json_as_str: business_events_cfg = business_events._get_config( util.get_vcap_services_data(), self.module_constants_with_metrics, ) + mock_read_dependencies_json_as_str.assert_called_once() + prefix = business_events.CONSTANTS_PREFIX assert business_events_cfg[f"{prefix}.ServerUrl"] == self.server_url