Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checkin-my subcommand for snipeit #58

Merged
merged 4 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions osfv_cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ just some examples.
osfv_cli snipeit check_out --rte_ip <rte_ip_address>
```

- Check in all your assets:

```bash
osfv_cli snipeit check_in_my
```

> Replace `<rte_ip_address>` 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.
Expand Down
82 changes: 78 additions & 4 deletions osfv_cli/src/osfv/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
macpijan marked this conversation as resolved.
Show resolved Hide resolved
"""
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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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":
Expand Down
Loading