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

use shared function for verison check #327

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rndmh3ro
Copy link
Collaborator

SUMMARY
ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

grafana_folder
grafana_team

@@ -52,3 +60,27 @@ def grafana_required_together():

def grafana_mutually_exclusive():
return [['url_username', 'grafana_api_key']]


def check_required_version(self, minimum_version):
Copy link
Collaborator

@rrey rrey Dec 9, 2023

Choose a reason for hiding this comment

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

Nice change proposal :)
I would rather create a BaseInterface class with this method and make all the interface classes inherit from it.
I guess that will be the same code.

Copy link
Collaborator Author

@rndmh3ro rndmh3ro Dec 11, 2023

Choose a reason for hiding this comment

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

Do you mean something like this?


class BaseInterface:
    def check_required_version(self, minimum_version):
        if not self._module.params.get("skip_version_check"):
            try:
                response, info = fetch_url(
                    self._module,
                    "%s/api/health" % (self.grafana_url),
                    headers=self.headers,
                    method="GET",
                )
                content = json.loads(response.read())
                version = content.get("version")
                major_version = int(version.split(".")[0])

            except GrafanaError as e:
                self._module.fail_json(failed=True, msg=to_text(e))

            if major_version < int(minimum_version):
                self._module.fail_json(
                    failed=True,
                    msg="Need at least Grafana version %s to use this feature."
                    % minimum_version,
                )

And then this?

class Interface1(BaseInterface):
    def __init__(self):
        super().__init__()

    def some_method(self):
        self.check_required_version(minimum_version)

If yes, I don't understand the point of this. Or do you mean something else?

Copy link
Collaborator

Choose a reason for hiding this comment

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

That is what I meant indeed.

You are using a function that requires providing self at every call, self being potentially any Grafana interface we have in the Collection that would require checking for a version.

So if you create a base class with this function and make all interface inherit from it, you are achieving the same thing with a more appropriate coding implementation (inheritance) and avoid passing self to a function which is what a class method does anyway.

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.

2 participants