~openstack-charmers-archive/charms/trusty/glance/trunk

« back to all changes in this revision

Viewing changes to charmhelpers/core/hookenv.py

  • Committer: Liam Young
  • Date: 2016-01-28 09:03:36 UTC
  • Revision ID: liam.young@canonical.com-20160128090336-ty1b224pda7g4nu1
16.01 Charm release

Show diffs side-by-side

added added

removed removed

Lines of Context:
491
491
 
492
492
 
493
493
@cached
 
494
def peer_relation_id():
 
495
    '''Get the peers relation id if a peers relation has been joined, else None.'''
 
496
    md = metadata()
 
497
    section = md.get('peers')
 
498
    if section:
 
499
        for key in section:
 
500
            relids = relation_ids(key)
 
501
            if relids:
 
502
                return relids[0]
 
503
    return None
 
504
 
 
505
 
 
506
@cached
494
507
def relation_to_interface(relation_name):
495
508
    """
496
509
    Given the name of a relation, return the interface that relation uses.
504
517
def relation_to_role_and_interface(relation_name):
505
518
    """
506
519
    Given the name of a relation, return the role and the name of the interface
507
 
    that relation uses (where role is one of ``provides``, ``requires``, or ``peer``).
 
520
    that relation uses (where role is one of ``provides``, ``requires``, or ``peers``).
508
521
 
509
522
    :returns: A tuple containing ``(role, interface)``, or ``(None, None)``.
510
523
    """
511
524
    _metadata = metadata()
512
 
    for role in ('provides', 'requires', 'peer'):
 
525
    for role in ('provides', 'requires', 'peers'):
513
526
        interface = _metadata.get(role, {}).get(relation_name, {}).get('interface')
514
527
        if interface:
515
528
            return role, interface
521
534
    """
522
535
    Given a role and interface name, return a list of relation names for the
523
536
    current charm that use that interface under that role (where role is one
524
 
    of ``provides``, ``requires``, or ``peer``).
 
537
    of ``provides``, ``requires``, or ``peers``).
525
538
 
526
539
    :returns: A list of relation names.
527
540
    """
542
555
    :returns: A list of relation names.
543
556
    """
544
557
    results = []
545
 
    for role in ('provides', 'requires', 'peer'):
 
558
    for role in ('provides', 'requires', 'peers'):
546
559
        results.extend(role_and_interface_to_relations(role, interface_name))
547
560
    return results
548
561
 
624
637
 
625
638
 
626
639
@cached
627
 
def storage_get(attribute="", storage_id=""):
 
640
def storage_get(attribute=None, storage_id=None):
628
641
    """Get storage attributes"""
629
642
    _args = ['storage-get', '--format=json']
630
643
    if storage_id:
638
651
 
639
652
 
640
653
@cached
641
 
def storage_list(storage_name=""):
 
654
def storage_list(storage_name=None):
642
655
    """List the storage IDs for the unit"""
643
656
    _args = ['storage-list', '--format=json']
644
657
    if storage_name:
820
833
 
821
834
def translate_exc(from_exc, to_exc):
822
835
    def inner_translate_exc1(f):
 
836
        @wraps(f)
823
837
        def inner_translate_exc2(*args, **kwargs):
824
838
            try:
825
839
                return f(*args, **kwargs)
864
878
    subprocess.check_call(cmd)
865
879
 
866
880
 
 
881
@translate_exc(from_exc=OSError, to_exc=NotImplementedError)
 
882
def payload_register(ptype, klass, pid):
 
883
    """ is used while a hook is running to let Juju know that a
 
884
        payload has been started."""
 
885
    cmd = ['payload-register']
 
886
    for x in [ptype, klass, pid]:
 
887
        cmd.append(x)
 
888
    subprocess.check_call(cmd)
 
889
 
 
890
 
 
891
@translate_exc(from_exc=OSError, to_exc=NotImplementedError)
 
892
def payload_unregister(klass, pid):
 
893
    """ is used while a hook is running to let Juju know
 
894
    that a payload has been manually stopped. The <class> and <id> provided
 
895
    must match a payload that has been previously registered with juju using
 
896
    payload-register."""
 
897
    cmd = ['payload-unregister']
 
898
    for x in [klass, pid]:
 
899
        cmd.append(x)
 
900
    subprocess.check_call(cmd)
 
901
 
 
902
 
 
903
@translate_exc(from_exc=OSError, to_exc=NotImplementedError)
 
904
def payload_status_set(klass, pid, status):
 
905
    """is used to update the current status of a registered payload.
 
906
    The <class> and <id> provided must match a payload that has been previously
 
907
    registered with juju using payload-register. The <status> must be one of the
 
908
    follow: starting, started, stopping, stopped"""
 
909
    cmd = ['payload-status-set']
 
910
    for x in [klass, pid, status]:
 
911
        cmd.append(x)
 
912
    subprocess.check_call(cmd)
 
913
 
 
914
 
867
915
@cached
868
916
def juju_version():
869
917
    """Full version string (eg. '1.23.3.1-trusty-amd64')"""