Skip to content

Commit

Permalink
Add support of balance in admin commands (#2391)
Browse files Browse the repository at this point in the history
* Add support of balance in admin commands

* Add new tests

* Add changelog
  • Loading branch information
romasku authored Oct 23, 2021
1 parent 1e078ab commit b8787bb
Show file tree
Hide file tree
Showing 21 changed files with 381 additions and 105 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.D/2391.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Added "Spent credits" to add "neuro admin get-cluster-users". Splitted 'quota' into 'balance' and 'quota':

- `neuro admin set-user-quota` changes only `max_running_jobs` quota.
- `neuro admin set-user-credits` changes only `credits` balance of user.
- `neuro admin add-user-credits` updates `credits` balance of user by delta.
33 changes: 27 additions & 6 deletions CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
* [neuro admin remove-cluster-user](#neuro-admin-remove-cluster-user)
* [neuro admin get-user-quota](#neuro-admin-get-user-quota)
* [neuro admin set-user-quota](#neuro-admin-set-user-quota)
* [neuro admin add-user-quota](#neuro-admin-add-user-quota)
* [neuro admin set-user-credits](#neuro-admin-set-user-credits)
* [neuro admin add-user-credits](#neuro-admin-add-user-credits)
* [neuro admin add-resource-preset](#neuro-admin-add-resource-preset)
* [neuro admin update-resource-preset](#neuro-admin-update-resource-preset)
* [neuro admin remove-resource-preset](#neuro-admin-remove-resource-preset)
Expand Down Expand Up @@ -228,7 +229,8 @@ Name | Description|
| _[neuro admin remove\-cluster-user](#neuro-admin-remove-cluster-user)_| Remove user access from the cluster |
| _[neuro admin get\-user-quota](#neuro-admin-get-user-quota)_| Get info about user quota in given cluster |
| _[neuro admin set\-user-quota](#neuro-admin-set-user-quota)_| Set user quota to given values |
| _[neuro admin add\-user-quota](#neuro-admin-add-user-quota)_| Add given values to user quota |
| _[neuro admin set\-user-credits](#neuro-admin-set-user-credits)_| Set user credits to given value |
| _[neuro admin add\-user-credits](#neuro-admin-add-user-credits)_| Add given values to user quota |
| _[neuro admin add\-resource-preset](#neuro-admin-add-resource-preset)_| Add new resource preset |
| _[neuro admin update\-resource-preset](#neuro-admin-update-resource-preset)_| Update existing resource preset |
| _[neuro admin remove\-resource-preset](#neuro-admin-remove-resource-preset)_| Remove resource preset |
Expand Down Expand Up @@ -405,28 +407,47 @@ neuro admin set-user-quota [OPTIONS] CLUSTER_NAME USER_NAME
Name | Description|
|----|------------|
|_--help_|Show this message and exit.|
|_\-c, --credits AMOUNT_|Maximum running jobs quota|
|_\-j, --jobs AMOUNT_|Maximum running jobs quota|




### neuro admin add-user-quota
### neuro admin set-user-credits

Set user credits to given value

**Usage:**

```bash
neuro admin set-user-credits [OPTIONS] CLUSTER_NAME USER_NAME
```

**Options:**

Name | Description|
|----|------------|
|_--help_|Show this message and exit.|
|_\-c, --credits AMOUNT_|Credits amount to set|




### neuro admin add-user-credits

Add given values to user quota

**Usage:**

```bash
neuro admin add-user-quota [OPTIONS] CLUSTER_NAME USER_NAME
neuro admin add-user-credits [OPTIONS] CLUSTER_NAME USER_NAME
```

**Options:**

Name | Description|
|----|------------|
|_--help_|Show this message and exit.|
|_\-c, --credits AMOUNT_|Maximum running jobs quota|
|_\-c, --credits AMOUNT_|Credits amount to add|



Expand Down
32 changes: 27 additions & 5 deletions neuro-cli/docs/admin.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Cluster administration commands.
| [_remove-cluster-user_](admin.md#remove-cluster-user) | Remove user access from the cluster |
| [_get-user-quota_](admin.md#get-user-quota) | Get info about user quota in given cluster |
| [_set-user-quota_](admin.md#set-user-quota) | Set user quota to given values |
| [_add-user-quota_](admin.md#add-user-quota) | Add given values to user quota |
| [_set-user-credits_](admin.md#set-user-credits) | Set user credits to given value |
| [_add-user-credits_](admin.md#add-user-credits) | Add given values to user quota |
| [_add-resource-preset_](admin.md#add-resource-preset) | Add new resource preset |
| [_update-resource-preset_](admin.md#update-resource-preset) | Update existing resource preset |
| [_remove-resource-preset_](admin.md#remove-resource-preset) | Remove resource preset |
Expand Down Expand Up @@ -219,20 +220,41 @@ Set user quota to given values
| Name | Description |
| :--- | :--- |
| _--help_ | Show this message and exit. |
| _-c, --credits AMOUNT_ | Maximum running jobs quota |
| _-j, --jobs AMOUNT_ | Maximum running jobs quota |



### add-user-quota
### set-user-credits

Set user credits to given value


#### Usage

```bash
neuro admin set-user-credits [OPTIONS] CLUSTER_NAME USER_NAME
```

Set user credits to given value

#### Options

| Name | Description |
| :--- | :--- |
| _--help_ | Show this message and exit. |
| _-c, --credits AMOUNT_ | Credits amount to set |



### add-user-credits

Add given values to user quota


#### Usage

```bash
neuro admin add-user-quota [OPTIONS] CLUSTER_NAME USER_NAME
neuro admin add-user-credits [OPTIONS] CLUSTER_NAME USER_NAME
```

Add given values to user quota
Expand All @@ -242,7 +264,7 @@ Add given values to user quota
| Name | Description |
| :--- | :--- |
| _--help_ | Show this message and exit. |
| _-c, --credits AMOUNT_ | Maximum running jobs quota |
| _-c, --credits AMOUNT_ | Credits amount to add |



Expand Down
73 changes: 51 additions & 22 deletions neuro-cli/src/neuro_cli/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
from neuro_sdk import Preset
from neuro_sdk.admin import _ClusterUserRoleType

from neuro_cli.formatters.config import BalanceFormatter

from .click_types import MEGABYTE
from .defaults import JOB_CPU_NUMBER, JOB_MEMORY_AMOUNT, PRESET_PRICE
from .formatters.admin import ClustersFormatter, ClusterUserFormatter
from .formatters.config import QuotaFormatter
from .formatters.config import AdminQuotaFormatter
from .root import Root
from .utils import argument, command, group, option

Expand Down Expand Up @@ -451,25 +453,20 @@ async def get_user_quota(
cluster_name=cluster_name,
user_name=user_name,
)
fmt = QuotaFormatter()
quota_fmt = AdminQuotaFormatter()
balance_fmt = BalanceFormatter()
root.print(
f"Quotas for [u]{rich_escape(user_with_quota.user_name)}[/u] "
f"Quota and balance for [u]{rich_escape(user_with_quota.user_name)}[/u] "
f"on cluster [u]{rich_escape(cluster_name)}[/u]:",
markup=True,
)
root.print(fmt(user_with_quota.quota))
root.print(quota_fmt(user_with_quota.quota))
root.print(balance_fmt(user_with_quota.balance))


@command()
@argument("cluster_name", required=True, type=str)
@argument("user_name", required=True, type=str)
@option(
"-c",
"--credits",
metavar="AMOUNT",
type=str,
help="Maximum running jobs quota",
)
@option(
"-j",
"--jobs",
Expand All @@ -481,20 +478,17 @@ async def set_user_quota(
root: Root,
cluster_name: str,
user_name: str,
credits: Optional[str],
jobs: Optional[int],
) -> None:
"""
Set user quota to given values
"""
credits_decimal = _parse_credits_value(credits)
user_with_quota = await root.client._admin.set_user_quota(
cluster_name=cluster_name,
user_name=user_name,
credits=credits_decimal,
total_running_jobs=jobs,
)
fmt = QuotaFormatter()
fmt = AdminQuotaFormatter()
root.print(
f"New quotas for [u]{rich_escape(user_with_quota.user_name)}[/u] "
f"on cluster [u]{rich_escape(cluster_name)}[/u]:",
Expand All @@ -511,9 +505,43 @@ async def set_user_quota(
"--credits",
metavar="AMOUNT",
type=str,
help="Maximum running jobs quota",
help="Credits amount to set",
)
async def set_user_credits(
root: Root,
cluster_name: str,
user_name: str,
credits: Optional[str],
) -> None:
"""
Set user credits to given value
"""
credits_decimal = _parse_credits_value(credits)
user_with_quota = await root.client._admin.set_user_credits(
cluster_name=cluster_name,
user_name=user_name,
credits=credits_decimal,
)
fmt = BalanceFormatter()
root.print(
f"New credits for [u]{rich_escape(user_with_quota.user_name)}[/u] "
f"on cluster [u]{rich_escape(cluster_name)}[/u]:",
markup=True,
)
root.print(fmt(user_with_quota.balance))


@command()
@argument("cluster_name", required=True, type=str)
@argument("user_name", required=True, type=str)
@option(
"-c",
"--credits",
metavar="AMOUNT",
type=str,
help="Credits amount to add",
)
async def add_user_quota(
async def add_user_credits(
root: Root,
cluster_name: str,
user_name: str,
Expand All @@ -523,18 +551,18 @@ async def add_user_quota(
Add given values to user quota
"""
additional_credits = _parse_credits_value(credits)
user_with_quota = await root.client._admin.add_user_quota(
user_with_quota = await root.client._admin.add_user_credits(
cluster_name,
user_name,
additional_credits=additional_credits,
)
fmt = QuotaFormatter()
fmt = BalanceFormatter()
root.print(
f"New quotas for [u]{rich_escape(user_with_quota.user_name)}[/u] "
f"New credits for [u]{rich_escape(user_with_quota.user_name)}[/u] "
f"on cluster [u]{rich_escape(cluster_name)}[/u]:",
markup=True,
)
root.print(fmt(user_with_quota.quota))
root.print(fmt(user_with_quota.balance))


async def _update_presets_and_fetch(root: Root, presets: Mapping[str, Preset]) -> None:
Expand Down Expand Up @@ -799,7 +827,8 @@ async def remove_resource_preset(root: Root, preset_name: str) -> None:

admin.add_command(get_user_quota)
admin.add_command(set_user_quota)
admin.add_command(add_user_quota)
admin.add_command(set_user_credits)
admin.add_command(add_user_credits)

admin.add_command(add_resource_preset)
admin.add_command(update_resource_preset)
Expand Down
4 changes: 3 additions & 1 deletion neuro-cli/src/neuro_cli/formatters/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __call__(self, clusters_users: Iterable[_ClusterUser]) -> RenderableType:
table.add_column("Full name")
table.add_column("Registered")
table.add_column("Credits")
table.add_column("Spent credits")
table.add_column("Max jobs")
rows = []

Expand All @@ -34,7 +35,8 @@ def __call__(self, clusters_users: Iterable[_ClusterUser]) -> RenderableType:
user.user_info.email,
user.user_info.full_name,
format_datetime_iso(user.user_info.created_at),
format_quota_details(user.quota.credits),
format_quota_details(user.balance.credits),
format_quota_details(user.balance.spent_credits),
format_quota_details(user.quota.total_running_jobs),
)
)
Expand Down
23 changes: 23 additions & 0 deletions neuro-cli/src/neuro_cli/formatters/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from rich.text import Text

from neuro_sdk import Cluster, Config, Preset
from neuro_sdk.admin import _Balance, _Quota
from neuro_sdk.users import Quota

from neuro_cli.utils import format_size
Expand Down Expand Up @@ -48,6 +49,26 @@ def __call__(self, quota: Quota) -> RenderableType:
)


class AdminQuotaFormatter:
def __call__(self, quota: _Quota) -> RenderableType:
jobs_details = format_quota_details(quota.total_running_jobs)
return RenderGroup(
Text.assemble(Text("Jobs", style="bold"), f": ", jobs_details),
)


class BalanceFormatter:
def __call__(self, balance: _Balance) -> RenderableType:
credits_details = format_quota_details(balance.credits)
spent_credits_details = format_quota_details(balance.spent_credits)
return RenderGroup(
Text.assemble(Text("Credits", style="bold"), f": ", credits_details),
Text.assemble(
Text("Spend credits", style="bold"), f": ", spent_credits_details
),
)


class ClustersFormatter:
def __call__(
self, clusters: Iterable[Cluster], default_name: Optional[str]
Expand Down Expand Up @@ -138,5 +159,7 @@ def __call__(self, aliases: Iterable[click.Command]) -> Table:
def format_quota_details(quota: Optional[Union[int, Decimal]]) -> str:
if quota is None:
return _QUOTA_NOT_SET
elif isinstance(quota, Decimal):
return f"{quota:.2f}"
else:
return str(quota)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Jobs: 10
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Jobs: infinity
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Jobs: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Credits: 10.00
Spend credits: 0.23
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Credits: infinity
Spend credits: 0.00
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Credits: 10.00
Spend credits: 0.33
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
╷ ╷ ╷ ╷ ╷ ╷
Name │ Role │ Email │ Full name │ Registered │ Credits │ Max jobs
╺━━━━━━━━┿━━━━━━━━━┿━━━━━━━━━━━━━━━━━━━━┿━━━━━━━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┿━━━━━━━━━━┿━━━━━━━━━━╸
alex │ user │ [email protected] │ │ │ 10 │ 2
andrew │ manager │ [email protected] │ andrew │ 2017-03-04T12:28:59.759433+00:00 │ 100 │ infinity
denis │ admin │ [email protected] │ denis admin │ 2017-03-04T12:28:59.759433+00:00 │ infinity │ infinity
ivan │ user │ [email protected] │ user │ 2017-03-04T12:28:59.759433+00:00 │ infinity │ 1
╵ ╵ ╵ ╵ ╵ ╵
╷ ╷ ╷ ╷ ╷ ╷
Name │ Role │ Email │ Full name │ Registered │ Credits │ Spent credits │ Max jobs
╺━━━━━━━━┿━━━━━━━━━┿━━━━━━━━━━━━━━━━━━━━┿━━━━━━━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┿━━━━━━━━━━┿━━━━━━━━━━━━━━━┿━━━━━━━━━━
alex │ user │ [email protected] │ │ │ 100.00 │ 20.00 │ 2
andrew │ manager │ [email protected] │ andrew │ 2017-03-04T12:28:59.759433+00:00 │ 100.00 │ 0.00 │ infinity
denis │ admin │ [email protected] │ denis admin │ 2017-03-04T12:28:59.759433+00:00 │ infinity │ 0.00 │ infinity
ivan │ user │ [email protected] │ user │ 2017-03-04T12:28:59.759433+00:00 │ infinity │ 0.00 │ 1
╵ ╵ ╵ ╵ ╵ ╵
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Credits: 10
Credits: 10.00
Jobs: 10
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Credits: 10
Credits: 10.00
Jobs: infinity
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Credits: 0
Credits: 0.00
Jobs: 0
Loading

0 comments on commit b8787bb

Please sign in to comment.