Skip to content

Commit

Permalink
Remove JSON parsing in iam detail command
Browse files Browse the repository at this point in the history
  • Loading branch information
royroyee committed Mar 13, 2024
1 parent 0a27613 commit 87ce3db
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 172 deletions.
126 changes: 6 additions & 120 deletions api/iam/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,95 +97,27 @@ func GetGroupList(ac *client.AlpaconClient) ([]GroupAttributes, error) {
return groupList, nil
}

func GetUserDetail(ac *client.AlpaconClient, userName string) ([]byte, error) {
var userDetails UserDetails

userID, err := GetUserIDByName(ac, userName)
if err != nil {
return nil, err
}

responseBody, err := ac.SendGetRequest(utils.BuildURL(userURL, userID, nil))
if err != nil {
return nil, err
}

err = json.Unmarshal(responseBody, &userDetails)
if err != nil {
return nil, err
}

groupNames, err := getGroupNames(ac, userID)
if err != nil {
return nil, err
}

userDetailAttributes := &UserDetailAttributes{
Username: userDetails.Username,
Name: fmt.Sprintf("%s %s", userDetails.LastName, userDetails.FirstName),
Description: userDetails.Description,
Email: userDetails.Email,
Phone: userDetails.Phone,
UID: userDetails.UID,
Shell: userDetails.Shell,
HomeDirectory: userDetails.HomeDirectory,
NumGroups: userDetails.NumGroups,
Groups: groupNames,
Tags: userDetails.Tags,
Status: getUserStatus(userDetails.IsActive, userDetails.IsStaff, userDetails.IsSuperuser),
LDAPStatus: getLDAPStatus(userDetails.IsLDAPUser),
}

userDetailJSON, err := json.Marshal(userDetailAttributes)
func GetUserDetail(ac *client.AlpaconClient, userId string) ([]byte, error) {
responseBody, err := ac.SendGetRequest(utils.BuildURL(userURL, userId, nil))
if err != nil {
return nil, err
}

return userDetailJSON, nil
return responseBody, nil
}

func GetGroupDetail(ac *client.AlpaconClient, groupName string) ([]byte, error) {
var groupDetails GroupDetails

groupID, err := GetGroupIDByName(ac, groupName)
if err != nil {
return nil, err
}

responseBody, err := ac.SendGetRequest(utils.BuildURL(groupURL, groupID, nil))
if err != nil {
return nil, err
}

err = json.Unmarshal(responseBody, &groupDetails)
if err != nil {
return nil, err
}

memberNames, err := getMemberNames(ac, groupID)
groupId, err := GetGroupIDByName(ac, groupName)
if err != nil {
return nil, err
}

groupDetailAttributes := &GroupDetailAttributes{
Name: groupDetails.Name,
DisplayName: groupDetails.DisplayName,
Tags: groupDetails.Tags,
Description: groupDetails.Description,
NumMembers: groupDetails.NumMembers,
Members: memberNames,
GID: groupDetails.GID,
LDAPStatus: getLDAPStatus(groupDetails.IsLDAPGroup),
Servers: len(groupDetails.Servers),
ServersNames: groupDetails.ServersNames,
}

groupDetailJson, err := json.Marshal(groupDetailAttributes)
responseBody, err := ac.SendGetRequest(utils.BuildURL(groupURL, groupId, nil))
if err != nil {
return nil, err
}

return groupDetailJson, nil
return responseBody, nil
}

func CreateUser(ac *client.AlpaconClient, userRequest UserCreateRequest) error {
Expand Down Expand Up @@ -369,49 +301,3 @@ func getLDAPStatus(isLDAP bool) string {

return "local"
}

func getGroupNames(ac *client.AlpaconClient, userID string) ([]string, error) {
params := map[string]string{
"user": userID,
}
responseBody, err := ac.SendGetRequest(utils.BuildURL(membershipURL, "", params))
if err != nil {
return nil, err
}

var membershipResponse []Membership
err = json.Unmarshal(responseBody, &membershipResponse)
if err != nil {
return nil, err
}

var result []string
for _, membership := range membershipResponse {
result = append(result, membership.GroupName)
}

return result, nil
}

func getMemberNames(ac *client.AlpaconClient, groupID string) ([]string, error) {
params := map[string]string{
"group": groupID,
}
responseBody, err := ac.SendGetRequest(utils.BuildURL(membershipURL, "", params))
if err != nil {
return nil, err
}

var membershipResponse []Membership
err = json.Unmarshal(responseBody, &membershipResponse)
if err != nil {
return nil, err
}

var result []string
for _, membership := range membershipResponse {
result = append(result, membership.UserName)
}

return result, nil
}
52 changes: 0 additions & 52 deletions api/iam/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,6 @@ type UserResponse struct {
DateJoined time.Time `json:"date_joined"`
}

type UserDetails struct {
ID string `json:"id"`
Username string `json:"username"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
Phone string `json:"phone"`
Tags string `json:"tags"`
Description string `json:"description"`
NumGroups int `json:"num_groups"`
UID int `json:"uid"`
Shell string `json:"shell"`
HomeDirectory string `json:"home_directory"`
IsActive bool `json:"is_active"`
IsStaff bool `json:"is_staff"`
IsSuperuser bool `json:"is_superuser"`
IsLDAPUser bool `json:"is_ldap_user"`
DateJoined string `json:"date_joined"`
LastLogin string `json:"last_login"`
LastLoginIP string `json:"last_login_ip"`
AddedAt string `json:"added_at"`
UpdatedAt string `json:"updated_at"`
}

type UserCreateRequest struct {
Username string `json:"username"`
Password string `json:"password"`
Expand All @@ -96,19 +72,6 @@ type GroupAttributes struct {
LDAPStatus string `json:"ldap_status"`
}

type GroupDetailAttributes struct {
Name string `json:"name"`
DisplayName string `json:"display_name"`
Tags string `json:"tags"`
Description string `json:"description"`
NumMembers int `json:"num_members"`
Members []string `json:"members"`
GID int `json:"gid"`
LDAPStatus string `json:"ldap_status"`
Servers int `json:"servers"`
ServersNames []string `json:"servers_names"`
}

type GroupResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Expand All @@ -121,21 +84,6 @@ type GroupResponse struct {
ServersNames []string `json:"servers_names"`
}

type GroupDetails struct {
ID string `json:"id"`
Name string `json:"name"`
DisplayName string `json:"display_name"`
Tags string `json:"tags"`
Description string `json:"description"`
NumMembers int `json:"num_members"`
GID int `json:"gid"`
IsLDAPGroup bool `json:"is_ldap_group"`
Servers []string `json:"servers"`
ServersNames []string `json:"servers_names"`
AddedAt string `json:"added_at"`
UpdatedAt string `json:"updated_at"`
}

type GroupCreateRequest struct {
Name string `json:"name"`
DisplayName string `json:"display_name"`
Expand Down

0 comments on commit 87ce3db

Please sign in to comment.