Added Get Operation and Bug Fixes
[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 client = http.Client{}
37
38 func httpEndPointBuider(uri string) string {
39
40         return "http://" + strings.TrimSpace(MECMClusterIP) + ":" + strings.TrimSpace(APPLCMPort) + uri
41
42 }
43
44 //Function to build the Get Requests for Application Package
45 //Management and Application Life Cycle Management.
46 func HttpGetRequestBuilder(uri string, body []byte) {
47
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")
52
53         if err != nil {
54                 log.Fatalln(err)
55         }
56         response, err := client.Do(request)
57         if err != nil {
58                 log.Fatalln(err)
59         }
60         defer response.Body.Close()
61
62         output, err := ioutil.ReadAll(response.Body)
63
64         if err != nil {
65                 log.Fatalln(err)
66         }
67         fmt.Println("Response Data: \n" + string(output))
68 }
69
70 //HTTP DELETE Message Builder
71 func HttpDeleteRequestBuilder(uri string, body []byte) {
72
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")
77
78         if err != nil {
79                 log.Fatalln(err)
80         }
81         response, err := client.Do(request)
82         if err != nil {
83                 log.Fatalln(err)
84         }
85         defer response.Body.Close()
86         output, err := ioutil.ReadAll(response.Body)
87
88         if err != nil {
89                 log.Fatalln(err)
90         }
91         fmt.Println("Response Data: \n" + string(output))
92 }
93
94 func HttpPostRequestBuilder(uri string, body []byte) error {
95
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")
101
102         if err != nil {
103                 log.Fatalln(err)
104         }
105         response, err := client.Do(request)
106         if err != nil {
107                 log.Fatalln(err)
108         }
109         defer response.Body.Close()
110
111         output, err := ioutil.ReadAll(response.Body)
112
113         if err != nil {
114                 log.Fatalln(err)
115         }
116
117         fmt.Println("Response Data: \n\n" + string(output))
118         return nil
119 }
120
121 func HttpMultiPartPostRequestBuilder(uri string, body []byte, file string) error {
122
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)
128         if err != nil {
129                 log.Fatalln(err)
130         }
131
132         response, err := client.Do(request)
133         if err != nil {
134                 log.Fatalln(err)
135         } else {
136                 body := &bytes.Buffer{}
137                 _, err := body.ReadFrom(response.Body)
138                 if err != nil {
139                         log.Fatal(err)
140                 }
141                 response.Body.Close()
142
143                 fmt.Println("Response Body:")
144
145                 fmt.Println(body)
146                 var result map[string]interface{}
147                 json.NewDecoder(response.Body).Decode(&result)
148
149                 fmt.Println("ID has to be send in Create Application Instance Request")
150         }
151         return nil
152 }
153
154 func getFilePathWithName(file string) string {
155
156         return ONBOARDPACKAGEPATH + common.PATHSLASH + file
157 }
158
159 func fileUploadRequest(uri string, paramName, filepath, filename string) (*http.Request, error) {
160
161         file, err := os.Open(filepath)
162         if err != nil {
163                 return nil, err
164         }
165
166         //close the file later
167         defer file.Close()
168
169         //Buffer to store the request body as bytes
170         //var requestBody bytes.Buffer
171         requestBody := &bytes.Buffer{}
172         multiPartWriter := multipart.NewWriter(requestBody)
173
174         fileWriter, err := multiPartWriter.CreateFormFile(paramName, filename)
175         if err != nil {
176                 log.Fatalln(err)
177         }
178
179         //Copy the actual file contents
180         _, err = io.Copy(fileWriter, file)
181         if err != nil {
182                 log.Fatalln(err)
183         }
184
185         //Close multiwriter
186         multiPartWriter.Close()
187         if err != nil {
188                 return nil, err
189         }
190
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")
194
195         if err != nil {
196                 log.Fatalln(err)
197         }
198
199         return request, err
200 }