-
Notifications
You must be signed in to change notification settings - Fork 85
feat(grafana_dashboard): Directly provide dashboard content #426
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
614756e
c81afe4
7258964
4ba5bc4
4d9f7d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -66,7 +66,8 @@ | |||||
| description: | ||||||
| - The path to the json file containing the Grafana dashboard to import or export. | ||||||
| - A http URL is also accepted (since 2.10). | ||||||
| - Required if C(state) is C(export) or C(present). | ||||||
| - Required if C(state) is C(export). | ||||||
| - Mutually exclusive with C(content) and C(dashboard_id). | ||||||
| aliases: [ dashboard_url ] | ||||||
| type: str | ||||||
| overwrite: | ||||||
|
|
@@ -77,6 +78,7 @@ | |||||
| dashboard_id: | ||||||
| description: | ||||||
| - Public Grafana.com dashboard id to import | ||||||
| - Mutually exclusive with C(content) and C(path). | ||||||
| version_added: "1.0.0" | ||||||
| type: str | ||||||
| dashboard_revision: | ||||||
|
|
@@ -90,6 +92,11 @@ | |||||
| - Set a commit message for the version history. | ||||||
| - Only used when C(state) is C(present). | ||||||
| type: str | ||||||
| content: | ||||||
| description: | ||||||
| - Grafana dashboard content. | ||||||
| - Mutually exclusive with C(path) and C(dashboard_id). | ||||||
| version_added: "2.3.0" | ||||||
| extends_documentation_fragment: | ||||||
| - community.grafana.basic_auth | ||||||
| - community.grafana.api_key | ||||||
|
|
@@ -157,6 +164,7 @@ | |||||
| from ansible_collections.community.grafana.plugins.module_utils.base import ( | ||||||
| grafana_argument_spec, | ||||||
| clean_url, | ||||||
| dict_str, | ||||||
| ) | ||||||
|
|
||||||
| __metaclass__ = type | ||||||
|
|
@@ -367,11 +375,18 @@ | |||||
| ) | ||||||
| payload = json.loads(r.read()) | ||||||
| else: | ||||||
| try: | ||||||
| with open(data["path"], "r", encoding="utf-8") as json_file: | ||||||
| payload = json.load(json_file) | ||||||
| except Exception as e: | ||||||
| raise GrafanaAPIException("Can't load json file %s" % to_native(e)) | ||||||
| if data["path"]: | ||||||
| try: | ||||||
| with open(data["path"], "r", encoding="utf-8") as json_file: | ||||||
| payload = json.load(json_file) | ||||||
| except Exception as e: | ||||||
| raise GrafanaAPIException("Can't load json file %s" % to_native(e)) | ||||||
| else: | ||||||
| content = data["content"] | ||||||
| if type(content) == dict: | ||||||
| payload = content | ||||||
| else: | ||||||
| payload = json.loads(content) | ||||||
|
|
||||||
| # Check that the dashboard JSON is nested under the 'dashboard' key | ||||||
| if "dashboard" not in payload: | ||||||
|
|
@@ -638,6 +653,7 @@ | |||||
| dashboard_revision=dict(type="str", default="1"), | ||||||
| overwrite=dict(type="bool", default=False), | ||||||
| commit_message=dict(type="str"), | ||||||
| content=dict(type=dict_str), | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://docs.ansible.com/ansible/latest/dev_guide/developing_program_flow_modules.html#argument-spec
Suggested change
I think it's better, if not mandatory, to stick with the officially specified argument types. With
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, might need help on that. I copied the usage from here: https://github.com/ansible-collections/kubernetes.core/blob/94c1f57f36a08c1ac245556824410c52d929ec21/plugins/module_utils/args_common.py#L85 I think it is like that to be able to have
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For example, you can set the try:
if not isinstance(module.params["content"], (dict, string_types)):
raise TypeError("content must be either a string or a dictionary")
if module.params["state"] == "present":
result = grafana_create_dashboard(module, module.params)
elif module.params["state"] == "absent":
result = grafana_delete_dashboard(module, module.params)
else:
result = grafana_export_dashboard(module, module.params)
except (TypeError, GrafanaAPIException, GrafanaMalformedJson) as e:
module.fail_json(failed=True, msg="error : %s" % to_native(e))
except GrafanaDeleteException as e:
module.fail_json(
failed=True, msg="error : Can't delete dashboard : %s" % to_native(e)
)
except GrafanaExportException as e:
module.fail_json(
failed=True, msg="error : Can't export dashboard : %s" % to_native(e)
)
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rndmh3ro Maybe you can share your opinion :)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rndmh3ro Please advise, so we can have this landed..
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rndmh3ro any update please? |
||||||
| ) | ||||||
| module = AnsibleModule( | ||||||
| argument_spec=argument_spec, | ||||||
|
|
@@ -649,7 +665,7 @@ | |||||
| mutually_exclusive=[ | ||||||
| ["url_username", "grafana_api_key"], | ||||||
| ["uid", "slug"], | ||||||
| ["path", "dashboard_id"], | ||||||
| ["path", "dashboard_id", "content"], | ||||||
| ["org_id", "org_name"], | ||||||
| ], | ||||||
| ) | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moving
dict_strto module_utils is fine, but not absolutely necessary, since this function isn't used multiple times.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did it as maybe other parts could use it as well. OK to keep?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Nemental ?