Browse Source

Merge pull request #9516 from caruccio/patch-3

Dont fail when datasource or dashboard already exists
OpenShift Merge Robot 6 years ago
parent
commit
119545fa2e
1 changed files with 44 additions and 4 deletions
  1. 44 4
      roles/openshift_grafana/tasks/install_grafana.yaml

+ 44 - 4
roles/openshift_grafana/tasks/install_grafana.yaml

@@ -175,11 +175,39 @@
     method: POST
     body: '{{ payload_data }}'
     body_format: json
+    status_code:
+    - 200
+    - 409
     headers:
       Content-Type: "Content-Type: application/json"
   register: add_ds
 
-- name: Regex set data soure name for openshift dashboard
+- block:
+  - name: Retrieve current grafana datasource
+    uri:
+      url: "{{ grafana_route }}/api/datasources/name/{{ grafana_datasource_name }}"
+      user: "{{ grafana_sa_token.stdout }}"
+      validate_certs: false
+      method: GET
+      status_code:
+      - 200
+    register: grafana_ds
+  - name: Update grafana datasource
+    uri:
+      url: "{{ grafana_route }}/api/datasources/{{ grafana_ds.json['id'] }}"
+      user: "{{ grafana_sa_token.stdout }}"
+      validate_certs: false
+      method: PUT
+      body: '{{ payload_data }}'
+      body_format: json
+      headers:
+        Content-Type: "Content-Type: application/json"
+      status_code:
+      - 200
+    register: update_ds
+  when: add_ds.status == 409
+
+- name: Regex set data source name for openshift dashboard
   replace:
     path: "{{ mktemp.stdout }}/openshift-cluster-monitoring.json"
     regexp: '{{ item.regexp }}'
@@ -191,7 +219,7 @@
   - regexp: 'Xs'
     replace: '{{ grafana_graph_granularity }}'
 
-- name: Regex set data soure name for node exporter
+- name: Regex set data source name for node exporter
   replace:
     path: "{{ mktemp.stdout }}/node-exporter-full-dashboard.json"
     regexp: '{{ item.regexp }}'
@@ -213,14 +241,20 @@
     src: "{{ cluster_monitoring_dashboard }}"
   register: slurpfile
 
+- set_fact:
+    dashboard_data: '{{ slurpfile["content"] | b64decode | from_json | combine({ "dashboard": { "overwrite": true } }, recursive=True) | to_json }}'
+
 - name: Add openshift dashboard
   uri:
     url: "{{ grafana_route }}/api/dashboards/db"
     user: "{{ grafana_sa_token.stdout }}"
     validate_certs: false
     method: POST
-    body: '{{ slurpfile["content"] | b64decode }}'
+    body: '{{ dashboard_data }}'
     body_format: json
+    status_code:
+    - 200
+    - 412
     headers:
       Content-Type: "Content-Type: application/json"
   register: add_ds
@@ -230,14 +264,20 @@
     src: "{{ node_exporter_dashboard }}"
   register: slurpfile
 
+- set_fact:
+    dashboard_data: '{{ slurpfile["content"] | b64decode | from_json | combine({ "dashboard": { "overwrite": true } }, recursive=True) | to_json }}'
+
 - name: Add node exporter dashboard
   uri:
     url: "{{ grafana_route }}/api/dashboards/db"
     user: "{{ grafana_sa_token.stdout }}"
     validate_certs: false
     method: POST
-    body: '{{ slurpfile["content"] | b64decode }}'
+    body: '{{ dashboard_data }}'
     body_format: json
+    status_code:
+    - 200
+    - 412
     headers:
       Content-Type: "Content-Type: application/json"
   register: add_ds