From 40cf3b995e22bf4aa55c61ed9a00e92aa349f0ba Mon Sep 17 00:00:00 2001 From: Seyeong Kim Date: Sat, 10 Apr 2021 00:47:56 +0900 Subject: [PATCH] Supporting application level relation data (#566) * Supporting application level relation data Adding app parameter to relation_get and relation_set to support application level relation data in the future * fix e712 * changes relation_get as recommended * Fix error unit or app need to be saperated * I mistook to take recommended code * Added test coverage Co-authored-by: Seyeong Kim Co-authored-by: Cory Johns --- charmhelpers/core/hookenv.py | 15 +++++++++++---- tests/core/test_hookenv.py | 11 +++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/charmhelpers/core/hookenv.py b/charmhelpers/core/hookenv.py index 778aa4b6e..47eebb514 100644 --- a/charmhelpers/core/hookenv.py +++ b/charmhelpers/core/hookenv.py @@ -468,15 +468,20 @@ def config(scope=None): @cached -def relation_get(attribute=None, unit=None, rid=None): +def relation_get(attribute=None, unit=None, rid=None, app=None): """Get relation information""" _args = ['relation-get', '--format=json'] + if app is not None: + if unit is not None: + raise ValueError("Cannot use both 'unit' and 'app'") + _args.append('--app') if rid: _args.append('-r') _args.append(rid) _args.append(attribute or '-') - if unit: - _args.append(unit) + # unit or application name + if unit or app: + _args.append(unit or app) try: return json.loads(subprocess.check_output(_args).decode('UTF-8')) except ValueError: @@ -487,12 +492,14 @@ def relation_get(attribute=None, unit=None, rid=None): raise -def relation_set(relation_id=None, relation_settings=None, **kwargs): +def relation_set(relation_id=None, relation_settings=None, app=False, **kwargs): """Set relation information for the current unit""" relation_settings = relation_settings if relation_settings else {} relation_cmd_line = ['relation-set'] accepts_file = "--file" in subprocess.check_output( relation_cmd_line + ["--help"], universal_newlines=True) + if app: + relation_cmd_line.append('--app') if relation_id is not None: relation_cmd_line.extend(('-r', relation_id)) settings = relation_settings.copy() diff --git a/tests/core/test_hookenv.py b/tests/core/test_hookenv.py index 5e0476366..301f028a2 100644 --- a/tests/core/test_hookenv.py +++ b/tests/core/test_hookenv.py @@ -1164,6 +1164,14 @@ def test_gets_relation(self, check_output): self.assertEqual(result['foo'], 'BAR') check_output.assert_called_with(['relation-get', '--format=json', '-']) + hookenv.relation_get(unit='foo/0') + check_output.assert_called_with(['relation-get', '--format=json', '-', 'foo/0']) + + hookenv.relation_get(app='foo') + check_output.assert_called_with(['relation-get', '--format=json', '--app', '-', 'foo']) + + self.assertRaises(ValueError, hookenv.relation_get, app='foo', unit='foo/0') + @patch('charmhelpers.core.hookenv.subprocess') def test_relation_get_none(self, mock_subprocess): mock_subprocess.check_output.return_value = b'null' @@ -1256,6 +1264,9 @@ def test_sets_relation_with_kwargs(self, check_call_, check_output, hookenv.relation_set(foo="bar") check_call_.assert_called_with(['relation-set', 'foo=bar']) + hookenv.relation_set(foo="bar", app=True) + check_call_.assert_called_with(['relation-set', '--app', 'foo=bar']) + @patch('charmhelpers.core.hookenv.local_unit') @patch('subprocess.check_output') @patch('subprocess.check_call')