|
| 1 | +from code42cli.commands import Command |
| 2 | +from code42cli.bulk import generate_template, BulkCommandType |
| 3 | +from code42cli.cmds.alerts.rules.user_rule import ( |
| 4 | + add_user, |
| 5 | + remove_user, |
| 6 | + get_rules, |
| 7 | + add_bulk_users, |
| 8 | + remove_bulk_users, |
| 9 | + show_rules, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +def _customize_add_arguments(argument_collection): |
| 14 | + rule_id = argument_collection.arg_configs[u"rule_id"] |
| 15 | + rule_id.set_help(u"Observer ID of the rule to be updated. Required.") |
| 16 | + rule_id.set_required(True) |
| 17 | + username = argument_collection.arg_configs[u"username"] |
| 18 | + username.set_help(u"The username of the user to add to the alert rule. Required.") |
| 19 | + username.set_required(True) |
| 20 | + |
| 21 | + |
| 22 | +def _customize_remove_arguments(argument_collection): |
| 23 | + rule_id = argument_collection.arg_configs[u"rule_id"] |
| 24 | + rule_id.set_help(u"Observer ID of the rule to be updated.") |
| 25 | + username = argument_collection.arg_configs[u"username"] |
| 26 | + username.set_help(u"The username of the user to remove from the alert rule.") |
| 27 | + |
| 28 | + |
| 29 | +def _customize_list_arguments(argument_collection): |
| 30 | + rule_id = argument_collection.arg_configs[u"rule_id"] |
| 31 | + rule_id.set_help(u"Observer ID of the rule.") |
| 32 | + |
| 33 | + |
| 34 | +def _customize_bulk_arguments(argument_collection): |
| 35 | + file_name = argument_collection.arg_configs[u"file_name"] |
| 36 | + file_name.set_help( |
| 37 | + u"The path to the csv file with columns 'rule_id,user_id' " |
| 38 | + u"for bulk adding users to the alert rule." |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +def _generate_template_file(cmd, path=None): |
| 43 | + """Generates a template file a user would need to fill-in for bulk operating. |
| 44 | +
|
| 45 | + Args: |
| 46 | + cmd (str or unicode): An option from the `BulkCommandType` enum specifying which type of file to |
| 47 | + generate. |
| 48 | + path (str or unicode, optional): A path to put the file after it's generated. If None, will use |
| 49 | + the current working directory. Defaults to None. |
| 50 | + """ |
| 51 | + handler = None |
| 52 | + if cmd == BulkCommandType.ADD: |
| 53 | + handler = add_user |
| 54 | + elif cmd == BulkCommandType.REMOVE: |
| 55 | + handler = remove_user |
| 56 | + |
| 57 | + generate_template(handler, path) |
| 58 | + |
| 59 | + |
| 60 | +def _load_bulk_generate_template_description(argument_collection): |
| 61 | + cmd_type = argument_collection.arg_configs[u"cmd"] |
| 62 | + cmd_type.set_help(u"The type of command the template with be used for.") |
| 63 | + cmd_type.set_choices(BulkCommandType()) |
| 64 | + |
| 65 | + |
| 66 | +class AlertRulesBulkCommands(object): |
| 67 | + @staticmethod |
| 68 | + def load_commands(): |
| 69 | + usage_prefix = u"code42 alert-rules bulk" |
| 70 | + |
| 71 | + generate_template_cmd = Command( |
| 72 | + u"generate-template", |
| 73 | + u"Generate the necessary csv template needed for bulk adding users.", |
| 74 | + u"{} generate-template <cmd> <optional args>".format(usage_prefix), |
| 75 | + handler=_generate_template_file, |
| 76 | + arg_customizer=_load_bulk_generate_template_description, |
| 77 | + ) |
| 78 | + |
| 79 | + bulk_add = Command( |
| 80 | + u"add", |
| 81 | + u"Update alert rule criteria to add users and all their aliases. " |
| 82 | + u"CSV file format: rule_id,username", |
| 83 | + u"{} add <filename>".format(usage_prefix), |
| 84 | + handler=add_bulk_users, |
| 85 | + arg_customizer=_customize_bulk_arguments, |
| 86 | + ) |
| 87 | + |
| 88 | + bulk_remove = Command( |
| 89 | + u"remove", |
| 90 | + u"Update alert rule criteria to remove users and all their aliases. " |
| 91 | + u"CSV file format: rule_id,username", |
| 92 | + u"{} remove <filename>".format(usage_prefix), |
| 93 | + handler=remove_bulk_users, |
| 94 | + arg_customizer=_customize_bulk_arguments, |
| 95 | + ) |
| 96 | + |
| 97 | + return [generate_template_cmd, bulk_add, bulk_remove] |
| 98 | + |
| 99 | + |
| 100 | +class AlertRulesCommands(object): |
| 101 | + @staticmethod |
| 102 | + def load_subcommands(): |
| 103 | + usage_prefix = u"code42 alert-rules" |
| 104 | + |
| 105 | + add = Command( |
| 106 | + u"add-user", |
| 107 | + u"Update alert rule criteria to monitor user aliases against the given username.", |
| 108 | + u"{} add-user --rule-id <id> --username <username>".format(usage_prefix), |
| 109 | + handler=add_user, |
| 110 | + arg_customizer=_customize_add_arguments, |
| 111 | + ) |
| 112 | + |
| 113 | + remove = Command( |
| 114 | + u"remove-user", |
| 115 | + u"Update alert rule criteria to remove a user and all their aliases.", |
| 116 | + u"{} remove-user <rule-id> --username <username>".format(usage_prefix), |
| 117 | + handler=remove_user, |
| 118 | + arg_customizer=_customize_remove_arguments, |
| 119 | + ) |
| 120 | + |
| 121 | + list_rules = Command( |
| 122 | + u"list", |
| 123 | + u"Fetch existing alert rules.", |
| 124 | + u"{} list".format(usage_prefix), |
| 125 | + handler=get_rules, |
| 126 | + ) |
| 127 | + |
| 128 | + show = Command( |
| 129 | + u"show", |
| 130 | + u"Fetch configured alert-rules against the rule ID.", |
| 131 | + u"{} show <rule-id>".format(usage_prefix), |
| 132 | + handler=show_rules, |
| 133 | + arg_customizer=_customize_list_arguments, |
| 134 | + ) |
| 135 | + |
| 136 | + bulk = Command( |
| 137 | + u"bulk", |
| 138 | + u"Tools for executing bulk commands.", |
| 139 | + subcommand_loader=AlertRulesBulkCommands.load_commands, |
| 140 | + ) |
| 141 | + |
| 142 | + return [add, remove, list_rules, show, bulk] |
0 commit comments