Skip to content

Commit

Permalink
Merge pull request #653 from mendix/DB-2086_send_dependencies_json_in…
Browse files Browse the repository at this point in the history
…_client_config_call_release_4.x

Db 2086 send dependencies json in client config call release 4.x
  • Loading branch information
sailhenz authored Jun 29, 2023
2 parents e3a6908 + bdceb36 commit 9c3c331
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
27 changes: 24 additions & 3 deletions buildpack/databroker/business_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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()
Expand All @@ -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:
Expand All @@ -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"
Expand Down
9 changes: 7 additions & 2 deletions tests/unit/test_databroker_business_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9c3c331

Please sign in to comment.