|
| 1 | +import itertools |
| 2 | +from typing import Iterator |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +import click |
| 6 | + |
| 7 | +from _incydr_cli import console |
| 8 | +from _incydr_cli import logging_options |
| 9 | +from _incydr_cli import render |
| 10 | +from _incydr_cli.cmds.options.output_options import columns_option |
| 11 | +from _incydr_cli.cmds.options.output_options import table_format_option |
| 12 | +from _incydr_cli.cmds.options.output_options import TableFormat |
| 13 | +from _incydr_cli.core import IncydrCommand |
| 14 | +from _incydr_cli.core import IncydrGroup |
| 15 | +from _incydr_sdk.core.client import Client |
| 16 | +from _incydr_sdk.risk_indicator_categories.models import RiskIndicator |
| 17 | + |
| 18 | + |
| 19 | +@click.group(cls=IncydrGroup) |
| 20 | +@logging_options |
| 21 | +def risk_indicator_categories(): |
| 22 | + """View and manage risk indicators.""" |
| 23 | + |
| 24 | + |
| 25 | +@risk_indicator_categories.command("list", cls=IncydrCommand) |
| 26 | +@table_format_option |
| 27 | +@columns_option |
| 28 | +@logging_options |
| 29 | +def list_categories( |
| 30 | + format_: Optional[TableFormat] = None, |
| 31 | + columns: Optional[str] = None, |
| 32 | +): |
| 33 | + """ |
| 34 | + List Risk Indicators by category and subcategory. |
| 35 | + """ |
| 36 | + client = Client() |
| 37 | + categories = client.risk_indicator_categories.v1.list_categories().categories |
| 38 | + |
| 39 | + if format_ == TableFormat.table: |
| 40 | + columns = columns or [ |
| 41 | + "id", |
| 42 | + "name", |
| 43 | + "description", |
| 44 | + "category_name", |
| 45 | + "category_id", |
| 46 | + "subcategory_name", |
| 47 | + "subcategory_id", |
| 48 | + "type", |
| 49 | + ] |
| 50 | + render.table( |
| 51 | + RiskIndicatorTableEntry, |
| 52 | + iter_risk_indicator_table_entries(categories), |
| 53 | + columns=columns, |
| 54 | + flat=False, |
| 55 | + ) |
| 56 | + elif format_ == TableFormat.csv: |
| 57 | + render.csv( |
| 58 | + RiskIndicatorTableEntry, |
| 59 | + iter_risk_indicator_table_entries(categories), |
| 60 | + columns=columns, |
| 61 | + flat=True, |
| 62 | + ) |
| 63 | + else: |
| 64 | + printed = False |
| 65 | + for indicator in iter_risk_indicator_table_entries(categories): |
| 66 | + printed = True |
| 67 | + if format_ == TableFormat.json_pretty: |
| 68 | + console.print_json(indicator.json()) |
| 69 | + else: |
| 70 | + click.echo(indicator.json()) |
| 71 | + if not printed: |
| 72 | + console.print("No results found.") |
| 73 | + |
| 74 | + |
| 75 | +class RiskIndicatorTableEntry(RiskIndicator): |
| 76 | + category_name: str |
| 77 | + category_id: str |
| 78 | + category_description: Optional[str] |
| 79 | + subcategory_name: str |
| 80 | + subcategory_id: str |
| 81 | + subcategory_description: Optional[str] |
| 82 | + type: str |
| 83 | + |
| 84 | + |
| 85 | +def iter_risk_indicator_table_entries(categories) -> Iterator[RiskIndicatorTableEntry]: |
| 86 | + for category in categories: |
| 87 | + for subcategory in category.subcategories: |
| 88 | + for indicator, indicator_type in itertools.chain( |
| 89 | + ((i, "standard") for i in subcategory.standard_indicators), |
| 90 | + ((i, "custom") for i in subcategory.custom_indicators), |
| 91 | + ): |
| 92 | + yield RiskIndicatorTableEntry( |
| 93 | + id=indicator.id, |
| 94 | + name=indicator.name, |
| 95 | + description=indicator.description, |
| 96 | + category_name=category.name, |
| 97 | + category_id=category.id, |
| 98 | + category_description=category.description, |
| 99 | + subcategory_name=subcategory.name, |
| 100 | + subcategory_id=subcategory.id, |
| 101 | + subcategory_description=subcategory.description, |
| 102 | + type=indicator_type, |
| 103 | + ) |
0 commit comments