Ubuntu: Get Remote unit name in subordinate charm


In my juju model I have a few applications running and I have a subordanite charm attached to one of those:

Unit                Workload  Agent  Machine  Public address  Ports            Message  mycharm/0*           active    idle   4   80/tcp           ready  wordpress/0*         unknown   idle   2   80/tcp    trustysub/0*       active    idle                      ready  

Now I want my trustysub subordinate charm to know the unit_name of the unit he is attached to (wordpress/0). So when I add the relation juju add-relation wordpress trustysub, I can write the unit name of Wordpress to a file or something.

@when('myrelation.available')  def write_unit_name():      # code to get unitname      f = open('myfile', 'w')      f.write('unitname')      f.close()      status_set('active', 'ready')  

my metadata.yml looks like this:

name: trustysub  summary: test charm  maintainer: Sebastien Pattyn <sebastien.pattyn@gmail.com>  description: |    This subordinate charm writes the unitname from the charm it has a relationship with, to a file  tags: ['misc']  subordinate: true  requires:    subrelation:      interface: juju-info      scope: container  series: ['trusty']  

Any idea on what I should add in my code to get the unitname of Wordpress in my subordinate charm


The Charmhelpers.core.hookenv package has some functions that can be used to get information about a a relation. Importing the package will give you the possibility to return the Remote unit by using the remote_unit() method. This will return the specific remote Unit for the current relation Hook.

You can use it like this.

from charmhelpers.core.hookenv import remote_unit    @when('myrelation.available')  def write_unit_name():      f = open('myfile', 'w')      f.write(remote_unit())      f.close()      status_set('active', 'ready')  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »