|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright 2025 NetBox Labs Inc |
| 3 | +"""CLI helper to ingest dry-run JSON messages into Diode.""" |
| 4 | + |
| 5 | +import argparse |
| 6 | +import sys |
| 7 | + |
| 8 | +from netboxlabs.diode.sdk import DiodeClient, load_dryrun_entities |
| 9 | + |
| 10 | + |
| 11 | +def main() -> None: |
| 12 | + """Ingest JSON files generated by ``DiodeDryRunClient``.""" |
| 13 | + parser = argparse.ArgumentParser( |
| 14 | + description="Load dry-run JSON messages and ingest them into Diode" |
| 15 | + ) |
| 16 | + parser.add_argument( |
| 17 | + "-t", |
| 18 | + "--target", |
| 19 | + required=True, |
| 20 | + help="gRPC target of the Diode server, e.g. grpc://localhost:8080/diode", |
| 21 | + ) |
| 22 | + parser.add_argument( |
| 23 | + "-a", |
| 24 | + "--app-name", |
| 25 | + required=True, |
| 26 | + help="Application name used when ingesting the dry-run messages", |
| 27 | + ) |
| 28 | + parser.add_argument( |
| 29 | + "-v", |
| 30 | + "--app-version", |
| 31 | + required=True, |
| 32 | + help="Application version used when ingesting the dry-run messages", |
| 33 | + ) |
| 34 | + parser.add_argument( |
| 35 | + "-c", |
| 36 | + "--client-id", |
| 37 | + help="OAuth2 client ID. Defaults to the DIODE_CLIENT_ID environment variable if not provided", |
| 38 | + ) |
| 39 | + parser.add_argument( |
| 40 | + "-k", |
| 41 | + "--client-secret", |
| 42 | + help="OAuth2 client secret. Defaults to the DIODE_CLIENT_SECRET environment variable if not provided", |
| 43 | + ) |
| 44 | + parser.add_argument( |
| 45 | + "files", |
| 46 | + nargs="+", |
| 47 | + metavar="FILE", |
| 48 | + help="Dry-run JSON files to ingest", |
| 49 | + ) |
| 50 | + |
| 51 | + args = parser.parse_args() |
| 52 | + |
| 53 | + with DiodeClient( |
| 54 | + target=args.target, |
| 55 | + app_name=args.app_name, |
| 56 | + app_version=args.app_version, |
| 57 | + client_id=args.client_id, |
| 58 | + client_secret=args.client_secret, |
| 59 | + ) as client: |
| 60 | + has_errors = False |
| 61 | + for file_path in args.files: |
| 62 | + entities = list(load_dryrun_entities(file_path)) |
| 63 | + if entities: |
| 64 | + response = client.ingest(entities=entities) |
| 65 | + if response.errors: |
| 66 | + print(f"Errors while ingesting {file_path}: {response.errors}", file=sys.stderr) |
| 67 | + has_errors = True |
| 68 | + else: |
| 69 | + print(f"Ingested {len(entities)} entities from {file_path}") |
| 70 | + if has_errors: |
| 71 | + sys.exit(1) |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + main() |
0 commit comments