8e64bc893607c855806f5695e1ea8e6d3db7865c
[ealt-edge.git] / ocd / cli / ealt / cmd / adapter / converter.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         "ealt/cmd/common"
21         model "ealt/cmd/model"
22         "encoding/json"
23         "fmt"
24         "log"
25         "strings"
26 )
27
28 func BuilderRequest(valueArgs []string, command string) error {
29
30         var URIString string
31
32         switch command {
33         case "NewAppCreateCommand":
34                 //Onboard Command
35                 //ealtedge/mepm/app_pkgm/v1/app_packages/
36                 //read the file from the system.
37                 URIString = common.AppmUriCreate
38                 var packageName string
39                 var body []byte
40                 body = jsonEmptyBodyFormat()
41                 packageName = strings.TrimSpace(valueArgs[0])
42                 HttpMultiPartPostRequestBuilder(URIString, body, packageName)
43
44         case "NewAppDeleteCommand":
45                 //The Delete Application Package URI
46                 //ealtedge/mepm/app_pkgm/v1/app_packages/{{ID}}
47                 var body []byte
48                 URIString = common.AppmUri + strings.TrimSpace(valueArgs[0])
49                 body = jsonEmptyBodyFormat()
50                 HttpDeleteRequestBuilder(URIString, body)
51
52         case "NewApplcmCreateCommand":
53                 //appLCM application Creation URI
54                 //ealtedge/mepm/app_lcm/v1/app_instances
55                 var body []byte
56
57                 URIString = common.ApplcmUriCreate
58                 //Assigning the AppLcm Create Command Line Flags to the Json Paylod.
59                 payload := model.CreateApplicationReq{AppDID: strings.TrimSpace(valueArgs[0]),
60                         AppInstancename:       strings.TrimSpace(valueArgs[1]),
61                         AppInstanceDescriptor: strings.TrimSpace(valueArgs[2])}
62                 body, err := json.Marshal(payload)
63
64                 if err != nil {
65                         log.Fatalln(err)
66                 }
67                 HttpPostRequestBuilder(URIString, body)
68
69         case "NewApplcmDeleteCommand":
70                 //appLCM Delete Application URI
71                 ///ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}
72                 var body []byte
73                 URIString = common.ApplcmUri + strings.TrimSpace(valueArgs[0])
74
75                 //Empty body for Delete Command.
76                 body = jsonEmptyBodyFormat()
77                 HttpDeleteRequestBuilder(URIString, body)
78
79         case "NewApplcmStartCommand":
80                 //applcm application instantiate uri
81                 //ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}/instantiate
82                 var body []byte
83
84                 URIString = common.ApplcmUri + strings.TrimSpace(valueArgs[0]) + common.InstantiateUri
85
86                 selectedMECHostInfo := model.SelectedMECHostInfo{HostName: strings.TrimSpace(valueArgs[1]),
87                         HostId: strings.TrimSpace(valueArgs[2])}
88                 //Payload
89                 payload := model.InstantiateApplicationReq{SelectedMECHostInfo: selectedMECHostInfo}
90                 body, err := json.Marshal(payload)
91                 if err != nil {
92                         fmt.Println(err)
93                 }
94                 HttpPostRequestBuilder(URIString, body)
95
96         case "NewApplcmTerminateCommand":
97                 //applcm application terminate uri
98                 //ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}/terminate
99                 var body []byte
100                 URIString = common.ApplcmUri + strings.TrimSpace(valueArgs[0]) + common.TerminateUri
101                 body = jsonEmptyBodyFormat()
102                 HttpPostRequestBuilder(URIString, body)
103         }
104         return nil
105 }
106
107 func jsonEmptyBodyFormat() []byte {
108         var jsonstr []byte
109         jsonstr = []byte(`{"":""}`)
110         return jsonstr
111 }