-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquery.py
More file actions
124 lines (109 loc) · 4.03 KB
/
Copy pathquery.py
File metadata and controls
124 lines (109 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import click
from click import ClickException
from scim2_client import SCIMClientError
from scim2_models import ResourceType
from scim2_models import Schema
from scim2_models import SearchRequest
from scim2_models import ServiceProviderConfig
from sphinx_click.rst_to_ansi_formatter import make_rst_to_ansi_formatter
from scim2_cli.utils import exception_to_click_error
from .utils import DOC_URL
from .utils import formatted_payload
@click.command(cls=make_rst_to_ansi_formatter(DOC_URL), name="query")
@click.pass_context
@click.argument("resource_type", required=False)
@click.argument("id", required=False)
@click.option(
"--attribute",
multiple=True,
help="A multi-valued list of strings indicating the names of resource attributes to return in the response, overriding the set of attributes that would be returned by default.",
)
@click.option(
"--excluded-attribute",
multiple=True,
help="A multi-valued list of strings indicating the names of resource attributes to be removed from the default set of attributes to return.",
)
@click.option(
"--start-index",
type=int,
help="An integer indicating the 1-based index of the first query result.",
)
@click.option(
"--count",
type=int,
help="An integer indicating the desired maximum number of query results per page.",
)
@click.option(
"--filter", help="The filter string used to request a subset of resources."
)
@click.option(
"--sort-by",
help="A string indicating the attribute whose value SHALL be used to order the returned responses.",
)
@click.option(
"--sort-order",
help="A string indicating the order in which the “sortBy” parameter is applied.",
)
@click.option(
"--indent/--no-indent",
is_flag=True,
default=True,
help="Indent JSON response payloads.",
)
def query_cli(
ctx,
resource_type: str | None,
id: str | None,
attribute: list[str],
excluded_attribute: list[str],
start_index: int,
count: int,
filter: str,
sort_by: str,
sort_order: str,
indent: bool,
):
"""Perform a `SCIM GET <https://www.rfc-editor.org/rfc/rfc7644#section-3.4.1>`_ request on the :code:`RESOURCE_TYPE` endpoint.
- If :code:`RESOURCE_TYPE` is :code:`user` and :code:`id` is `1234`, then the request will made on the :code:`/Users/1234` endpoint.
- If :code:`RESOURCE_TYPE` is :code:`user` and :code:`id` is not set, then the request will made on the :code:`/Users` endpoint.
- If :code:`RESOURCE_TYPE` is not set, then the request will made on the :code:`/` endpoint.
Data passed in JSON format to stdin is sent as request arguments and all the other query arguments are ignored:
.. code-block:: bash
echo '{"startIndex": 50, "count": 10}' | query user
"""
for model in (Schema, ResourceType, ServiceProviderConfig):
ctx.obj["resource_models"][model.__name__.lower()] = model
if resource_type:
try:
resource_type = ctx.obj["resource_models"][resource_type]
except KeyError as exc:
ok_values = ", ".join(ctx.obj["resource_models"])
raise ClickException(
f"Unknown resource type '{resource_type}. Available values are: {ok_values}'"
) from exc
if ctx.obj.get("stdin"):
check_request_payload = False
payload = ctx.obj.get("stdin")
else:
check_request_payload = True
payload = SearchRequest(
attributes=attribute,
excluded_attributes=excluded_attribute,
start_index=start_index,
count=count,
filter=filter,
sort_by=sort_by,
sort_order=sort_order,
)
try:
response = ctx.obj["client"].query(
resource_type,
id,
search_request=payload,
check_request_payload=check_request_payload,
raise_scim_errors=False,
)
except SCIMClientError as scim_exc:
raise exception_to_click_error(scim_exc) from scim_exc
payload = formatted_payload(response.model_dump(), indent)
click.echo(payload)