Skip to content

Commit

Permalink
Remove '/' as a safe char from datasource name quote function call
Browse files Browse the repository at this point in the history
Closes: #125
  • Loading branch information
rrey committed Nov 26, 2021
1 parent 33a7747 commit 4797adc
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 15 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/fix_slashes_in_datasources.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- Fix issue with datasource names that could not contain slashes (#125)
4 changes: 2 additions & 2 deletions plugins/modules/grafana_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,11 +630,11 @@ def switch_organisation(self, org_id):
def datasource_by_name(self, name):
datasource_exists = False
ds = {}
url = "/api/datasources/name/%s" % quote(name)
url = "/api/datasources/name/%s" % quote(name, safe='')
return self._send_request(url, headers=self.headers, method='GET')

def delete_datasource(self, name):
url = "/api/datasources/name/%s" % quote(name)
url = "/api/datasources/name/%s" % quote(name, safe='')
self._send_request(url, headers=self.headers, method='DELETE')

def update_datasource(self, ds_id, data):
Expand Down
26 changes: 13 additions & 13 deletions tests/integration/targets/grafana_datasource/tasks/elastic.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
- name: Create elasticsearch datasource
register: result
grafana_datasource:
name: datasource-elastic
name: "datasource/elastic"
grafana_url: "{{ grafana_url }}"
grafana_user: "{{ grafana_username }}"
grafana_password: "{{ grafana_password }}"
Expand Down Expand Up @@ -35,19 +35,19 @@
- result.datasource.jsonData.timeField == '@timestamp'
- not result.datasource.jsonData.tlsAuth
- result.datasource.jsonData.tlsAuthWithCACert
- result.datasource.name == 'datasource-elastic'
- result.datasource.name == 'datasource/elastic'
- result.datasource.orgId == 1
- result.datasource.password == ''
- result.datasource.type == 'elasticsearch'
- result.datasource.url == 'https://elastic.company.com:9200'
- result.datasource.user == ''
- not result.datasource.withCredentials
- "result.msg == 'Datasource datasource-elastic created'"
- "result.msg == 'Datasource datasource/elastic created'"

- name: Check elasticsearch datasource creation idempotency
register: result
grafana_datasource:
name: datasource-elastic
name: "datasource/elastic"
grafana_url: "{{ grafana_url }}"
grafana_user: "{{ grafana_username }}"
grafana_password: "{{ grafana_password }}"
Expand Down Expand Up @@ -81,7 +81,7 @@
- result.datasource.jsonData.timeField == '@timestamp'
- not result.datasource.jsonData.tlsAuth
- result.datasource.jsonData.tlsAuthWithCACert
- result.datasource.name == 'datasource-elastic'
- result.datasource.name == 'datasource/elastic'
- result.datasource.orgId == 1
- result.datasource.password == ''
- result.datasource.type == 'elasticsearch'
Expand All @@ -92,7 +92,7 @@
- name: update elasticsearch datasource creation
register: result
grafana_datasource:
name: datasource-elastic
name: "datasource/elastic"
grafana_url: "{{ grafana_url }}"
grafana_user: "{{ grafana_username }}"
grafana_password: "{{ grafana_password }}"
Expand Down Expand Up @@ -126,7 +126,7 @@
- result.datasource.jsonData.timeField == '@timestamp'
- not result.datasource.jsonData.tlsAuth
- result.datasource.jsonData.tlsAuthWithCACert
- result.datasource.name == 'datasource-elastic'
- result.datasource.name == 'datasource/elastic'
- result.datasource.orgId == 1
- result.datasource.password == ''
- result.datasource.type == 'elasticsearch'
Expand All @@ -137,7 +137,7 @@
- name: update elasticsearch datasource (ignoring secureJsonData)
register: result
grafana_datasource:
name: datasource-elastic
name: "datasource/elastic"
grafana_url: "{{ grafana_url }}"
grafana_user: "{{ grafana_username }}"
grafana_password: "{{ grafana_password }}"
Expand Down Expand Up @@ -176,7 +176,7 @@
- result.datasource.jsonData.timeField == '@timestamp'
- not result.datasource.jsonData.tlsAuth
- result.datasource.jsonData.tlsAuthWithCACert
- result.datasource.name == 'datasource-elastic'
- result.datasource.name == 'datasource/elastic'
- result.datasource.orgId == 1
- result.datasource.password == ''
- result.datasource.type == 'elasticsearch'
Expand All @@ -188,7 +188,7 @@
- name: update elasticsearch datasource (including secureJsonData)
register: result
grafana_datasource:
name: datasource-elastic
name: "datasource/elastic"
grafana_url: "{{ grafana_url }}"
grafana_user: "{{ grafana_username }}"
grafana_password: "{{ grafana_password }}"
Expand Down Expand Up @@ -227,7 +227,7 @@
- result.datasource.jsonData.timeField == '@timestamp'
- not result.datasource.jsonData.tlsAuth
- result.datasource.jsonData.tlsAuthWithCACert
- result.datasource.name == 'datasource-elastic'
- result.datasource.name == 'datasource/elastic'
- result.datasource.orgId == 1
- result.datasource.password == ''
- result.datasource.type == 'elasticsearch'
Expand All @@ -241,7 +241,7 @@
- name: Delete elasticsearch datasource
register: result
grafana_datasource:
name: datasource-elastic
name: "datasource/elastic"
grafana_url: "{{ grafana_url }}"
grafana_user: "{{ grafana_username }}"
grafana_password: "{{ grafana_password }}"
Expand All @@ -266,7 +266,7 @@
- name: Delete elasticsearch datasource (idempotency)
register: result
grafana_datasource:
name: datasource-elastic
name: "datasource/elastic"
grafana_url: "{{ grafana_url }}"
grafana_user: "{{ grafana_username }}"
grafana_password: "{{ grafana_password }}"
Expand Down
46 changes: 46 additions & 0 deletions tests/integration/targets/grafana_datasource/tasks/issues.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---

- name: test datasource name with slash
register: result
grafana_datasource:
name: "datasource/elastic"
grafana_url: "{{ grafana_url }}"
grafana_user: "{{ grafana_username }}"
grafana_password: "{{ grafana_password }}"
org_id: '1'
ds_type: elasticsearch
ds_url: https://elastic.company.com:9200
database: '[logstash_]YYYY.MM.DD'
basic_auth_user: grafana
basic_auth_password: '******'
time_field: '@timestamp'
time_interval: 1m
interval: Daily
es_version: 56
max_concurrent_shard_requests: 42
tls_ca_cert: /etc/ssl/certs/ca.pem

- assert:
that:
- result.changed
- result.datasource.basicAuth
- result.datasource.basicAuthUser == 'grafana'
- result.datasource.access == 'proxy'
- result.datasource.database == '[logstash_]YYYY.MM.DD'
- not result.datasource.isDefault
- result.datasource.jsonData.esVersion == 56
- result.datasource.jsonData.interval == 'Daily'
- result.datasource.jsonData.maxConcurrentShardRequests == 42
- result.datasource.jsonData.timeField == '@timestamp'
- not result.datasource.jsonData.tlsAuth
- not result.datasource.jsonData.tlsAuthWithCACert
- result.datasource.name == 'datasource/elastic'
- result.datasource.orgId == 1
- result.datasource.password == ''
- result.datasource.type == 'elasticsearch'
- result.datasource.url == 'https://elastic.company.com:9200'
- result.datasource.user == ''
- not result.datasource.withCredentials
- "result.msg == 'Datasource datasource/elastic created'"

...

0 comments on commit 4797adc

Please sign in to comment.