To support SSL mode in CLI
[ealt-edge.git] / ocd / cli / ealt / cmd / adapter / httphelper.go
1 /*
2 Copyright 2020 Huawei Technologies Co., Ltd.
3
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
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
17 package adapter
18
19 import (
20         "bytes"
21         "ealt/cmd/common"
22         "encoding/json"
23         "fmt"
24         "io"
25         "io/ioutil"
26         "log"
27         "mime/multipart"
28         "net/http"
29         "os"
30         "strings"
31 )
32
33 var MECMClusterIP = os.Getenv("MECMClusterIP")
34 var APPLCMPort = os.Getenv("MECMClusterPort")
35 var ONBOARDPACKAGEPATH = os.Getenv("ONBOARDPACKAGEPATH")
36 var sslmode = os.Getenv("EALTSSLMode")
37
38 func httpEndPointBuider(uri string) string {
39         localURI := strings.TrimSpace(MECMClusterIP) + ":" + strings.TrimSpace(APPLCMPort) + uri
40         if sslmode == "1" {
41                 return "https://" + localURI
42         }
43         return "http://" + localURI
44 }
45
46 //Function to build the Get Requests for Application Package
47 //Management and Application Life Cycle Management.
48 func HttpGetRequestBuilder(uri string, body []byte) {
49
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)
54         if err != nil {
55                 log.Fatalln(err)
56         }
57         client := GetHttpClient()
58         response, err := client.Do(request)
59         if err != nil {
60                 log.Fatalln(err)
61         }
62         defer response.Body.Close()
63
64         output, err := ioutil.ReadAll(response.Body)
65
66         if err != nil {
67                 log.Fatalln(err)
68         }
69         fmt.Println("Response Data: \n" + string(output))
70 }
71
72 //HTTP DELETE Message Builder
73 func HttpDeleteRequestBuilder(uri string, body []byte) {
74
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)
79
80         if err != nil {
81                 log.Fatalln(err)
82         }
83         client := GetHttpClient()
84         response, err := client.Do(request)
85         if err != nil {
86                 log.Fatalln(err)
87         }
88         defer response.Body.Close()
89         output, err := ioutil.ReadAll(response.Body)
90
91         if err != nil {
92                 log.Fatalln(err)
93         }
94         fmt.Println("Response Data: \n" + string(output))
95 }
96
97 func HttpPostRequestBuilder(uri string, body []byte) error {
98
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)
104
105         if err != nil {
106                 log.Fatalln(err)
107         }
108         client := GetHttpClient()
109         response, err := client.Do(request)
110         if err != nil {
111                 log.Fatalln(err)
112         }
113         defer response.Body.Close()
114
115         output, err := ioutil.ReadAll(response.Body)
116
117         if err != nil {
118                 log.Fatalln(err)
119         }
120
121         fmt.Println("Response Data: \n\n" + string(output))
122         return nil
123 }
124
125 func HttpMultiPartPostRequestBuilder(uri string, body []byte, file string) error {
126
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)
132         if err != nil {
133                 log.Fatalln(err)
134         }
135         client := GetHttpClient()
136         response, err := client.Do(request)
137         if err != nil {
138                 log.Fatalln(err)
139         } else {
140                 body := &bytes.Buffer{}
141                 _, err := body.ReadFrom(response.Body)
142                 if err != nil {
143                         log.Fatal(err)
144                 }
145                 response.Body.Close()
146
147                 fmt.Println("Response Body:")
148
149                 fmt.Println(body)
150                 var result map[string]interface{}
151                 json.NewDecoder(response.Body).Decode(&result)
152
153                 fmt.Println("ID has to be send in Create Application Instance Request")
154         }
155         return nil
156 }
157
158 func getFilePathWithName(file string) string {
159
160         return ONBOARDPACKAGEPATH + common.PATHSLASH + file
161 }
162
163 func fileUploadRequest(uri string, paramName, filepath, filename string) (*http.Request, error) {
164
165         file, err := os.Open(filepath)
166         if err != nil {
167                 return nil, err
168         }
169
170         //close the file later
171         defer file.Close()
172
173         //Buffer to store the request body as bytes
174         //var requestBody bytes.Buffer
175         requestBody := &bytes.Buffer{}
176         multiPartWriter := multipart.NewWriter(requestBody)
177
178         fileWriter, err := multiPartWriter.CreateFormFile(paramName, filename)
179         if err != nil {
180                 log.Fatalln(err)
181         }
182
183         //Copy the actual file contents
184         _, err = io.Copy(fileWriter, file)
185         if err != nil {
186                 log.Fatalln(err)
187         }
188
189         //Close multiwriter
190         multiPartWriter.Close()
191         if err != nil {
192                 return nil, err
193         }
194
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")
198
199         if err != nil {
200                 log.Fatalln(err)
201         }
202
203         return request, err
204 }