Skip to content

Commit

Permalink
WIP; removed state-dump-v2 command and api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
stolpeo committed Dec 17, 2024
1 parent fec7cc6 commit 99c3098
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 437 deletions.
6 changes: 5 additions & 1 deletion adminsec/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def _sync_ldap(write=False, verbose=False, ldapcon=None):
first_name = userinfo.givenName
last_name = userinfo.sn
mail = userinfo.mail
name = userinfo.displayName
name = userinfo.cn
display_name = userinfo.displayName
disabled = True

if userAccountControl:
Expand All @@ -82,6 +83,9 @@ def _sync_ldap(write=False, verbose=False, ldapcon=None):
if name:
user.name = name[0]

if display_name:
user.display_name = display_name[0]

user.is_active = not disabled

if user.hpcuser_user.exists():
Expand Down
5 changes: 0 additions & 5 deletions adminsec/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,6 @@
view=views_api.HpcProjectCreateRequestRetrieveUpdateApiView.as_view(),
name="api-hpcprojectcreaterequest-retrieveupdate",
),
path(
"api/hpcaccess-status/",
view=views_api.HpcAccessStatusApiView.as_view(),
name="api-hpcaccess-status",
),
]

urlpatterns = urlpatterns_ui + urlpatterns_api
16 changes: 0 additions & 16 deletions adminsec/views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
HpcUser,
)
from usersec.serializers import (
HpcAccessStatusSerializer,
HpcGroupCreateRequestSerializer,
HpcGroupSerializer,
HpcProjectCreateRequestSerializer,
Expand Down Expand Up @@ -218,18 +217,3 @@ def perform_update(self, serializer):
raise ValidationError(errors)

super().perform_update(serializer)


class HpcAccessStatusApiView(RetrieveAPIView):
"""API view for listing all users."""

serializer_class = HpcAccessStatusSerializer
permission_classes = [IsAdminUser | IsHpcAdminUser]

def get_object(self):
"""Return the object to be used in the view."""
return HpcAccessStatus(
hpc_users=HpcUser.objects.all(),
hpc_groups=HpcGroup.objects.all(),
hpc_projects=HpcProject.objects.all(),
)
1 change: 1 addition & 0 deletions hpcaccess/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class User(AbstractUser):
phone = CharField(_("Phone number of User"), blank=True, null=True, max_length=32)
email = EmailField(_("email address"), blank=True, null=True)
uid = IntegerField(_("UID of User"), blank=True, null=True)
display_name = CharField(_("Display Name"), blank=True, null=True, max_length=255)
consented_to_terms = BooleanField(
_("Terms consent status"),
default=False,
Expand Down
94 changes: 4 additions & 90 deletions usersec/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class HpcUserAbstractSerializer(HpcObjectAbstractSerializer):
full_name = serializers.SerializerMethodField()
first_name = serializers.SerializerMethodField()
last_name = serializers.SerializerMethodField()
display_name = serializers.SerializerMethodField()
phone_number = serializers.SerializerMethodField()
home_directory = serializers.CharField()
login_shell = serializers.CharField()
Expand All @@ -65,12 +66,15 @@ def get_first_name(self, obj) -> Optional[str]:
def get_phone_number(self, obj) -> Optional[str]:
return obj.user.phone

def get_display_name(self, obj) -> Optional[str]:
return obj.user.display_name
class Meta:
fields = HpcObjectAbstractSerializer.Meta.fields + [
"email",
"full_name",
"first_name",
"last_name",
"display_name",
"phone_number",
"primary_group",
"resources_requested",
Expand Down Expand Up @@ -98,36 +102,6 @@ class Meta:
]


class HpcUserStatusSerializer(HpcUserAbstractSerializer, serializers.ModelSerializer):
"""Serializer for HpcUser model."""

primary_group = serializers.SerializerMethodField()

def get_primary_group(self, obj):
if obj.primary_group is None:
return HPC_ALUMNI_GROUP
return obj.primary_group.name

class Meta:
model = HpcUser
fields = [
"uid",
"email",
"full_name",
"first_name",
"last_name",
"phone_number",
"primary_group",
"resources_requested",
"status",
"description",
"username",
"expiration",
"home_directory",
"login_shell",
]


class HpcUserVersionSerializer(HpcUserAbstractSerializer, serializers.ModelSerializer):
"""Serializer for HpcUserVersion model."""

Expand Down Expand Up @@ -195,26 +169,6 @@ class Meta:
]


class HpcGroupStatusSerializer(HpcGroupAbstractSerializer, serializers.ModelSerializer):
"""Serializer for HpcGroup model."""

owner = serializers.SlugRelatedField(slug_field="username", read_only=True)

class Meta:
model = HpcUser
fields = [
"owner",
"delegate",
"resources_requested",
"status",
"description",
"name",
"folders",
"expiration",
"gid",
]


class HpcProjectAbstractSerializer(HpcObjectAbstractSerializer):
"""Common base class for HPC project serializers."""

Expand Down Expand Up @@ -271,29 +225,6 @@ class Meta:
]


class HpcProjectStatusSerializer(HpcProjectAbstractSerializer, serializers.ModelSerializer):
"""Serializer for HpcProject model."""

group = serializers.SlugRelatedField(slug_field="name", read_only=True)
delegate = serializers.SlugRelatedField(slug_field="username", read_only=True)
members = serializers.SlugRelatedField(slug_field="username", many=True, read_only=True)

class Meta:
model = HpcUser
fields = [
"gid",
"group",
"delegate",
"resources_requested",
"status",
"description",
"name",
"folders",
"expiration",
"members",
]


class HpcRequestAbstractSerializer(HpcObjectAbstractSerializer):
"""Common base class for HPC request serializers."""

Expand Down Expand Up @@ -451,20 +382,3 @@ class Meta:
"primary_group",
"full_name",
]


class HpcAccessStatusSerializer(serializers.Serializer):
"""Serializer for HpcAccessStatus model."""

hpc_users = serializers.SerializerMethodField()
hpc_groups = serializers.SerializerMethodField()
hpc_projects = serializers.SerializerMethodField()

def get_hpc_users(self, obj):
return HpcUserStatusSerializer(obj.hpc_users, many=True).data

def get_hpc_groups(self, obj):
return HpcGroupStatusSerializer(obj.hpc_groups, many=True).data

def get_hpc_projects(self, obj):
return HpcProjectStatusSerializer(obj.hpc_projects, many=True).data
73 changes: 0 additions & 73 deletions usersec/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
from usersec.serializers import (
HpcGroupCreateRequestSerializer,
HpcGroupSerializer,
HpcGroupStatusSerializer,
HpcProjectCreateRequestSerializer,
HpcProjectSerializer,
HpcProjectStatusSerializer,
HpcUserSerializer,
HpcUserStatusSerializer,
)
from usersec.tests.factories import (
HpcGroupCreateRequestFactory,
Expand Down Expand Up @@ -106,73 +103,3 @@ def testSerializerExisting(self):
result["group"] = "group_uuid_placeholder"
result["name_requested"] = "name_requested_placeholder"
self.assertMatchSnapshot(result)


@freeze_time(FROZEN_TIME)
class TestHpcUserStatusSerializer(ResetSequenceMixin, TestCaseSnap, TestCasePlus):
def setUp(self):
super().setUp()
self.hpc_user = HpcUserFactory()

def testSerializerExisting(self):
serializer = HpcUserStatusSerializer(self.hpc_user)
result = dict(serializer.data)
result["email"] = "email_placeholder"
result["primary_group"] = "primary_group_name_placeholder"
result["phone_number"] = "phone_number_placeholder"
result["full_name"] = "name_placeholder"
result["first_name"] = "first_name_placeholder"
result["last_name"] = "last_name_placeholder"
self.assertMatchSnapshot(result)


@freeze_time(FROZEN_TIME)
class TestHpcGroupStatusSerializer(ResetSequenceMixin, TestCaseSnap, TestCasePlus):
def setUp(self):
super().setUp()
self.hpc_group = HpcGroupFactory()

def testSerializerExisting(self):
serializer = HpcGroupStatusSerializer(self.hpc_group)
result = dict(serializer.data)
self.assertMatchSnapshot(result)


@freeze_time(FROZEN_TIME)
class TestHpcProjectStatusSerializer(ResetSequenceMixin, TestCaseSnap, TestCasePlus):
def setUp(self):
super().setUp()
self.hpc_project = HpcProjectFactory()

def testSerializerExisting(self):
serializer = HpcProjectStatusSerializer(self.hpc_project)
result = dict(serializer.data)
result["group"] = "group_name_placeholder"
self.assertMatchSnapshot(result)


# TODO somehow the tests fail, but the serializer and API actually work
# @freeze_time(FROZEN_TIME)
# class TestHcpaccessStatus(ResetSequenceMixin, TestCaseSnap, TestCasePlus):
# def setUp(self):
# super().setUp()
# hpc_group = HpcGroupFactory()
# HpcProjectFactory(group=hpc_group)
# HpcUserFactory(primary_group=hpc_group)
# self.hpc_access_status = HpcAccessStatus(
# hpc_users=HpcUser.objects.all(),
# hpc_groups=HpcGroup.objects.all(),
# hpc_projects=HpcProject.objects.all(),
# )

# def testSerializerExisting(self):
# serializer = HpcAccessStatusSerializer(self.hpc_access_status)
# result = dict(serializer.data)
# result["hpc_users"][0]["email"] = "email_placeholder"
# result["hpc_users"][0]["primary_group"] = "primary_group_name_placeholder"
# result["hpc_users"][0]["phone_number"] = "phone_number_placeholder"
# result["hpc_users"][0]["full_name"] = "name_placeholder"
# result["hpc_users"][0]["first_name"] = "first_name_placeholder"
# result["hpc_users"][0]["last_name"] = "last_name_placeholder"
# result["hpc_users"][0]["uid"] = 2000
# self.assertMatchSnapshot(result)
15 changes: 0 additions & 15 deletions utils/cli/hpc_access_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
TargetStateBuilder,
TargetStateComparison,
convert_to_hpcaccess_state,
convert_to_hpcaccess_state_v2,
deploy_hpcaccess_state,
fs_validation,
gather_hpcaccess_state,
Expand Down Expand Up @@ -80,20 +79,6 @@ def dump_data(
console_out.print_json(data=hpcaccess_state.model_dump(mode="json"))


@app.command("state-dump-v2")
def dump_data_v2(
config_path: Annotated[
str, typer.Option(..., help="path to configuration file")
] = "/etc/hpc-access-cli/config.json",
):
"""dump system state as hpc-access state"""
settings = load_settings(config_path)
console_err.print_json(data=settings.model_dump(mode="json"))
system_state = gather_system_state(settings)
hpcaccess_state = convert_to_hpcaccess_state_v2(system_state)
console_out.print_json(data=hpcaccess_state.model_dump(mode="json"))


@app.command("state-sync")
def sync_data(
config_path: Annotated[
Expand Down
Loading

0 comments on commit 99c3098

Please sign in to comment.