Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

send dependencies.json in client-config request #644

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
abhishekamte-mx marked this conversation as resolved.
Show resolved Hide resolved
}

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