From 0e9e7a0e3aecff3c3397a723cc7429af7ba2c0d9 Mon Sep 17 00:00:00 2001 From: royroyee Date: Thu, 29 Feb 2024 16:37:17 +0900 Subject: [PATCH] Refactor URL parameter handling for clarity --- api/agent/agent.go | 22 +++---------- api/auth/auth.go | 22 ++++++++----- api/cert/cert.go | 47 +++++++++++++++------------ api/event/event.go | 19 +++++------ api/iam/iam.go | 69 ++++++++++++++++++++++++---------------- api/log/log.go | 16 +++------- api/note/note.go | 19 ++++------- api/packages/packages.go | 23 ++++++++++---- api/server/server.go | 22 ++++++++----- cmd/token/token.go | 3 ++ utils/utils.go | 21 ++++++++++++ 11 files changed, 162 insertions(+), 121 deletions(-) diff --git a/api/agent/agent.go b/api/agent/agent.go index 0f5358c..5db9fe2 100644 --- a/api/agent/agent.go +++ b/api/agent/agent.go @@ -4,11 +4,12 @@ import ( "fmt" "github.com/alpacanetworks/alpacon-cli/api/server" "github.com/alpacanetworks/alpacon-cli/client" - "net/url" + "github.com/alpacanetworks/alpacon-cli/utils" + "path" ) const ( - baseURL = "/api/servers/servers" + baseURL = "/api/servers/servers/" upgradeAction = "upgrade_agent" restartAction = "restart_agent" shutdownAction = "shutdown_agent" @@ -26,11 +27,6 @@ func RequestAgentAction(ac *client.AlpaconClient, serverName string, action stri return err } - url, err := buildURL(serverID) - if err != nil { - return err - } - actionValue, ok := actionMap[action] if !ok { return fmt.Errorf("invalid action: %s. Valid actions are: upgrade, restart, shutdown", action) @@ -40,15 +36,7 @@ func RequestAgentAction(ac *client.AlpaconClient, serverName string, action stri Action: actionValue, } - _, err = ac.SendPostRequest(url, request) + relativePath := path.Join(serverID, "actions") + _, err = ac.SendPostRequest(utils.BuildURL(baseURL, relativePath, nil), request) return err } - -func buildURL(serverID string) (string, error) { - url, err := url.JoinPath(baseURL, serverID, "/actions/") - if err != nil { - return "", err - } - - return url, nil -} diff --git a/api/auth/auth.go b/api/auth/auth.go index 4356d97..5f5031c 100644 --- a/api/auth/auth.go +++ b/api/auth/auth.go @@ -10,13 +10,13 @@ import ( "github.com/alpacanetworks/alpacon-cli/utils" "io" "net/http" + "strconv" ) const ( - loginURL = "/api/auth/login/" - tokenURL = "/api/auth/tokens/" - getTokenIDURL = "/api/auth/tokens/?=name" - statusURL = "/api/status/" + loginURL = "/api/auth/login/" + tokenURL = "/api/auth/tokens/" + statusURL = "/api/status/" ) func LoginAndSaveCredentials(loginReq *LoginRequest, token string) error { @@ -106,9 +106,12 @@ func GetAPITokenList(ac *client.AlpaconClient) ([]APITokenAttributes, error) { page := 1 const pageSize = 100 + params := map[string]string{ + "page": strconv.Itoa(page), + "page_size": fmt.Sprintf("%d", pageSize), + } for { - params := utils.CreatePaginationParams(page, pageSize) - responseBody, err := ac.SendGetRequest(tokenURL + "?" + params) + responseBody, err := ac.SendGetRequest(utils.BuildURL(tokenURL, "", params)) if err != nil { return nil, err } @@ -137,7 +140,10 @@ func GetAPITokenList(ac *client.AlpaconClient) ([]APITokenAttributes, error) { } func getAPITokenIDByName(ac *client.AlpaconClient, tokenName string) (string, error) { - body, err := ac.SendGetRequest(getTokenIDURL + tokenName) + params := map[string]string{ + "name": tokenName, + } + body, err := ac.SendGetRequest(utils.BuildURL(tokenURL, "", params)) if err != nil { return "", err } @@ -156,7 +162,7 @@ func getAPITokenIDByName(ac *client.AlpaconClient, tokenName string) (string, er } func DeleteAPIToken(ac *client.AlpaconClient, tokenID string) error { - _, err := ac.SendDeleteRequest(tokenURL + tokenID + "/") + _, err := ac.SendDeleteRequest(utils.BuildURL(tokenURL, tokenID, nil)) if err != nil { return err } diff --git a/api/cert/cert.go b/api/cert/cert.go index 59a0f85..f5c1591 100644 --- a/api/cert/cert.go +++ b/api/cert/cert.go @@ -6,7 +6,7 @@ import ( "fmt" "github.com/alpacanetworks/alpacon-cli/client" "github.com/alpacanetworks/alpacon-cli/utils" - "net/url" + "path" "strconv" ) @@ -65,8 +65,14 @@ func GetCSRList(ac *client.AlpaconClient, state string) ([]CSRAttributes, error) page := 1 const pageSize = 100 + params := map[string]string{ + "state": state, + "page": strconv.Itoa(page), + "page_size": fmt.Sprintf("%d", pageSize), + } + for { - responseBody, err := ac.SendGetRequest(buildURL(state, page, pageSize)) + responseBody, err := ac.SendGetRequest(utils.BuildURL(signRequestURL, "", params)) if err != nil { return nil, err } @@ -104,9 +110,13 @@ func GetAuthorityList(ac *client.AlpaconClient) ([]AuthorityAttributes, error) { page := 1 const pageSize = 100 + params := map[string]string{ + "page": strconv.Itoa(page), + "page_size": fmt.Sprintf("%d", pageSize), + } + for { - params := utils.CreatePaginationParams(page, pageSize) - responseBody, err := ac.SendGetRequest(authorityURL + "?" + params) + responseBody, err := ac.SendGetRequest(utils.BuildURL(authorityURL, "", params)) if err != nil { return nil, err } @@ -141,7 +151,7 @@ func GetAuthorityList(ac *client.AlpaconClient) ([]AuthorityAttributes, error) { } func GetAuthorityDetail(ac *client.AlpaconClient, authorityId string) ([]byte, error) { - body, err := ac.SendGetRequest(authorityURL + authorityId) + body, err := ac.SendGetRequest(utils.BuildURL(authorityURL, authorityId, nil)) if err != nil { return nil, err } @@ -150,7 +160,7 @@ func GetAuthorityDetail(ac *client.AlpaconClient, authorityId string) ([]byte, e } func GetCSRDetail(ac *client.AlpaconClient, csrId string) ([]byte, error) { - body, err := ac.SendGetRequest(signRequestURL + csrId) + body, err := ac.SendGetRequest(utils.BuildURL(signRequestURL, csrId, nil)) if err != nil { return nil, err } @@ -159,7 +169,7 @@ func GetCSRDetail(ac *client.AlpaconClient, csrId string) ([]byte, error) { } func GetCertificateDetail(ac *client.AlpaconClient, certId string) ([]byte, error) { - body, err := ac.SendGetRequest(certURL + certId) + body, err := ac.SendGetRequest(utils.BuildURL(certURL, certId, nil)) if err != nil { return nil, err } @@ -168,7 +178,8 @@ func GetCertificateDetail(ac *client.AlpaconClient, certId string) ([]byte, erro } func ApproveCSR(ac *client.AlpaconClient, csrId string) ([]byte, error) { - responseBody, err := ac.SendPostRequest(signRequestURL+csrId+"/approve/", bytes.NewBuffer([]byte("{}"))) + relativePath := path.Join(csrId, "approve") + responseBody, err := ac.SendPostRequest(utils.BuildURL(signRequestURL, relativePath, nil), bytes.NewBuffer([]byte("{}"))) if err != nil { return nil, err } @@ -177,7 +188,8 @@ func ApproveCSR(ac *client.AlpaconClient, csrId string) ([]byte, error) { } func DenyCSR(ac *client.AlpaconClient, csrId string) ([]byte, error) { - responseBody, err := ac.SendPostRequest(signRequestURL+csrId+"/deny/", bytes.NewBuffer([]byte("{}"))) + relativePath := path.Join(csrId, "deny") + responseBody, err := ac.SendPostRequest(utils.BuildURL(signRequestURL, relativePath, nil), bytes.NewBuffer([]byte("{}"))) if err != nil { return nil, err } @@ -186,7 +198,7 @@ func DenyCSR(ac *client.AlpaconClient, csrId string) ([]byte, error) { } func DeleteCSR(ac *client.AlpaconClient, csrId string) error { - _, err := ac.SendDeleteRequest(signRequestURL + csrId + "/") + _, err := ac.SendDeleteRequest(utils.BuildURL(signRequestURL, csrId, nil)) if err != nil { return err } @@ -199,9 +211,12 @@ func GetCertificateList(ac *client.AlpaconClient) ([]CertificateAttributes, erro page := 1 const pageSize = 100 + params := map[string]string{ + "page": strconv.Itoa(page), + "page_size": fmt.Sprintf("%d", pageSize), + } for { - params := utils.CreatePaginationParams(page, pageSize) - responseBody, err := ac.SendGetRequest(certURL + "?" + params) + responseBody, err := ac.SendGetRequest(utils.BuildURL(certURL, "", params)) if err != nil { return nil, err } @@ -251,11 +266,3 @@ func DownloadCertificate(ac *client.AlpaconClient, csrId string, filePath string return nil } - -func buildURL(state string, page int, pageSize int) string { - params := url.Values{} - params.Add("state", state) - params.Add("page", strconv.Itoa(page)) - params.Add("page_size", fmt.Sprintf("%d", pageSize)) - return signRequestURL + "?" + params.Encode() -} diff --git a/api/event/event.go b/api/event/event.go index 4b43c02..21a7713 100644 --- a/api/event/event.go +++ b/api/event/event.go @@ -8,7 +8,7 @@ import ( "github.com/alpacanetworks/alpacon-cli/api/server" "github.com/alpacanetworks/alpacon-cli/client" "github.com/alpacanetworks/alpacon-cli/utils" - "net/url" + "path" "time" ) @@ -31,7 +31,12 @@ func GetEventList(ac *client.AlpaconClient, pageSize int, serverName string, use return nil, err } } - responseBody, err := ac.SendGetRequest(buildURL(serverID, userID, pageSize)) + + relativePath := path.Join(serverID, userID) + params := map[string]string{ + "page_size": fmt.Sprintf("%d", pageSize), + } + responseBody, err := ac.SendGetRequest(utils.BuildURL(getEventURL, relativePath, params)) if err != nil { return nil, err } @@ -107,7 +112,7 @@ func PollCommandExecution(ac *client.AlpaconClient, cmdId string) (EventDetails, case <-timer.C: return response, errors.New("command execution timed out") case <-ticker.C: - responseBody, err := ac.SendGetRequest(getEventURL + cmdId) + responseBody, err := ac.SendGetRequest(utils.BuildURL(getEventURL, cmdId, nil)) if err != nil { continue } @@ -125,11 +130,3 @@ func PollCommandExecution(ac *client.AlpaconClient, cmdId string) (EventDetails, } } } - -func buildURL(serverID, userID string, pageSize int) string { - params := url.Values{} - params.Add("server", serverID) - params.Add("requested_by", userID) - params.Add("page_size", fmt.Sprintf("%d", pageSize)) - return getEventURL + "?" + params.Encode() -} diff --git a/api/iam/iam.go b/api/iam/iam.go index 403657e..9f947d3 100644 --- a/api/iam/iam.go +++ b/api/iam/iam.go @@ -6,19 +6,13 @@ import ( "fmt" "github.com/alpacanetworks/alpacon-cli/client" "github.com/alpacanetworks/alpacon-cli/utils" - "net/url" + "strconv" ) const ( - userURL = "/api/iam/users/" - getUserIDURL = "/api/iam/users/?username=" - + userURL = "/api/iam/users/" groupURL = "/api/iam/groups/" - getGroupIDURL = "/api/iam/groups/?name=" - - getUserMembershipURL = "/api/iam/memberships/?user=" - getGroupMembershipURL = "/api/iam/memberships/?group=" - membershipURL = "/api/iam/memberships/" + membershipURL = "/api/iam/memberships/" ) func GetUserList(ac *client.AlpaconClient) ([]UserAttributes, error) { @@ -26,9 +20,12 @@ func GetUserList(ac *client.AlpaconClient) ([]UserAttributes, error) { page := 1 const pageSize = 100 + params := map[string]string{ + "page": strconv.Itoa(page), + "page_size": fmt.Sprintf("%d", pageSize), + } for { - params := utils.CreatePaginationParams(page, pageSize) - responseBody, err := ac.SendGetRequest(userURL + "?" + params) + responseBody, err := ac.SendGetRequest(utils.BuildURL(userURL, "", params)) if err != nil { return nil, err } @@ -64,9 +61,12 @@ func GetGroupList(ac *client.AlpaconClient) ([]GroupAttributes, error) { page := 1 const pageSize = 100 + params := map[string]string{ + "page": strconv.Itoa(page), + "page_size": fmt.Sprintf("%d", pageSize), + } for { - params := utils.CreatePaginationParams(page, pageSize) - responseBody, err := ac.SendGetRequest(groupURL + "?" + params) + responseBody, err := ac.SendGetRequest(utils.BuildURL(groupURL, "", params)) if err != nil { return nil, err } @@ -104,7 +104,7 @@ func GetUserDetail(ac *client.AlpaconClient, userName string) ([]byte, error) { return nil, err } - responseBody, err := ac.SendGetRequest(userURL + userID) + responseBody, err := ac.SendGetRequest(utils.BuildURL(userURL, userID, nil)) if err != nil { return nil, err } @@ -151,7 +151,7 @@ func GetGroupDetail(ac *client.AlpaconClient, groupName string) ([]byte, error) return nil, err } - responseBody, err := ac.SendGetRequest(groupURL + groupID) + responseBody, err := ac.SendGetRequest(utils.BuildURL(groupURL, groupID, nil)) if err != nil { return nil, err } @@ -212,7 +212,7 @@ func DeleteUser(ac *client.AlpaconClient, userName string) error { return err } - _, err = ac.SendDeleteRequest(userURL + userID + "/") + _, err = ac.SendDeleteRequest(utils.BuildURL(userURL, userID, nil)) if err != nil { return err } @@ -226,7 +226,7 @@ func DeleteGroup(ac *client.AlpaconClient, groupName string) error { return err } - _, err = ac.SendDeleteRequest(groupURL + groupID + "/") + _, err = ac.SendDeleteRequest(utils.BuildURL(groupURL, groupID, nil)) if err != nil { return err } @@ -265,11 +265,11 @@ func DeleteMember(ac *client.AlpaconClient, memberDeleteRequest MemberDeleteRequ return err } - params := url.Values{} - params.Add("user", memberID) - params.Add("group", groupID) - - responseBody, err := ac.SendGetRequest(membershipURL + "?" + params.Encode()) + params := map[string]string{ + "user": memberID, + "group": groupID, + } + responseBody, err := ac.SendGetRequest(utils.BuildURL(membershipURL, "", params)) if err != nil { return err } @@ -280,7 +280,7 @@ func DeleteMember(ac *client.AlpaconClient, memberDeleteRequest MemberDeleteRequ return err } - _, err = ac.SendDeleteRequest(membershipURL + memberDetails[0].ID + "/") + _, err = ac.SendDeleteRequest(utils.BuildURL(membershipURL, memberDetails[0].ID, nil)) if err != nil { return err } @@ -289,7 +289,11 @@ func DeleteMember(ac *client.AlpaconClient, memberDeleteRequest MemberDeleteRequ } func GetUserIDByName(ac *client.AlpaconClient, userName string) (string, error) { - responseBody, err := ac.SendGetRequest(getUserIDURL + userName) + params := map[string]string{ + "username": userName, + } + + responseBody, err := ac.SendGetRequest(utils.BuildURL(userURL, "", params)) if err != nil { return "", err } @@ -308,7 +312,7 @@ func GetUserIDByName(ac *client.AlpaconClient, userName string) (string, error) } func GetUserNameByID(ac *client.AlpaconClient, userID string) (string, error) { - responseBody, err := ac.SendGetRequest(userURL + userID) + responseBody, err := ac.SendGetRequest(utils.BuildURL(userURL, userID, nil)) if err != nil { return "", err } @@ -323,7 +327,10 @@ func GetUserNameByID(ac *client.AlpaconClient, userID string) (string, error) { } func GetGroupIDByName(ac *client.AlpaconClient, groupName string) (string, error) { - responseBody, err := ac.SendGetRequest(getGroupIDURL + groupName) + params := map[string]string{ + "name": groupName, + } + responseBody, err := ac.SendGetRequest(utils.BuildURL(groupURL, "", params)) if err != nil { return "", err } @@ -363,7 +370,10 @@ func getLDAPStatus(isLDAP bool) string { } func getGroupNames(ac *client.AlpaconClient, userID string) ([]string, error) { - responseBody, err := ac.SendGetRequest(getUserMembershipURL + userID) + params := map[string]string{ + "user": userID, + } + responseBody, err := ac.SendGetRequest(utils.BuildURL(membershipURL, "", params)) if err != nil { return nil, err } @@ -383,7 +393,10 @@ func getGroupNames(ac *client.AlpaconClient, userID string) ([]string, error) { } func getMemberNames(ac *client.AlpaconClient, groupID string) ([]string, error) { - responseBody, err := ac.SendGetRequest(getGroupMembershipURL + groupID) + params := map[string]string{ + "group": groupID, + } + responseBody, err := ac.SendGetRequest(utils.BuildURL(membershipURL, "", params)) if err != nil { return nil, err } diff --git a/api/log/log.go b/api/log/log.go index 83d8a06..d1543b9 100644 --- a/api/log/log.go +++ b/api/log/log.go @@ -6,7 +6,6 @@ import ( "github.com/alpacanetworks/alpacon-cli/api/server" "github.com/alpacanetworks/alpacon-cli/client" "github.com/alpacanetworks/alpacon-cli/utils" - "net/url" ) const ( @@ -19,9 +18,11 @@ func GetSystemLogList(ac *client.AlpaconClient, serverName string, pageSize int) return nil, err } - url := buildURL(serverID, pageSize) - - responseBody, err := ac.SendGetRequest(url) + params := map[string]string{ + "server": serverID, + "page_size": fmt.Sprintf("%d", pageSize), + } + responseBody, err := ac.SendGetRequest(utils.BuildURL(getSystemLogURL, "", params)) if err != nil { return nil, err } @@ -61,10 +62,3 @@ func getLogLevel(level int) string { return "UNKNOWN" } } - -func buildURL(serverID string, pageSize int) string { - params := url.Values{} - params.Add("server", serverID) - params.Add("page_size", fmt.Sprintf("%d", pageSize)) - return getSystemLogURL + "?" + params.Encode() -} diff --git a/api/note/note.go b/api/note/note.go index e0b31a4..a45363c 100644 --- a/api/note/note.go +++ b/api/note/note.go @@ -6,7 +6,7 @@ import ( "github.com/alpacanetworks/alpacon-cli/api/iam" "github.com/alpacanetworks/alpacon-cli/api/server" "github.com/alpacanetworks/alpacon-cli/client" - "net/url" + "github.com/alpacanetworks/alpacon-cli/utils" ) const ( @@ -25,9 +25,11 @@ func GetNoteList(ac *client.AlpaconClient, serverName string, pageSize int) ([]N } } - url := buildURL(serverID, pageSize) - - responseBody, err := ac.SendGetRequest(url) + params := map[string]string{ + "serverID": serverID, + "page_size": fmt.Sprintf("%d", pageSize), + } + responseBody, err := ac.SendGetRequest(utils.BuildURL(noteURL, "", params)) if err != nil { return nil, err } @@ -78,17 +80,10 @@ func CreateNote(ac *client.AlpaconClient, noteRequest NoteCreateRequest) error { } func DeleteNote(ac *client.AlpaconClient, noteID string) error { - _, err := ac.SendDeleteRequest(noteURL + noteID + "/") + _, err := ac.SendDeleteRequest(utils.BuildURL(noteURL, noteID, nil)) if err != nil { return err } return err } - -func buildURL(serverID string, pageSize int) string { - params := url.Values{} - params.Add("server", serverID) - params.Add("page_size", fmt.Sprintf("%d", pageSize)) - return noteURL + "?" + params.Encode() -} diff --git a/api/packages/packages.go b/api/packages/packages.go index df1b68f..8b06d1f 100644 --- a/api/packages/packages.go +++ b/api/packages/packages.go @@ -4,11 +4,13 @@ import ( "bytes" "encoding/json" "errors" + "fmt" "github.com/alpacanetworks/alpacon-cli/client" "github.com/alpacanetworks/alpacon-cli/utils" "io" "mime/multipart" "path/filepath" + "strconv" ) const ( @@ -21,9 +23,12 @@ func GetSystemPackageEntry(ac *client.AlpaconClient) ([]SystemPackage, error) { page := 1 const pageSize = 100 + params := map[string]string{ + "page": strconv.Itoa(page), + "page_size": fmt.Sprintf("%d", pageSize), + } for { - params := utils.CreatePaginationParams(page, pageSize) - responseBody, err := ac.SendGetRequest(systemPackageEntryURL + "?" + params) + responseBody, err := ac.SendGetRequest(utils.BuildURL(systemPackageEntryURL, "", params)) if err != nil { return nil, err } @@ -56,9 +61,12 @@ func GetPythonPackageEntry(ac *client.AlpaconClient) ([]PythonPackage, error) { page := 1 const pageSize = 100 + params := map[string]string{ + "page": strconv.Itoa(page), + "page_size": fmt.Sprintf("%d", pageSize), + } for { - params := utils.CreatePaginationParams(page, pageSize) - responseBody, err := ac.SendGetRequest(pythonPackageEntryURL + "?" + params) + responseBody, err := ac.SendGetRequest(utils.BuildURL(pythonPackageEntryURL, "", params)) if err != nil { return nil, err } @@ -96,7 +104,10 @@ func GetPackageIDByName(ac *client.AlpaconClient, fileName string, packageType s url = systemPackageEntryURL } - body, err := ac.SendGetRequest(url + "?name=" + fileName) + params := map[string]string{ + "name": fileName, + } + body, err := ac.SendGetRequest(utils.BuildURL(url, "", params)) if err != nil { return "", err } @@ -167,7 +178,7 @@ func DownloadPackage(ac *client.AlpaconClient, fileName string, dest string, pac var downloadURL DownloadURL - respBody, err := ac.SendGetRequest(url + packageID) + respBody, err := ac.SendGetRequest(utils.BuildURL(url, packageID, nil)) if err != nil { return err } diff --git a/api/server/server.go b/api/server/server.go index 3cbae3f..42d6236 100644 --- a/api/server/server.go +++ b/api/server/server.go @@ -6,11 +6,11 @@ import ( "fmt" "github.com/alpacanetworks/alpacon-cli/client" "github.com/alpacanetworks/alpacon-cli/utils" + "strconv" ) const ( - serverURL = "/api/servers/servers/" - getServerIDURL = "/api/servers/servers/?name=" + serverURL = "/api/servers/servers/" ) func GetServerList(ac *client.AlpaconClient) ([]ServerAttributes, error) { @@ -18,9 +18,12 @@ func GetServerList(ac *client.AlpaconClient) ([]ServerAttributes, error) { page := 1 const pageSize = 100 + params := map[string]string{ + "page": strconv.Itoa(page), + "page_size": fmt.Sprintf("%d", pageSize), + } for { - params := utils.CreatePaginationParams(page, pageSize) - responseBody, err := ac.SendGetRequest(serverURL + "?" + params) + responseBody, err := ac.SendGetRequest(utils.BuildURL(serverURL, "", params)) if err != nil { return nil, err } @@ -55,7 +58,7 @@ func GetServerDetail(ac *client.AlpaconClient, serverName string) ([]byte, error return nil, err } - body, err := ac.SendGetRequest(serverURL + serverID) + body, err := ac.SendGetRequest(utils.BuildURL(serverURL, serverID, nil)) if err != nil { return nil, err } @@ -69,7 +72,7 @@ func DeleteServer(ac *client.AlpaconClient, serverName string) error { return err } - _, err = ac.SendDeleteRequest(serverURL + serverID + "/") + _, err = ac.SendDeleteRequest(utils.BuildURL(serverURL, serverID, nil)) if err != nil { return err } @@ -78,7 +81,10 @@ func DeleteServer(ac *client.AlpaconClient, serverName string) error { } func GetServerIDByName(ac *client.AlpaconClient, serverName string) (string, error) { - body, err := ac.SendGetRequest(getServerIDURL + serverName) + params := map[string]string{ + "name": serverName, + } + body, err := ac.SendGetRequest(utils.BuildURL(serverURL, "", params)) if err != nil { return "", err } @@ -97,7 +103,7 @@ func GetServerIDByName(ac *client.AlpaconClient, serverName string) (string, err } func GetServerNameByID(ac *client.AlpaconClient, serverID string) (string, error) { - body, err := ac.SendGetRequest(serverURL + serverID) + body, err := ac.SendGetRequest(utils.BuildURL(serverURL, serverID, nil)) if err != nil { return "", err } diff --git a/cmd/token/token.go b/cmd/token/token.go index f6d01e4..b2e7d0b 100644 --- a/cmd/token/token.go +++ b/cmd/token/token.go @@ -21,4 +21,7 @@ func init() { TokenCmd.AddCommand(tokenCreateCmd) TokenCmd.AddCommand(tokenListCmd) TokenCmd.AddCommand(tokenDeleteCmd) + + // ACL + TokenCmd.AddCommand(AclCmd) } diff --git a/utils/utils.go b/utils/utils.go index b26e652..5ffe4d0 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -6,6 +6,7 @@ import ( "golang.org/x/term" "net/url" "os" + "path" "path/filepath" "strconv" "strings" @@ -219,6 +220,26 @@ func BoolPointerToString(value *bool) string { return "false" } +func BuildURL(basePath, relativePath string, params map[string]string) string { + u, err := url.Parse(basePath) + if err != nil { + CliError("Failed to parse base URL") + } + + u.Path = path.Join(u.Path, relativePath) + if !strings.HasSuffix(u.Path, "/") { + u.Path += "/" + } + q := u.Query() + + for key, value := range params { + q.Set(key, value) + } + + u.RawQuery = q.Encode() + return u.String() +} + func StringToStringPointer(value string) *string { if value == "" { return nil