Skip to content

Commit 67e21d5

Browse files
author
Juliya Smith
authored
Gen template devices (#213)
1 parent 66be4e2 commit 67e21d5

File tree

5 files changed

+61
-8
lines changed

5 files changed

+61
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ how a consumer would use the library (e.g. adding unit tests, updating documenta
2020
- `devices list-backup-sets` to retrieve detailed info about device backup sets.
2121
- `devices bulk deactivate` to deactivate a list of devices.
2222
- `devices bulk reactivate` to reactivate a list of devices.
23+
- `devices bulk generate-template` to create a blank CSV file for bulk commands.
2324

2425
- `code42 departing-employee list` command.
2526

src/code42cli/bulk.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def write_template_file(path, columns=None, flat_item=None):
2929
)
3030

3131

32-
def generate_template_cmd_factory(group_name, commands_dict):
32+
def generate_template_cmd_factory(group_name, commands_dict, help_message=None):
3333
"""Helper function that creates a `generate-template` click command that can be added to `bulk`
3434
sub-command groups.
3535
@@ -42,8 +42,12 @@ def generate_template_cmd_factory(group_name, commands_dict):
4242
If a cmd takes a flat file, value should be a string indicating what item the flat file
4343
rows should contain.
4444
"""
45+
help_message = (
46+
help_message
47+
or "Generate the CSV template needed for bulk adding/removing users."
48+
)
4549

46-
@click.command()
50+
@click.command(help=help_message)
4751
@click.argument("cmd", type=click.Choice(list(commands_dict)))
4852
@click.option(
4953
"--path",
@@ -52,9 +56,6 @@ def generate_template_cmd_factory(group_name, commands_dict):
5256
help="Write template file to specific file path/name.",
5357
)
5458
def generate_template(cmd, path):
55-
"""\b
56-
Generate the CSV template needed for bulk adding/removing users.
57-
"""
5859
columns = commands_dict[cmd]
5960
if not path:
6061
filename = "{}_bulk_{}.csv".format(group_name, cmd.replace("-", "_"))

src/code42cli/cmds/alert_rules.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def bulk(state):
117117

118118

119119
@bulk.command(
120-
help="Bulk add users to alert rules from a csv file. CSV file format: {}".format(
120+
help="Bulk add users to alert rules from a CSV file. CSV file format: {}".format(
121121
",".join(ALERT_RULES_CSV_HEADERS)
122122
)
123123
)

src/code42cli/cmds/devices.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from py42 import exceptions
88
from py42.exceptions import Py42NotFoundError
99

10+
from code42cli.bulk import generate_template_cmd_factory
1011
from code42cli.bulk import run_bulk_process
1112
from code42cli.click_ext.groups import OrderedGroup
1213
from code42cli.click_ext.options import incompatible_with
@@ -456,8 +457,22 @@ def bulk(state):
456457
pass
457458

458459

460+
_bulk_device_activation_headers = ["guid"]
461+
462+
463+
devices_generate_template = generate_template_cmd_factory(
464+
group_name="devices",
465+
commands_dict={
466+
"reactivate": _bulk_device_activation_headers,
467+
"deactivate": _bulk_device_activation_headers,
468+
},
469+
help_message="Generate the CSV template needed for bulk device commands.",
470+
)
471+
bulk.add_command(devices_generate_template)
472+
473+
459474
@bulk.command(name="deactivate")
460-
@read_csv_arg(headers=["guid"])
475+
@read_csv_arg(headers=_bulk_device_activation_headers)
461476
@change_device_name_option
462477
@purge_date_option
463478
@format_option
@@ -488,7 +503,7 @@ def handle_row(**row):
488503

489504

490505
@bulk.command(name="reactivate")
491-
@read_csv_arg(headers=["guid"])
506+
@read_csv_arg(headers=_bulk_device_activation_headers)
492507
@format_option
493508
@sdk_options()
494509
def bulk_reactivate(state, csv_rows, format):

tests/test_bulk.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from code42cli import errors
66
from code42cli import PRODUCT_NAME
77
from code42cli.bulk import BulkProcessor
8+
from code42cli.bulk import generate_template_cmd_factory
89
from code42cli.bulk import run_bulk_process
910
from code42cli.logger import get_view_error_details_message
1011

@@ -31,6 +32,41 @@ def func_with_one_arg(sdk, profile, test1):
3132
pass
3233

3334

35+
def test_generate_template_cmd_factory_returns_expected_command():
36+
add_headers = ["foo", "bar"]
37+
remove_headers = ["test"]
38+
help_message = "HELP!"
39+
template = generate_template_cmd_factory(
40+
group_name="cmd-group",
41+
commands_dict={"add": add_headers, "remove": remove_headers},
42+
help_message=help_message,
43+
)
44+
assert template.help == help_message
45+
assert template.name == "generate-template"
46+
assert len(template.params) == 2
47+
assert template.params[0].name == "cmd"
48+
assert template.params[0].type.choices == ["add", "remove"]
49+
assert template.params[1].name == "path"
50+
51+
52+
def test_generate_template_cmd_factory_when_using_defaults_returns_expected_command():
53+
add_headers = ["foo", "bar"]
54+
remove_headers = ["test"]
55+
template = generate_template_cmd_factory(
56+
group_name="cmd-group",
57+
commands_dict={"add": add_headers, "remove": remove_headers},
58+
)
59+
assert (
60+
template.help
61+
== "Generate the CSV template needed for bulk adding/removing users."
62+
)
63+
assert template.name == "generate-template"
64+
assert len(template.params) == 2
65+
assert template.params[0].name == "cmd"
66+
assert template.params[0].type.choices == ["add", "remove"]
67+
assert template.params[1].name == "path"
68+
69+
3470
def test_run_bulk_process_calls_run(bulk_processor, bulk_processor_factory):
3571
errors.ERRORED = False
3672
run_bulk_process(func_with_one_arg, None)

0 commit comments

Comments
 (0)