Skip to content

Commit

Permalink
Added website relation
Browse files Browse the repository at this point in the history
The website relation will make it possible to relate the
controller charm to haproxy.
  • Loading branch information
alesstimec committed Apr 25, 2022
1 parent ef98966 commit 5747cb6
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
2 changes: 2 additions & 0 deletions metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ summary: |
provides:
dashboard:
interface: juju-dashboard
website:
interface: http
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ops
charmhelpers
36 changes: 35 additions & 1 deletion src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
# Licensed under the GPLv3, see LICENSE file for details.

import logging
import os
import yaml

from charmhelpers.core import hookenv
from ops.charm import CharmBase
from ops.main import main
from ops.model import ActiveStatus
from ops.model import ActiveStatus, BlockedStatus
from ops.framework import StoredState

logger = logging.getLogger(__name__)
Expand All @@ -21,6 +24,8 @@ def __init__(self, *args):
self.framework.observe(self.on.start, self._on_start)
self.framework.observe(
self.on.dashboard_relation_joined, self._on_dashboard_relation_joined)
self.framework.observe(
self.on.website_relation_joined, self._on_website_relation_joined)

def _on_start(self, _):
self.unit.status = ActiveStatus()
Expand All @@ -38,6 +43,35 @@ def _on_dashboard_relation_joined(self, event):
relation.data[self.app]['identity-provider-url'] = self.config['identity-provider-url']
relation.data[self.app]['is-juju'] = str(self.config['is-juju'])

def _on_website_relation_joined(self, event):
"""Connect a website relation."""
logger.info("got a new website relation: %r", event)
port = api_port()
if port is None:
logger.error("machine does not appear to be a controller")
self.unit.status = BlockedStatus('machine does not appear to be a controller')
return

for relation in self.model.relations['website']:
ingress_address = hookenv.ingress_address(relation.id, hookenv.local_unit())
relation.data[self.unit]['hostname'] = ingress_address
relation.data[self.unit]['private-address'] = ingress_address
relation.data[self.unit]['port'] = str(port)


def api_port():
''' api_port determines the port that the controller's API server is
listening on. If the machine does not appear to be a juju
controller then None is returned.
'''
machine = os.getenv('JUJU_MACHINE_ID')
if machine is None:
return None
path = '/var/lib/juju/agents/machine-{}/agent.conf'.format(machine)
with open(path) as f:
params = yaml.safe_load(f)
return params.get('apiport')


if __name__ == "__main__":
main(JujuControllerCharm)
3 changes: 3 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import ops.testing

ops.testing.SIMULATE_CAN_CONNECT = True
26 changes: 26 additions & 0 deletions tests/test_charm.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
# Copyright 2021 Canonical Ltd.
# Licensed under the GPLv3, see LICENSE file for details.

import os
import unittest
from unittest.mock import patch, mock_open

from ops.testing import Harness
from charm import JujuControllerCharm


agent_conf = '''
apiport: 17070
'''


class TestCharm(unittest.TestCase):
def test_relation_joined(self):
harness = Harness(JujuControllerCharm)
Expand All @@ -23,3 +30,22 @@ def test_relation_joined(self):
self.assertEqual(data["controller-url"], "wss://controller/api")
self.assertEqual(data["is-juju"], "true")
self.assertEqual(data.get("identity-provider-url"), None)

@patch.dict(os.environ, {
"JUJU_MACHINE_ID": "machine-0",
"JUJU_UNIT_NAME": "controller/0"
})
@patch("charmhelpers.core.hookenv.ingress_address")
@patch("builtins.open", new_callable=mock_open, read_data=agent_conf)
def test_website_relation_joined(self, open, ingress_address):
ingress_address.return_value = "192.168.1.17"
harness = Harness(JujuControllerCharm)
self.addCleanup(harness.cleanup)
harness.begin()
relation_id = harness.add_relation('website', 'haproxy')
harness.add_relation_unit(relation_id, 'haproxy/0')

data = harness.get_relation_data(relation_id, 'juju-controller/0')
self.assertEqual(data["hostname"], "192.168.1.17")
self.assertEqual(data["private-address"], "192.168.1.17")
self.assertEqual(data["port"], '17070')

0 comments on commit 5747cb6

Please sign in to comment.