from charms.reactive import when, when_not, set_state, when_any, hook
from charmhelpers.core.hookenv import (
    related_units,
    relation_ids,
    relation_get,
    log,
    status_set,
    config,
    service_name
)
from charmhelpers.core import hookenv
from charms.reactive.helpers import data_changed
from charmhelpers.core.templating import render
import json
import os


@when_not('logstash-input-elasticsearch.installed')
def install_logstash_input_elasticsearch():
    # Nothing required
    set_state('logstash-input-elasticsearch.installed')
    status_set('waiting', 'waiting for elasticsearch')


@when_any('host-system.available', 'host-system.connected')
@when('elasticsearch.available')
def connect_to_elasticsearch(elasticsearch):
    write_config()
    status_set('active', 'Ready')
    set_state('logstash-input-elasticsearch.started')


def elasticsearch_servers():
    hosts = []
    config = hookenv.config()
    for rel_id in relation_ids('elasticsearch'):
        for unit in related_units(rel_id):
            port = relation_get('port', unit, rel_id)
            host = relation_get('private-address', unit, rel_id)
            if port:
                hosts.append("{}:{}".format(host, port))
    return hosts


@hook('config-changed')
def write_config():
    config = hookenv.config()
    hosts = elasticsearch_servers()
    if data_changed('elasticsearch_servers', hosts) or data_changed('template', config['template']):
        log("Writing config")
        if(hosts):
            from jinja2 import Template
            app_name = hookenv.service_name()
            template = Template(config['template'])
            hosts_str = ', '.join(map(lambda x: "'{}'".format(x), hosts))
            with open('/etc/logstash/conf.d/{}.conf'.format(app_name), 'w') as conf_file:
                conf_file.write(str(
                    template.render({
                        'hosts': hosts_str
                        }
                    )
                ))
        else:
            log("No elasticsearch servers connected. Removing config.")
            try:
                app_name = hookenv.service_name()
                os.remove('/etc/logstash/conf.d/{}.conf'.format(app_name))
            except FileNotFoundError:
                pass

@@ -686,25 +686,25 @@
 csearch.
-available
+connected
 ')%0Adef c
@@ -852,16 +852,121 @@
 ted')%0A%0A%0A
+@when('elasticsearchself.broken')%0Adef disconnect_from_elasticsearch(elasticsearch):%0A    write_config()%0A%0A%0A
 def elas
@@ -1761,16 +1761,30 @@
 s_str = 
+%22%5B%7B%7D%5D%22.format(
 ', '.joi
@@ -1824,16 +1824,17 @@
  hosts))
+)
 %0A       

Fix host string. (Was not array)