Add support for influxdb-relay

Change-Id: I06b691b599660c1663306c6316a00de57885c848
diff --git a/README.rst b/README.rst
index c79788f..ab729ac 100644
--- a/README.rst
+++ b/README.rst
@@ -264,6 +264,29 @@
             database: mydb1
             privilege: all
 
+InfluxDB relay with HTTP outputs:
+
+.. code-block:: yaml
+
+    influxdb:
+      relay:
+        enabled: true
+        listen:
+          http_backend:
+            type: http
+            bind:
+              address: 127.0.0.1
+              port: 9096
+            output:
+              server1:
+                location: http://server1:8086/write
+                timeout: 20s
+                buffer_size_mb: 512
+                max_batch_kb: 1024
+                max_delay_interval: 30s
+              server2:
+                location: http://server2:8086/write
+  
 Read more
 =========
 
diff --git a/influxdb/files/influxdb-relay.conf b/influxdb/files/influxdb-relay.conf
new file mode 100644
index 0000000..6d173a8
--- /dev/null
+++ b/influxdb/files/influxdb-relay.conf
@@ -0,0 +1,54 @@
+{%- from "influxdb/map.jinja" import relay with context -%}
+
+{%- if relay.get('enabled') %}
+
+{%- for name, listen in relay.listen.iteritems()|sort %}
+
+{%- if listen.get('enabled', True) and listen.get('type', 'http') in ('http', 'udp') %}
+
+{%- set listen_type = listen.get('type', 'http') %}
+[[{{ listen_type }}]]
+name = "{{ name }}"
+bind-addr = "{{ listen.bind.get('address', '0.0.0.0') }}:{{ listen.bind.port }}"
+{%- if listen_type == 'http' and listen.default_retention_policy is defined %}
+default-retention-policy = "{{ listen.default_retention_policy }}"
+{%- endif %}
+{%- if listen_type == 'udp' and listen.precision is defined %}
+precision = "{{ listen.precision }}"
+{%- endif %}
+{%- if listen_type == 'udp' and listen.read_buffer is defined %}
+read-buffer = {{ listen.read_buffer|int }}
+{%- endif %}
+
+{%- set outputs = [] %}
+{%- for output_name, output in listen.get('output', {}).iteritems()|sort %}
+  {%- set tmp = ['name = "{}"'.format(output_name), 'location = "{}"'.format( output.location)] %}
+  {%- if listen_type == 'http' and output.timeout is defined %}
+    {%- do tmp.append('timeout = "{}"'.format(output.timeout)) %}
+  {%- endif %}
+  {%- if listen_type == 'http' and output.buffer_size_mb is defined %}
+    {%- do tmp.append('buffer-size-mb = {}'.format(output.buffer_size_mb)) %}
+  {%- endif %}
+  {%- if listen_type == 'http' and output.max_batch_kb is defined %}
+    {%- do tmp.append('max-batch-kb = {}'.format(output.max_batch_kb)) %}
+  {%- endif %}
+  {%- if listen_type == 'http' and output.max_delay_interval is defined %}
+    {%- do tmp.append('max-delay-interval = "{}"'.format(output.max_delay_interval)) %}
+  {%- endif %}
+  {%- if listen_type == 'udp' and output.mtu is defined %}
+    {%- do tmp.append('mtu = {}'.format(output.mtu)) %}
+  {%- endif %}
+  {%- do outputs.append(tmp) %}
+{%- endfor %}
+
+output = [
+{%- for output in outputs %}
+  { {{ output|join(', ') }} },
+{%- endfor %}
+]
+
+{%- endif %}
+
+{%- endfor %}
+
+{%- endif %}
diff --git a/influxdb/init.sls b/influxdb/init.sls
index a878715..95c7731 100644
--- a/influxdb/init.sls
+++ b/influxdb/init.sls
@@ -6,4 +6,7 @@
 {%- if pillar.influxdb.client is defined %}
 - influxdb.client
 {%- endif %}
+{%- if pillar.influxdb.relay is defined %}
+- influxdb.relay
+{%- endif %}
 {%- endif %}
diff --git a/influxdb/map.jinja b/influxdb/map.jinja
index d2d4a89..88ba241 100644
--- a/influxdb/map.jinja
+++ b/influxdb/map.jinja
@@ -48,3 +48,11 @@
     'default': {
     },
 }, merge=salt['pillar.get']('influxdb:client')) %}
+
+{%- set relay = salt['grains.filter_by']({
+    'default': {
+        'pkgs': ['influxdb-relay'],
+        'service': 'influxdb-relay',
+        'listen': {},
+    },
+}, merge=salt['pillar.get']('influxdb:relay')) %}
diff --git a/influxdb/relay.sls b/influxdb/relay.sls
new file mode 100644
index 0000000..58f0413
--- /dev/null
+++ b/influxdb/relay.sls
@@ -0,0 +1,26 @@
+{%- from "influxdb/map.jinja" import relay with context %}
+{%- if relay.get('enabled') %}
+
+influxdb_relay_packages:
+  pkg.installed:
+  - names: {{ relay.pkgs }}
+
+influxdb_relay_config:
+  file.managed:
+  - name: //etc/influxdb-relay/influxdb-relay.conf
+  - source: salt://influxdb/files/influxdb-relay.conf
+  - template: jinja
+  - require:
+    - pkg: influxdb_relay_packages
+
+influxdb_relay_service:
+  service.running:
+  - enable: true
+  - name: {{ relay.service }}
+{%- if grains.get('noservices') %}
+  - onlyif: /bin/false
+{%- endif %}
+  - watch:
+    - file: influxdb_relay_config
+
+{%- endif %}
diff --git a/metadata/service/relay/cluster.yml b/metadata/service/relay/cluster.yml
new file mode 100644
index 0000000..4708ea3
--- /dev/null
+++ b/metadata/service/relay/cluster.yml
@@ -0,0 +1,38 @@
+applications:
+- influxdb
+classes:
+- service.influxdb.support
+parameters:
+  _param:
+    influxdb_relay_timeout: 10s
+    influxdb_relay_buffer_size_mb: 512
+    influxdb_relay_max_batch_kb: 512
+    influxdb_relay_max_delay_inteval: 10s
+  influxdb:
+    relay:
+      enabled: true
+      listen:
+        http:
+          type: http
+          bind:
+            address: ${_param:cluster_local_address}
+            port: 9096
+          output:
+            influxdb01:
+              location: http://${_param:cluster_node01_address}:8086/write
+              timeout: ${_param:influxdb_relay_timeout}
+              buffer_size_mb: ${_param:influxdb_relay_buffer_size_mb}
+              max_batch_kb: ${_param:influxdb_relay_max_batch_kb}
+              max_delay_interval: ${_param:influxdb_relay_max_delay_inteval}
+            influxdb02:
+              location: http://${_param:cluster_node02_address}:8086/write
+              timeout: ${_param:influxdb_relay_timeout}
+              buffer_size_mb: ${_param:influxdb_relay_buffer_size_mb}
+              max_batch_kb: ${_param:influxdb_relay_max_batch_kb}
+              max_delay_interval: ${_param:influxdb_relay_max_delay_inteval}
+            influxdb03:
+              location: http://${_param:cluster_node03_address}:8086/write
+              timeout: ${_param:influxdb_relay_timeout}
+              buffer_size_mb: ${_param:influxdb_relay_buffer_size_mb}
+              max_batch_kb: ${_param:influxdb_relay_max_batch_kb}
+              max_delay_interval: ${_param:influxdb_relay_max_delay_inteval}
diff --git a/tests/pillar/relay.sls b/tests/pillar/relay.sls
new file mode 100644
index 0000000..7da8b73
--- /dev/null
+++ b/tests/pillar/relay.sls
@@ -0,0 +1,29 @@
+influxdb:
+  relay:
+    enabled: true
+    listen:
+      http_backend:
+        type: http
+        bind:
+          address: 127.0.0.1
+          port: 9096
+        output:
+          server1:
+            location: http://server1:8086/write
+            timeout: 20s
+            buffer_size_mb: 512
+            max_batch_kb: 1024
+            max_delay_interval: 30s
+          server2:
+            location: http://server2:8086/write
+      udp_backend:
+        type: udp
+        bind:
+          address: 127.0.0.1
+          port: 9196
+        output:
+          server1:
+            location: http://server1:8086/write
+            mtu: 1500
+          server2:
+            location: http://server2:8086/write