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

Fix frontend not connecting to api #51

Merged
merged 2 commits into from
Oct 23, 2023
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
37 changes: 18 additions & 19 deletions frontend/charm/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"""Test Observer frontend charm."""

import logging
import sys
from typing import Tuple

import ops
Expand All @@ -31,7 +30,7 @@ def __init__(self, *args):
self.pebble_service_name = "test-observer-frontend"
self.container = self.unit.get_container("frontend")

self.framework.observe(self.on.frontend_pebble_ready, self._on_frontend_pebble_ready)
self.framework.observe(self.on.frontend_pebble_ready, self._update_layer_and_restart)
self.framework.observe(self.on.config_changed, self._on_config_changed)
self.framework.observe(
self.on.test_observer_rest_api_relation_joined,
Expand All @@ -56,12 +55,6 @@ def _setup_ingress(self):
service_port=int(self.config["port"]),
)

def _on_frontend_pebble_ready(self, event: ops.PebbleReadyEvent):
container = event.workload
container.add_layer("frontend", self._pebble_layer, combine=True)
container.replan()
self.unit.status = ops.ActiveStatus()

def _on_config_changed(self, event):
is_valid, reason = self._config_is_valid(self.config)

Expand Down Expand Up @@ -162,29 +155,35 @@ def _update_layer_and_restart(self, event):
self.unit.status = MaintenanceStatus(f"Updating {self.pebble_service_name} layer")

if self.container.can_connect():
self.container.push(
"/etc/nginx/sites-available/test-observer-frontend",
self.nginx_config(base_uri=self._api_url),
make_dirs=True,
)
self.container.add_layer(self.pebble_service_name, self._pebble_layer, combine=True)
self.container.restart(self.pebble_service_name)
self.unit.status = ActiveStatus()
api_url = self._api_url
if api_url:
self.container.push(
"/etc/nginx/sites-available/test-observer-frontend",
self.nginx_config(base_uri=api_url),
make_dirs=True,
)
self.container.add_layer(
self.pebble_service_name, self._pebble_layer, combine=True
)
self.container.replan()
self.unit.status = ActiveStatus()
else:
self._handle_no_api_relation()
else:
self.unit.status = WaitingStatus("Waiting for Pebble for API to set available state")

@property
def _api_url(self):
def _api_url(self) -> str | None:
api_relation = self.model.get_relation("test-observer-rest-api")

if api_relation is None:
self._handle_no_api_relation()
sys.exit()
return

relation_data = api_relation.data[api_relation.app]
if not relation_data:
self.unit.status = WaitingStatus("Waiting for test observer api relation data")
sys.exit()
return

hostname = relation_data["hostname"]
port = relation_data["port"]
Expand Down
24 changes: 4 additions & 20 deletions frontend/charm/tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,12 @@ def setUp(self):
self.addCleanup(self.harness.cleanup)
self.harness.begin()

def test_pebble_ready(self):
expected_plan = {
"services": {
"test-observer-frontend": {
"override": "replace",
"summary": "nginx",
"command": "nginx -g 'daemon off;'",
"startup": "enabled",
}
},
}

def test_pebble_ready_no_relation(self):
self.harness.container_pebble_ready("frontend")
updated_plan = self.harness.get_container_pebble_plan("frontend").to_dict()

self.assertEqual(expected_plan, updated_plan)

service = self.harness.model.unit.get_container("frontend").get_service(
"test-observer-frontend"
self.assertEqual(
self.harness.model.unit.status,
ops.MaintenanceStatus("test-observer-rest-api relation not connected."),
)
self.assertTrue(service.is_running())
self.assertEqual(self.harness.model.unit.status, ops.ActiveStatus())

def test_relating(self):
harness = ops.testing.Harness(TestObserverFrontendCharm)
Expand Down