Skip to content

Commit

Permalink
add authorization header to migrate api call
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfa committed Oct 23, 2022
1 parent 3ec9a41 commit 229ebca
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
3 changes: 2 additions & 1 deletion pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
Expand Down Expand Up @@ -66,7 +67,7 @@ func GetZones() (config.Region, error) {
arvanConfig := config.GetConfigInfo()
arvanURL, err := url.Parse(arvanConfig.GetServer())
if err != nil {
return regions, nil
return regions, fmt.Errorf("invalid config")
}

httpReq, err := http.NewRequest("GET", arvanURL.Scheme + "://" + arvanURL.Host+regionsEndpoint, nil)
Expand Down
22 changes: 18 additions & 4 deletions pkg/paas/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"strconv"
"strings"
"net/url"

"github.com/arvancloud/cli/pkg/api"
"github.com/arvancloud/cli/pkg/config"
Expand All @@ -23,7 +24,7 @@ import (
)

const (
migrationEndpoint = "/paas/v1/migrations"
migrationEndpoint = "/paas/v1/migrate"
redColor = "\033[31m"
greenColor = "\033[32m"
yellowColor = "\033[33m"
Expand Down Expand Up @@ -95,10 +96,13 @@ func NewCmdMigrate(in io.Reader, out, errout io.Writer) *cobra.Command {
requset := Request{
Namespace: project,
Source: currentRegionName,
Destination: destinationRegion.RegionName,
Destination: fmt.Sprintf("%s-%s", destinationRegion.RegionName, destinationRegion.Name),
}

migrate(requset)
err = migrate(requset)
if err != nil {
log.Println(err)
}
},
}

Expand Down Expand Up @@ -233,11 +237,21 @@ func httpPost(endpoint string, payload interface{}) (*Response, error) {
if err != nil {
return nil, err
}
arvanConfig := config.GetConfigInfo()
arvanURL, err := url.Parse(arvanConfig.GetServer())
if err != nil {
return nil, fmt.Errorf("invalid config")
}

httpReq, err := http.NewRequest(http.MethodPost, getArvanPaasServerBase()+endpoint, bytes.NewBuffer(requestBody))
httpReq, err := http.NewRequest(http.MethodPost, arvanURL.Scheme + "://" + arvanURL.Host+endpoint, bytes.NewBuffer(requestBody))
if err != nil {
return nil, err
}
apikey := arvanConfig.GetApiKey()
if apikey != "" {
httpReq.Header.Add("Authorization", apikey)
}

httpReq.Header.Add("accept", "application/json")
httpReq.Header.Add("User-Agent", rest.DefaultKubernetesUserAgent())
httpResp, err := http.DefaultClient.Do(httpReq)
Expand Down

0 comments on commit 229ebca

Please sign in to comment.