Added Create and Delete CLI Commands
[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 func HttpDeleteRequestBuilder(uri string, body []byte) {
45
46         uri = httpEndPointBuider(uri)
47         request, err := http.NewRequest(http.MethodDelete, uri, bytes.NewBuffer(body))
48         request.Header.Set("Content-Type", "application/json")
49
50         if err != nil {
51                 log.Fatalln(err)
52         }
53
54         response, err := client.Do(request)
55         if err != nil {
56                 log.Fatalln(err)
57         }
58         defer response.Body.Close()
59
60         output, err := ioutil.ReadAll(response.Body)
61
62         if err != nil {
63                 log.Fatalln(err)
64         }
65
66         fmt.Println(string(output))
67
68 }
69
70 func HttpPostRequestBuilder(uri string, body []byte) error {
71
72         uri = httpEndPointBuider(uri)
73         request, err := http.NewRequest(http.MethodPost, uri, bytes.NewBuffer(body))
74         request.Header.Set("Content-Type", "application/json")
75
76         fmt.Println(request)
77
78         if err != nil {
79                 log.Fatalln(err)
80         }
81
82         response, err := client.Do(request)
83         if err != nil {
84                 log.Fatalln(err)
85         }
86         defer response.Body.Close()
87
88         output, err := ioutil.ReadAll(response.Body)
89
90         if err != nil {
91                 log.Fatalln(err)
92         }
93
94         fmt.Println(string(output))
95         return nil
96 }
97
98 func HttpMultiPartPostRequestBuilder(uri string, body []byte, file string) error {
99
100         filepath := getFilePathWithName(file)
101         uri = httpEndPointBuider(uri)
102
103         request, err := fileUploadRequest(uri, "file", filepath)
104
105         response, err := client.Do(request)
106         if err != nil {
107                 log.Fatalln(err)
108         }
109
110         var result map[string]interface{}
111         json.NewDecoder(response.Body).Decode(&result)
112         fmt.Println(result)
113
114         return nil
115 }
116
117 func getFilePathWithName(file string) string {
118
119         return ONBOARDPACKAGEPATH + common.PATHSLASH + file
120 }
121
122 func fileUploadRequest(uri string, paramName, filepath string) (*http.Request, error) {
123
124         file, err := os.Open(filepath)
125         if err != nil {
126                 return nil, err
127         }
128
129         //close the file later
130         defer file.Close()
131
132         //Buffer to store the request body as bytes
133         var requestBody bytes.Buffer
134         multiPartWriter := multipart.NewWriter(&requestBody)
135
136         fileWriter, err := multiPartWriter.CreateFormFile("file_field", filepath)
137         if err != nil {
138                 log.Fatalln(err)
139         }
140
141         //Copy the actual file contents
142         _, err = io.Copy(fileWriter, file)
143         if err != nil {
144                 log.Fatalln(err)
145         }
146
147         //Close multiwriter
148         multiPartWriter.Close()
149
150         request, err := http.NewRequest(http.MethodPost, uri, &requestBody)
151         if err != nil {
152                 log.Fatalln(err)
153         }
154         request.Header.Set("Content-Type", "multipart/form-data")
155         return request, err
156 }