Added Get Operation and Bug Fixes
[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 "NewAppInfoCommand":
45                 URIString = common.AppmUri
46                 var body []byte
47                 URIString = common.AppmUri + strings.TrimSpace(valueArgs[0])
48                 body = jsonEmptyBodyFormat()
49                 HttpGetRequestBuilder(URIString, body)
50
51         case "NewAppDeleteCommand":
52                 //The Delete Application Package URI
53                 //ealtedge/mepm/app_pkgm/v1/app_packages/{{ID}}
54                 var body []byte
55                 URIString = common.AppmUri + strings.TrimSpace(valueArgs[0])
56                 body = jsonEmptyBodyFormat()
57                 HttpDeleteRequestBuilder(URIString, body)
58
59         case "NewApplcmCreateCommand":
60                 //appLCM application Creation URI
61                 //ealtedge/mepm/app_lcm/v1/app_instances
62                 var body []byte
63
64                 URIString = common.ApplcmUriCreate
65                 //Assigning the AppLcm Create Command Line Flags to the Json Paylod.
66                 payload := model.CreateApplicationReq{AppDID: strings.TrimSpace(valueArgs[0]),
67                         AppInstancename:       strings.TrimSpace(valueArgs[1]),
68                         AppInstanceDescriptor: strings.TrimSpace(valueArgs[2])}
69                 body, err := json.Marshal(payload)
70
71                 if err != nil {
72                         log.Fatalln(err)
73                 }
74                 HttpPostRequestBuilder(URIString, body)
75
76         case "NewApplcmInfoCommand":
77                 //appLCM Get Application URI
78                 ///ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}
79                 var body []byte
80                 URIString = common.ApplcmUri + strings.TrimSpace(valueArgs[0])
81
82                 //Empty body for Delete Command.
83                 body = jsonEmptyBodyFormat()
84                 HttpGetRequestBuilder(URIString, body)
85
86         case "NewApplcmDeleteCommand":
87                 //appLCM Delete Application URI
88                 ///ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}
89                 var body []byte
90                 URIString = common.ApplcmUri + strings.TrimSpace(valueArgs[0])
91
92                 //Empty body for Delete Command.
93                 body = jsonEmptyBodyFormat()
94                 HttpDeleteRequestBuilder(URIString, body)
95
96         case "NewApplcmStartCommand":
97                 //applcm application instantiate uri
98                 //ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}/instantiate
99                 var body []byte
100
101                 URIString = common.ApplcmUri + strings.TrimSpace(valueArgs[0]) + common.InstantiateUri
102
103                 selectedMECHostInfo := model.SelectedMECHostInfo{HostName: strings.TrimSpace(valueArgs[1]),
104                         HostId: strings.TrimSpace(valueArgs[2])}
105                 //Payload
106                 payload := model.InstantiateApplicationReq{SelectedMECHostInfo: selectedMECHostInfo}
107                 body, err := json.Marshal(payload)
108                 if err != nil {
109                         fmt.Println(err)
110                 }
111                 HttpPostRequestBuilder(URIString, body)
112
113         case "NewApplcmTerminateCommand":
114                 //applcm application terminate uri
115                 //ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}/terminate
116                 var body []byte
117                 URIString = common.ApplcmUri + strings.TrimSpace(valueArgs[0]) + common.TerminateUri
118                 body = jsonEmptyBodyFormat()
119                 HttpPostRequestBuilder(URIString, body)
120         }
121         return nil
122 }
123
124 func jsonEmptyBodyFormat() []byte {
125         var jsonstr []byte
126         jsonstr = []byte(`{"":""}`)
127         return jsonstr
128 }