Closed Nemental closed 8 months ago
All modified and coverable lines are covered by tests :white_check_mark:
Comparison is base (
879e206
) 69.25% compared to head (90f9783
) 70.82%.
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
I actually liked your first try (https://github.com/ansible-collections/community.grafana/pull/345/commits/41596d36689fbd9e9f3ab94bffe08425fc3892c4) better, because there you didn't redefine params["org_id"]
.
And now that I think of it: Now we check twice, if org_name
is set. Could we define a new function for this in the GrafanaInterface
-class, e.g. get_org_id
and use it at these two places?
Aside, for another issue: We obviously need tests for idempotency.
@rndmh3ro thank you for your review! In the GrafanaInterface
class is already a function called organization_by_name
, which was implemented in #332. You can check my recent changes. Maybe it's a solution we can go with.
This is what I had in mind:
diff --git a/plugins/modules/grafana_datasource.py b/plugins/modules/grafana_datasource.py
index 910e16b..3784999 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 @@ class GrafanaInterface(object):
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)
This way org_id
is only defined in the init-function of the class. with self.org_id
we can then use it outside of the class in the payload function.
I think, this should work. My local tests are failing with another error, so can you please test this?
@rndmh3ro Yes it works :) Great suggestion to define the variable for org_id this way. Thank you.
SUMMARY
If
org_name
is set instead oforg_id
, comparing the diff (after/before) to determine whether something has changed or not does not work.ISSUE TYPE
COMPONENT NAME
grafana_datasource
ADDITIONAL INFORMATION