Skip to content

Commit

Permalink
Refactor URL parameter handling for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
royroyee committed Feb 29, 2024
1 parent 8c067a4 commit 0e9e7a0
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 121 deletions.
22 changes: 5 additions & 17 deletions api/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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
}
22 changes: 14 additions & 8 deletions api/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
47 changes: 27 additions & 20 deletions api/cert/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"github.com/alpacanetworks/alpacon-cli/client"
"github.com/alpacanetworks/alpacon-cli/utils"
"net/url"
"path"
"strconv"
)

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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()
}
19 changes: 8 additions & 11 deletions api/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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()
}
Loading

0 comments on commit 0e9e7a0

Please sign in to comment.