Skip to content

Conversation

@Hipska
Copy link

@Hipska Hipska commented May 5, 2025

SUMMARY

Add an option to directly pass the dashboard definition instead of via path. This reduces the need to first generate/place a JSON file on the filesystem.

Fixes #417

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

grafana_dashboard

ADDITIONAL INFORMATION

@codecov
Copy link

codecov bot commented May 5, 2025

Codecov Report

Attention: Patch coverage is 33.33333% with 10 lines in your changes missing coverage. Please review.

Project coverage is 75.53%. Comparing base (d3b332e) to head (7258964).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
plugins/modules/grafana_dashboard.py 30.00% 6 Missing and 1 partial ⚠️
plugins/module_utils/base.py 40.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #426      +/-   ##
==========================================
+ Coverage   72.84%   75.53%   +2.69%     
==========================================
  Files          21       17       -4     
  Lines        2261     2089     -172     
  Branches      475      457      -18     
==========================================
- Hits         1647     1578      -69     
+ Misses        445      340     -105     
- Partials      169      171       +2     
Flag Coverage Δ
integration 70.95% <33.33%> (-0.35%) ⬇️
sanity ?
units 65.14% <40.00%> (-0.11%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Nemental
Copy link
Collaborator

Nemental commented May 5, 2025

Hi, it's great that you got started on this so quickly. We definitely need integration testing for this new feature. There are plenty of examples you can use as orientation.
https://github.com/ansible-collections/community.grafana/tree/main/tests/integration/targets/grafana_dashboard

@Hipska
Copy link
Author

Hipska commented May 5, 2025

Of course. I first wanted to know if I was going in the right direction, since this is my first contribution..

Hipska added 2 commits May 6, 2025 08:57
* main:
  docs: added new fragments for release
  docs: added new fragments for release
  docs: update galaxy
  docs: update changelog
  test(grafana_dashboard): check if v11 subfolder api available
  chore(grafana_dashboard): search for folder by title and uid
  chore(grafana_dashboard): role parent_folder reference
  test(grafana_dashboard): refactor folder and subfolder tests
  chore(grafana_dashboard): rename parent_uid to parent_folder
  docs(grafana_dashboard): change text of changelog fragment
  Allow managing dashboards in subfolders
@Hipska Hipska force-pushed the feat/dashboard_content branch from a5f6d29 to 7258964 Compare May 6, 2025 07:02
@Hipska
Copy link
Author

Hipska commented May 27, 2025

@Nemental is this going in the right direction? I want to know so I can start making integration tests for it..

dashboard_revision=dict(type="str", default="1"),
overwrite=dict(type="bool", default=False),
commit_message=dict(type="str"),
content=dict(type=dict_str),
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
content=dict(type=dict_str),
content=dict(type="raw"),

I think it's better, if not mandatory, to stick with the officially specified argument types. With raw, you can pass the content unvalidated and then perform the actual validation of the content immediately afterward.

Copy link
Author

@Hipska Hipska May 27, 2025

Choose a reason for hiding this comment

The 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 ansible-lint to check the argument type?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, you can set the type="raw" and then add your try-isinstance and except-TypeError to the already existing try/except block. Something like this (not tested):

    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)
        )

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rndmh3ro Maybe you can share your opinion :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rndmh3ro Please advise, so we can have this landed..

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rndmh3ro any update please?

Comment on lines +56 to +61


def dict_str(value):
if isinstance(value, (dict, string_types)):
return value
raise TypeError
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving dict_str to module_utils is fine, but not absolutely necessary, since this function isn't used multiple times.

Copy link
Author

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?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nemental
Copy link
Collaborator

@Nemental is this going in the right direction? I want to know so I can start making integration tests for it..

You're doing great, thank you for your contribution! I've left a few comments.

Hipska added 2 commits May 27, 2025 16:13
# Conflicts:
#	plugins/module_utils/base.py
#	plugins/modules/grafana_dashboard.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Directly pass dashboard content

2 participants