From 41596d36689fbd9e9f3ab94bffe08425fc3892c4 Mon Sep 17 00:00:00 2001 From: Nemental <15136847+Nemental@users.noreply.github.com> Date: Wed, 17 Jan 2024 14:19:36 +0100 Subject: [PATCH 1/7] fix: orgId by name if defined to compare diff --- plugins/modules/grafana_datasource.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/plugins/modules/grafana_datasource.py b/plugins/modules/grafana_datasource.py index be631836..ffc8d952 100644 --- a/plugins/modules/grafana_datasource.py +++ b/plugins/modules/grafana_datasource.py @@ -559,9 +559,9 @@ def compare_datasources(new, current, compareSecureData=True): return dict(before=current, after=new) -def get_datasource_payload(data): +def get_datasource_payload(data, org_id): payload = { - "orgId": data["org_id"], + "orgId": org_id, "name": data["name"], "uid": data["uid"], "type": data["ds_type"], @@ -921,9 +921,14 @@ def main(): grafana_iface = GrafanaInterface(module) ds = grafana_iface.datasource_by_name(name) + org_id = ( + grafana_iface.organization_by_name(module.params["org_name"]) + if module.params["org_name"] + else module.params["org_id"] + ) if state == "present": - payload = get_datasource_payload(module.params) + payload = get_datasource_payload(module.params, org_id) if ds is None: grafana_iface.create_datasource(payload) ds = grafana_iface.datasource_by_name(name) From 18730b2fed6a895b9ece48d742fad36df327bd53 Mon Sep 17 00:00:00 2001 From: Nemental <15136847+Nemental@users.noreply.github.com> Date: Wed, 17 Jan 2024 14:30:28 +0100 Subject: [PATCH 2/7] chore: changelog fragment --- changelogs/fragments/345-datasource-compare-diff-orgid.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelogs/fragments/345-datasource-compare-diff-orgid.yml diff --git a/changelogs/fragments/345-datasource-compare-diff-orgid.yml b/changelogs/fragments/345-datasource-compare-diff-orgid.yml new file mode 100644 index 00000000..14de8aca --- /dev/null +++ b/changelogs/fragments/345-datasource-compare-diff-orgid.yml @@ -0,0 +1,3 @@ +--- +bugfix: + - `grafana_datasource` get orgId by name if defined to compare diff From 0e475bfa40cee9ace16c6e9c36be0afca3516d94 Mon Sep 17 00:00:00 2001 From: Nemental <15136847+Nemental@users.noreply.github.com> Date: Wed, 17 Jan 2024 14:33:25 +0100 Subject: [PATCH 3/7] chore: fragment category --- changelogs/fragments/345-datasource-compare-diff-orgid.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/345-datasource-compare-diff-orgid.yml b/changelogs/fragments/345-datasource-compare-diff-orgid.yml index 14de8aca..a535c776 100644 --- a/changelogs/fragments/345-datasource-compare-diff-orgid.yml +++ b/changelogs/fragments/345-datasource-compare-diff-orgid.yml @@ -1,3 +1,3 @@ --- -bugfix: +bugfixes: - `grafana_datasource` get orgId by name if defined to compare diff From fce424098154a78faf9c8a1193621b65100f9172 Mon Sep 17 00:00:00 2001 From: Nemental <15136847+Nemental@users.noreply.github.com> Date: Wed, 17 Jan 2024 15:05:14 +0100 Subject: [PATCH 4/7] chore: simplify --- plugins/modules/grafana_datasource.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/plugins/modules/grafana_datasource.py b/plugins/modules/grafana_datasource.py index ffc8d952..c911900f 100644 --- a/plugins/modules/grafana_datasource.py +++ b/plugins/modules/grafana_datasource.py @@ -559,9 +559,9 @@ def compare_datasources(new, current, compareSecureData=True): return dict(before=current, after=new) -def get_datasource_payload(data, org_id): +def get_datasource_payload(data): payload = { - "orgId": org_id, + "orgId": data["org_id"], "name": data["name"], "uid": data["uid"], "type": data["ds_type"], @@ -921,14 +921,15 @@ def main(): grafana_iface = GrafanaInterface(module) ds = grafana_iface.datasource_by_name(name) - org_id = ( - grafana_iface.organization_by_name(module.params["org_name"]) - if module.params["org_name"] - else module.params["org_id"] - ) if state == "present": - payload = get_datasource_payload(module.params, org_id) + params = module.params + params["org_id"] = ( + grafana_iface.organization_by_name(params["org_name"]) + if params["org_name"] + else params["org_id"] + ) + payload = get_datasource_payload(params) if ds is None: grafana_iface.create_datasource(payload) ds = grafana_iface.datasource_by_name(name) From 00016749197bf2807a10468fc394cebe5878413f Mon Sep 17 00:00:00 2001 From: Nemental <15136847+Nemental@users.noreply.github.com> Date: Wed, 17 Jan 2024 15:12:12 +0100 Subject: [PATCH 5/7] chore: fixed changelog fragment --- changelogs/fragments/345-datasource-compare-diff-orgid.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/345-datasource-compare-diff-orgid.yml b/changelogs/fragments/345-datasource-compare-diff-orgid.yml index a535c776..11c21141 100644 --- a/changelogs/fragments/345-datasource-compare-diff-orgid.yml +++ b/changelogs/fragments/345-datasource-compare-diff-orgid.yml @@ -1,3 +1,3 @@ --- bugfixes: - - `grafana_datasource` get orgId by name if defined to compare diff + - Fixed orgId handling in diff comparison for `grafana_datasource` if using org_name From 31ff484596d9aea803151fa27badcc9041d1c16f Mon Sep 17 00:00:00 2001 From: Nemental <15136847+Nemental@users.noreply.github.com> Date: Thu, 18 Jan 2024 10:57:41 +0100 Subject: [PATCH 6/7] refactor: get ds payload optional org_id parameter --- plugins/modules/grafana_datasource.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/plugins/modules/grafana_datasource.py b/plugins/modules/grafana_datasource.py index c911900f..910e16bb 100644 --- a/plugins/modules/grafana_datasource.py +++ b/plugins/modules/grafana_datasource.py @@ -559,9 +559,9 @@ def compare_datasources(new, current, compareSecureData=True): return dict(before=current, after=new) -def get_datasource_payload(data): +def get_datasource_payload(data, org_id=None): payload = { - "orgId": data["org_id"], + "orgId": data["org_id"] if org_id is None else org_id, "name": data["name"], "uid": data["uid"], "type": data["ds_type"], @@ -923,13 +923,12 @@ def main(): ds = grafana_iface.datasource_by_name(name) if state == "present": - params = module.params - params["org_id"] = ( - grafana_iface.organization_by_name(params["org_name"]) - if params["org_name"] - else params["org_id"] + org_id = ( + grafana_iface.organization_by_name(module.params["org_name"]) + if module.params["org_name"] + else module.params["org_id"] ) - payload = get_datasource_payload(params) + payload = get_datasource_payload(module.params, org_id) if ds is None: grafana_iface.create_datasource(payload) ds = grafana_iface.datasource_by_name(name) From 90f97830559e286f7b171e6c76099f074eda453d Mon Sep 17 00:00:00 2001 From: Nemental <15136847+Nemental@users.noreply.github.com> Date: Thu, 18 Jan 2024 13:36:07 +0100 Subject: [PATCH 7/7] refactor: definition of org_id variable --- plugins/modules/grafana_datasource.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/plugins/modules/grafana_datasource.py b/plugins/modules/grafana_datasource.py index 910e16bb..37849998 100644 --- a/plugins/modules/grafana_datasource.py +++ b/plugins/modules/grafana_datasource.py @@ -688,6 +688,7 @@ class GrafanaInterface(object): def __init__(self, module): self._module = module self.grafana_url = base.clean_url(module.params.get("url")) + self.org_id = None # {{{ Authentication header self.headers = {"Content-Type": "application/json"} if module.params.get("grafana_api_key", None): @@ -698,12 +699,12 @@ def __init__(self, module): self.headers["Authorization"] = basic_auth_header( module.params["url_username"], module.params["url_password"] ) - org_id = ( + self.org_id = ( self.organization_by_name(module.params["org_name"]) if module.params["org_name"] else module.params["org_id"] ) - self.switch_organization(org_id) + self.switch_organization(self.org_id) # }}} def _send_request(self, url, data=None, headers=None, method="GET"): @@ -923,12 +924,7 @@ def main(): ds = grafana_iface.datasource_by_name(name) if state == "present": - org_id = ( - grafana_iface.organization_by_name(module.params["org_name"]) - if module.params["org_name"] - else module.params["org_id"] - ) - payload = get_datasource_payload(module.params, org_id) + payload = get_datasource_payload(module.params, grafana_iface.org_id) if ds is None: grafana_iface.create_datasource(payload) ds = grafana_iface.datasource_by_name(name)