diff --git a/osfv_cli/README.md b/osfv_cli/README.md index 968b87f..146435d 100644 --- a/osfv_cli/README.md +++ b/osfv_cli/README.md @@ -112,6 +112,12 @@ just some examples. osfv_cli snipeit check_out --rte_ip ``` +- Check in all your assets: + + ```bash + osfv_cli snipeit check_in_my + ``` + > Replace `` with the actual RTE IP address of the asset you > want to check out. The script will identify the asset based on the RTE IP and > perform the check-out process. diff --git a/osfv_cli/src/osfv/cli/cli.py b/osfv_cli/src/osfv/cli/cli.py index b38cd88..c6babc2 100755 --- a/osfv_cli/src/osfv/cli/cli.py +++ b/osfv_cli/src/osfv/cli/cli.py @@ -39,9 +39,11 @@ def check_in_asset(snipeit_api, asset_id): if success: print(f"Asset {asset_id} successfully checked in.") + return True else: print(f"Error checking in asset {asset_id}") print(f"Response data: {data}") + return False # List used assets @@ -60,25 +62,88 @@ def list_used_assets(snipeit_api, args): print_asset_details(asset) -# List unused assets -def list_my_assets(snipeit_api, args): +def get_my_assets(snipeit_api): + """ + Gets a list of assets assigned to the current user + + Args: + snipeit_api: The API client used to interact with the Snipe-IT API. + + Returns: + List of assets assigned to the current user + """ all_assets = snipeit_api.get_all_assets() used_assets = [asset for asset in all_assets if asset["assigned_to"] is not None] - my_assets = [ + return [ asset for asset in used_assets if asset["assigned_to"]["id"] is snipeit_api.cfg_user_id ] + +def list_my_assets(snipeit_api, args): + """ + Lists all assets assigned to the current user. + + Args: + snipeit_api: The API client used to interact with the Snipe-IT API. + args: Command-line arguments object which contains the following attributes: + - json: A boolean indicating to output the list as json. + Returns: + Boolean: False if no assets were assigned to the user, True otherwise + """ + my_assets = get_my_assets(snipeit_api) + if not my_assets: print("No used assets found.") - return + return False if args.json: print(json.dumps(my_assets)) else: for asset in my_assets: print_asset_details(asset) + return True + + +def check_in_my(snipeit_api, args): + """ + Lists all assets assigned to the current user, checks in all of them + except those which are in a category listed in `categories_to_ignore`. + + Args: + snipeit_api: The API client used to interact with the Snipe-IT API. + args: Command-line arguments object which contains the following attributes: + - yes: A boolean indicating to skip the confirmation prompt. + Returns: + None + """ + categories_to_ignore = ["Employee Laptop"] + my_assets = get_my_assets(snipeit_api) + + my_assets = [ + asset + for asset in my_assets + if not set(asset["category"].values()) & set(categories_to_ignore) + ] + if not list_my_assets(snipeit_api, args): + return + + if not args.yes: + print(f"Are you sure you want to check in {len(my_assets)} assets? [y/N]") + if input() != "y": + print(f"Checking in {len(my_assets)} assets aborted.") + return + + failed = [] + for asset in my_assets: + if not check_in_asset(snipeit_api, asset["id"]): + failed = failed.append(asset) + + if failed: + print(f"Failed to check-in {len(failed)} assets:") + else: + print(f"{len(my_assets)} assets checked in successfully.") # List unused assets @@ -566,6 +631,13 @@ def main(): check_in_group.add_argument("--asset_id", type=int, help="Asset ID") check_in_group.add_argument("--rte_ip", type=str, help="RTE IP address") + check_in_my_parser = snipeit_subparsers.add_parser( + "check_in_my", help="Check in all my used assets, except work laptops" + ) + check_in_my_parser.add_argument( + "-y", "--yes", action="store_true", help="Skips the confirmation" + ) + # RTE subcommands rte_parser.add_argument("--rte_ip", type=str, help="RTE IP address", required=True) rte_parser.add_argument( @@ -705,6 +777,8 @@ def main(): check_in_asset(snipeit_api, asset_id) else: print(f"No asset found with RTE IP: {args.rte_ip}") + elif args.snipeit_cmd == "check_in_my": + check_in_my(snipeit_api, args) elif args.snipeit_cmd == "user_add": snipeit_api.user_add(args.first_name, args.last_name, args.company_name) elif args.snipeit_cmd == "user_del":