Skip to content

Commit 4b7b970

Browse files
authored
Use of GroupMetadata instead of ServerlessGroup (#1604)
* remove serverless group model * created group metadata
1 parent 497fb0b commit 4b7b970

File tree

8 files changed

+94
-29
lines changed

8 files changed

+94
-29
lines changed

gateway/api/admin.py

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
"""Admin module."""
22

33
from django.contrib import admin
4-
from django.contrib.auth.models import Group
54
from api.models import (
5+
GroupMetadata,
66
JobConfig,
77
Provider,
88
Program,
99
ComputeResource,
1010
Job,
1111
RuntimeJob,
12-
ServerlessGroup,
1312
)
1413

1514

16-
admin.site.unregister(Group)
17-
18-
1915
@admin.register(JobConfig)
2016
class JobConfigAdmin(admin.ModelAdmin):
2117
"""JobConfigAdmin."""
@@ -62,9 +58,8 @@ class RuntimeJobAdmin(admin.ModelAdmin):
6258
search_fields = ["job__id"]
6359

6460

65-
@admin.register(ServerlessGroup)
66-
class ServerlessGroupAdmin(admin.ModelAdmin):
67-
"""ServerlessGroupAdmin."""
61+
@admin.register(GroupMetadata)
62+
class GroupMetadataAdmin(admin.ModelAdmin):
63+
"""RuntimeJobAdmin."""
6864

69-
search_fields = ["name", "account"]
70-
filter_horizontal = ["permissions"]
65+
search_fields = ["account"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Generated by Django 4.2.19 on 2025-03-11 15:47
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("api", "0034_serverlessgroup"),
10+
]
11+
12+
operations = [
13+
migrations.DeleteModel(
14+
name="ServerlessGroup",
15+
),
16+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Generated by Django 4.2.19 on 2025-03-11 16:26
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
import uuid
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
("auth", "0012_alter_user_first_name_max_length"),
12+
("api", "0035_delete_serverlessgroup"),
13+
]
14+
15+
operations = [
16+
migrations.CreateModel(
17+
name="GroupMetadata",
18+
fields=[
19+
(
20+
"id",
21+
models.UUIDField(
22+
default=uuid.uuid4,
23+
editable=False,
24+
primary_key=True,
25+
serialize=False,
26+
),
27+
),
28+
("created", models.DateTimeField(auto_now_add=True)),
29+
(
30+
"account",
31+
models.CharField(
32+
blank=True, default=None, max_length=255, null=True
33+
),
34+
),
35+
(
36+
"group",
37+
models.OneToOneField(
38+
on_delete=django.db.models.deletion.CASCADE,
39+
related_name="metadata",
40+
to="auth.group",
41+
),
42+
),
43+
],
44+
),
45+
]

gateway/api/models.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -235,18 +235,20 @@ class RuntimeJob(models.Model):
235235
)
236236

237237

238-
class ServerlessGroup(Group):
238+
class GroupMetadata(models.Model):
239239
"""
240-
This model extends Group to store new fields from other resources.
240+
This model will store metadata from different resources for Group
241241
"""
242242

243-
parent_group = models.OneToOneField(
244-
Group,
245-
on_delete=models.CASCADE,
246-
parent_link=True,
247-
primary_key=True,
248-
db_column="parent_group",
249-
)
243+
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
244+
created = models.DateTimeField(auto_now_add=True, editable=False)
250245

251246
# This field will store the account_id from IBM Cloud.
252247
account = models.CharField(max_length=255, blank=True, null=True, default=None)
248+
249+
group = models.OneToOneField(
250+
Group, on_delete=models.CASCADE, related_name="metadata"
251+
)
252+
253+
def __str__(self):
254+
return f"{self.id}"

gateway/api/repositories/users.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from django.db.models import Q
1010

1111
from api.domain.authentication.authentication_group import AuthenticationGroup
12-
from api.models import ServerlessGroup
12+
from api.models import GroupMetadata
1313

1414

1515
User = get_user_model()
@@ -85,13 +85,16 @@ def restart_user_groups(
8585

8686
logger.debug("Update [%s] groups", len(authentication_groups))
8787
for authentication_group in authentication_groups:
88-
group, created = ServerlessGroup.objects.get_or_create(
89-
name=authentication_group.group_name,
90-
account=authentication_group.account,
88+
group, created = Group.objects.get_or_create(
89+
name=authentication_group.group_name
9190
)
9291
if created:
9392
for permission in permissions:
9493
group.permissions.add(permission)
94+
if authentication_group.account is not None:
95+
GroupMetadata.objects.create(
96+
group=group, account=authentication_group.account
97+
)
9598
group.user_set.add(user)
9699
new_groups.append(group)
97100

gateway/tests/api/authentication/custom_token/test_ibm_cloud.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,12 @@ def test_default_authentication_workflow(
9393
groups_names_list,
9494
["AccessGroupId-23afbcd24-00a0-00ab-ab0c-1a23b4c567de"],
9595
)
96+
9697
groups = user.groups.all()
97-
self.assertEqual(
98-
groups[0].serverlessgroup.account,
99-
"abc18abcd41546508b35dfe0627109c4",
100-
)
98+
for group in groups:
99+
self.assertEqual(
100+
group.metadata.account, "abc18abcd41546508b35dfe0627109c4"
101+
)
101102

102103
for group in user.groups.all():
103104
permissions = list(group.permissions.values_list("codename", flat=True))

gateway/tests/api/authentication/custom_token/test_quantum_platform.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ def test_custom_token_authentication(self, get_network_mock: MagicMock):
6868
self.assertListEqual(groups_names_list, ["ibm-q", "ibm-q/open"])
6969

7070
groups = user.groups.all()
71-
self.assertIsNone(groups[0].serverlessgroup.account)
71+
for group in groups:
72+
metadata = getattr(groups, "metadata", None)
73+
self.assertIsNone(metadata)
7274

7375
for group in user.groups.all():
7476
permissions = list(group.permissions.values_list("codename", flat=True))

gateway/tests/api/authentication/mock_token/test_mock_token.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ def test_default_authentication_workflow(self):
4545
self.assertEqual(provider_groups, ["mockgroup"])
4646

4747
groups = user.groups.all()
48-
self.assertIsNone(groups[0].serverlessgroup.account)
48+
metadata = getattr(groups[0], "metadata", None)
49+
self.assertIsNone(metadata)
4950

5051
def test_incorrect_authorization_header(self):
5152
"""This test verifies that user is None if the authentication fails."""

0 commit comments

Comments
 (0)