Skip to content

Commit

Permalink
Add keep-uid argument to preserve the uid provided in file (#24)
Browse files Browse the repository at this point in the history
Also, satisfy CI due to an unrelated error.

---------

Co-authored-by: Vitalii_Rymar <[email protected]>
  • Loading branch information
vrymar and vitaliirymar authored Oct 15, 2024
1 parent aeece73 commit fbd5375
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 26 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions grafana_import/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class myArgs:
"overwrite",
"allow_new",
"verbose",
"keep_uid",
]

def __init__(self):
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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,
}
)

Expand Down
18 changes: 14 additions & 4 deletions grafana_import/grafana.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -305,18 +308,25 @@ 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(
"Dashboard with the same title already exists in this folder with another uid. "
"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

Expand Down
40 changes: 19 additions & 21 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down

0 comments on commit fbd5375

Please sign in to comment.