From 09c3ce2f72d9c08ad736a33dd6b447c910c13029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Go=C5=82a=C5=9B?= Date: Mon, 2 Sep 2024 12:03:13 +0200 Subject: [PATCH 1/4] src/osfv/cli/cli.py: Add checkin-my subcommand for snipeit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Filip Gołaś --- osfv_cli/src/osfv/cli/cli.py | 50 +++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/osfv_cli/src/osfv/cli/cli.py b/osfv_cli/src/osfv/cli/cli.py index b38cd88..232d5be 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,59 @@ 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): 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 ] + +# List my assets +def list_my_assets(snipeit_api, args): + 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 + + +# Check in all my assets +def check_in_my(snipeit_api, args): + 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 + + 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 +602,10 @@ 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" + ) + # RTE subcommands rte_parser.add_argument("--rte_ip", type=str, help="RTE IP address", required=True) rte_parser.add_argument( @@ -705,6 +745,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": From d0be7d5dd9023524c7bd9d5cc3ee25e96e2202a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Go=C5=82a=C5=9B?= Date: Mon, 2 Sep 2024 12:28:32 +0200 Subject: [PATCH 2/4] cli.py: Add option to skip confirmation in check_in_my MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Filip Gołaś --- osfv_cli/src/osfv/cli/cli.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/osfv_cli/src/osfv/cli/cli.py b/osfv_cli/src/osfv/cli/cli.py index 232d5be..4b0a6d4 100755 --- a/osfv_cli/src/osfv/cli/cli.py +++ b/osfv_cli/src/osfv/cli/cli.py @@ -101,10 +101,11 @@ def check_in_my(snipeit_api, args): if not list_my_assets(snipeit_api, args): return - 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 + 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: @@ -605,6 +606,9 @@ def main(): check_in_my_parser = snipeit_subparsers.add_parser( "check_in_my", help="Check in all my used assets" ) + 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) From 85b4491980fb452fe3d90474caf7fd29292dd4ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Go=C5=82a=C5=9B?= Date: Mon, 2 Sep 2024 12:36:30 +0200 Subject: [PATCH 3/4] README.md: Add check_in_my command MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Filip Gołaś --- osfv_cli/README.md | 6 ++++++ 1 file changed, 6 insertions(+) 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. From 4507066785b6bbd87bf6ada2ef4fcaf3429fe7f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Go=C5=82a=C5=9B?= Date: Tue, 3 Sep 2024 14:54:37 +0200 Subject: [PATCH 4/4] cli.py: Add docstrings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Filip Gołaś --- osfv_cli/src/osfv/cli/cli.py | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/osfv_cli/src/osfv/cli/cli.py b/osfv_cli/src/osfv/cli/cli.py index 4b0a6d4..c6babc2 100755 --- a/osfv_cli/src/osfv/cli/cli.py +++ b/osfv_cli/src/osfv/cli/cli.py @@ -63,6 +63,15 @@ def list_used_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] return [ @@ -72,8 +81,17 @@ def get_my_assets(snipeit_api): ] -# List my assets 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: @@ -88,8 +106,18 @@ def list_my_assets(snipeit_api, args): return True -# Check in all my assets 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) @@ -604,7 +632,7 @@ def main(): 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" + "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"