Add Gnocchi datasource for Grafana

Change-Id: I7a48be1b447b84f672e4462fde29cbe044613acd
Closes-bug: PROD-21747
diff --git a/_states/grafana3_datasource.py b/_states/grafana3_datasource.py
index c5fb7ff..5efa8ba 100644
--- a/_states/grafana3_datasource.py
+++ b/_states/grafana3_datasource.py
@@ -61,6 +61,9 @@
             basic_auth=False,
             basic_auth_user='',
             basic_auth_password='',
+            mode=None,
+            domain='default',
+            project=None,
             is_default=False,
             profile='grafana'):
     '''
@@ -97,6 +100,15 @@
     basic_auth_password
         Optional - HTTP basic auth password.
 
+    mode
+        Optional - Gnocchi authentication mode.
+
+    domain
+        Optional - Gnocchi domain, defaults to "default".
+
+    project
+        Optional - Keystone user for Gnocchi.
+
     is_default
         Optional - Set data source as default. Default: False
     '''
@@ -113,6 +125,9 @@
                           basic_auth=basic_auth,
                           basic_auth_user=basic_auth_user,
                           basic_auth_password=basic_auth_password,
+                          mode=mode,
+                          domain=domain,
+                          project=project,
                           is_default=is_default)
 
     if datasource:
@@ -194,21 +209,20 @@
     for datasource in data:
         if datasource['name'] == name:
             return datasource
-    return None
 
 
 def _get_headers(profile):
 
     headers = {'Content-type': 'application/json'}
 
-    if profile.get('grafana_token', False):
+    if profile.get('grafana_token'):
         headers['Authorization'] = 'Bearer {0}'.format(profile['grafana_token'])
 
     return headers
 
 
 def _get_auth(profile):
-    if profile.get('grafana_token', False):
+    if profile.get('grafana_token'):
         return None
 
     return requests.auth.HTTPBasicAuth(
@@ -227,10 +241,13 @@
                    basic_auth=False,
                    basic_auth_user='',
                    basic_auth_password='',
+                   mode=None,
+                   domain=None,
+                   project=None,
                    is_default=False,
                    type_logo_url='public/app/plugins/datasource/influxdb/img/influxdb_logo.svg',
                    with_credentials=False):
-    return {
+    data = {
         'name': name,
         'type': type,
         'url': url,
@@ -245,6 +262,18 @@
         'typeLogoUrl': type_logo_url,
         'withCredentials': with_credentials,
     }
+    if data['type'] == 'gnocchixyz-gnocchi-datasource':
+        json_data = {}
+        for special in ['mode', 'domain', 'project', 'user', 'password']:
+            value = locals().get(special)
+            if value is not None:
+                if special == 'user':
+                    json_data['username'] = value
+                else:
+                    json_data[special] = value
+        if json_data:
+            data['jsonData'] = json_data
+    return data
 
 
 def _diff(old, new):
@@ -252,7 +281,7 @@
     old = old.copy()
     new = new.copy()
     for key in old_keys:
-        if key == 'id' or key == 'orgId':
+        if key in ['id', 'orgId']:
             del old[key]
         # New versions of Grafana can introduce new keys that are not present
         # in _get_json_data.
diff --git a/grafana/client/init.sls b/grafana/client/init.sls
index c9101dc..129d52a 100644
--- a/grafana/client/init.sls
+++ b/grafana/client/init.sls
@@ -9,7 +9,11 @@
   grafana3_datasource.present:
   - name: {{ datasource.name|default(datasource_name) }}
   - type: {{ datasource.type }}
-  - url: http://{{ datasource.host }}:{{ datasource.get('port', 80) }}
+  {%- if datasource.port is defined %}
+  - url: {{ datasource.get('protocol', 'http') }}://{{ datasource.host }}:{{ datasource.port }}{{ datasource.get('url_path', '') }}
+  {%- else %}
+  - url: {{ datasource.get('protocol', 'http') }}://{{ datasource.host }}{{ datasource.get('url_path', '') }}
+  {%- endif %}
   {%- if datasource.access is defined %}
   - access: proxy
   {%- endif %}
@@ -23,6 +27,13 @@
   {%- if datasource.database is defined %}
   - database: {{ datasource.database }}
   {%- endif %}
+  {%- if datasource.mode is defined %}
+  - mode: {{ datasource.mode }}
+    {%- if datasource.mode == 'keystone' %}
+  - domain: {{ datasource.get('domain', 'default') }}
+  - project: {{ datasource.get('project', 'service') }}
+    {%- endif %}
+  {%- endif %}
 
 {%- endfor %}