From 87ce3dba6d4ce1381f2b9e8983656a121e553f88 Mon Sep 17 00:00:00 2001 From: royroyee Date: Wed, 13 Mar 2024 22:07:59 +0900 Subject: [PATCH] Remove JSON parsing in iam detail command --- api/iam/iam.go | 126 +++-------------------------------------------- api/iam/types.go | 52 ------------------- 2 files changed, 6 insertions(+), 172 deletions(-) diff --git a/api/iam/iam.go b/api/iam/iam.go index f76f53e..3a9a534 100644 --- a/api/iam/iam.go +++ b/api/iam/iam.go @@ -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 { @@ -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 -} diff --git a/api/iam/types.go b/api/iam/types.go index 4be8a11..3bc2033 100644 --- a/api/iam/types.go +++ b/api/iam/types.go @@ -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"` @@ -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"` @@ -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"`