2 Copyright 2020 Huawei Technologies Co., Ltd.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
33 var MECMClusterIP = os.Getenv("MECMClusterIP")
34 var APPLCMPort = os.Getenv("MECMClusterPort")
35 var ONBOARDPACKAGEPATH = os.Getenv("ONBOARDPACKAGEPATH")
36 var sslmode = os.Getenv("EALTSSLMode")
38 func httpEndPointBuider(uri string) string {
39 localURI := strings.TrimSpace(MECMClusterIP) + ":" + strings.TrimSpace(APPLCMPort) + uri
41 return "https://" + localURI
43 return "http://" + localURI
46 //Function to build the Get Requests for Application Package
47 //Management and Application Life Cycle Management.
48 func HttpGetRequestBuilder(uri string, body []byte) {
50 uri = httpEndPointBuider(uri)
51 fmt.Println("Request URL :\t" + uri)
52 request, err := http.NewRequest(http.MethodGet, uri, bytes.NewBuffer(body))
53 request.Header.Set(common.ContentType, common.ApplicationJson)
57 client := GetHttpClient()
58 response, err := client.Do(request)
62 defer response.Body.Close()
64 output, err := ioutil.ReadAll(response.Body)
69 fmt.Println("Response Data: \n" + string(output))
72 //HTTP DELETE Message Builder
73 func HttpDeleteRequestBuilder(uri string, body []byte) {
75 uri = httpEndPointBuider(uri)
76 fmt.Println("Request URL :\t" + uri)
77 request, err := http.NewRequest(http.MethodDelete, uri, bytes.NewBuffer(body))
78 request.Header.Set(common.ContentType, common.ApplicationJson)
83 client := GetHttpClient()
84 response, err := client.Do(request)
88 defer response.Body.Close()
89 output, err := ioutil.ReadAll(response.Body)
94 fmt.Println("Response Data: \n" + string(output))
97 func HttpPostRequestBuilder(uri string, body []byte) error {
99 uri = httpEndPointBuider(uri)
100 fmt.Println("Request URL :\t" + uri)
101 fmt.Println("Request Body :\t" + string(body) + "\n")
102 request, err := http.NewRequest(http.MethodPost, uri, bytes.NewBuffer(body))
103 request.Header.Set(common.ContentType, common.ApplicationJson)
108 client := GetHttpClient()
109 response, err := client.Do(request)
113 defer response.Body.Close()
115 output, err := ioutil.ReadAll(response.Body)
121 fmt.Println("Response Data: \n\n" + string(output))
125 func HttpMultiPartPostRequestBuilder(uri string, body []byte, file string) error {
127 filepath := getFilePathWithName(file)
128 fmt.Println("File Path :" + filepath)
129 uri = httpEndPointBuider(uri)
130 fmt.Println("Request URL :\t" + uri)
131 request, err := fileUploadRequest(uri, "file", filepath, file)
135 client := GetHttpClient()
136 response, err := client.Do(request)
140 body := &bytes.Buffer{}
141 _, err := body.ReadFrom(response.Body)
145 response.Body.Close()
147 fmt.Println("Response Body:")
150 var result map[string]interface{}
151 json.NewDecoder(response.Body).Decode(&result)
153 fmt.Println("ID has to be send in Create Application Instance Request")
158 func getFilePathWithName(file string) string {
160 return ONBOARDPACKAGEPATH + common.PATHSLASH + file
163 func fileUploadRequest(uri string, paramName, filepath, filename string) (*http.Request, error) {
165 file, err := os.Open(filepath)
170 //close the file later
173 //Buffer to store the request body as bytes
174 //var requestBody bytes.Buffer
175 requestBody := &bytes.Buffer{}
176 multiPartWriter := multipart.NewWriter(requestBody)
178 fileWriter, err := multiPartWriter.CreateFormFile(paramName, filename)
183 //Copy the actual file contents
184 _, err = io.Copy(fileWriter, file)
190 multiPartWriter.Close()
195 request, err := http.NewRequest(http.MethodPost, uri, requestBody)
196 request.Header.Set(common.ContentType, multiPartWriter.FormDataContentType())
197 //request.Header.Set("Content-Type", "multipart/form-data")