From fbd5375001ae6a6ec3ad39f41d205f1820c9a926 Mon Sep 17 00:00:00 2001 From: Vitalii Rymar <17924151+vrymar@users.noreply.github.com> Date: Tue, 15 Oct 2024 18:31:32 +0300 Subject: [PATCH] Add keep-uid argument to preserve the uid provided in file (#24) Also, satisfy CI due to an unrelated error. --------- Co-authored-by: Vitalii_Rymar --- HISTORY.md | 1 + README.md | 3 ++- grafana_import/cli.py | 14 ++++++++++++++ grafana_import/grafana.py | 18 ++++++++++++++---- pyproject.toml | 40 +++++++++++++++++++-------------------- 5 files changed, 50 insertions(+), 26 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index f84d1a5..408187c 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -5,6 +5,7 @@ ## Unreleased - Fixed folder argument issue - Fixed import dashboards into a folder +- Added keep-uid argument to preserve the dashboard uid provided in file Thanks, @vrymar. diff --git a/README.md b/README.md index 29538de..4e460f3 100644 --- a/README.md +++ b/README.md @@ -220,7 +220,7 @@ grafana-import import --overwrite -i /path/to/gl-dashboard.py ```shell usage: grafana-import [-h] [-a] [-b BASE_PATH] [-c CONFIG_FILE] [-d DASHBOARD_NAME] [-g GRAFANA_LABEL] - [-f GRAFANA_FOLDER] [-i DASHBOARD_FILE] [-o] [-p] [-v] + [-f GRAFANA_FOLDER] [-i DASHBOARD_FILE] [-o] [-p] [-v] [-k] [-V] [ACTION] @@ -257,6 +257,7 @@ optional arguments: the folder name where to import into Grafana. -i DASHBOARD_FILE, --dashboard_file DASHBOARD_FILE path to the dashboard file to import into Grafana. + -k --keep_uid keep uid defined in dashboard file to import into Grafana. When dashboard is overriden, the uid is also overriden. -o, --overwrite if a dashboard with same name exists in folder, overwrite it with this new one. -r, --reload Watch the input dashboard for changes on disk, and diff --git a/grafana_import/cli.py b/grafana_import/cli.py index c777ce4..e9cdb8c 100644 --- a/grafana_import/cli.py +++ b/grafana_import/cli.py @@ -70,6 +70,7 @@ class myArgs: "overwrite", "allow_new", "verbose", + "keep_uid", ] def __init__(self): @@ -164,6 +165,15 @@ def main(): "import: import a local dashboard file (previously exported) to Grafana.\n" "remove: lookup for dashboard name in Grafana and remove it from Grafana server.", ) + + parser.add_argument( + "-k", + "--keep_uid", + action="store_true", + default=False, + help="when importing dashboard, keep dashboard uid defined in the json file.", + ) + inArgs = myArgs() args = parser.parse_args(namespace=inArgs) @@ -213,11 +223,15 @@ def main(): if "export_suffix" not in config["general"] or config["general"]["export_suffix"] is None: config["general"]["export_suffix"] = "_%Y%m%d%H%M%S" + if args.keep_uid is None: + args.keep_uid = False + params = grafana_settings(url=args.grafana_url, config=config, label=args.grafana_label) params.update( { "overwrite": args.overwrite, "allow_new": args.allow_new, + "keep_uid": args.keep_uid, } ) diff --git a/grafana_import/grafana.py b/grafana_import/grafana.py index 6e98c99..3df737a 100644 --- a/grafana_import/grafana.py +++ b/grafana_import/grafana.py @@ -99,6 +99,9 @@ def __init__(self, **kwargs): # * allow to create new dashboard with same name in specified folder. self.allow_new = kwargs.get("allow_new", False) + # * when importing dash, keep dashboard uid defined in the json file. + self.keep_uid = kwargs.get("keep_uid", False) + # * try to connect to the API try: res = self.grafana_api.health.check() @@ -305,9 +308,14 @@ def import_dashboard(self, dashboard: t.Dict[str, t.Any]) -> bool: ) # ** case d) send a copy to existing dash : update existing elif new_dash["folderId"] == old_dash["folderId"]: - if "uid" not in new_dash["dashboard"] or new_dash["dashboard"]["uid"] != old_dash["uid"]: + if ( + "uid" not in new_dash["dashboard"] + or new_dash["dashboard"]["uid"] != old_dash["uid"] + or new_dash["dashboard"]["id"] != old_dash["id"] + ): if self.overwrite: - new_dash["dashboard"]["uid"] = old_dash["uid"] + if not self.keep_uid: + new_dash["dashboard"]["uid"] = old_dash["uid"] new_dash["dashboard"]["id"] = old_dash["id"] else: raise GrafanaClient.GrafanaBadInputError( @@ -315,8 +323,10 @@ def import_dashboard(self, dashboard: t.Dict[str, t.Any]) -> bool: "Use `overwrite` to permit overwriting it." ) else: - # force the creation of a new dashboard - new_dash["dashboard"]["uid"] = None + if not self.keep_uid: + # force the creation of a new dashboard + new_dash["dashboard"]["uid"] = None + new_dash["dashboard"]["id"] = None new_dash["overwrite"] = False diff --git a/pyproject.toml b/pyproject.toml index 07ec61b..7d2bfe9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,50 +5,48 @@ line-length = 120 line-length = 120 lint.select = [ - # Bandit - "S", - # Bugbear - "B", # Builtins "A", + # Bugbear + "B", # comprehensions "C4", + # Pycodestyle + "E", # eradicate "ERA", - # flake8-2020 - "YTT", + # Pyflakes + "F", # isort "I", # pandas-vet "PD", + # return + "RET", + # Bandit + "S", # print "T20", - # Pycodestyle - "E", "W", - # Pyflakes - "F", - # return - "RET", + # flake8-2020 + "YTT", ] lint.extend-ignore = [ ] -[tool.ruff.lint.per-file-ignores] -"tests/*" = [ - # Use of `assert` detected - "S101", -] -"grafana_import/cli.py" = [ +lint.per-file-ignores."grafana_import/cli.py" = [ # `print` found "T201", ] - # =================== # Tasks configuration # =================== +lint.per-file-ignores."tests/*" = [ + # Use of `assert` detected + "S101", +] [tool.pytest.ini_options] addopts = "-rA --verbosity=3 --cov --cov-report=term-missing --cov-report=xml" @@ -69,14 +67,14 @@ branch = false omit = [ "tests/*", ] -source = ["grafana_import"] +source = [ "grafana_import" ] [tool.coverage.report] fail_under = 0 show_missing = true [tool.mypy] -packages = ["grafana_import"] +packages = [ "grafana_import" ] install_types = true ignore_missing_imports = true implicit_optional = true