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 client = http.Client{}
38 func httpEndPointBuider(uri string) string {
40 return "http://" + strings.TrimSpace(MECMClusterIP) + ":" + strings.TrimSpace(APPLCMPort) + uri
44 //Function to build the Get Requests for Application Package
45 //Management and Application Life Cycle Management.
46 func HttpGetRequestBuilder(uri string, body []byte) {
48 uri = httpEndPointBuider(uri)
49 fmt.Println("Request URL :\t" + uri)
50 request, err := http.NewRequest(http.MethodGet, uri, bytes.NewBuffer(body))
51 request.Header.Set("Content-Type", "application/json")
56 response, err := client.Do(request)
60 defer response.Body.Close()
62 output, err := ioutil.ReadAll(response.Body)
67 fmt.Println("Response Data: \n" + string(output))
70 //HTTP DELETE Message Builder
71 func HttpDeleteRequestBuilder(uri string, body []byte) {
73 uri = httpEndPointBuider(uri)
74 fmt.Println("Request URL :\t" + uri)
75 request, err := http.NewRequest(http.MethodDelete, uri, bytes.NewBuffer(body))
76 request.Header.Set("Content-Type", "application/json")
81 response, err := client.Do(request)
85 defer response.Body.Close()
86 output, err := ioutil.ReadAll(response.Body)
91 fmt.Println("Response Data: \n" + string(output))
94 func HttpPostRequestBuilder(uri string, body []byte) error {
96 uri = httpEndPointBuider(uri)
97 fmt.Println("Request URL :\t" + uri)
98 fmt.Println("Request Body :\t" + string(body) + "\n")
99 request, err := http.NewRequest(http.MethodPost, uri, bytes.NewBuffer(body))
100 request.Header.Set("Content-Type", "application/json")
105 response, err := client.Do(request)
109 defer response.Body.Close()
111 output, err := ioutil.ReadAll(response.Body)
117 fmt.Println("Response Data: \n\n" + string(output))
121 func HttpMultiPartPostRequestBuilder(uri string, body []byte, file string) error {
123 filepath := getFilePathWithName(file)
124 fmt.Println("File Path :" + filepath)
125 uri = httpEndPointBuider(uri)
126 fmt.Println("Request URL :\t" + uri)
127 request, err := fileUploadRequest(uri, "file", filepath, file)
132 response, err := client.Do(request)
136 body := &bytes.Buffer{}
137 _, err := body.ReadFrom(response.Body)
141 response.Body.Close()
143 fmt.Println("Response Body:")
146 var result map[string]interface{}
147 json.NewDecoder(response.Body).Decode(&result)
149 fmt.Println("ID has to be send in Create Application Instance Request")
154 func getFilePathWithName(file string) string {
156 return ONBOARDPACKAGEPATH + common.PATHSLASH + file
159 func fileUploadRequest(uri string, paramName, filepath, filename string) (*http.Request, error) {
161 file, err := os.Open(filepath)
166 //close the file later
169 //Buffer to store the request body as bytes
170 //var requestBody bytes.Buffer
171 requestBody := &bytes.Buffer{}
172 multiPartWriter := multipart.NewWriter(requestBody)
174 fileWriter, err := multiPartWriter.CreateFormFile(paramName, filename)
179 //Copy the actual file contents
180 _, err = io.Copy(fileWriter, file)
186 multiPartWriter.Close()
191 request, err := http.NewRequest(http.MethodPost, uri, requestBody)
192 request.Header.Set("Content-Type", multiPartWriter.FormDataContentType())
193 //request.Header.Set("Content-Type", "multipart/form-data")