Skip to content

Commit fbd5375

Browse files
vrymarvitaliirymar
andauthored
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 <[email protected]>
1 parent aeece73 commit fbd5375

File tree

5 files changed

+50
-26
lines changed

5 files changed

+50
-26
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
## Unreleased
66
- Fixed folder argument issue
77
- Fixed import dashboards into a folder
8+
- Added keep-uid argument to preserve the dashboard uid provided in file
89

910
Thanks, @vrymar.
1011

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ grafana-import import --overwrite -i /path/to/gl-dashboard.py
220220
```shell
221221
usage: grafana-import [-h] [-a] [-b BASE_PATH] [-c CONFIG_FILE]
222222
[-d DASHBOARD_NAME] [-g GRAFANA_LABEL]
223-
[-f GRAFANA_FOLDER] [-i DASHBOARD_FILE] [-o] [-p] [-v]
223+
[-f GRAFANA_FOLDER] [-i DASHBOARD_FILE] [-o] [-p] [-v] [-k]
224224
[-V]
225225
[ACTION]
226226
@@ -257,6 +257,7 @@ optional arguments:
257257
the folder name where to import into Grafana.
258258
-i DASHBOARD_FILE, --dashboard_file DASHBOARD_FILE
259259
path to the dashboard file to import into Grafana.
260+
-k --keep_uid keep uid defined in dashboard file to import into Grafana. When dashboard is overriden, the uid is also overriden.
260261
-o, --overwrite if a dashboard with same name exists in folder,
261262
overwrite it with this new one.
262263
-r, --reload Watch the input dashboard for changes on disk, and

grafana_import/cli.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class myArgs:
7070
"overwrite",
7171
"allow_new",
7272
"verbose",
73+
"keep_uid",
7374
]
7475

7576
def __init__(self):
@@ -164,6 +165,15 @@ def main():
164165
"import: import a local dashboard file (previously exported) to Grafana.\n"
165166
"remove: lookup for dashboard name in Grafana and remove it from Grafana server.",
166167
)
168+
169+
parser.add_argument(
170+
"-k",
171+
"--keep_uid",
172+
action="store_true",
173+
default=False,
174+
help="when importing dashboard, keep dashboard uid defined in the json file.",
175+
)
176+
167177
inArgs = myArgs()
168178
args = parser.parse_args(namespace=inArgs)
169179

@@ -213,11 +223,15 @@ def main():
213223
if "export_suffix" not in config["general"] or config["general"]["export_suffix"] is None:
214224
config["general"]["export_suffix"] = "_%Y%m%d%H%M%S"
215225

226+
if args.keep_uid is None:
227+
args.keep_uid = False
228+
216229
params = grafana_settings(url=args.grafana_url, config=config, label=args.grafana_label)
217230
params.update(
218231
{
219232
"overwrite": args.overwrite,
220233
"allow_new": args.allow_new,
234+
"keep_uid": args.keep_uid,
221235
}
222236
)
223237

grafana_import/grafana.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ def __init__(self, **kwargs):
9999
# * allow to create new dashboard with same name in specified folder.
100100
self.allow_new = kwargs.get("allow_new", False)
101101

102+
# * when importing dash, keep dashboard uid defined in the json file.
103+
self.keep_uid = kwargs.get("keep_uid", False)
104+
102105
# * try to connect to the API
103106
try:
104107
res = self.grafana_api.health.check()
@@ -305,18 +308,25 @@ def import_dashboard(self, dashboard: t.Dict[str, t.Any]) -> bool:
305308
)
306309
# ** case d) send a copy to existing dash : update existing
307310
elif new_dash["folderId"] == old_dash["folderId"]:
308-
if "uid" not in new_dash["dashboard"] or new_dash["dashboard"]["uid"] != old_dash["uid"]:
311+
if (
312+
"uid" not in new_dash["dashboard"]
313+
or new_dash["dashboard"]["uid"] != old_dash["uid"]
314+
or new_dash["dashboard"]["id"] != old_dash["id"]
315+
):
309316
if self.overwrite:
310-
new_dash["dashboard"]["uid"] = old_dash["uid"]
317+
if not self.keep_uid:
318+
new_dash["dashboard"]["uid"] = old_dash["uid"]
311319
new_dash["dashboard"]["id"] = old_dash["id"]
312320
else:
313321
raise GrafanaClient.GrafanaBadInputError(
314322
"Dashboard with the same title already exists in this folder with another uid. "
315323
"Use `overwrite` to permit overwriting it."
316324
)
317325
else:
318-
# force the creation of a new dashboard
319-
new_dash["dashboard"]["uid"] = None
326+
if not self.keep_uid:
327+
# force the creation of a new dashboard
328+
new_dash["dashboard"]["uid"] = None
329+
320330
new_dash["dashboard"]["id"] = None
321331
new_dash["overwrite"] = False
322332

pyproject.toml

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,48 @@ line-length = 120
55
line-length = 120
66

77
lint.select = [
8-
# Bandit
9-
"S",
10-
# Bugbear
11-
"B",
128
# Builtins
139
"A",
10+
# Bugbear
11+
"B",
1412
# comprehensions
1513
"C4",
14+
# Pycodestyle
15+
"E",
1616
# eradicate
1717
"ERA",
18-
# flake8-2020
19-
"YTT",
18+
# Pyflakes
19+
"F",
2020
# isort
2121
"I",
2222
# pandas-vet
2323
"PD",
24+
# return
25+
"RET",
26+
# Bandit
27+
"S",
2428
# print
2529
"T20",
26-
# Pycodestyle
27-
"E",
2830
"W",
29-
# Pyflakes
30-
"F",
31-
# return
32-
"RET",
31+
# flake8-2020
32+
"YTT",
3333
]
3434

3535
lint.extend-ignore = [
3636
]
3737

38-
[tool.ruff.lint.per-file-ignores]
39-
"tests/*" = [
40-
# Use of `assert` detected
41-
"S101",
42-
]
43-
"grafana_import/cli.py" = [
38+
lint.per-file-ignores."grafana_import/cli.py" = [
4439
# `print` found
4540
"T201",
4641
]
4742

48-
4943
# ===================
5044
# Tasks configuration
5145
# ===================
46+
lint.per-file-ignores."tests/*" = [
47+
# Use of `assert` detected
48+
"S101",
49+
]
5250

5351
[tool.pytest.ini_options]
5452
addopts = "-rA --verbosity=3 --cov --cov-report=term-missing --cov-report=xml"
@@ -69,14 +67,14 @@ branch = false
6967
omit = [
7068
"tests/*",
7169
]
72-
source = ["grafana_import"]
70+
source = [ "grafana_import" ]
7371

7472
[tool.coverage.report]
7573
fail_under = 0
7674
show_missing = true
7775

7876
[tool.mypy]
79-
packages = ["grafana_import"]
77+
packages = [ "grafana_import" ]
8078
install_types = true
8179
ignore_missing_imports = true
8280
implicit_optional = true

0 commit comments

Comments
 (0)