Skip to content

Commit

Permalink
Merge pull request #504 from npamudika/2.x.x
Browse files Browse the repository at this point in the history
Add proxy support for APIMCLI
  • Loading branch information
npamudika authored Oct 15, 2020
2 parents 4ef3d97 + dd79072 commit 7bef61f
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 90 deletions.
65 changes: 20 additions & 45 deletions import-export-cli/cmd/importAPI.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package cmd

import (
"bytes"
"crypto/tls"
"encoding/json"
"encoding/pem"
"errors"
Expand All @@ -36,8 +35,8 @@ import (
"regexp"
"strconv"
"strings"
"time"

"github.com/go-resty/resty"
"github.com/wso2/product-apim-tooling/import-export-cli/specs/params"

"github.com/mitchellh/go-homedir"
Expand Down Expand Up @@ -720,7 +719,7 @@ func validateApiDefinition(def *v2.APIDefinition) error {
// Helper function for forming multi-part form data
// Returns the formed http request and errors
func NewFileUploadRequest(uri string, method string, params map[string]string, paramName, path,
b64encodedCredentials string) (*http.Request, error) {
b64encodedCredentials string) (*resty.Response, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
Expand All @@ -743,64 +742,40 @@ func NewFileUploadRequest(uri string, method string, params map[string]string, p
return nil, err
}

request, err := http.NewRequest(method, uri, body)
request.Header.Add(utils.HeaderAuthorization, utils.HeaderValueAuthBasicPrefix+" "+b64encodedCredentials)
request.Header.Add(utils.HeaderContentType, writer.FormDataContentType())
request.Header.Add(utils.HeaderAccept, "*/*")
request.Header.Add(utils.HeaderConnection, utils.HeaderValueKeepAlive)
// Set headers
headers := make(map[string]string)
headers[utils.HeaderContentType] = writer.FormDataContentType()
headers[utils.HeaderAuthorization] = utils.HeaderValueAuthBasicPrefix+" "+b64encodedCredentials
headers[utils.HeaderAccept] = "*/*"
headers[utils.HeaderConnection] = utils.HeaderValueKeepAlive

return request, err
if method == http.MethodPost {
resp, err := utils.InvokePOSTRequestWithBytes(uri, headers, body.Bytes())
return resp, err
} else {
resp, err := utils.InvokePUTRequestWithBytes(uri, headers, body.Bytes())
return resp, err
}
}

// importAPI imports an API to the API manager
func importAPI(endpoint, httpMethod, filePath, accessToken string, extraParams map[string]string) error {
req, err := NewFileUploadRequest(endpoint, httpMethod, extraParams, "file",
resp, err := NewFileUploadRequest(endpoint, httpMethod, extraParams, "file",
filePath, accessToken)
if err != nil {
return err
}

var tr *http.Transport
if utils.Insecure {
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
} else {
tr = &http.Transport{}
}

client := &http.Client{
Transport: tr,
Timeout: time.Duration(utils.HttpRequestTimeout) * time.Second,
}

resp, err := client.Do(req)
if err != nil {
utils.Logln(utils.LogPrefixError, err)
return err
}

//var bodyContent []byte
if resp.StatusCode == http.StatusCreated || resp.StatusCode == http.StatusOK {
if resp.StatusCode() == http.StatusCreated || resp.StatusCode() == http.StatusOK {
// 201 Created or 200 OK
_ = resp.Body.Close()
fmt.Println("Successfully imported API")
fmt.Println("Successfully imported API.")
return nil
} else {
// We have an HTTP error
fmt.Println("Error importing API.")
fmt.Println("Status: " + resp.Status)

bodyBuf, err := ioutil.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil {
return err
}

strBody := string(bodyBuf)
fmt.Println("Response:", strBody)

return errors.New(resp.Status)
fmt.Println("Status: " + resp.Status())
return errors.New(resp.Status())
}
}

Expand Down
62 changes: 18 additions & 44 deletions import-export-cli/cmd/importApp.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@ package cmd

import (
"bytes"
"crypto/tls"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strconv"
"time"

"github.com/go-resty/resty"
"github.com/spf13/cobra"
"github.com/wso2/product-apim-tooling/import-export-cli/credentials"
"github.com/wso2/product-apim-tooling/import-export-cli/utils"
Expand Down Expand Up @@ -111,7 +110,7 @@ func ImportApplication(filename, appOwner, adminEndpiont, accessToken, exportDir
adminEndpiont = utils.AppendSlashToString(adminEndpiont)

applicationImportEndpoint := adminEndpiont + "import/applications"
url := applicationImportEndpoint + "?appOwner=" + appOwner + utils.SearchAndTag + "preserveOwner=" +
applicationImportUrl := applicationImportEndpoint + "?appOwner=" + appOwner + utils.SearchAndTag + "preserveOwner=" +
strconv.FormatBool(preserveOwner) + utils.SearchAndTag + "skipSubscriptions=" +
strconv.FormatBool(skipSubscriptions) + utils.SearchAndTag + "skipApplicationKeys=" + strconv.FormatBool(importAppSkipKeys) +
utils.SearchAndTag + "update=" + strconv.FormatBool(importAppUpdateApplication)
Expand All @@ -125,55 +124,27 @@ func ImportApplication(filename, appOwner, adminEndpiont, accessToken, exportDir

extraParams := map[string]string{}

req, err := NewAppFileUploadRequest(url, extraParams, "file", zipFilePath, accessToken)
resp, err := NewAppFileUploadRequest(applicationImportUrl, extraParams, "file", zipFilePath, accessToken)
if err != nil {
utils.HandleErrorAndExit("Error creating request.", err)
}

var tr *http.Transport
if utils.Insecure {
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
} else {
tr = &http.Transport{}
utils.HandleErrorAndExit("Error executing request.", err)
}

client := &http.Client{
Transport: tr,
Timeout: time.Duration(utils.HttpRequestTimeout) * time.Second,
}

resp, err := client.Do(req)

if err != nil {
utils.Logln(utils.LogPrefixError, err)
} else {
//var bodyContent []byte

if resp.StatusCode == http.StatusCreated || resp.StatusCode == http.StatusOK ||
resp.StatusCode == http.StatusMultiStatus {
if resp.StatusCode() == http.StatusCreated || resp.StatusCode() == http.StatusOK ||
resp.StatusCode() == http.StatusMultiStatus {
// 207 Multi Status or 201 Created or 200 OK
fmt.Printf("\nCompleted importing the Application '" + filename + "'\n")
} else {
fmt.Printf("\nUnable to import the Application\n")
fmt.Println("Status: " + resp.Status)
fmt.Println("Status: " + resp.Status())
}

//fmt.Println(resp.Header)
//resp.Body.Read(bodyContent)
//resp.Body.Close()
//fmt.Println(bodyContent)
}

return resp, err
return resp.RawResponse, err
}

// NewFileUploadRequest form an HTTP Put request
// Helper function for forming multi-part form data
// Returns the formed http request and errors
func NewAppFileUploadRequest(uri string, params map[string]string, paramName, path,
accessToken string) (*http.Request, error) {
accessToken string) (*resty.Response, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
Expand All @@ -196,13 +167,16 @@ func NewAppFileUploadRequest(uri string, params map[string]string, paramName, pa
return nil, err
}

request, err := http.NewRequest(http.MethodPost, uri, body)
request.Header.Add(utils.HeaderAuthorization, utils.HeaderValueAuthBearerPrefix+" "+accessToken)
request.Header.Add(utils.HeaderContentType, writer.FormDataContentType())
request.Header.Add(utils.HeaderAccept, "*/*")
request.Header.Add(utils.HeaderConnection, utils.HeaderValueKeepAlive)
// set headers
headers := make(map[string]string)
headers[utils.HeaderContentType] = writer.FormDataContentType()
headers[utils.HeaderAuthorization] = utils.HeaderValueAuthBearerPrefix+" "+accessToken
headers[utils.HeaderAccept] = "*/*"
headers[utils.HeaderConnection] = utils.HeaderValueKeepAlive

return request, err
resp, err := utils.InvokePOSTRequestWithBytes(uri, headers, body.Bytes())

return resp, err
}

func init() {
Expand Down
68 changes: 67 additions & 1 deletion import-export-cli/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,75 @@ func InvokePOSTRequest(url string, headers map[string]string, body string) (*res
if Insecure {
resty.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) // To bypass errors in SSL certificates
}
if os.Getenv("HTTP_PROXY") != "" {
resty.SetProxy(os.Getenv("HTTP_PROXY"))
} else if os.Getenv("HTTPS_PROXY") != "" {
resty.SetProxy(os.Getenv("HTTPS_PROXY"))
} else if os.Getenv("http_proxy") != "" {
resty.SetProxy(os.Getenv("http_proxy"))
} else if os.Getenv("https_proxy") != "" {
resty.SetProxy(os.Getenv("https_proxy"))
}
resty.SetTimeout(time.Duration(HttpRequestTimeout) * time.Millisecond)
resp, err := resty.R().SetHeaders(headers).SetBody(body).Post(url)

return resp, err
}

// Invoke http-post request using go-resty with byte[] body
func InvokePOSTRequestWithBytes(url string, headers map[string]string, body []byte) (*resty.Response, error) {
if Insecure {
resty.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) // To bypass errors in SSL certificates
}
if os.Getenv("HTTP_PROXY") != "" {
resty.SetProxy(os.Getenv("HTTP_PROXY"))
} else if os.Getenv("HTTPS_PROXY") != "" {
resty.SetProxy(os.Getenv("HTTPS_PROXY"))
} else if os.Getenv("http_proxy") != "" {
resty.SetProxy(os.Getenv("http_proxy"))
} else if os.Getenv("https_proxy") != "" {
resty.SetProxy(os.Getenv("https_proxy"))
}
resty.SetTimeout(time.Duration(HttpRequestTimeout) * time.Millisecond)
resp, err := resty.R().SetHeaders(headers).SetBody(body).Post(url)

return resp, err
}

// Invoke http-post request using go-resty with byte[] body
func InvokePUTRequestWithBytes(url string, headers map[string]string, body []byte) (*resty.Response, error) {
if Insecure {
resty.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) // To bypass errors in SSL certificates
}
if os.Getenv("HTTP_PROXY") != "" {
resty.SetProxy(os.Getenv("HTTP_PROXY"))
} else if os.Getenv("HTTPS_PROXY") != "" {
resty.SetProxy(os.Getenv("HTTPS_PROXY"))
} else if os.Getenv("http_proxy") != "" {
resty.SetProxy(os.Getenv("http_proxy"))
} else if os.Getenv("https_proxy") != "" {
resty.SetProxy(os.Getenv("https_proxy"))
}
resty.SetTimeout(time.Duration(HttpRequestTimeout) * time.Millisecond)
resp, err := resty.R().SetHeaders(headers).SetBody(body).Put(url)

return resp, err
}

// Invoke http-get request using go-resty
func InvokeGETRequest(url string, headers map[string]string) (*resty.Response, error) {
if Insecure {
resty.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) // To bypass errors in SSL certificates
}
if os.Getenv("HTTP_PROXY") != "" {
resty.SetProxy(os.Getenv("HTTP_PROXY"))
} else if os.Getenv("HTTPS_PROXY") != "" {
resty.SetProxy(os.Getenv("HTTPS_PROXY"))
} else if os.Getenv("http_proxy") != "" {
resty.SetProxy(os.Getenv("http_proxy"))
} else if os.Getenv("https_proxy") != "" {
resty.SetProxy(os.Getenv("https_proxy"))
}
resty.SetTimeout(time.Duration(HttpRequestTimeout) * time.Millisecond)
resp, err := resty.R().SetHeaders(headers).Get(url)

Expand All @@ -60,6 +118,15 @@ func InvokeGETRequestWithQueryParam(queryParam string, paramValue string, url st
if Insecure {
resty.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) // To bypass errors in SSL certificates
}
if os.Getenv("HTTP_PROXY") != "" {
resty.SetProxy(os.Getenv("HTTP_PROXY"))
} else if os.Getenv("HTTPS_PROXY") != "" {
resty.SetProxy(os.Getenv("HTTPS_PROXY"))
} else if os.Getenv("http_proxy") != "" {
resty.SetProxy(os.Getenv("http_proxy"))
} else if os.Getenv("https_proxy") != "" {
resty.SetProxy(os.Getenv("https_proxy"))
}
resty.SetTimeout(time.Duration(HttpRequestTimeout) * time.Millisecond)
resp, err := resty.R().SetHeaders(headers).SetQueryParam(queryParam, paramValue).Get(url)

Expand Down Expand Up @@ -137,5 +204,4 @@ func WriteToFileSystem(exportAPIName, exportAPIVersion, exportEnvironment, expor
}
fmt.Println("Succesfully exported API!")
fmt.Println("Find the exported API at " + pFile)

}

0 comments on commit 7bef61f

Please sign in to comment.