|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +import click |
| 4 | +from rich.panel import Panel |
| 5 | + |
| 6 | +from _incydr_cli import console |
| 7 | +from _incydr_cli import logging_options |
| 8 | +from _incydr_cli import render |
| 9 | +from _incydr_cli.cmds.options.output_options import columns_option |
| 10 | +from _incydr_cli.cmds.options.output_options import single_format_option |
| 11 | +from _incydr_cli.cmds.options.output_options import SingleFormat |
| 12 | +from _incydr_cli.cmds.options.output_options import table_format_option |
| 13 | +from _incydr_cli.cmds.options.output_options import TableFormat |
| 14 | +from _incydr_cli.core import IncydrCommand |
| 15 | +from _incydr_cli.core import IncydrGroup |
| 16 | +from _incydr_sdk.core.client import Client |
| 17 | +from _incydr_sdk.orgs.models import Org |
| 18 | +from _incydr_sdk.utils import model_as_card |
| 19 | + |
| 20 | + |
| 21 | +@click.group(cls=IncydrGroup) |
| 22 | +@logging_options |
| 23 | +def orgs(): |
| 24 | + """View and manage orgs.""" |
| 25 | + |
| 26 | + |
| 27 | +@orgs.command("activate", cls=IncydrCommand) |
| 28 | +@click.argument("org_guid") |
| 29 | +@logging_options |
| 30 | +def activate_org(org_guid: str): |
| 31 | + """ |
| 32 | + Activate the given org. |
| 33 | + """ |
| 34 | + client = Client() |
| 35 | + client.orgs.v1.activate(org_guid) |
| 36 | + console.print(f"Org '{org_guid}' successfully activated.") |
| 37 | + |
| 38 | + |
| 39 | +@orgs.command("list", cls=IncydrCommand) |
| 40 | +@click.option( |
| 41 | + "--active/--inactive", |
| 42 | + default=None, |
| 43 | + help="Filter by active or inactive orgs. Defaults to returning both when when neither option is passed.", |
| 44 | +) |
| 45 | +@table_format_option |
| 46 | +@columns_option |
| 47 | +@logging_options |
| 48 | +def list_( |
| 49 | + active: Optional[bool], |
| 50 | + format_: TableFormat, |
| 51 | + columns: Optional[str], |
| 52 | +): |
| 53 | + """ |
| 54 | + List orgs. |
| 55 | + """ |
| 56 | + client = Client() |
| 57 | + orgs_ = client.orgs.v1.list(active=active).orgs |
| 58 | + |
| 59 | + if format_ == TableFormat.csv: |
| 60 | + render.csv(Org, orgs_, columns=columns, flat=True) |
| 61 | + elif format_ == TableFormat.table: |
| 62 | + render.table(Org, orgs_, columns=columns, flat=False) |
| 63 | + elif format_ == TableFormat.json_pretty: |
| 64 | + for item in orgs_: |
| 65 | + console.print_json(item.json()) |
| 66 | + else: |
| 67 | + for item in orgs_: |
| 68 | + click.echo(item.json()) |
| 69 | + |
| 70 | + |
| 71 | +@orgs.command("create", cls=IncydrCommand) |
| 72 | +@click.argument("name") |
| 73 | +@click.option( |
| 74 | + "--external-reference", |
| 75 | + default=None, |
| 76 | + help="The external reference string for the org. Defaults to None.", |
| 77 | +) |
| 78 | +@click.option( |
| 79 | + "--notes", |
| 80 | + default=None, |
| 81 | + help="The notes string for the org. Defaults to None.", |
| 82 | +) |
| 83 | +@click.option( |
| 84 | + "--parent-org-guid", |
| 85 | + default=None, |
| 86 | + help="The org guid for the created org's parent. Defaults to your tenant's parent org.", |
| 87 | +) |
| 88 | +@single_format_option |
| 89 | +@columns_option |
| 90 | +@logging_options |
| 91 | +def create( |
| 92 | + name: str, |
| 93 | + external_reference: Optional[str], |
| 94 | + notes: Optional[str], |
| 95 | + parent_org_guid: Optional[str], |
| 96 | + format_: TableFormat, |
| 97 | + columns: Optional[str], |
| 98 | +): |
| 99 | + """ |
| 100 | + Create a new org. |
| 101 | + """ |
| 102 | + client = Client() |
| 103 | + org = client.orgs.v1.create( |
| 104 | + org_name=name, |
| 105 | + org_ext_ref=external_reference, |
| 106 | + parent_org_guid=parent_org_guid, |
| 107 | + notes=notes, |
| 108 | + ) |
| 109 | + |
| 110 | + if format_ == SingleFormat.rich and client.settings.use_rich: |
| 111 | + console.print(Panel.fit(model_as_card(org), title=f"Org {org.org_name}")) |
| 112 | + elif format_ == SingleFormat.json_pretty: |
| 113 | + console.print_json(org.json()) |
| 114 | + else: |
| 115 | + click.echo(org.json()) |
| 116 | + |
| 117 | + |
| 118 | +@orgs.command("deactivate", cls=IncydrCommand) |
| 119 | +@click.argument("org_guid") |
| 120 | +@logging_options |
| 121 | +def deactivate_org(org_guid: str): |
| 122 | + """ |
| 123 | + Deactivate the given org. |
| 124 | + """ |
| 125 | + client = Client() |
| 126 | + client.orgs.v1.deactivate(org_guid) |
| 127 | + console.print(f"Org '{org_guid}' successfully deactivated.") |
| 128 | + |
| 129 | + |
| 130 | +@orgs.command("show", cls=IncydrCommand) |
| 131 | +@click.argument("org_guid") |
| 132 | +@single_format_option |
| 133 | +@columns_option |
| 134 | +@logging_options |
| 135 | +def show( |
| 136 | + org_guid: str, |
| 137 | + format_: TableFormat, |
| 138 | + columns: Optional[str], |
| 139 | +): |
| 140 | + """ |
| 141 | + View details of an org. |
| 142 | + """ |
| 143 | + client = Client() |
| 144 | + org = client.orgs.v1.get_org(org_guid=org_guid) |
| 145 | + |
| 146 | + if format_ == SingleFormat.rich and client.settings.use_rich: |
| 147 | + console.print(Panel.fit(model_as_card(org), title=f"Org: {org.org_name}")) |
| 148 | + elif format_ == SingleFormat.json_pretty: |
| 149 | + console.print_json(org.json()) |
| 150 | + else: |
| 151 | + click.echo(org.json()) |
| 152 | + |
| 153 | + |
| 154 | +@orgs.command("update", cls=IncydrCommand) |
| 155 | +@click.argument("org_guid") |
| 156 | +@click.option( |
| 157 | + "--name", help="The name of the org being updated. Required.", required=True |
| 158 | +) |
| 159 | +@click.option( |
| 160 | + "--external-reference", |
| 161 | + default=None, |
| 162 | + help="The external reference string for the org. Defaults to None.", |
| 163 | +) |
| 164 | +@click.option( |
| 165 | + "--notes", |
| 166 | + default=None, |
| 167 | + help="The notes string for the org. Defaults to None.", |
| 168 | +) |
| 169 | +@single_format_option |
| 170 | +@columns_option |
| 171 | +@logging_options |
| 172 | +def update( |
| 173 | + org_guid: str, |
| 174 | + name: str, |
| 175 | + external_reference: Optional[str], |
| 176 | + notes: Optional[str], |
| 177 | + format_: TableFormat, |
| 178 | + columns: Optional[str], |
| 179 | +): |
| 180 | + """ |
| 181 | + Update an org. |
| 182 | + """ |
| 183 | + client = Client() |
| 184 | + org = client.orgs.v1.update( |
| 185 | + org_guid=org_guid, org_name=name, org_ext_ref=external_reference, notes=notes |
| 186 | + ) |
| 187 | + |
| 188 | + if format_ == SingleFormat.rich and client.settings.use_rich: |
| 189 | + console.print(Panel.fit(model_as_card(org), title=f"Org {org.org_name}")) |
| 190 | + elif format_ == SingleFormat.json_pretty: |
| 191 | + console.print_json(org.json()) |
| 192 | + else: |
| 193 | + click.echo(org.json()) |
0 commit comments