~niedbalski/charm-helpers/fix-1442443-complement

« back to all changes in this revision

Viewing changes to charmhelpers/core/hookenv.py

  • Committer: Tim Van Steenburgh
  • Date: 2015-09-24 13:46:04 UTC
  • mfrom: (454.1.2 charm-helpers)
  • Revision ID: tim.van.steenburgh@canonical.com-20150924134604-7rijn08kkj1qvvyx
[axwalk] Add storage_list and storage_get

Show diffs side-by-side

added added

removed removed

Lines of Context:
623
623
    return unit_get('private-address')
624
624
 
625
625
 
 
626
@cached
 
627
def storage_get(attribute="", storage_id=""):
 
628
    """Get storage attributes"""
 
629
    _args = ['storage-get', '--format=json']
 
630
    if storage_id:
 
631
        _args.extend(('-s', storage_id))
 
632
    if attribute:
 
633
        _args.append(attribute)
 
634
    try:
 
635
        return json.loads(subprocess.check_output(_args).decode('UTF-8'))
 
636
    except ValueError:
 
637
        return None
 
638
 
 
639
 
 
640
@cached
 
641
def storage_list(storage_name=""):
 
642
    """List the storage IDs for the unit"""
 
643
    _args = ['storage-list', '--format=json']
 
644
    if storage_name:
 
645
        _args.append(storage_name)
 
646
    try:
 
647
        return json.loads(subprocess.check_output(_args).decode('UTF-8'))
 
648
    except ValueError:
 
649
        return None
 
650
    except OSError as e:
 
651
        import errno
 
652
        if e.errno == errno.ENOENT:
 
653
            # storage-list does not exist
 
654
            return []
 
655
        raise
 
656
 
 
657
 
626
658
class UnregisteredHookError(Exception):
627
659
    """Raised when an undefined hook is called"""
628
660
    pass