Skip to content

Commit

Permalink
feat: finalize hpc access cli state sync (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
stolpeo committed Dec 18, 2024
1 parent 210c78f commit 76068d9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 67 deletions.
18 changes: 9 additions & 9 deletions utils/cli/hpc_access_cli/ldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from hpc_access_cli.config import LdapSettings
from hpc_access_cli.models import (
LOGIN_SHELL_DISABLED,
Gecos,
# Gecos,
LdapGroup,
LdapGroupOp,
LdapUser,
Expand Down Expand Up @@ -80,7 +80,7 @@ def load_users(self) -> List[LdapUser]:
"uidNumber",
"gidNumber",
"homeDirectory",
"gecos",
# "gecos",
"loginShell",
"mail",
"displayName",
Expand All @@ -90,8 +90,8 @@ def load_users(self) -> List[LdapUser]:
raise Exception("Failed to search for users.")
result = []
for entry in self.connection.entries:
gecos_str = attribute_as_str(entry.gecos)
gecos = Gecos.from_string(gecos_str) if gecos_str else None
# gecos_str = attribute_as_str(entry.gecos)
# gecos = Gecos.from_string(gecos_str) if gecos_str else None
uid_str = attribute_as_str(entry.uidNumber)
uid_number = int(uid_str) if uid_str else None
if not uid_number:
Expand Down Expand Up @@ -126,7 +126,7 @@ def load_users(self) -> List[LdapUser]:
gid_number=gid_number,
home_directory=home_directory,
login_shell=login_shell,
gecos=gecos,
# gecos=None,
ssh_public_key=attribute_list_as_str_list(entry.sshPublicKey),
)
)
Expand Down Expand Up @@ -226,10 +226,10 @@ def _user_op_update(
applied_diff = {}
for key, value in diff.items():
key = humps.camelize(key)
if key == "gecos":
gecos: Gecos = value or Gecos() # type: ignore
applied_diff[key] = Gecos.model_validate(gecos).to_string()
elif key == "sshPublicKey":
# if key == "gecos":
# gecos: Gecos = value or Gecos() # type: ignore
# applied_diff[key] = Gecos.model_validate(gecos).to_string()
if key == "sshPublicKey":
# We only support clearing this list for now which is fine as the
# SSH keys live in the upstream ADs only.
applied_diff[key] = [(ldap3.MODIFY_DELETE, x) for x in writable[key]]
Expand Down
88 changes: 45 additions & 43 deletions utils/cli/hpc_access_cli/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,45 +97,45 @@ def from_path(path: str) -> "FsDirectory":
)


class Gecos(BaseModel):
"""GECOS information about a user."""

#: The full name of the user.
full_name: Optional[str] = None
#: The office location of the user.
office_location: Optional[str] = None
#: The office phone number of the user.
office_phone: Optional[str] = None
#: The home phone number of the user.
home_phone: Optional[str] = None
#: The other information about the user.
other: Optional[str] = None

def to_string(self):
"""Convert the GECOS information to a GECOS string."""
return ",".join(
[
self.full_name if self.full_name else "",
self.office_location if self.office_location else "",
self.office_phone if self.office_phone else "",
self.home_phone if self.home_phone else "",
self.other if self.other else "",
]
)

@staticmethod
def from_string(gecos: str) -> "Gecos":
"""Create a new instance from a GECOS string."""
parts = gecos.split(",", 4)
if len(parts) < 5:
parts.extend([""] * (5 - len(parts)))
return Gecos(
full_name=parts[0] if parts[0] != "None" else None,
office_location=parts[1] if parts[1] != "None" else None,
office_phone=parts[2] if parts[2] != "None" else None,
home_phone=parts[3] if parts[3] != "None" else None,
other=parts[4] if parts[4] != "None" else None,
)
# class Gecos(BaseModel):
# """GECOS information about a user."""

# #: The full name of the user.
# full_name: Optional[str] = None
# #: The office location of the user.
# office_location: Optional[str] = None
# #: The office phone number of the user.
# office_phone: Optional[str] = None
# #: The home phone number of the user.
# home_phone: Optional[str] = None
# #: The other information about the user.
# other: Optional[str] = None

# def to_string(self):
# """Convert the GECOS information to a GECOS string."""
# return ",".join(
# [
# self.full_name if self.full_name else "",
# self.office_location if self.office_location else "",
# self.office_phone if self.office_phone else "",
# self.home_phone if self.home_phone else "",
# self.other if self.other else "",
# ]
# )

# @staticmethod
# def from_string(gecos: str) -> "Gecos":
# """Create a new instance from a GECOS string."""
# parts = gecos.split(",", 4)
# if len(parts) < 5:
# parts.extend([""] * (5 - len(parts)))
# return Gecos(
# full_name=parts[0] if parts[0] != "None" else None,
# office_location=parts[1] if parts[1] != "None" else None,
# office_phone=parts[2] if parts[2] != "None" else None,
# home_phone=parts[3] if parts[3] != "None" else None,
# other=parts[4] if parts[4] != "None" else None,
# )


class LdapUser(BaseModel):
Expand All @@ -161,10 +161,12 @@ class LdapUser(BaseModel):
home_directory: str
#: The login shell of the user.
login_shell: str
#: The GECOS information of the user.
gecos: Optional[Gecos]
#: Public SSH keys.
ssh_public_key: List[str]
# #: The GECOS information of the user.
# gecos: Optional[Gecos]
# #: Public SSH keys.
# ssh_public_key: List[str]
#: Telephone number.
telephone_number: Optional[str]


class LdapGroup(BaseModel):
Expand Down
30 changes: 15 additions & 15 deletions utils/cli/hpc_access_cli/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
LOGIN_SHELL_DISABLED,
FsDirectory,
FsDirectoryOp,
Gecos,
# Gecos,
GroupFolders,
HpcaccessState,
HpcaccessStateV2,
Expand Down Expand Up @@ -313,12 +313,13 @@ def _build_ldap_users(self, hpcaccess_state: HpcaccessState) -> Dict[str, LdapUs
"""Build the LDAP users from the hpc-access state."""
result = {}
for user in hpcaccess_state.hpc_users.values():
gecos = Gecos(
full_name=user.full_name,
office_location=None,
office_phone=user.phone_number,
other=None,
)
# gecos = Gecos(
# full_name=user.full_name,
# office_location=None,
# office_phone=user.phone_number,
# home_phone=None,
# other=None,
# )
if user.primary_group:
hpc_group = hpcaccess_state.hpc_groups[user.primary_group]
group_gid = hpc_group.gid or HPC_ALUMNIS_GID
Expand All @@ -331,15 +332,14 @@ def _build_ldap_users(self, hpcaccess_state: HpcaccessState) -> Dict[str, LdapUs
given_name=user.first_name,
uid=user.username,
mail=user.email,
gecos=gecos,
# gecos=None,
uid_number=user.uid,
gid_number=group_gid,
# user.home_directory
home_directory=f"{BASE_PATH_TIER1}/home/users/{user.username}",
# user.login_shell
login_shell="/usr/bin/bash",
home_directory=user.home_directory,
login_shell=user.login_shell,
telephone_number=user.phone_number,
# SSH keys are managed via upstream LDAP.
ssh_public_key=[],
# ssh_public_key=[],
)
return result

Expand Down Expand Up @@ -517,7 +517,7 @@ def build_hpcuser(u: LdapUser, quotas: Dict[str, str]) -> HpcUser:
first_name=u.given_name,
last_name=u.sn,
email=u.mail,
phone_number=u.gecos.office_phone if u.gecos else None,
phone_number=u.telephone_number,
resources_requested=ResourceDataUser(**quotas),
resources_used=ResourceDataUser(
tier1_home=0,
Expand Down Expand Up @@ -689,7 +689,7 @@ def build_hpcuser(u: LdapUser, quotas: Dict[str, str]) -> HpcUserV2:
first_name=u.given_name,
last_name=u.sn,
email=u.mail,
phone_number=u.gecos.office_phone if u.gecos else None,
phone_number=u.telephone_number,
resources_requested=ResourceDataUser(**quotas),
status=status,
uid=u.uid_number,
Expand Down

0 comments on commit 76068d9

Please sign in to comment.