Skip to content

Tenants Commands

CLI commands for managing tenants and tenant audience access.

Explicit CLI commands for tenant management.

register_cli

register_cli(app: Typer, ctx_provider: Callable[[], AppContext], logger: Logger) -> None

Register tenant management commands.

Source code in qsh/plugins/tenants/cli.py
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
def register_cli(
    app: typer.Typer, ctx_provider: Callable[[], AppContext], logger: logging.Logger
) -> None:
    """Register tenant management commands."""
    resource_app = typer.Typer(help="Manage tenants.", no_args_is_help=True)
    audiences_app = typer.Typer(help="Manage tenant audiences.", no_args_is_help=True)

    @resource_app.command("list")
    def list_cmd(
        show_all: Annotated[
            bool, typer.Option("--all", help=USER_TENANT_SHOW_ALL_PAGES_HELP)
        ] = False,
        page: Annotated[int, typer.Option("--page", help=PAGE_INDEX_HELP)] = 0,
        size: Annotated[int | None, typer.Option("--size", help=PAGE_SIZE_HELP)] = None,
        output: Annotated[
            str | None, typer.Option("-o", "--output", help=OUTPUT_FORMAT_HELP)
        ] = None,
        wide: Annotated[bool, typer.Option("--wide", help=WIDE_TABLE_HELP)] = False,
    ) -> None:
        """List tenants."""
        ctx = ctx_provider()
        logger.debug("Listing tenants: all=%s page=%s size=%s", show_all, page, size)

        client = TenantsClient(ctx)
        if show_all:
            payload = collect_pages_payload(client.iter_pages(size=DEFAULT_ALL_PAGE_SIZE))
        else:
            payload = page_to_payload(client.list_page(page=page, size=size))

        render_with_optional_schema(
            payload,
            _TENANTS_LIST_SCHEMA,
            ctx=ctx,
            output=output,
            wide=wide,
        )

    @resource_app.command("get")
    def get_cmd(
        tenant_id: Annotated[UUID, typer.Argument(help="Tenant ID")],
        output: Annotated[
            str | None, typer.Option("-o", "--output", help=OUTPUT_FORMAT_HELP)
        ] = None,
    ) -> None:
        """Get a tenant by UUID."""
        ctx = ctx_provider()
        logger.debug("Getting tenant: id=%s", tenant_id)

        result = TenantsClient(ctx).get(id=tenant_id)
        render(serialize_model(result), output or ctx.effective_output_format, wrap=ctx.wrap)

    @resource_app.command("create")
    def create_cmd(
        data: Annotated[
            str,
            typer.Argument(help="Tenant data as JSON, @file.json, file://path, or '-' for stdin"),
        ],
        output: Annotated[
            str | None, typer.Option("-o", "--output", help=OUTPUT_FORMAT_HELP)
        ] = None,
    ) -> None:
        """Create a tenant."""
        ctx = ctx_provider()
        logger.debug("Creating tenant")

        payload = load_json_like(data)
        model = validate_model_payload(payload, CreateTenantRequest, field_name="tenant body")
        result = TenantsClient(ctx).create(body=model)
        render(serialize_model(result), output or ctx.effective_output_format, wrap=ctx.wrap)

    @audiences_app.command("get")
    def audiences_get_cmd(
        tenant_id: Annotated[UUID, typer.Argument(help="Tenant ID")],
        output: Annotated[
            str | None, typer.Option("-o", "--output", help=OUTPUT_FORMAT_HELP)
        ] = None,
        wide: Annotated[bool, typer.Option("--wide", help=WIDE_TABLE_HELP)] = False,
    ) -> None:
        """Get tenant audience access."""
        ctx = ctx_provider()
        logger.debug("Getting tenant audiences: id=%s", tenant_id)

        payload = serialize_model(TenantsClient(ctx).get_audiences(id=tenant_id))
        render_with_optional_schema(
            payload,
            _AUDIENCES_SCHEMA,
            ctx=ctx,
            output=output,
            wide=wide,
        )

    @audiences_app.command("set")
    def audiences_set_cmd(
        tenant_id: Annotated[UUID, typer.Argument(help="Tenant ID")],
        data: Annotated[
            str,
            typer.Argument(
                help="Audience data as JSON, @file.json, file://path, or '-' for stdin"
            ),
        ],
        output: Annotated[
            str | None, typer.Option("-o", "--output", help=OUTPUT_FORMAT_HELP)
        ] = None,
        wide: Annotated[bool, typer.Option("--wide", help=WIDE_TABLE_HELP)] = False,
    ) -> None:
        """Set tenant audience access."""
        ctx = ctx_provider()
        logger.debug("Setting tenant audiences: id=%s", tenant_id)

        payload = load_json_like(data)
        model = validate_model_payload(payload, TenantQpuAccess, field_name="tenant audiences body")
        result = TenantsClient(ctx).set_audiences(id=tenant_id, body=model)
        render_with_optional_schema(
            serialize_model(result),
            _AUDIENCES_SCHEMA,
            ctx=ctx,
            output=output,
            wide=wide,
        )

    resource_app.add_typer(audiences_app, name="audiences")
    app.add_typer(resource_app, name="tenants")
    logger.debug("Registered tenants CLI commands")