Skip to content

Commit

Permalink
grafana_folder|teams: add skip_version_check parameter
Browse files Browse the repository at this point in the history
Parameter allows to skip version check and execute module anyway.

Closes: #147
  • Loading branch information
Rémi REY committed Feb 14, 2021
1 parent 92d8275 commit f4be030
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/1.2.0.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bugfixes:
- Fix issue with url when grafana_url has a trailing slash (#135)
- grafana_dashboard : Fix reference before assignment issue (#146)
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace: community
name: grafana
version: 1.1.0
version: 1.2.0
readme: README.md
authors:
- Rémi REY (@rrey)
Expand Down
31 changes: 25 additions & 6 deletions plugins/modules/grafana_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
short_description: Manage Grafana Folders
description:
- Create/update/delete Grafana Folders through the Folders API.
requirements:
- The Folders API is only available starting Grafana 5 and the module will fail if the server version is lower than version 5.
options:
name:
Expand All @@ -43,6 +44,14 @@
default: present
type: str
choices: ["present", "absent"]
skip_version_check:
description:
- Skip Grafana version check and try to reach api endpoint anyway.
- This parameter can be useful if you enabled `hide_version` in grafana.ini
required: False
type: bool
default: False
version_added: "1.2.0"
extends_documentation_fragment:
- community.grafana.basic_auth
- community.grafana.api_key
Expand Down Expand Up @@ -162,6 +171,9 @@

__metaclass__ = type

class GrafanaError(Exception):
pass


class GrafanaFolderInterface(object):

Expand All @@ -174,10 +186,14 @@ def __init__(self, module):
else:
self.headers["Authorization"] = basic_auth_header(module.params['url_username'], module.params['url_password'])
# }}}
self.grafana_url = clean_url(module.params.get("url"))
grafana_version = self.get_version()
if grafana_version["major"] < 5:
self._module.fail_json(failed=True, msg="Folders API is available starting Grafana v5")
self.grafana_url = module.params.get("url")
if module.params.get("skip_version_check") is False:
try:
grafana_version = self.get_version()
except GrafanaError as e:
self._module.fail_json(failed=True, msg=to_text(e))
if grafana_version["major"] < 5:
self._module.fail_json(failed=True, msg="Folders API is available starting Grafana v5")

def _send_request(self, url, data=None, headers=None, method="GET"):
if data is not None:
Expand Down Expand Up @@ -205,8 +221,10 @@ def get_version(self):
url = "/api/health"
response = self._send_request(url, data=None, headers=self.headers, method="GET")
version = response.get("version")
major, minor, rev = version.split(".")
return {"major": int(major), "minor": int(minor), "rev": int(rev)}
if version is not None:
major, minor, rev = version.split(".")
return {"major": int(major), "minor": int(minor), "rev": int(rev)}
raise GrafanaError("Failed to retrieve version from '%s'" % url)

def create_folder(self, title):
url = "/api/folders"
Expand Down Expand Up @@ -242,6 +260,7 @@ def setup_module_object():
argument_spec.update(
name=dict(type='str', aliases=['title'], required=True),
state=dict(type='str', default='present', choices=['present', 'absent']),
skip_version_check=dict(type='bool', default=False),
)


Expand Down
33 changes: 27 additions & 6 deletions plugins/modules/grafana_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
description:
- Create/update/delete Grafana Teams through the Teams API.
- Also allows to add members in the team (if members exists).
requirements:
- The Teams API is only available starting Grafana 5 and the module will fail if the server version is lower than version 5.
options:
name:
Expand Down Expand Up @@ -60,6 +61,14 @@
- list of members found on the Team.
default: False
type: bool
skip_version_check:
description:
- Skip Grafana version check and try to reach api endpoint anyway.
- This parameter can be useful if you enabled `hide_version` in grafana.ini
required: False
type: bool
default: False
version_added: "1.2.0"
extends_documentation_fragment:
- community.grafana.basic_auth
- community.grafana.api_key
Expand Down Expand Up @@ -162,11 +171,16 @@

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.urls import fetch_url, basic_auth_header
from ansible.module_utils._text import to_text
from ansible_collections.community.grafana.plugins.module_utils.base import grafana_argument_spec, grafana_required_together, grafana_mutually_exclusive, clean_url

__metaclass__ = type


class GrafanaError(Exception):
pass


class GrafanaTeamInterface(object):

def __init__(self, module):
Expand All @@ -178,10 +192,14 @@ def __init__(self, module):
else:
self.headers["Authorization"] = basic_auth_header(module.params['url_username'], module.params['url_password'])
# }}}
self.grafana_url = clean_url(module.params.get("url"))
grafana_version = self.get_version()
if grafana_version["major"] < 5:
self._module.fail_json(failed=True, msg="Teams API is available starting Grafana v5")
self.grafana_url = module.params.get("url")
if module.params.get("skip_version_check") is False:
try:
grafana_version = self.get_version()
except GrafanaError as e:
self._module.fail_json(failed=True, msg=to_text(e))
if grafana_version["major"] < 5:
self._module.fail_json(failed=True, msg="Folders API is available starting Grafana v5")

def _send_request(self, url, data=None, headers=None, method="GET"):
if data is not None:
Expand All @@ -208,8 +226,10 @@ def get_version(self):
url = "/api/health"
response = self._send_request(url, data=None, headers=self.headers, method="GET")
version = response.get("version")
major, minor, rev = version.split(".")
return {"major": int(major), "minor": int(minor), "rev": int(rev)}
if version is not None:
major, minor, rev = version.split(".")
return {"major": int(major), "minor": int(minor), "rev": int(rev)}
raise GrafanaError("Failed to retrieve version from '%s'" % url)

def create_team(self, name, email):
url = "/api/teams"
Expand Down Expand Up @@ -278,6 +298,7 @@ def setup_module_object():
email=dict(type='str', required=True),
members=dict(type='list', elements='str', required=False),
enforce_members=dict(type='bool', default=False),
skip_version_check=dict(type='bool', default=False),
)


Expand Down

0 comments on commit f4be030

Please sign in to comment.