-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfhir_upload.py
More file actions
779 lines (647 loc) · 25.8 KB
/
Copy pathfhir_upload.py
File metadata and controls
779 lines (647 loc) · 25.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "requests>=2.28.0",
# "pyyaml>=6.0",
# "click>=8.0",
# "tenacity>=8.0",
# "tabulate>=0.9.0",
# ]
# ///
"""FHIR Resource Uploader
Uploads FHIR resources from a local directory to a FHIR server
via the FHIR Gateway with Keycloak authentication.
Usage:
python fhir_upload.py [OPTIONS]
See --help for full usage, or README.md for operator guide.
"""
import json
import mimetypes
import os
import re
import subprocess
import sys
import time
from dataclasses import dataclass, field
from pathlib import Path
from typing import Optional
import click
import requests
import yaml
from tabulate import tabulate
from tenacity import Retrying, retry_if_exception_type, stop_after_attempt
# ---------------------------------------------------------------------------
# Constants
# ---------------------------------------------------------------------------
TRANSIENT_STATUS_CODES = {429, 500, 502, 503, 504}
REQUEST_TIMEOUT = 30 # seconds per HTTP request
MAX_ATTEMPTS = 4 # 1 initial + 3 retries (delays: 1s, 2s, 4s)
MIME_TYPES = {
".json": "application/json",
".html": "text/html",
".xml": "application/xml",
".txt": "text/plain",
".properties": "text/plain",
}
JUNK_EXTENSIONS = {".swp", ".tmp", ".bak"}
ENV_OVERRIDES = {
"FHIR_UPLOAD_USERNAME": "username",
"FHIR_UPLOAD_PASSWORD": "password",
"FHIR_UPLOAD_CLIENT_SECRET": "client_secret",
}
# ---------------------------------------------------------------------------
# Data classes
# ---------------------------------------------------------------------------
@dataclass
class FolderEntry:
name: str
resource_type: str | None = None
@dataclass
class AppConfig:
fhir_base_url: str
token_url: str
client_id: str
grant_type: str
username: str = ""
password: str = ""
client_secret: str = ""
resource_dir: str = "."
processing_order: list[FolderEntry] = field(default_factory=list)
@dataclass
class ResourceFile:
path: Path
folder: str
resource_type: str
resource_id: str
raw: bytes
content_type: str = "application/fhir+json"
rel_path: str = "" # path relative to resource_dir, set by discover_resources
@property
def key(self) -> str:
return f"{self.resource_type}/{self.resource_id}"
@dataclass
class UploadResult:
folder: str
resource: str
status: str # "ok" | "failed" | "skipped" | "invalid" | "dry-run"
code: Optional[int] = None
reason: str = ""
response_body: str = ""
file_path: str = "" # path relative to resource_dir (empty for invalid files)
# ---------------------------------------------------------------------------
# Config loading
# ---------------------------------------------------------------------------
def load_config(config_path: str) -> AppConfig:
path = Path(config_path)
if not path.exists():
raise click.ClickException(f"Config file not found: {config_path}")
with open(path, encoding="utf-8") as f:
raw = yaml.safe_load(f)
if not isinstance(raw, dict):
raise click.ClickException("Config file must be a YAML mapping")
kc = raw.get("keycloak") or {}
grant_type = kc.get("grant_type", "password")
processing_order = _parse_processing_order(raw.get("processing_order") or [])
cfg = AppConfig(
fhir_base_url=raw.get("fhir_base_url") or "",
token_url=kc.get("token_url") or "",
client_id=kc.get("client_id") or "",
grant_type=grant_type,
username=kc.get("username") or "",
password=kc.get("password") or "",
client_secret=kc.get("client_secret") or "",
resource_dir=str(
(Path(config_path).resolve().parent / (raw.get("resource_dir") or ".")).resolve()
),
processing_order=processing_order,
)
# Environment variable overrides take precedence over YAML values
for env_var, attr in ENV_OVERRIDES.items():
val = os.environ.get(env_var)
if val is not None:
setattr(cfg, attr, val)
_validate_config(cfg)
_warn_insecure_urls(cfg)
return cfg
def _parse_processing_order(entries: list) -> list[FolderEntry]:
result: list[FolderEntry] = []
for i, entry in enumerate(entries):
if entry is None:
click.echo(f"WARNING: Ignoring empty entry at processing_order[{i}]", err=True)
continue
if isinstance(entry, str):
result.append(FolderEntry(name=entry))
elif isinstance(entry, dict):
unknown = set(entry.keys()) - {"name", "resource_type"}
if unknown:
raise click.ClickException(
f"processing_order[{i}]: unknown keys: {', '.join(sorted(unknown))}"
)
name = entry.get("name")
if not name or not isinstance(name, str):
raise click.ClickException(
f"processing_order[{i}]: 'name' must be a non-empty string"
)
resource_type = entry.get("resource_type")
if resource_type is not None and (
not isinstance(resource_type, str) or not resource_type.strip()
):
raise click.ClickException(
f"processing_order[{i}]: 'resource_type' must be a non-empty string"
)
result.append(FolderEntry(name=name, resource_type=resource_type))
else:
raise click.ClickException(
f"processing_order[{i}]: entry must be a string or mapping, got {type(entry).__name__}"
)
return result
def slugify_id(filename: str) -> str:
stem = Path(filename).stem
s = stem.lower()
s = re.sub(r"[^a-z0-9.\-]", "-", s)
s = re.sub(r"-{2,}", "-", s)
s = s.strip("-.")
return s[:64]
def _get_mime_type(file_path: Path) -> str:
ext = file_path.suffix.lower()
if ext in MIME_TYPES:
return MIME_TYPES[ext]
guessed, _ = mimetypes.guess_type(file_path.name)
return guessed or "application/octet-stream"
def _validate_config(cfg: AppConfig) -> None:
errors = []
if not cfg.fhir_base_url:
errors.append("fhir_base_url is required")
if not cfg.token_url:
errors.append("keycloak.token_url is required")
if not cfg.client_id:
errors.append("keycloak.client_id is required")
if cfg.grant_type == "password":
if not cfg.username:
errors.append("keycloak.username is required (or set FHIR_UPLOAD_USERNAME)")
if not cfg.password:
errors.append("keycloak.password is required (or set FHIR_UPLOAD_PASSWORD)")
elif cfg.grant_type == "client_credentials":
if not cfg.client_secret:
errors.append("keycloak.client_secret is required (or set FHIR_UPLOAD_CLIENT_SECRET)")
else:
errors.append(
f"keycloak.grant_type must be 'password' or 'client_credentials', got: {cfg.grant_type!r}"
)
if not cfg.processing_order:
errors.append("processing_order must be a non-empty list")
if not Path(cfg.resource_dir).exists():
errors.append(f"resource_dir does not exist: {cfg.resource_dir!r}")
if errors:
raise click.ClickException("Config validation failed:\n " + "\n ".join(errors))
def _warn_insecure_urls(cfg: AppConfig) -> None:
localhost = ("http://localhost", "http://127.0.0.1", "http://[::1]")
for label, url in [
("fhir_base_url", cfg.fhir_base_url),
("keycloak.token_url", cfg.token_url),
]:
if url.startswith("http://") and not any(url.startswith(p) for p in localhost):
click.echo(
f"WARNING: {label} uses HTTP on a non-localhost address.\n"
" Credentials and tokens will be transmitted in the clear.",
err=True,
)
# ---------------------------------------------------------------------------
# Authentication
# ---------------------------------------------------------------------------
class AuthClient:
def __init__(self, cfg: AppConfig):
self._cfg = cfg
self.session = requests.Session()
self._refresh_token: Optional[str] = None
def _set_token(self, access_token: str, refresh_token: Optional[str]) -> None:
self.session.headers["Authorization"] = f"Bearer {access_token}"
self._refresh_token = refresh_token
def authenticate(self) -> None:
"""Obtain a fresh access token from Keycloak."""
tokens = self._post_token(self._build_grant_data())
self._set_token(tokens["access_token"], tokens.get("refresh_token"))
def refresh_or_reauthenticate(self) -> None:
"""Called on 401 -- try refresh token first, then fall back to full re-auth."""
if self._refresh_token and self._try_refresh():
return
click.echo(" Re-authenticating...", err=True)
self.authenticate()
def _try_refresh(self) -> bool:
data = {
"grant_type": "refresh_token",
"client_id": self._cfg.client_id,
"refresh_token": self._refresh_token,
}
if self._cfg.client_secret:
data["client_secret"] = self._cfg.client_secret
try:
tokens = self._post_token(data)
self._set_token(tokens["access_token"], tokens.get("refresh_token", self._refresh_token))
return True
except click.ClickException:
return False
def _build_grant_data(self) -> dict:
data: dict = {
"grant_type": self._cfg.grant_type,
"client_id": self._cfg.client_id,
}
if self._cfg.grant_type == "password":
data["username"] = self._cfg.username
data["password"] = self._cfg.password
if self._cfg.client_secret:
data["client_secret"] = self._cfg.client_secret
return data
def _post_token(self, data: dict) -> dict:
try:
resp = requests.post(self._cfg.token_url, data=data, timeout=REQUEST_TIMEOUT)
except requests.exceptions.ConnectionError:
raise click.ClickException(f"Cannot connect to Keycloak at {self._cfg.token_url}")
except requests.exceptions.Timeout:
raise click.ClickException("Timeout connecting to Keycloak")
try:
body = resp.json()
except ValueError:
raise click.ClickException(
f"Keycloak returned non-JSON response (HTTP {resp.status_code})"
)
if "error" in body:
raise click.ClickException(
f"Authentication failed: {body.get('error_description') or body['error']}"
)
if resp.status_code != 200:
raise click.ClickException(f"Authentication failed with HTTP {resp.status_code}")
return body
# ---------------------------------------------------------------------------
# Git pull
# ---------------------------------------------------------------------------
def git_pull(resource_dir: str, no_pull: bool) -> None:
if no_pull:
return
if not (Path(resource_dir) / ".git").exists():
click.echo(f"INFO: {resource_dir!r} is not a Git repository -- skipping git pull.")
return
try:
result = subprocess.run(
["git", "pull"], cwd=resource_dir, capture_output=True, text=True
)
except FileNotFoundError:
raise click.ClickException("git is not installed or not found in PATH")
if result.returncode != 0:
click.echo(result.stderr.strip() or result.stdout.strip(), err=True)
raise click.ClickException("git pull failed -- resolve the issue and re-run")
click.echo(f"git pull: {result.stdout.strip() or 'Already up to date.'}")
# ---------------------------------------------------------------------------
# Resource discovery & validation
# ---------------------------------------------------------------------------
def _is_junk_file(file_path: Path) -> bool:
if file_path.suffix.lower() in JUNK_EXTENSIONS:
return True
return any(part.startswith(".") for part in file_path.parts)
def discover_resources(
cfg: AppConfig, folder_filter: Optional[str]
) -> tuple[list[ResourceFile], list[UploadResult]]:
folders = cfg.processing_order
subpath_filter: str | None = None
if folder_filter:
# Split by first "/" to extract optional subpath
parts = folder_filter.split("/", 1)
filter_name = parts[0]
subpath_filter = parts[1] if len(parts) > 1 else None
matched = [e for e in folders if e.name == filter_name]
if not matched:
available = ", ".join(e.name for e in folders)
raise click.ClickException(
f"Folder {filter_name!r} is not in processing_order. "
f"Available: {available}"
)
folders = matched
resources: list[ResourceFile] = []
issues: list[UploadResult] = []
seen: dict[str, str] = {} # key -> path relative to resource_dir
for entry in folders:
folder_name = entry.name
folder_path = Path(cfg.resource_dir) / folder_name
if not folder_path.exists():
click.echo(f"WARNING: Folder not found: {folder_path}", err=True)
continue
# Determine which files to scan
if subpath_filter:
target = folder_path / subpath_filter
if target.is_file():
files = [target]
elif target.is_dir():
if entry.resource_type:
files = sorted(f for f in target.rglob("*.*") if not _is_junk_file(f))
else:
files = sorted(target.rglob("*.json"))
else:
raise click.ClickException(
f"Subpath '{subpath_filter}' not found in folder '{folder_name}'"
)
elif entry.resource_type:
files = sorted(f for f in folder_path.rglob("*.*") if not _is_junk_file(f))
else:
files = sorted(folder_path.rglob("*.json"))
if not files:
continue
for file_path in files:
parsed = _parse_resource_file(
file_path, folder_name, folder_path,
resource_type_override=entry.resource_type,
)
if isinstance(parsed, UploadResult):
issues.append(parsed)
continue
rel = str(file_path.relative_to(Path(cfg.resource_dir)))
if parsed.key in seen:
issues.append(UploadResult(
folder=folder_name,
resource=parsed.key,
status="skipped",
file_path=rel,
reason=f"Duplicate (first seen at {seen[parsed.key]})",
))
continue
seen[parsed.key] = rel
parsed.rel_path = rel
resources.append(parsed)
return resources, issues
def _parse_resource_file(
file_path: Path, folder_name: str, folder_path: Path,
resource_type_override: str | None = None,
) -> ResourceFile | UploadResult:
"""Parse and validate a resource file. Returns ResourceFile on success."""
display_name = str(file_path.relative_to(folder_path))
def fail(reason: str) -> UploadResult:
return UploadResult(
folder=folder_name, resource=display_name, status="invalid", reason=reason
)
try:
raw = file_path.read_bytes()
except OSError as e:
return fail(str(e))
if resource_type_override:
resource_id = slugify_id(file_path.name)
if not resource_id:
return fail("Cannot derive a valid resource ID from filename")
return ResourceFile(
path=file_path,
folder=folder_name,
resource_type=resource_type_override,
resource_id=resource_id,
raw=raw,
content_type=_get_mime_type(file_path),
)
if raw.startswith(b"\xef\xbb\xbf"): # Strip UTF-8 BOM
raw = raw[3:]
try:
data = json.loads(raw.decode("utf-8"))
except (json.JSONDecodeError, UnicodeDecodeError) as e:
return fail(f"Invalid JSON: {e}")
if not isinstance(data, dict):
return fail("JSON root must be an object")
resource_type = data.get("resourceType")
if not resource_type or not isinstance(resource_type, str):
return fail("Missing or empty 'resourceType' field")
resource_id = data.get("id")
if not resource_id or not isinstance(resource_id, str):
return fail("Missing or empty 'id' field")
return ResourceFile(
path=file_path,
folder=folder_name,
resource_type=resource_type,
resource_id=resource_id,
raw=raw,
)
# ---------------------------------------------------------------------------
# Upload
# ---------------------------------------------------------------------------
class _RetryableError(Exception):
"""Raised for transient errors that tenacity should retry."""
def __init__(self, exc_or_response, retry_after: Optional[float] = None):
self.original = exc_or_response
self.retry_after = retry_after
self.response: Optional[requests.Response] = (
exc_or_response if isinstance(exc_or_response, requests.Response) else None
)
def _tenacity_wait(retry_state) -> float:
"""Exponential backoff (1s, 2s, 4s), overridden by Retry-After header for 429."""
exc = retry_state.outcome.exception()
if isinstance(exc, _RetryableError) and exc.retry_after:
return exc.retry_after
# attempt_number is 1-indexed; gives waits of 1, 2, 4 seconds
return 2 ** (retry_state.attempt_number - 1)
def upload_resource(
resource: ResourceFile, auth: AuthClient, base_url: str, verbose: bool
) -> UploadResult:
url = f"{base_url.rstrip('/')}/{resource.resource_type}/{resource.resource_id}"
def put_once() -> requests.Response:
"""Single PUT attempt; handles one 401 re-auth inline (not a tenacity retry)."""
try:
resp = auth.session.put(
url, data=resource.raw,
headers={"Content-Type": resource.content_type},
timeout=REQUEST_TIMEOUT,
)
except requests.exceptions.RequestException as e:
raise _RetryableError(e)
if resp.status_code == 401:
auth.refresh_or_reauthenticate()
try:
resp = auth.session.put(
url, data=resource.raw,
headers={"Content-Type": resource.content_type},
timeout=REQUEST_TIMEOUT,
)
except requests.exceptions.RequestException as e:
raise _RetryableError(e)
if resp.status_code in TRANSIENT_STATUS_CODES:
retry_after: Optional[float] = None
try:
retry_after = float(resp.headers.get("Retry-After", 0)) or None
except (ValueError, TypeError):
pass
raise _RetryableError(resp, retry_after=retry_after)
return resp
try:
for attempt in Retrying(
retry=retry_if_exception_type(_RetryableError),
stop=stop_after_attempt(MAX_ATTEMPTS),
wait=_tenacity_wait,
reraise=True,
):
with attempt:
resp = put_once()
except _RetryableError as e:
if e.response is not None:
return UploadResult(
folder=resource.folder,
resource=resource.key,
status="failed",
code=e.response.status_code,
reason=e.response.reason,
response_body=e.response.text if verbose else "",
file_path=resource.rel_path,
)
return UploadResult(
folder=resource.folder,
resource=resource.key,
status="failed",
reason=str(e.original),
file_path=resource.rel_path,
)
if resp.status_code >= 400:
return UploadResult(
folder=resource.folder,
resource=resource.key,
status="failed",
code=resp.status_code,
reason=resp.reason,
response_body=resp.text if verbose else "",
file_path=resource.rel_path,
)
return UploadResult(
folder=resource.folder,
resource=resource.key,
status="ok",
code=resp.status_code,
reason=resp.reason,
file_path=resource.rel_path,
)
# ---------------------------------------------------------------------------
# Reporting
# ---------------------------------------------------------------------------
def print_summary(
results: list[UploadResult], issues: list[UploadResult], verbose: bool
) -> int:
all_results = results + issues
# Group by folder, preserving processing order
by_folder: dict[str, list[UploadResult]] = {}
for r in all_results:
by_folder.setdefault(r.folder, []).append(r)
click.echo("\nUpload Summary")
click.echo("-" * 60)
for folder_name, folder_results in by_folder.items():
click.echo(f"Folder: {folder_name}")
rows = []
for r in folder_results:
if r.status == "ok":
icon, action = "[OK]", "PUT"
detail = f"{r.code} {r.reason}"
elif r.status == "dry-run":
icon, action = "[--]", "PUT"
detail = "Would upload"
elif r.status == "skipped":
icon, action = "[!!]", "SKIP"
detail = r.reason
else: # "failed" or "invalid"
icon = "[FAIL]"
action = "PUT" if r.status == "failed" else "SKIP"
detail = f"{r.code} {r.reason}" if r.code else r.reason
rows.append([f" {icon}", action, r.resource, detail])
if r.file_path:
rows.append(["", "", f" {r.file_path}", ""])
click.echo(tabulate(rows, tablefmt="plain"))
if verbose:
for r in folder_results:
if r.response_body:
click.echo(f"\n Response body for {r.resource}:\n {r.response_body[:1000]}")
click.echo("-" * 60)
n_ok = sum(1 for r in results if r.status == "ok")
n_dry = sum(1 for r in results if r.status == "dry-run")
n_failed = sum(1 for r in results if r.status == "failed")
n_skipped = sum(1 for r in all_results if r.status == "skipped")
n_invalid = sum(1 for r in all_results if r.status == "invalid")
n_total = len(all_results)
parts = [f"Total: {n_total} files"]
if n_dry:
parts.append(f"{n_dry} would upload (dry-run)")
else:
parts.append(f"{n_ok} uploaded")
if n_failed:
parts.append(f"{n_failed} failed")
if n_skipped:
parts.append(f"{n_skipped} duplicate")
if n_invalid:
parts.append(f"{n_invalid} invalid")
click.echo(" | ".join(parts))
return 1 if n_failed > 0 or n_invalid > 0 else 0
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
@click.command()
@click.option(
"--config", "config_path",
default="upload-config.yaml", show_default=True,
help="Path to config YAML file.",
)
@click.option(
"--dry-run", is_flag=True,
help="Authenticate, validate, and show what would be uploaded -- without uploading.",
)
@click.option(
"--no-pull", is_flag=True,
help="Skip git pull before uploading.",
)
@click.option(
"--folder", "folder_filter",
default=None, metavar="FOLDER[/PATH]",
help="Upload only this folder or subpath (e.g. 'app', 'app/profiles', 'app/file.json'). Folder must be listed in processing_order.",
)
@click.option(
"--verbose", is_flag=True,
help="Show full response bodies on failure.",
)
def main(
config_path: str,
dry_run: bool,
no_pull: bool,
folder_filter: Optional[str],
verbose: bool,
) -> None:
"""Upload FHIR resources to a FHIR server via Keycloak authentication.
Reads configuration from upload-config.yaml (override with --config).
Sensitive fields (password, client_secret, username) can be supplied via
FHIR_UPLOAD_PASSWORD, FHIR_UPLOAD_CLIENT_SECRET, FHIR_UPLOAD_USERNAME
environment variables instead of the config file.
"""
cfg = load_config(config_path)
git_pull(cfg.resource_dir, no_pull)
click.echo("Scanning resources...")
resources, issues = discover_resources(cfg, folder_filter)
n_invalid = sum(1 for r in issues if r.status == "invalid")
n_dupes = sum(1 for r in issues if r.status == "skipped")
click.echo(f" Found {len(resources)} valid resource(s) to process.")
if n_invalid:
click.echo(f" {n_invalid} invalid file(s) will be skipped.", err=True)
if n_dupes:
click.echo(f" {n_dupes} duplicate(s) will be skipped.", err=True)
if not resources:
if issues:
sys.exit(print_summary([], issues, verbose))
click.echo("No resources found.")
sys.exit(0)
click.echo("Authenticating with Keycloak...")
auth = AuthClient(cfg)
auth.authenticate()
click.echo("Authentication successful.")
if dry_run:
click.echo(f"\nDry-run mode -- no uploads will be performed.")
dry_results = [
UploadResult(folder=r.folder, resource=r.key, status="dry-run", file_path=r.rel_path)
for r in resources
]
sys.exit(print_summary(dry_results, issues, verbose))
click.echo(f"\nUploading {len(resources)} resource(s)...")
results: list[UploadResult] = []
for i, resource in enumerate(resources, 1):
result = upload_resource(resource, auth, cfg.fhir_base_url, verbose)
results.append(result)
icon = "[OK]" if result.status == "ok" else "[FAIL]"
status_str = f"{result.code} {result.reason}" if result.code else result.reason
click.echo(f" [{i}/{len(resources)}] {icon} {resource.key} -> {status_str}")
sys.exit(print_summary(results, issues, verbose))
if __name__ == "__main__":
main()