Skip to content

Commit

Permalink
fix: Read region configuration to create watcher client
Browse files Browse the repository at this point in the history
- fix/Read watcher configuration from clusterdb to build watcher client
- doc/Update guests_on_hypervisor func docstring
- doc/Add comment for TIMEOUT and TIMEOUT_INTERVAL variables
  • Loading branch information
jneo8 committed Jan 10, 2025
1 parent 7805ec9 commit aeb87ba
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion sunbeam-python/sunbeam/core/openstack_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def guests_on_hypervisor(
"""Return a list of guests that run on the given hypervisor.
:param hypervisor_name: Name of hypervisor
:param jhelper: Juju helpers for retrieving admin credentials
:param conn: Admin connection
:raises: openstack.exceptions.SDKException
"""
return list(
Expand Down
17 changes: 12 additions & 5 deletions sunbeam-python/sunbeam/core/watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@
from watcherclient.common.apiclient.exceptions import NotFound
from watcherclient.v1 import client as watcher_client

from sunbeam.core.common import SunbeamException
from sunbeam.core.common import SunbeamException, read_config
from sunbeam.core.deployment import Deployment
from sunbeam.core.juju import JujuHelper
from sunbeam.core.openstack_api import get_admin_connection
from sunbeam.steps.openstack import REGION_CONFIG_KEY

LOG = logging.getLogger(__name__)

# Timeout while waiting for the watcher resource to reach the target state.
TIMEOUT = 60 * 3
# Sleep interval between querying watcher resources.
SLEEP_INTERVAL = 5
ENABLE_MAINTENANCE_AUDIT_TEMPLATE_NAME = "Sunbeam Cluster Maintaining Template"
ENABLE_MAINTENANCE_STRATEGY_NAME = "host_maintenance"
Expand All @@ -39,12 +43,15 @@
WORKLOAD_BALANCING_AUDIT_TEMPLATE_NAME = "Sunbeam Cluster Workload Balancing Template"


def get_watcher_client(jhelper: JujuHelper) -> watcher_client.Client:
conn = get_admin_connection(jhelper=jhelper)
def get_watcher_client(deployment: Deployment) -> watcher_client.Client:
region = read_config(deployment.get_client(), REGION_CONFIG_KEY)["region"]
conn = get_admin_connection(
jhelper=JujuHelper(deployment.get_connected_controller())
)

watcher_endpoint = conn.session.get_endpoint(
service_type="infra-optim",
# TODO: get region
region_name="RegionOne",
region_name=region,
)
return watcher_client.Client(session=conn.session, endpoint=watcher_endpoint)

Expand Down
27 changes: 21 additions & 6 deletions sunbeam-python/tests/unit/sunbeam/core/test_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,38 @@

import sunbeam.core.watcher as watcher_helper
from sunbeam.core.common import SunbeamException
from sunbeam.core.juju import JujuHelper
from sunbeam.core.deployment import Deployment


@patch("sunbeam.core.watcher.read_config")
@patch("sunbeam.core.watcher.JujuHelper")
@patch("sunbeam.core.watcher.get_admin_connection")
@patch("sunbeam.core.watcher.watcher_client.Client")
def test_get_watcher_client(mock_watcher_client, mock_get_admin_connection):
def test_get_watcher_client(
mock_watcher_client,
mock_get_admin_connection,
mock_jhelper,
mock_read_config,
):
mock_conn = Mock()
mock_conn.session.get_endpoint.return_value = "fake_endpoint"
mock_read_config.return_value = {"region": "fake_region"}
mock_get_admin_connection.return_value = mock_conn
mock_jhelper = Mock(spec=JujuHelper)
mock_deployment = Mock(spec=Deployment)

client = watcher_helper.get_watcher_client(mock_jhelper)
client = watcher_helper.get_watcher_client(mock_deployment)

mock_read_config.assert_called_once_with(
mock_deployment.get_client.return_value, "Region"
)
mock_jhelper.assert_called_once_with(
mock_deployment.get_connected_controller.return_value
)
mock_get_admin_connection.assert_called_once_with(jhelper=mock_jhelper.return_value)

mock_get_admin_connection.assert_called_once_with(jhelper=mock_jhelper)
mock_conn.session.get_endpoint.assert_called_once_with(
service_type="infra-optim",
region_name="RegionOne",
region_name="fake_region",
)
mock_watcher_client.assert_called_once_with(
session=mock_conn.session,
Expand Down

0 comments on commit aeb87ba

Please sign in to comment.