def devices_list(opts): def devlist_printer(rsp): devslist = rsp.json() logging.info("Devices:") if opts.format == 'plain': for dev in devslist: attrs = repack_attrs(dev.get('attributes')) result = "" if opts.attributes: attributes = opts.attributes.split(",") for attribute in attributes: attribute = attribute.strip() if attribute == 'id': result = result + "{}={} ".format(attribute, dev['id']) elif attribute == 'updated': result = result + "{}={} ".format(attribute, dev['updated_ts']) else: result = result + "{}={} ".format(attribute, attrs.get(attribute, '<undefined>')) print(result) elif opts.format == 'json': jsonprinter(rsp) # TODO add pagination (go through all pages) url = inventory_url(opts.service, '/devices?per_page={}'.format(opts.limit)) with api_from_opts(opts) as api: do_simple_get(api, url, printer=devlist_printer)
def device_show(opts): url = inventory_url(opts.service, '/devices/{}'.format(opts.device)) with api_from_opts(opts) as api: rsp = do_simple_get(api, url) logging.debug("%r", rsp.status_code) dump_device_attributes(rsp.json())
def device_group(opts): url = inventory_url(opts.service, '/devices/{}/group'.format(opts.device)) if not opts.group_set and not opts.group_delete: with api_from_opts(opts) as api: do_simple_get(api, url) elif opts.group_set: group = { 'group': opts.group_set, } method = 'PUT' elif opts.group_delete: url = inventory_url(opts.service, '/devices/{}/group/{}'.format(opts.device, opts.group_delete)) group = { 'group': opts.group_delete, } method = 'DELETE' with api_from_opts(opts) as api: do_request(api, url, method=method, success=204, json=group)
def devices_list(opts): def devlist_printer(rsp): devslist = rsp.json() print('devices:') for dev in devslist: attrs = repack_attrs(dev.get('attributes')) print(' {} (type: {}, updated: {})'.format( dev['id'], attrs.get('device_type', '<undefined>'), dev['updated_ts'])) url = inventory_url(opts.service, '/devices') with api_from_opts(opts) as api: do_simple_get(api, url, printer=devlist_printer)
def group_show(opts): url = inventory_url(opts.service, 'groups/{}/devices'.format(opts.group)) with api_from_opts(opts) as api: do_simple_get(api, url)
def group_list(opts): url = inventory_url(opts.service, 'groups') with api_from_opts(opts) as api: do_simple_get(api, url)