|
1 | | -from cbapi.protection import CbEnterpriseProtectionAPI, Computer |
| 1 | +from cbapi.example_helpers import get_cb_protection_object, build_cli_parser |
| 2 | +from cbapi.protection.models import Computer |
2 | 3 | from collections import defaultdict |
3 | 4 | import sys |
4 | | -import argparse |
| 5 | +from six import iteritems |
5 | 6 |
|
6 | 7 |
|
7 | 8 | def main(): |
8 | | - parser = argparse.ArgumentParser("delete duplicate computers") |
9 | | - parser.add_argument("--profile", "-p", help="profile name", default="default") |
| 9 | + parser = build_cli_parser("Delete duplicate computers") |
10 | 10 | parser.add_argument("--dry-run", "-d", help="perform a dry run, don't actually delete the computers", |
11 | 11 | action="store_true", dest="dry_run") |
12 | 12 |
|
13 | 13 | args = parser.parse_args() |
14 | | - p = CbEnterpriseProtectionAPI(profile=args.profile) |
| 14 | + p = get_cb_protection_object(args) |
15 | 15 |
|
16 | 16 | computer_list = defaultdict(list) |
17 | 17 | for computer in p.select(Computer).where("deleted:false"): |
18 | 18 | computer_list[computer.name].append({"id": computer.id, "offline": computer.daysOffline}) |
19 | 19 |
|
20 | | - for computer_name, computer_ids in computer_list.iteritems(): |
| 20 | + for computer_name, computer_ids in iteritems(computer_list): |
21 | 21 | if len(computer_ids) > 1: |
22 | 22 | sorted_computers = sorted(computer_ids, key=lambda x: x["offline"], reverse=True) |
23 | 23 | for computer_id in sorted_computers[:-1]: |
24 | | - print("deleting computer id %d (offline %d days, hostname %s)" % (computer_id["id"], |
25 | | - computer_id["offline"], |
26 | | - computer_name)) |
27 | | - if not args.dry_run: |
28 | | - print("deleting from server...") |
29 | | - p.select(Computer, computer_id["id"]).delete() |
| 24 | + if computer_id["offline"] > 0: |
| 25 | + print("deleting computer id %d (offline %d days, hostname %s)" % (computer_id["id"], |
| 26 | + computer_id["offline"], |
| 27 | + computer_name)) |
| 28 | + if not args.dry_run: |
| 29 | + print("deleting from server...") |
| 30 | + p.select(Computer, computer_id["id"]).delete() |
30 | 31 |
|
31 | 32 | if __name__ == '__main__': |
32 | 33 | sys.exit(main()) |
0 commit comments