Skip to content
This repository was archived by the owner on Sep 22, 2023. It is now read-only.

Commit 4899ed3

Browse files
authored
feat: Add support for group query by name (#170)
1 parent 8918de1 commit 4899ed3

File tree

3 files changed

+116
-16
lines changed

3 files changed

+116
-16
lines changed

changes/170.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add `admin groups-by-name` to resolve group information by the name

src/ai/backend/client/cli/admin/groups.py

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,68 @@ def group(gid):
3030
]
3131
with Session() as session:
3232
try:
33-
resp = session.Group.detail(gid=gid,
34-
fields=(item[1] for item in fields))
33+
item = session.Group.detail(
34+
gid=gid,
35+
fields=(item[1] for item in fields),
36+
)
3537
except Exception as e:
3638
print_error(e)
3739
sys.exit(1)
3840
rows = []
39-
if resp is None:
40-
print('There is no such group.')
41+
if item is None:
42+
print_fail('There is no such group.')
4143
sys.exit(1)
4244
for name, key in fields:
43-
if key in resp:
44-
rows.append((name, resp[key]))
45+
if key in item:
46+
rows.append((name, item[key]))
4547
print(tabulate(rows, headers=('Field', 'Value')))
4648

4749

50+
@admin.command()
51+
@click.argument('name', type=str)
52+
def groups_by_name(name: str):
53+
'''
54+
Show the information about the group(s) having the given name.
55+
Two or more groups in different domains may have the same name,
56+
so this may print out information of multiple groups if queried
57+
by a superadmin.
58+
59+
\b
60+
name: Group name.
61+
'''
62+
fields = [
63+
('ID', 'id'),
64+
('Name', 'name'),
65+
('Domain', 'domain_name'),
66+
('Description', 'description'),
67+
('Active?', 'is_active'),
68+
('Created At', 'created_at'),
69+
('Total Resource Slots', 'total_resource_slots'),
70+
('Allowed vFolder Hosts', 'allowed_vfolder_hosts'),
71+
]
72+
with Session() as session:
73+
try:
74+
items = session.Group.from_name(
75+
name,
76+
fields=(item[1] for item in fields),
77+
)
78+
except Exception as e:
79+
print_error(e)
80+
sys.exit(1)
81+
rows = []
82+
if not items:
83+
print_fail('There is no such group.')
84+
sys.exit(1)
85+
show_splitter = (len(items) > 1)
86+
for item_idx, item in enumerate(items):
87+
if show_splitter and item_idx > 0:
88+
print("=" * 40)
89+
for name, key in fields:
90+
if key in item:
91+
rows.append((name, item[key]))
92+
print(tabulate(rows, headers=('Field', 'Value')))
93+
94+
4895
@admin.group(invoke_without_command=True)
4996
@click.pass_context
5097
@click.option('-d', '--domain-name', type=str, default=None,
@@ -65,6 +112,7 @@ def groups(ctx, domain_name):
65112
('Created At', 'created_at'),
66113
('Total Resource Slots', 'total_resource_slots'),
67114
('Allowed vFolder Hosts', 'allowed_vfolder_hosts'),
115+
('Allowed scaling groups', 'scaling_groups'),
68116
]
69117
with Session() as session:
70118
try:
@@ -74,7 +122,7 @@ def groups(ctx, domain_name):
74122
print_error(e)
75123
sys.exit(1)
76124
if len(resp) < 1:
77-
print('There is no group.')
125+
print_fail('There is no group.')
78126
return
79127
fields = [field for field in fields if field[1] in resp[0]]
80128
print(tabulate((item.values() for item in resp),

src/ai/backend/client/func/group.py

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@
88
'Group',
99
)
1010

11+
_default_list_fields = (
12+
'id',
13+
'name',
14+
'is_active',
15+
'created_at',
16+
'integration_id',
17+
)
18+
_default_detail_fields = (
19+
'id',
20+
'name',
21+
'description',
22+
'is_active',
23+
'created_at',
24+
'domain_name',
25+
'total_resource_slots',
26+
'allowed_vfolder_hosts',
27+
'integration_id',
28+
)
29+
1130

1231
class Group(BaseFunction):
1332
"""
@@ -22,19 +41,52 @@ class Group(BaseFunction):
2241

2342
@api_function
2443
@classmethod
25-
async def list(cls, domain_name: str,
26-
fields: Iterable[str] = None) -> Sequence[dict]:
44+
async def from_name(
45+
cls,
46+
name: str,
47+
*,
48+
fields: Iterable[str] = None,
49+
domain_name: str = None,
50+
) -> Sequence[dict]:
51+
"""
52+
Find the group(s) by its name.
53+
It may return multiple groups when there are groups with the same name
54+
in different domains and it is invoked with a super-admin account
55+
without setting the domain name.
56+
57+
:param domain_name: Name of domain to get groups from.
58+
:param fields: Per-group query fields to fetch.
59+
"""
60+
if fields is None:
61+
fields = _default_detail_fields
62+
query = textwrap.dedent("""\
63+
query($name: String!, $domain_name: String) {
64+
groups_by_name(name: $name, domain_name: $domain_name) {$fields}
65+
}
66+
""")
67+
query = query.replace('$fields', ' '.join(fields))
68+
variables = {
69+
'name': name,
70+
'domain_name': domain_name,
71+
}
72+
data = await api_session.get().Admin._query(query, variables)
73+
return data['groups_by_name']
74+
75+
@api_function
76+
@classmethod
77+
async def list(
78+
cls,
79+
domain_name: str,
80+
fields: Iterable[str] = None,
81+
) -> Sequence[dict]:
2782
"""
2883
Fetches the list of groups.
2984
3085
:param domain_name: Name of domain to list groups.
31-
:param fields: Additional per-group query fields to fetch.
86+
:param fields: Per-group query fields to fetch.
3287
"""
3388
if fields is None:
34-
fields = ('id', 'name', 'description', 'is_active',
35-
'created_at', 'domain_name',
36-
'total_resource_slots', 'allowed_vfolder_hosts',
37-
'integration_id')
89+
fields = _default_list_fields
3890
query = textwrap.dedent("""\
3991
query($domain_name: String) {
4092
groups(domain_name: $domain_name) {$fields}
@@ -55,8 +107,7 @@ async def detail(cls, gid: str, fields: Iterable[str] = None) -> Sequence[dict]:
55107
:param fields: Additional per-group query fields to fetch.
56108
"""
57109
if fields is None:
58-
fields = ('id', 'name', 'description', 'is_active', 'created_at', 'domain_name',
59-
'total_resource_slots', 'allowed_vfolder_hosts', 'integration_id')
110+
fields = _default_detail_fields
60111
query = textwrap.dedent("""\
61112
query($gid: UUID!) {
62113
group(id: $gid) {$fields}

0 commit comments

Comments
 (0)