-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrisk_profiles.py
More file actions
205 lines (188 loc) · 6.03 KB
/
risk_profiles.py
File metadata and controls
205 lines (188 loc) · 6.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
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
from typing import Optional
import click
from rich.panel import Panel
from _incydr_cli import console
from _incydr_cli import logging_options
from _incydr_cli import render
from _incydr_cli.cmds.options.output_options import columns_option
from _incydr_cli.cmds.options.output_options import single_format_option
from _incydr_cli.cmds.options.output_options import SingleFormat
from _incydr_cli.cmds.options.output_options import table_format_option
from _incydr_cli.cmds.options.output_options import TableFormat
from _incydr_cli.cmds.options.profile_filter_options import profile_filter_options
from _incydr_cli.cmds.options.utils import user_lookup_callback
from _incydr_cli.cmds.utils import deprecation_warning
from _incydr_cli.core import incompatible_with
from _incydr_cli.core import IncydrCommand
from _incydr_cli.core import IncydrGroup
from _incydr_sdk.core.client import Client
from _incydr_sdk.exceptions import DateParseError
from _incydr_sdk.risk_profiles.models import RiskProfile
from _incydr_sdk.utils import model_as_card
# Deprecated September 2024.
DEPRECATION_TEXT = "DeprecationWarning: Risk Profile commands are deprecated. Use the 'incydr actors' command group instead."
@click.group(cls=IncydrGroup)
@logging_options
def risk_profiles():
"""DEPRECATED. Use the Actors command group instead. View and manage risk profiles."""
deprecation_warning(DEPRECATION_TEXT)
@risk_profiles.command("list", cls=IncydrCommand)
@table_format_option
@columns_option
@profile_filter_options
@logging_options
def list_(
format_: TableFormat,
columns: Optional[str],
manager: Optional[str],
title: Optional[str],
division: Optional[str],
department: Optional[str],
employment_type: Optional[str],
country: Optional[str],
region: Optional[str],
locality: Optional[str],
active: Optional[bool],
deleted: Optional[bool],
support_user: Optional[bool],
):
"""
List risk profiles.
"""
client = Client()
profiles = client.risk_profiles.v1.iter_all(
manager_id=manager,
title=title,
division=division,
department=department,
employment_type=employment_type,
country=country,
region=region,
locality=locality,
active=active,
deleted=deleted,
support_user=support_user,
)
if format_ == TableFormat.csv:
render.csv(RiskProfile, profiles, columns=columns, flat=True)
elif format_ == TableFormat.table:
columns = columns or [
"username",
"user_id",
"display_name",
"cloud_aliases",
"active",
"start_date",
"end_date",
]
render.table(RiskProfile, profiles, columns=columns, flat=False)
else:
printed = False
if format_ == TableFormat.json_pretty:
for p in profiles:
printed = True
console.print_json(p.json())
else:
for p in profiles:
printed = True
click.echo(p.json())
if not printed:
console.print("No results found.")
@risk_profiles.command(cls=IncydrCommand)
@click.argument("user", callback=user_lookup_callback)
@single_format_option
@logging_options
def show(
user: str,
format_: SingleFormat,
):
"""
Show details for a risk profile.
Accepts a user ID or a username. Performs an additional lookup if a username is passed.
"""
client = Client()
profile = client.risk_profiles.v1.get_risk_profile(user)
if format_ == SingleFormat.rich and client.settings.use_rich:
console.print(
Panel.fit(model_as_card(profile), title=f"Risk Profile {profile.username}")
)
elif format_ == SingleFormat.json_pretty:
console.print_json(profile.json())
else:
click.echo(profile.json())
@risk_profiles.command(cls=IncydrCommand)
@click.argument("user", callback=user_lookup_callback)
@click.option(
"--start-date",
default=None,
help="Update a user's starting date. Accepts a date in yyyy-MM-dd (UTC) format.",
)
@click.option(
"--end-date",
default=None,
help="Update a user's departure date. Accepts a date in yyyy-MM-dd (UTC) format.",
)
@click.option(
"--notes", default=None, help="Update the optional notes on a user's profile."
)
@click.option(
"--clear-start-date",
is_flag=True,
default=False,
help="Clear the start date on a user's profile. Incompatible with --start-date.",
cls=incompatible_with("start_date"),
)
@click.option(
"--clear-end-date",
is_flag=True,
default=False,
help="Clear the end date on a user's profile. Incompatible with --end-date.",
cls=incompatible_with("end_date"),
)
@click.option(
"--clear-notes",
is_flag=True,
default=False,
help="Clear the notes on a user's profile. Incompatible with --notes.",
cls=incompatible_with("notes"),
)
@logging_options
def update(
user,
start_date=None,
end_date=None,
notes=None,
clear_start_date=None,
clear_end_date=None,
clear_notes=None,
):
"""
Update a risk profile.
Accepts a user ID or a username. Performs an additional lookup if a username is passed.
"""
if not any(
[start_date, end_date, notes, clear_start_date, clear_end_date, clear_notes]
):
raise click.UsageError(
"At least one of --start-date, --end-date, or --notes, or one of their corresponding clear flags, "
"is required to update a risk profile."
)
client = Client()
if clear_start_date:
start_date = ""
if clear_end_date:
end_date = ""
if clear_notes:
notes = ""
try:
updated_profile = client.risk_profiles.v1.update(
user, notes, start_date, end_date
)
if client.settings.use_rich:
console.print(
Panel.fit(model_as_card(updated_profile), title="Updated Risk Profile")
)
else:
console.print(updated_profile.json(), highlight=False)
except DateParseError as err:
raise err