Skip to content

Commit

Permalink
fixup! Add commands to manage credits
Browse files Browse the repository at this point in the history
  • Loading branch information
tomach committed Nov 23, 2023
1 parent 8ea1db7 commit 9865e5b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
21 changes: 18 additions & 3 deletions croud/organizations/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def _transform_file_size(size_bytes):
def org_credits_create(args: Namespace) -> None:
client = Client.from_args(args)
payload = {
"amount": args.amount,
"amount": int(args.amount * 100),
"expiration_date": args.expiration_date,
"comment": args.comment,
}
Expand All @@ -287,14 +287,17 @@ def org_credits_create(args: Namespace) -> None:
],
success_message="Credit created.",
output_fmt=get_output_format(args),
transforms={
"original_amount": _transform_credits_amount_to_usd,
},
)


def org_credits_edit(args: Namespace) -> None:
client = Client.from_args(args)
payload = {}
if getattr(args, "amount") is not None:
payload["amount"] = args.amount
payload["amount"] = int(args.amount * 100)
if args.expiration_date:
payload["expiration_date"] = args.expiration_date
if args.comment:
Expand All @@ -318,6 +321,9 @@ def org_credits_edit(args: Namespace) -> None:
],
output_fmt=get_output_format(args),
success_message="Credit edited.",
transforms={
"original_amount": _transform_credits_amount_to_usd,
},
)


Expand All @@ -341,10 +347,19 @@ def org_credits_list(args: Namespace) -> None:
"status",
],
output_fmt=get_output_format(args),
success_message="Credit expired.",
transforms={
"original_amount": _transform_credits_amount_to_usd,
"remaining_amount": _transform_credits_amount_to_usd,
},
)


def _transform_credits_amount_to_usd(amount_cents):
if not amount_cents:
return 0
return f"${round(amount_cents / 100, 2)}"


def org_credits_expire(args: Namespace) -> None:
client = Client.from_args(args)
data, errors = client.delete(
Expand Down
30 changes: 15 additions & 15 deletions docs/commands/organizations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,11 @@ Example
sh$ croud organizations credits list \
--org-id f6c39580-5719-431d-a508-0cee4f9e8209
+--------------------------------------+-------------------+---------------------+------------+----------+
| id | original_amount | expiration_date | comment | status |
|--------------------------------------+-------------------+---------------------+------------+----------|
| f8207787-8458-4cab-94c1-4ca84a702154 | 300 | 2023-12-24T11:34:56 | Free Trial | ACTIVE |
+--------------------------------------+-------------------+---------------------+------------+----------+
+--------------------------------------+-----------------+------------------+---------------------+------------+----------+
| id | original_amount | remaining_amount | expiration_date | comment | status |
|--------------------------------------+-----------------+------------------+---------------------+------------+----------|
| f8207787-8458-4cab-94c1-4ca84a702154 | $300.0 | $300.0 | 2023-12-24T11:34:56 | Free Trial | ACTIVE |
+--------------------------------------+-----------------+------------------+---------------------+------------+----------+
``organizations credits create``
Expand All @@ -451,11 +451,11 @@ Example
--expiration-date 2023-12-24T12:34:56 \
--comment "Free Trial" \
--sudo
+--------------------------------------+-------------------+---------------------+------------+----------+
| id | original_amount | expiration_date | comment | status |
|--------------------------------------+-------------------+---------------------+------------+----------|
| f8207787-8458-4cab-94c1-4ca84a702154 | 300 | 2023-12-24T11:34:56 | Free Trial | ACTIVE |
+--------------------------------------+-------------------+---------------------+------------+----------+
+--------------------------------------+-----------------+---------------------+------------+----------+
| id | original_amount | expiration_date | comment | status |
|--------------------------------------+-----------------+---------------------+------------+----------|
| f8207787-8458-4cab-94c1-4ca84a702154 | $300.0 | 2023-12-24T11:34:56 | Free Trial | ACTIVE |
+--------------------------------------+-----------------+---------------------+------------+----------+
==> Success: Credit created.
.. note::
Expand All @@ -482,11 +482,11 @@ Example
--credit-id f8207787-8458-4cab-94c1-4ca84a702154
--amount 500
--sudo
+--------------------------------------+-------------------+---------------------+------------+----------+
| id | original_amount | expiration_date | comment | status |
|--------------------------------------+-------------------+---------------------+------------+----------|
| f8207787-8458-4cab-94c1-4ca84a702154 | 500 | 2023-12-24T11:34:56 | Free Trial | ACTIVE |
+--------------------------------------+-------------------+---------------------+------------+----------+
+--------------------------------------+-----------------+---------------------+------------+----------+
| id | original_amount | expiration_date | comment | status |
|--------------------------------------+-----------------+---------------------+------------+----------|
| f8207787-8458-4cab-94c1-4ca84a702154 | $500.0 | 2023-12-24T11:34:56 | Free Trial | ACTIVE |
+--------------------------------------+-----------------+---------------------+------------+----------+
==> Success: Credit edited.
.. note::
Expand Down
6 changes: 3 additions & 3 deletions tests/commands/test_organizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ def test_organizations_credits_create(mock_request):
RequestMethod.POST,
f"/api/v2/organizations/{org_id}/credits/",
body={
"amount": amount,
"amount": amount * 100,
"expiration_date": expiration_date.isoformat(),
"comment": comment,
},
Expand Down Expand Up @@ -634,7 +634,7 @@ def test_organizations_credits_edit(mock_request):
RequestMethod.PATCH,
f"/api/v2/organizations/{org_id}/credits/my_credit_id/",
body={
"amount": 234.56,
"amount": 23456,
"expiration_date": expiration_date.isoformat(),
"comment": "Starter Package",
},
Expand All @@ -660,7 +660,7 @@ def test_organizations_credits_edit_amount(mock_request):
mock_request,
RequestMethod.PATCH,
f"/api/v2/organizations/{org_id}/credits/my_credit_id/",
body={"amount": 234.56},
body={"amount": 23456},
)


Expand Down

0 comments on commit 9865e5b

Please sign in to comment.