Manage grains using support metadata

Change-Id: I8120bb44a2af56a70d6aa6cf8abc8106bf36b0c8
diff --git a/collectd/client.sls b/collectd/client.sls
index d7f11b2..e0fbf5d 100644
--- a/collectd/client.sls
+++ b/collectd/client.sls
@@ -1,4 +1,4 @@
-{%- from "collectd/map.jinja" import client with context %}
+{%- from "collectd/map.jinja" import client, service_grains with context %}
 {%- if client.enabled %}
 
 include:
@@ -12,44 +12,6 @@
   - require:
     - pkg: collectd_client_packages
 
-{%- set service_grains = {'collectd': {'remote_plugin': {}, 'local_plugin': {}}} %}
-
-{%- for service_name in salt['pillar.ls']()|sort %}
-{%- set service = salt['pillar.items'](service_name)[service_name] %}
-{%- if service.get('_support', {}).get('collectd', {}).get('enabled', False) %}
-
-{%- set grains_fragment_file = service_name+'/meta/collectd.yml' %}
-{%- macro load_grains_file() %}{% include grains_fragment_file ignore missing %}{% endmacro %}
-{%- set grains_yaml = load_grains_file()|load_yaml %}
-
-{%- if grains_yaml is mapping %}
-{%- set service_grains = salt['grains.filter_by']({'default': service_grains}, merge={'collectd': grains_yaml}) %}
-{%- endif %}
-
-{%- endif %}
-{%- endfor %}
-
-collectd_client_grain:
-  file.managed:
-  - name: /etc/salt/grains.d/collectd
-  - source: salt://collectd/files/collectd.grain
-  - template: jinja
-  - user: root
-  - mode: 600
-  - defaults:
-    service_grains: {{ service_grains|yaml }}
-  - require:
-    - pkg: collectd_client_packages
-    - file: collectd_client_grains_dir
-
-collectd_client_grain_validity_check:
-  cmd.wait:
-  - name: python -c "import yaml; stream = file('/etc/salt/grains.d/collectd', 'r'); yaml.load(stream); stream.close()"
-  - require:
-    - pkg: collectd_client_packages
-  - watch:
-    - file: collectd_client_grain
-
 {%- set plugins = service_grains.collectd.local_plugin %}
 {%- include "collectd/_service.sls" %}
 
diff --git a/collectd/files/collectd.grain b/collectd/files/collectd.grain
deleted file mode 100644
index 3e3b373..0000000
--- a/collectd/files/collectd.grain
+++ /dev/null
@@ -1 +0,0 @@
-{{ service_grains|yaml(False) }}
diff --git a/collectd/map.jinja b/collectd/map.jinja
index 1077513..650c640 100644
--- a/collectd/map.jinja
+++ b/collectd/map.jinja
@@ -51,3 +51,15 @@
         'automatic_starting': True,
     }
 }, merge=salt['pillar.get']('collectd:remote_client')) %}
+
+{%- set service_grains = {'collectd': {'remote_plugin': {}, 'local_plugin': {}}} %}
+{%- for service_name, service in pillar.items() %}
+  {%- if service.get('_support', {}).get('collectd', {}).get('enabled', False) %}
+    {%- set grains_fragment_file = service_name+'/meta/collectd.yml' %}
+    {%- macro load_grains_file() %}{% include grains_fragment_file ignore missing %}{% endmacro %}
+    {%- set grains_yaml = load_grains_file()|load_yaml %}
+    {%- if grains_yaml is mapping %}
+      {%- set service_grains = salt['grains.filter_by']({'default': service_grains}, merge={'collectd': grains_yaml}) %}
+    {%- endif %}
+  {%- endif %}
+{%- endfor %}
diff --git a/collectd/meta/salt.yml b/collectd/meta/salt.yml
new file mode 100644
index 0000000..30f0f1f
--- /dev/null
+++ b/collectd/meta/salt.yml
@@ -0,0 +1,6 @@
+grain:
+  {%- if pillar.collectd.client is defined %}
+  {%- from "collectd/map.jinja" import service_grains with context -%}
+  collectd:
+    {{ service_grains|yaml(False)|indent(4) }}
+  {%- endif %}