|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | + |
| 4 | +import sys |
| 5 | +from cbapi.response.models import SensorGroup, Site |
| 6 | +from cbapi.example_helpers import build_cli_parser, get_cb_response_object |
| 7 | +from cbapi.errors import ServerError |
| 8 | +import logging |
| 9 | + |
| 10 | +log = logging.getLogger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +def list_sensor_groups(cb, parser, args): |
| 14 | + for g in cb.select(SensorGroup): |
| 15 | + print(g) |
| 16 | + print("") |
| 17 | + |
| 18 | + |
| 19 | +def add_sensor_group(cb, parser, args): |
| 20 | + g = cb.create(SensorGroup) |
| 21 | + |
| 22 | + if args.site_name: |
| 23 | + site = cb.select(Site).where("name:{0}".format(args.site_name)).first() |
| 24 | + if not site: |
| 25 | + print("Could not find site named {0}".format(args.site_name)) |
| 26 | + return 1 |
| 27 | + elif args.site_id: |
| 28 | + site = cb.select(Site).where("id:{0}".format(args.site_id)).first() |
| 29 | + if not site: |
| 30 | + print("Could not find site ID {0}".format(args.site_id)) |
| 31 | + return 1 |
| 32 | + else: |
| 33 | + # Just pick the first site by the lowest ID |
| 34 | + site = min(cb.select(Site), key=lambda x: x.id) |
| 35 | + |
| 36 | + g.name = args.new_group_name |
| 37 | + g.site = site |
| 38 | + |
| 39 | + try: |
| 40 | + g.save() |
| 41 | + except ServerError as se: |
| 42 | + print("Received HTTP error {0} when attempting to add sensor group: {1}".format(se.error_code, str(se))) |
| 43 | + except Exception as e: |
| 44 | + print("Could not add sensor group: {0:s}".format(str(e))) |
| 45 | + else: |
| 46 | + log.debug("New Sensor Group: {0:s}".format(str(g))) |
| 47 | + print("Added sensor group. New sensor group ID is {0:d}".format(g.id)) |
| 48 | + |
| 49 | + |
| 50 | +def delete_sensor_group(cb, parser, args): |
| 51 | + try: |
| 52 | + if args.id: |
| 53 | + attempted_to_find = "ID of {0:d}".format(args.id) |
| 54 | + groups = [cb.select(SensorGroup, args.id, force_init=True)] |
| 55 | + else: |
| 56 | + attempted_to_find = "name {0:s}".format(args.groupname) |
| 57 | + groups = cb.select(SensorGroup).where("name:{0:s}".format(args.groupname))[::] |
| 58 | + if not len(groups): |
| 59 | + raise Exception("No sensor groups match") |
| 60 | + except Exception as e: |
| 61 | + print("Could not find sensor group with {0:s}: {1:s}".format(attempted_to_find, str(e))) |
| 62 | + return |
| 63 | + |
| 64 | + num_matching_sensor_groups = len(groups) |
| 65 | + if num_matching_sensor_groups > 1 and not args.force: |
| 66 | + print("{0:d} sensor groups match {1:s} and --force not specified. No action taken.".format(num_matching_sensor_groups, |
| 67 | + attempted_to_find)) |
| 68 | + return |
| 69 | + |
| 70 | + for g in groups: |
| 71 | + try: |
| 72 | + g.delete() |
| 73 | + except Exception as e: |
| 74 | + print("Could not delete sensor group with {0:s}: {1:s}".format(attempted_to_find, str(e))) |
| 75 | + else: |
| 76 | + print("Deleted sensor group id {0:d} with name {1:s}".format(g.id, g.name)) |
| 77 | + |
| 78 | + |
| 79 | +def main(): |
| 80 | + parser = build_cli_parser() |
| 81 | + commands = parser.add_subparsers(help="Sensor Group commands", dest="command_name") |
| 82 | + |
| 83 | + list_command = commands.add_parser("list", help="List all configured sensor groups") |
| 84 | + |
| 85 | + add_command = commands.add_parser("add", help="Add new sensor group") |
| 86 | + add_command.add_argument("-n", "--name", action="store", help="Sensor group name", required=True, |
| 87 | + dest="new_group_name") |
| 88 | + site_group = add_command.add_mutually_exclusive_group(required=False) |
| 89 | + site_group.add_argument("-s", "--site", action="store", help="Site name", dest="site_name") |
| 90 | + site_group.add_argument("-i", "--site-id", action="store", help="Site ID", dest="site_id") |
| 91 | + |
| 92 | + del_command = commands.add_parser("delete", help="Delete sensor groups") |
| 93 | + del_sensor_group_specifier = del_command.add_mutually_exclusive_group(required=True) |
| 94 | + del_sensor_group_specifier.add_argument("-i", "--id", type=int, help="ID of sensor group to delete") |
| 95 | + del_sensor_group_specifier.add_argument("-n", "--name", help="Name of sensor group to delete. Specify --force to delete" |
| 96 | + " multiple sensor groups that have the same name", dest="groupname") |
| 97 | + del_command.add_argument("--force", help="If NAME matches multiple sensor groups, delete all matching sensor groups", |
| 98 | + action="store_true", default=False) |
| 99 | + |
| 100 | + args = parser.parse_args() |
| 101 | + cb = get_cb_response_object(args) |
| 102 | + |
| 103 | + if args.command_name == "list": |
| 104 | + return list_sensor_groups(cb, parser, args) |
| 105 | + elif args.command_name == "add": |
| 106 | + return add_sensor_group(cb, parser, args) |
| 107 | + elif args.command_name == "delete": |
| 108 | + return delete_sensor_group(cb, parser, args) |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + sys.exit(main()) |
0 commit comments