From a0db0eb0396853d41e5da79a663bcb9f3c0ce865 Mon Sep 17 00:00:00 2001 From: Sanoj Punchihewa Date: Mon, 11 Jan 2021 22:06:43 +0530 Subject: [PATCH] Fix review comments and refactor code --- import-export-cli/impl/common.go | 9 +++++---- import-export-cli/impl/importApp.go | 2 +- import-export-cli/mi/impl/common.go | 18 +++++++++++------- import-export-cli/mi/impl/user.go | 24 ++++++++++++++---------- import-export-cli/utils/utils.go | 25 +------------------------ 5 files changed, 32 insertions(+), 46 deletions(-) diff --git a/import-export-cli/impl/common.go b/import-export-cli/impl/common.go index 02bdb2185..78efdf5bd 100644 --- a/import-export-cli/impl/common.go +++ b/import-export-cli/impl/common.go @@ -21,8 +21,6 @@ package impl import ( "bytes" "errors" - "github.com/Jeffail/gabs" - jsoniter "github.com/json-iterator/go" "io" "io/ioutil" "mime/multipart" @@ -31,6 +29,9 @@ import ( "strings" "text/template" + "github.com/Jeffail/gabs" + jsoniter "github.com/json-iterator/go" + "github.com/go-resty/resty" "github.com/wso2/product-apim-tooling/import-export-cli/box" "github.com/wso2/product-apim-tooling/import-export-cli/utils" @@ -75,7 +76,7 @@ func ExecuteNewFileUploadRequest(uri string, params map[string]string, paramName headers[utils.HeaderAccept] = "application/json" headers[utils.HeaderConnection] = utils.HeaderValueKeepAlive - resp, err := utils.InvokePOSTRequestWithBytes(uri, headers, body.Bytes()) + resp, err := utils.InvokePOSTRequest(uri, headers, body.Bytes()) return resp, err } @@ -168,4 +169,4 @@ func IncludeMetaFileToZip(sourceZipFile, targetZipFile, metaFile string, metaDat utils.HandleErrorAndExit("Error creating the final zip archive", err) } return nil -} \ No newline at end of file +} diff --git a/import-export-cli/impl/importApp.go b/import-export-cli/impl/importApp.go index 7af952ca7..ef234a065 100644 --- a/import-export-cli/impl/importApp.go +++ b/import-export-cli/impl/importApp.go @@ -196,7 +196,7 @@ func NewAppFileUploadRequest(uri string, params map[string]string, paramName, pa headers[utils.HeaderAccept] = "*/*" headers[utils.HeaderConnection] = utils.HeaderValueKeepAlive - resp, err := utils.InvokePOSTRequestWithBytes(uri, headers, body.Bytes()) + resp, err := utils.InvokePOSTRequest(uri, headers, body.Bytes()) return resp, err } diff --git a/import-export-cli/mi/impl/common.go b/import-export-cli/mi/impl/common.go index 2356bbf64..4dfceb1e8 100644 --- a/import-export-cli/mi/impl/common.go +++ b/import-export-cli/mi/impl/common.go @@ -28,7 +28,6 @@ import ( "text/template" "github.com/go-resty/resty" - "github.com/renstrom/dedent" "github.com/wso2/product-apim-tooling/import-export-cli/credentials" "github.com/wso2/product-apim-tooling/import-export-cli/formatter" "github.com/wso2/product-apim-tooling/import-export-cli/utils" @@ -37,6 +36,11 @@ import ( // miHTTPRetryCount default retry count for HTTP calls const miHTTPRetryCount = 2 +type updateArtifactRequestBody struct { + Name string `json:"name"` + Status string `json:"status"` +} + // unmarshalData unmarshal data from the response to the respective struct // @param url: url of rest api // @param params: parameters for the HTTP call @@ -144,7 +148,7 @@ func invokePATCHRequestWithRetry(url string, body map[string]string, env string) }) } -func invokePOSTRequestWithRetry(url, body, env string) (*resty.Response, error) { +func invokePOSTRequestWithRetry(env, url string, body interface{}) (*resty.Response, error) { return retryHTTPCall(miHTTPRetryCount, env, func(accessToken string) (*resty.Response, error) { headers := make(map[string]string) headers[utils.HeaderAuthorization] = utils.HeaderValueAuthBearerPrefix + " " + accessToken @@ -240,10 +244,10 @@ func createErrorWithResponseBody(resp string, err error) error { } func updateArtifactState(url, artifactName, state, env string) (string, error) { - body := dedent.Dedent(`{ - "name": "` + artifactName + `", - "status": "` + state + `" - }`) - resp, err := invokePOSTRequestWithRetry(url, body, env) + body := updateArtifactRequestBody{ + Name: artifactName, + Status: state, + } + resp, err := invokePOSTRequestWithRetry(env, url, body) return handleResponse(resp, err, url, "Message", "Error") } diff --git a/import-export-cli/mi/impl/user.go b/import-export-cli/mi/impl/user.go index a90b763b3..673a40be2 100644 --- a/import-export-cli/mi/impl/user.go +++ b/import-export-cli/mi/impl/user.go @@ -21,21 +21,25 @@ package impl import ( "strings" - "github.com/renstrom/dedent" "github.com/wso2/product-apim-tooling/import-export-cli/utils" ) +type newUserRequestBody struct { + UserID string `json:"userID"` + Password string `json:"password"` + IsAdmin string `json:"isAdmin"` +} + // AddMIUser adds a new user to the micro integrator in a given environment func AddMIUser(env, userName, password, isAdmin string) (interface{}, error) { isAdmin = resolveIsAdmin(isAdmin) - body := dedent.Dedent(`{ - "userId": "` + userName + `", - "password": "` + password + `", - "isAdmin": "` + isAdmin + `" - }`) - + body := newUserRequestBody{ + UserID: userName, + Password: password, + IsAdmin: isAdmin, + } url := utils.GetMIManagementEndpointOfResource(utils.MiManagementUserResource, env, utils.MainConfigFilePath) - resp, err := addNewMIUser(url, body, env) + resp, err := addNewMIUser(env, url, body) if err != nil { return nil, createErrorWithResponseBody(resp, err) } @@ -52,8 +56,8 @@ func DeleteMIUser(env, userName string) (interface{}, error) { return resp, nil } -func addNewMIUser(url, body, env string) (string, error) { - resp, err := invokePOSTRequestWithRetry(url, body, env) +func addNewMIUser(env, url string, body interface{}) (string, error) { + resp, err := invokePOSTRequestWithRetry(env, url, body) return handleResponse(resp, err, url, "status", "Error") } diff --git a/import-export-cli/utils/utils.go b/import-export-cli/utils/utils.go index e85a5cf13..4b852e330 100644 --- a/import-export-cli/utils/utils.go +++ b/import-export-cli/utils/utils.go @@ -33,30 +33,7 @@ import ( "golang.org/x/crypto/ssh/terminal" ) -// Invoke http-post request using go-resty -func InvokePOSTRequest(url string, headers map[string]string, body string) (*resty.Response, error) { - if Insecure { - resty.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) // To bypass errors in SSL certificates - } else { - resty.SetTLSClientConfig(GetTlsConfigWithCertificate()) - } - 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) { +func InvokePOSTRequest(url string, headers map[string]string, body interface{}) (*resty.Response, error) { if Insecure { resty.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) // To bypass errors in SSL certificates } else {