def _get_event_args(charm, bound_event): event_type = bound_event.event_type model = charm.framework.model if issubclass(event_type, ops.charm.RelationEvent): relation_name = os.environ['JUJU_RELATION'] relation_id = int(os.environ['JUJU_RELATION_ID'].split(':')[-1]) relation = model.get_relation(relation_name, relation_id) else: relation = None remote_app_name = os.environ.get('JUJU_REMOTE_APP', '') remote_unit_name = os.environ.get('JUJU_REMOTE_UNIT', '') if remote_app_name or remote_unit_name: if not remote_app_name: if '/' not in remote_unit_name: raise RuntimeError( 'invalid remote unit name: {}'.format(remote_unit_name)) remote_app_name = remote_unit_name.split('/')[0] args = [relation, model.get_app(remote_app_name)] if remote_unit_name: args.append(model.get_unit(remote_unit_name)) return args, {} elif relation: return [relation], {} return [], {}
def _get_event_args(charm, bound_event): event_type = bound_event.event_type model = charm.framework.model relation = None if issubclass(event_type, ops.charm.WorkloadEvent): workload_name = os.environ['JUJU_WORKLOAD_NAME'] container = model.unit.get_container(workload_name) return [container], {} elif issubclass(event_type, ops.charm.StorageEvent): storage_id = os.environ.get("JUJU_STORAGE_ID", "") if storage_id: storage_name = storage_id.split("/")[0] else: # Before JUJU_STORAGE_ID exists, take the event name as # <storage_name>_storage_<attached|detached> and replace it with <storage_name> storage_name = "-".join(bound_event.event_kind.split("_")[:-2]) storages = model.storages[storage_name] id, storage_location = model._backend._storage_event_details() if len(storages) == 1: storage = storages[0] else: # If there's more than one value, pick the right one. We'll realize the key on lookup storage = next((s for s in storages if s.id == id), None) storage.location = storage_location return [storage], {} elif issubclass(event_type, ops.charm.RelationEvent): relation_name = os.environ['JUJU_RELATION'] relation_id = int(os.environ['JUJU_RELATION_ID'].split(':')[-1]) relation = model.get_relation(relation_name, relation_id) remote_app_name = os.environ.get('JUJU_REMOTE_APP', '') remote_unit_name = os.environ.get('JUJU_REMOTE_UNIT', '') departing_unit_name = os.environ.get('JUJU_DEPARTING_UNIT', '') if not remote_app_name and remote_unit_name: if '/' not in remote_unit_name: raise RuntimeError( 'invalid remote unit name: {}'.format(remote_unit_name)) remote_app_name = remote_unit_name.split('/')[0] kwargs = {} if remote_app_name: kwargs['app'] = model.get_app(remote_app_name) if remote_unit_name: kwargs['unit'] = model.get_unit(remote_unit_name) if departing_unit_name: kwargs['departing_unit_name'] = departing_unit_name if relation: return [relation], kwargs return [], {}