|
| 1 | +from code42cli.cmds.detectionlists.enums import BulkCommandType, DetectionLists |
| 2 | +from code42cli.cmds.detectionlists.commands import DetectionListCommandFactory, create_usage_prefix |
| 3 | +from code42cli.bulk import generate_template, create_bulk_processor |
| 4 | + |
| 5 | + |
| 6 | +_NAME = DetectionLists.HIGH_RISK |
| 7 | +_USAGE_PREFIX = create_usage_prefix(_NAME) |
| 8 | + |
| 9 | + |
| 10 | +def load_subcommands(): |
| 11 | + factory = DetectionListCommandFactory(_NAME) |
| 12 | + bulk = factory.create_bulk_command(lambda: load_bulk_subcommands(factory)) |
| 13 | + add = factory.create_add_command(add_high_risk_employee, _load_add_description) |
| 14 | + return [bulk, add] |
| 15 | + |
| 16 | + |
| 17 | +def load_bulk_subcommands(factory): |
| 18 | + generate_template_cmd = factory.create_bulk_generate_template_command(generate_csv_file) |
| 19 | + add = factory.create_bulk_add_command(bulk_add_high_risk_employees) |
| 20 | + return [generate_template_cmd, add] |
| 21 | + |
| 22 | + |
| 23 | +def generate_csv_file(cmd, path=None): |
| 24 | + """Generates a csv template a user would need to fill-in for bulk adding users to the high |
| 25 | + risk detection list.""" |
| 26 | + handler = None |
| 27 | + if cmd == BulkCommandType.ADD: |
| 28 | + handler = add_high_risk_employee |
| 29 | + generate_template(handler, path) |
| 30 | + |
| 31 | + |
| 32 | +def bulk_add_high_risk_employees(sdk, profile, csv_file): |
| 33 | + """Takes a csv file in the form `username,cloud_aliases,risk_factors,notes` with each row |
| 34 | + representing an employee and adds each employee to the high risk detection list in a bulk |
| 35 | + fashion. |
| 36 | + |
| 37 | + Args: |
| 38 | + sdk (py42.sdk.SDKClient): The py42 sdk. |
| 39 | + profile (Code42Profile): The profile under which to execute this command. |
| 40 | + csv_file (str): The path to the csv file containing rows of users. |
| 41 | + """ |
| 42 | + processor = create_bulk_processor( |
| 43 | + csv_file, lambda **kwargs: add_high_risk_employee(sdk, profile, **kwargs) |
| 44 | + ) |
| 45 | + processor.run() |
| 46 | + |
| 47 | + |
| 48 | +def add_high_risk_employee( |
| 49 | + sdk, profile, username, cloud_aliases=None, risk_factors=None, notes=None |
| 50 | +): |
| 51 | + """Adds the user with the given username to the high risk detection list. |
| 52 | + |
| 53 | + Args: |
| 54 | + sdk (py42.sdk.SDKClient): The py42 sdk. |
| 55 | + profile (Code42Profile): The profile under which to execute this command. |
| 56 | + username (str): The username for the user. |
| 57 | + cloud_aliases (iter[str]): A list of cloud aliases associated with the user. |
| 58 | + risk_factors (iter[str]): The list of risk factors associated with the user. |
| 59 | + notes (str): Notes about the user. |
| 60 | + """ |
| 61 | + |
| 62 | + |
| 63 | +def _load_add_description(argument_collection): |
| 64 | + username = argument_collection.arg_configs[u"username"] |
| 65 | + risk_factors = argument_collection.arg_configs[u"risk_factors"] |
| 66 | + username.set_help(u"A user profile ID for detection lists.") |
| 67 | + risk_factors.set_help(u"Risk factors associated with the employee.") |
0 commit comments