|
| 1 | +import typer |
| 2 | +import json |
| 3 | +from typing_extensions import Annotated |
| 4 | +from cortexapps_cli.utils import print_output_with_context |
| 5 | +from cortexapps_cli.command_options import ListCommandOptions |
| 6 | + |
| 7 | +app = typer.Typer( |
| 8 | + help="Secrets commands", |
| 9 | + no_args_is_help=True |
| 10 | +) |
| 11 | + |
| 12 | +@app.command() |
| 13 | +def list( |
| 14 | + ctx: typer.Context, |
| 15 | + page: ListCommandOptions.page = None, |
| 16 | + page_size: ListCommandOptions.page_size = 250, |
| 17 | + table_output: ListCommandOptions.table_output = False, |
| 18 | + csv_output: ListCommandOptions.csv_output = False, |
| 19 | + columns: ListCommandOptions.columns = [], |
| 20 | + no_headers: ListCommandOptions.no_headers = False, |
| 21 | + filters: ListCommandOptions.filters = [], |
| 22 | + sort: ListCommandOptions.sort = [], |
| 23 | +): |
| 24 | + """ |
| 25 | + List secrets |
| 26 | + """ |
| 27 | + client = ctx.obj["client"] |
| 28 | + |
| 29 | + params = { |
| 30 | + "page": page, |
| 31 | + "pageSize": page_size |
| 32 | + } |
| 33 | + |
| 34 | + if (table_output or csv_output) and not ctx.params.get('columns'): |
| 35 | + ctx.params['columns'] = [ |
| 36 | + "ID=id", |
| 37 | + "Name=name", |
| 38 | + "Tag=tag", |
| 39 | + ] |
| 40 | + |
| 41 | + # remove any params that are None |
| 42 | + params = {k: v for k, v in params.items() if v is not None} |
| 43 | + |
| 44 | + if page is None: |
| 45 | + r = client.fetch("api/v1/secrets", params=params) |
| 46 | + else: |
| 47 | + r = client.get("api/v1/secrets", params=params) |
| 48 | + print_output_with_context(ctx, r) |
| 49 | + |
| 50 | +@app.command() |
| 51 | +def get( |
| 52 | + ctx: typer.Context, |
| 53 | + tag_or_id: str = typer.Option(..., "--tag-or-id", "-t", help="Secret tag or ID"), |
| 54 | +): |
| 55 | + """ |
| 56 | + Get a secret by tag or ID |
| 57 | + """ |
| 58 | + client = ctx.obj["client"] |
| 59 | + r = client.get(f"api/v1/secrets/{tag_or_id}") |
| 60 | + print_output_with_context(ctx, r) |
| 61 | + |
| 62 | +@app.command() |
| 63 | +def create( |
| 64 | + ctx: typer.Context, |
| 65 | + file_input: Annotated[typer.FileText, typer.Option("--file", "-f", help="File containing secret definition (name, secret, tag); can be passed as stdin with -, example: -f-")] = ..., |
| 66 | +): |
| 67 | + """ |
| 68 | + Create a secret |
| 69 | +
|
| 70 | + Provide a JSON file with the secret definition including required fields: |
| 71 | + - name: human-readable label for the secret |
| 72 | + - secret: the actual secret value |
| 73 | + - tag: unique identifier for the secret |
| 74 | + """ |
| 75 | + client = ctx.obj["client"] |
| 76 | + data = json.loads("".join([line for line in file_input])) |
| 77 | + r = client.post("api/v1/secrets", data=data) |
| 78 | + print_output_with_context(ctx, r) |
| 79 | + |
| 80 | +@app.command() |
| 81 | +def update( |
| 82 | + ctx: typer.Context, |
| 83 | + tag_or_id: str = typer.Option(..., "--tag-or-id", "-t", help="Secret tag or ID"), |
| 84 | + file_input: Annotated[typer.FileText, typer.Option("--file", "-f", help="File containing fields to update (name, secret); can be passed as stdin with -, example: -f-")] = ..., |
| 85 | +): |
| 86 | + """ |
| 87 | + Update a secret |
| 88 | +
|
| 89 | + Provide a JSON file with the fields to update (name and/or secret are optional). |
| 90 | + """ |
| 91 | + client = ctx.obj["client"] |
| 92 | + data = json.loads("".join([line for line in file_input])) |
| 93 | + r = client.put(f"api/v1/secrets/{tag_or_id}", data=data) |
| 94 | + print_output_with_context(ctx, r) |
| 95 | + |
| 96 | +@app.command() |
| 97 | +def delete( |
| 98 | + ctx: typer.Context, |
| 99 | + tag_or_id: str = typer.Option(..., "--tag-or-id", "-t", help="Secret tag or ID"), |
| 100 | +): |
| 101 | + """ |
| 102 | + Delete a secret |
| 103 | + """ |
| 104 | + client = ctx.obj["client"] |
| 105 | + client.delete(f"api/v1/secrets/{tag_or_id}") |
0 commit comments