From 1b8b99dfc1e4cca2d16d20627d55c56fe31e7d39 Mon Sep 17 00:00:00 2001 From: Fabio Verboso Date: Thu, 27 Sep 2018 12:58:47 +0200 Subject: [PATCH] Command to list the boards of a fleet. Change-Id: Ica347ca88867e17e9b2c85c19fe54d9fc0b4bc8e --- iotronicclient/v1/fleet.py | 64 ++++++++++++++++++++++ iotronicclient/v1/fleet_shell.py | 91 ++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+) diff --git a/iotronicclient/v1/fleet.py b/iotronicclient/v1/fleet.py index f1909a9..62da4ab 100644 --- a/iotronicclient/v1/fleet.py +++ b/iotronicclient/v1/fleet.py @@ -95,3 +95,67 @@ class FleetManager(base.CreateManager): def update(self, fleet_id, patch, http_method='PATCH'): return self._update(resource_id=fleet_id, patch=patch, method=http_method) + + def boards_in_fleets(self, status=None, marker=None, limit=None, + detail=False, sort_key=None, sort_dir=None, + fields=None, + project=None, fleet=None): + """Retrieve a list of boards. + + :param marker: Optional, the UUID of a board, eg the last + board from a previous result set. Return + the next result set. + :param limit: The maximum number of results to return per + request, if: + + 1) limit > 0, the maximum number of boards to return. + 2) limit == 0, return the entire list of boards. + 3) limit param is NOT specified (None), the number of items + returned respect the maximum imposed by the Iotronic API + (see Iotronic's api.max_limit option). + + :param detail: Optional, boolean whether to return detailed information + about boards. + + :param sort_key: Optional, field used for sorting. + + :param sort_dir: Optional, direction of sorting, either 'asc' (the + default) or 'desc'. + + :param fields: Optional, a list with a specified set of fields + of the resource to be returned. Can not be used + when 'detail' is set. + + :param project: Optional string value to get + only boards of the project. + + :returns: A list of boards. + + """ + + if limit is not None: + limit = int(limit) + + if detail and fields: + raise exc.InvalidAttribute(_("Can't fetch a subset of fields " + "with 'detail' set")) + + filters = utils.common_filters(marker, limit, sort_key, sort_dir, + fields) + if project is not None: + filters.append('project=%s' % project) + if status is not None: + filters.append('status=%s' % status) + + path = fleet + '/' + + if detail: + path += 'detail' + if filters: + path += '?' + '&'.join(filters) + + if limit is None: + return self._list(self._path(path), "boards") + else: + return self._list_pagination(self._path(path), "boards", + limit=limit) diff --git a/iotronicclient/v1/fleet_shell.py b/iotronicclient/v1/fleet_shell.py index 7143a92..eca487c 100644 --- a/iotronicclient/v1/fleet_shell.py +++ b/iotronicclient/v1/fleet_shell.py @@ -116,6 +116,97 @@ def do_fleet_list(cc, args): json_flag=args.json) +@cliutils.arg( + 'fleet', + metavar='', + help="Name or UUID of the fleet ") +@cliutils.arg( + '--limit', + metavar='', + type=int, + help='Maximum number of boards to return per request, ' + '0 for no limit. Default is the maximum number used ' + 'by the Iotronic API Service.') +@cliutils.arg( + '--marker', + metavar='', + help='Board UUID (for example, of the last board in the list from ' + 'a previous request). Returns the list of boards after this UUID.') +@cliutils.arg( + '--sort-key', + metavar='', + help='Board field that will be used for sorting.') +@cliutils.arg( + '--status', + metavar='', + help='Filter by board status ') +@cliutils.arg( + '--sort-dir', + metavar='', + choices=['asc', 'desc'], + help='Sort direction: "asc" (the default) or "desc".') +@cliutils.arg( + '--project', + metavar='', + help="Project of the list.") +@cliutils.arg( + '--detail', + dest='detail', + action='store_true', + default=False, + help="Show detailed information about the boards.") +@cliutils.arg( + '--fields', + nargs='+', + dest='fields', + metavar='', + action='append', + default=[], + help="One or more board fields. Only these fields will be fetched from " + "the server. Can not be used when '--detail' is specified.") +def do_boards_in_fleets(cc, args): + """List the boards which are registered in a Iotronic Fleet.""" + fields = args.fields[0] if args.fields else None + utils.check_empty_arg(args.fleet, '') + utils.check_for_invalid_fields( + fields, res_fields.FLEET_DETAILED_RESOURCE.fields) + + params = {} + params['fleet'] = args.fleet + + if args.status: + params['status'] = args.status + + if args.project is not None: + params['project'] = args.project + + if args.detail: + fields = res_fields.BOARD_DETAILED_RESOURCE.fields + field_labels = res_fields.BOARD_DETAILED_RESOURCE.labels + elif args.fields: + utils.check_for_invalid_fields( + args.fields[0], res_fields.BOARD_DETAILED_RESOURCE.fields) + resource = res_fields.Resource(args.fields[0]) + fields = resource.fields + field_labels = resource.labels + else: + fields = res_fields.BOARD_RESOURCE.fields + field_labels = res_fields.BOARD_RESOURCE.labels + + sort_fields = res_fields.BOARD_DETAILED_RESOURCE.sort_fields + sort_field_labels = res_fields.BOARD_DETAILED_RESOURCE.sort_labels + + params.update(utils.common_params_for_list(args, + sort_fields, + sort_field_labels)) + + boards = cc.fleet.boards_in_fleets(**params) + cliutils.print_list(boards, fields, + field_labels=field_labels, + sortby_index=None, + json_flag=args.json) + + @cliutils.arg( 'name', metavar='',