Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(datasource): manage datasources by org_name #332

Merged
merged 11 commits into from
Jan 4, 2024
Merged
4 changes: 4 additions & 0 deletions changelogs/fragments/332-datasource-by-org-name.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---

minor_changes:
- Add parameter `org_name` to `grafana_datasource`
35 changes: 30 additions & 5 deletions plugins/modules/grafana_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,19 @@
default: false
org_id:
description:
- Grafana Organisation ID in which the datasource should be created.
- Grafana organization ID in which the datasource should be created.
- Not used when C(grafana_api_key) is set, because the C(grafana_api_key) only
belong to one organisation.
belongs to one organization.
- Mutually exclusive with `org_name`.
default: 1
type: int
org_name:
description:
- Grafana organization name in which the datasource should be created.
- Not used when C(grafana_api_key) is set, because the C(grafana_api_key) only
belongs to one organization.
- Mutually exclusive with `org_id`.
type: str
state:
description:
- Status of the datasource
Expand Down Expand Up @@ -609,7 +617,6 @@

# datasource type related parameters
if data["ds_type"] == "elasticsearch":

json_data["maxConcurrentShardRequests"] = data["max_concurrent_shard_requests"]
json_data["timeField"] = data["time_field"]
if data.get("interval"):
Expand Down Expand Up @@ -690,7 +697,12 @@
self.headers["Authorization"] = basic_auth_header(
module.params["url_username"], module.params["url_password"]
)
self.switch_organisation(module.params["org_id"])
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)
# }}}

def _send_request(self, url, data=None, headers=None, method="GET"):
Expand Down Expand Up @@ -721,10 +733,21 @@
% (status_code, url, data),
)

def switch_organisation(self, org_id):
def switch_organization(self, org_id):
url = "/api/user/using/%d" % org_id
response = self._send_request(url, headers=self.headers, method="POST")

def organization_by_name(self, org_name):
url = "/api/user/orgs"
organizations = self._send_request(url, headers=self.headers, method="GET")

Check warning on line 742 in plugins/modules/grafana_datasource.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/grafana_datasource.py#L741-L742

Added lines #L741 - L742 were not covered by tests
orga = next((org for org in organizations if org["name"] == org_name))
if orga:
return orga["orgId"]

Check warning on line 745 in plugins/modules/grafana_datasource.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/grafana_datasource.py#L745

Added line #L745 was not covered by tests

return self._module.fail_json(

Check warning on line 747 in plugins/modules/grafana_datasource.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/grafana_datasource.py#L747

Added line #L747 was not covered by tests
failed=True, msg="Current user isn't member of organization: %s" % org_name
)

def datasource_by_name(self, name):
datasource_exists = False
ds = {}
Expand Down Expand Up @@ -783,6 +806,7 @@
tls_skip_verify=dict(type="bool", default=False),
is_default=dict(default=False, type="bool"),
org_id=dict(default=1, type="int"),
org_name=dict(type="str"),
es_version=dict(
type="str",
default="7.10+",
Expand Down Expand Up @@ -866,6 +890,7 @@
mutually_exclusive=[
["url_username", "grafana_api_key"],
["tls_ca_cert", "tls_skip_verify"],
["org_id", "org_name"],
],
required_if=[
["state", "present", ["ds_type", "ds_url"]],
Expand Down
Loading