Skip to content

Commit

Permalink
Supporting application level relation data (#566)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Cory Johns <[email protected]>
  • Loading branch information
3 people authored Apr 9, 2021
1 parent ab2a506 commit 40cf3b9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
15 changes: 11 additions & 4 deletions charmhelpers/core/hookenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
Expand Down
11 changes: 11 additions & 0 deletions tests/core/test_hookenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 40cf3b9

Please sign in to comment.